[ACCEPTED]-How to cut a sub-part of an image using Emgu CV (or OpenCV)?-emgucv

Accepted answer
Score: 13
  1. Set the ROI (Region of Interest) of the 10 image you are working with this will mean 9 any calculation is only done over this area.

    image.ROI 8 = new Rectangle(x,Y,Width,Height);

  2. Calculate 7 the Average of the ROI where "TYPE" is image 6 dependant Bgr for colour Gray for Grayscale

TYPE 5 average = image.GetAverage(image);

  1. When youve finished reset your image ROI so you can see the whole image again.

All the 4 process does is loop through each pixel 3 adds its value then divides by the total 2 number of pixels. Saves you writing the 1 code yourself.

Thanks Chris

Score: 3

I think that newer versions of OpenCV (2.3+) have 7 a different method of doing ROIs. Here's 6 what the manual says:

// create a new 320x240 image
Mat img(Size(320,240),CV_8UC3);
// select a ROI
Mat roi(img, Rect(10,10,100,100));
// fill the ROI with (0,255,0) (which is green in RGB space);
// the original 320x240 image will be modified
roi = Scalar(0,255,0);

Here is what I did 5 in one instance:

// adding a header on top of image
Mat dst = Mat::zeros(frame.rows + HEADER_HEIGHT, frame.cols, CV_8UC3); 
// frame portion
Mat roi(dst, Rect(0, HEADER_HEIGHT-1, frame.cols, frame.rows));
// header portion
Mat head(dst, Rect(0,0,frame.cols, HEADER_HEIGHT));
// zeros to clear the header portion
Mat zhead = Mat::zeros(head.rows, head.cols, CV_8UC3);

frame.copyTo(roi); // copy new image to image portion of dst
zhead.copyTo(head); // clear the header portion of dst

You can use any of the subframes 4 (roi and head in my example) to calculate the average 3 of the region. There is an adjustROI function to 2 move the region of interest and a function 1 locateROI that may also be of use.

More Related questions