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

前端 未结 2 495
忘了有多久
忘了有多久 2021-01-18 08:55

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

2条回答
  •  一个人的身影
    2021-01-18 09:15

    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.

提交回复
热议问题