Ask Your Question

How to get a ROI with arbitrary shape?

asked 2017-11-27 08:25:43 -0600

AlexChan gravatar image

updated 2017-11-27 08:26:16 -0600

Hello, the code is here

cv::Mat src = cv::imread("1.bmp", CV_LOAD_IMAGE_UNCHANGED);
cv::Mat mask = cv::Mat::ones(src.size(), CV_8UC1);
mask.col(20).setTo(255); // arbitary shape
cv::Mat roi;
src.copyTo(roi, mask); // get the roi

This is 1.bmp

image description

This is mask

image description

This is roi

image description

If I excute roi = roi + 10, the black parts of roi will also add 10, it's not what I want. How to get a roi without the black parts?


edit retag flag offensive close merge delete


you could use the same mask with add

berak gravatar imageberak ( 2017-11-27 08:31:55 -0600 )edit

Thanks for reply. Adding with a mask has solved this problem, but what about other operation? For example, roi.setTo(255), matching roi to another picture... Can all the operation just influence the roi region? thanks :)

AlexChan gravatar imageAlexChan ( 2017-11-27 19:39:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-12-07 03:58:30 -0600

AlexChan gravatar image

I have found a way to solve this problem.

When you have got the mask, just keep it in the memory. After your operation for the image, do xx.copyTo(image, mask) again. Then the operation of ROI region will remain in the result, and the other region will be zero.

Note for copyTo():

src.copyTo(dst, mask) // src and dst should be different, and dst should be empty.
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-11-27 08:25:43 -0600

Seen: 48 times

Last updated: Dec 07