Search code examples
c++opencvmatroi

Getting ROI from a Circle/Point


I have two points in an image, centre left eye (X, Y) and centre right eye (X, Y). I have drawn circles around both eyes using cv::circle, and this is fine. But what I'm now trying to do is get the ROI of the circles I've drawn, i.e. extract the eyes and save them in a new Mat.

This is my current result:

...But as I said above, just need to work on extracting the circles around the eyes into a new Mat, one for each eye.

This is my code:

cv::Mat plotImage;

plotImage = cv::imread("C:/temp/face.jpg", cv::IMREAD_COLOR);

cv::Point leftEye(person.GetLeftEyePoint().X, person.GetLeftEyePoint().Y);
cv::Point rightEye(person.GetRightEyePoint().X, person.GetRightEyePoint().Y);

cv::circle(plotImage, leftEye, 15, cv::Scalar(255, 255));
cv::circle(plotImage, rightEye, 15, cv::Scalar(255, 255));

cv::imwrite("C:\\temp\\plotImg.jpg", plotImage);

I've found the following links, but I can't seem to make sense of them/apply them to what I'm trying to do: http://answers.opencv.org/question/18784/crop-image-using-hough-circle/

Selecting a Region OpenCV

Define image ROI with OpenCV in C

Any help/guidance is appreciated! Thank you!


Solution

  • let's restrict it to one eye for simplicity:

    enter image description here

    // (badly handpicked coords):
    Point cen(157,215);
    int radius = 15;
    
    //get the Rect containing the circle:
    Rect r(cen.x-radius, cen.y-radius, radius*2,radius*2);
    
    // obtain the image ROI:
    Mat roi(plotImage, r);
    
    // make a black mask, same size:
    Mat mask(roi.size(), roi.type(), Scalar::all(0));
    // with a white, filled circle in it:
    circle(mask, Point(radius,radius), radius, Scalar::all(255), -1);
    
    // combine roi & mask:
    Mat eye_cropped = roi & mask;
    

    enter image description here