using countNonZeroInRegion


i have two questions:


i’m trying to use the function countNonZeroInRegion() on a binarized videofeed. basically, i want to detect the difference in two frames in a smaller region of the video, but a region that is always changing its x/y coordinates and width/height. i can do this manually, but was wondering if there’s a function that i can use that will let me target an area and return a value for the amount of activity that has occurred in that area.


what is ROI? in trying to use countNonZeroInRegion(), i encountered SetImageROI(). i would luv to understand this.

I think that’s how countNonZeroInRegion works:

countNonZeroInRegion(int x, int y, int w, int h)

you pass in the x,y,w and h of the region of pixels you’d like to count (very useful for creating region based triggers, etc).

for example, if you have a 320x240 image, but you only care about top, left quadrant of the image you could day:

int nWhitePixelsTL = countNonZeroInRegion(0,0,160,120);

and the bottom right quadrant, would be:

int nWhitePixelsBR = countNonZeroInRegion(160,120,160,120);


ROI is part of the iplImage struct - ROI = region of interest. many operations take the ROI into account…–…-intro.html

“specifies image region to be processed”

for most of what we have written, with the grayscalImage class, etc, we process the whole image, but there are times when it makes sense to process a sub image. in that case, you can get and set the ROI, then return it back to normal (ROI is a state change, so if you change it, all other oprations will use that new ROI, so important to set it back).

there are functions to get and set the ROI, such as:

void cvSetImageROI(IplImage* image, CvRect rect);
void cvResetImageROI(IplImage* image);
vRect cvGetImageROI(const IplImage* image);

that’s what we do w/ countNonZeroInRegion –

cvSetImageROI(cvImage, cvRect(x,y,w,h));
// do something !!
cvResetImageROI( cvImage );

hope that helps!!!