Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I want to cut a sub-purt of an image (or crop it) using Emgu CV (or OpenCV) and calculate average color of that part; looking for changes.

Thanks

like image 717
Kaveh Shahbazian Avatar asked Jun 29 '11 06:06

Kaveh Shahbazian


2 Answers

  1. Set the ROI (Region of Interest) of the image you are working with this will mean any calculation is only done over this area.

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

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

TYPE average = image.GetAverage(image);

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

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

Thanks Chris

like image 73
Chris Avatar answered Nov 07 '22 05:11

Chris


I think that newer versions of OpenCV (2.3+) have a different method of doing ROIs. Here's 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 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 (roi and head in my example) to calculate the average of the region. There is an adjustROI function to move the region of interest and a function locateROI that may also be of use.

like image 21
zzzz Avatar answered Nov 07 '22 04:11

zzzz