ofxCv, cropping affected area/ 'region of interest'

So I need to limit the area that is being tracked from my camera input. I have 1280x960 from camera, but I need the contour tracker to work on 1280x480. Does anybody have any tips? Is there any way to crop my camera input? Or change the ‘region of interest’ on my contour finder? I have heard of ROI for openCV, but I don’t know how to use it with McDonald’s ofxCv add-on.

Thank you all!

You pass a cv::Rect to a cv::Mat.

cv::Rect crop_roi = cv::Rect(x, y, width, height);
cv::Mat full = (...);
cv::Mat crop = full(crop_roi).clone();

Thanks! Where exactly would I do that? I see where the mat is passed in in the actual contour finder but I don’t want to alter the add-on to do it. I just have my camera (1280x720), and I am passing it into my contour finder.

I am getting “No viable conversion from ‘ofVideoGrabber’ to ‘cv::mat’”

There is a convenience method of going back and forth between openFrameworks image types and a cv::Mat. Use a function called toCv(). It returns a cv::Mat and accepts any openFrameworks object that uses ofPixels.

Later, if you want to use a function that only accepts an ofImage you can use the toOf(cv::Mat, ofImage) function to convert back from a cv::Mat to an ofImage type.

In testApp.h

ofVidGrabber cam;
cv::Mat cam_mat;
cv::Mat crop;

ContourFinder finder;
```
In testApp.cpp - in ```update()``` or wherever...
```
cam_mat = toCv(cam);
cv::Rect crop_roi = cv::Rect(x, y, width, height);
crop = cam_mat(crop_roi).clone();
finder.findContours(crop);
```
2 Likes