Getting ROI from a Circle/Point
Asked Answered
H

1

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!

Hurlee answered 8/10, 2014 at 7:4 Comment(2)
how did you obtain the points ?Microsporophyll
I'm using an SDK which analyses/finds centre eye points. I've created my own Person class which gets/sets these values.Hurlee
M
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

Microsporophyll answered 8/10, 2014 at 8:3 Comment(6)
unsure, if you really wanted the circular mask. otherwise, 'roi' would be already the answer.Microsporophyll
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)));Hurlee
...Sorry, that was: croppedEye.copyTo(plotImage(cv::Rect(person.GetLeftEyePoint().X, person.GetLeftEyePoint().Y, croppedEye.cols, croppedEye.rows)));Hurlee
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. ?Microsporophyll
Ah okay. Will try that out, thank you!! I currently have this: imgur.com/p68mg11Hurlee
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.Microsporophyll

© 2022 - 2024 — McMap. All rights reserved.