5

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!

Community
  • 1
  • 1
LKB
  • 1,020
  • 5
  • 22
  • 46

1 Answers1

10

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

berak
  • 39,159
  • 9
  • 91
  • 89
  • unsure, if you really wanted the circular mask. otherwise, 'roi' would be already the answer. – berak Oct 08 '14 at 08:30
  • 1
    So I used the eye_cropped (combined roi & mask) image and conducted analysis to see if the eye was red (so I only want to analyse the circle itself). This is OK because the outside of the circle (the Rect) is black. However, now I'm merging the eye_cropped image back onto the main image, this is a problem because of the surrounding black Rect. How would I go about removing the black outside so I can merge the eye (I've changed the eye colour) back onto the main image? `roi.copyTo(plotImage(cv::Rect(person.GetLeftEyePoint().X, person.GetLeftEyePoint().Y, roi.cols, roi.rows)));` – LKB Oct 10 '14 at 07:03
  • 1
    ...Sorry, that was: `croppedEye.copyTo(plotImage(cv::Rect(person.GetLeftEyePoint().X, person.GetLeftEyePoint().Y, croppedEye.cols, croppedEye.rows)));` – LKB Oct 10 '14 at 07:11
  • keep the roi image (without the mask) around, draw into that, merge back to original image ? in other words, keep copies of any operation, you cannot reverse. ? – berak Oct 10 '14 at 07:15
  • Ah okay. Will try that out, thank you!! I currently have this: http://imgur.com/p68mg11 – LKB Oct 10 '14 at 07:16
  • also, careful, the pixels in the roi still point to the orinal image, you want to clone() it , before drawing into that. manipulating eye_cropped is ok, that was a new image already. – berak Oct 10 '14 at 07:20