Ask Your Question

Perform functions on a ROI

asked 2017-10-27 04:57:44 -0500

Andriezel gravatar image

updated 2019-12-09 07:45:59 -0500

ojesus gravatar image

Hi everyone!

I'm working on a project and I want to select a ROI with my mouse.

Then I want to do the following things:

  • Make everything on the outside of the rectangle zero/black.
  • Perform some thresholding functions in the ROI.
  • Put it back in to the original image.

I don't know how to achieve this. Here's some pseudo code:

for loop to iterate through image{
     if(point x and y are not the same as the roi x and y ){
          make the pixel black

..perform some thresholding on ROI..

//copy roi back to image
image.copyTo(result, roi);

Can someone help me out here?


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2017-10-27 07:35:28 -0500

theaniolad gravatar image

updated 2017-10-27 08:07:59 -0500

To your second question how to perform a function on the original Image:

By default, OpenCV works with the original Mat when you select a subMat:

Mat mySubmat =  img(myROI); //myROI can be a cv::Rect or use two of cv::Range instead.

Therefore, by calling any OpenCV function that takes cv::InputArray and cv::Outputarray you can simply work on the original ROI by using the SubMat as input and output.

cv::threshold(mySubmat,mySubmat,....); //BTW: thresholding will only work on image if single channel image

To your first question. There are probably several ways how to do this: This one works, but there might be faster options:

Mat myMask = Mat(img.size(), CV_8UC1, 255); //create completely white mask
myMask(myROI).setTo(0); //black out roi
img.setTo(Vec3b(0,0,0), myMask); //set all areas of img to black that are within the white area of the mask.
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-10-27 04:57:44 -0500

Seen: 934 times

Last updated: Oct 27 '17