Getting ROI from a Circle/Point

冷暖自知 提交于 2019-12-09 18:36:22

问题


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!


回答1:


let's restrict it to one eye for simplicity:

// (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;



来源:https://stackoverflow.com/questions/26250968/getting-roi-from-a-circle-point

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!