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

I want to cut out an image sub-item (or crop it) using Emgu CV (or OpenCV) and calculate the average color of this part; looking for change.

thanks

+6
source share
2 answers
  • Set the ROI (Area of โ€‹โ€‹Interest) of the image with which you work, will mean that any calculation is performed only in this area.

    image.ROI = new rectangle (x, Y, width, height);

  • Calculate the average ROI value, where "TYPE" depends on the Bgr image for the color "Gray" for shades of gray

TYPE average = image.GetAverage (image);

  • When you have finished resetting your ROI image to see the whole image again.

The whole process loops through each pixel, adds its value, and then divides by the total number of pixels. It saves that you write the code yourself.

Thank you, Chris

+12
source

I think newer versions of OpenCV (2.3+) have a different way of doing ROI. This is 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 value of the region. There is an adjustROI function for moving an area of โ€‹โ€‹interest and a locateROI function, which can also be useful.

+1
source

Source: https://habr.com/ru/post/891613/


All Articles