# How can I divide an image into two specific polygons?

Hi , I am not sure that the question has been asked or not. I have an image shown in first picture. And i detect humans in the picture. But i want to find where the detected humans located. I create two or more polygons in an image shown the second picture and need to find regions of detected humans. For example, suppose i have 5 detected humans in the image and then i need to find how many of them is in region 1 and how many of them is in region 2. I have coordinates of detected humans, and i developed an algorithm in it i compare coordinates of the regions and detected humans' coordinates. But i wonder i can use a different approach in such scenario. I would be appreciated if any help .

edit retag close merge delete

Sort by » oldest newest most voted

if you manage to describe your polygons as a list of points, you can apply pointPolygonTest()

(you don't need to segment the image for this)

more

i think rotatedRectangleIntersection() is also useful

you can try the code below showing the idea ( sorry for lack of explanation):

cv::Mat img = cv::imread("test.png");

cv::RotatedRect rr1(cv::Point2f(0,0),cv::Size2f(660,800),61); //experimentally created

cv::Rect detection1(300,200, 64, 128); // just for test try to change x,y,width,height

cv::RotatedRect rrdetection1(cv::Point2f(detection1.x + (detection1.width / 2), detection1.y + (detection1.height / 2)),
cv::Size2f(detection1.width, detection1.height), 0);

//visualization
cv::Point2f vtx1[4];
rr1.points(vtx1);
for (int i = 0; i < 4; i++)
cv::line(img, vtx1[i], vtx1[(i + 1) % 4], cv::Scalar(0, 0, 255), 2);

std::vector<cv::Point2f> intersections;
cv::rotatedRectangleIntersection(rr1, rrdetection1, intersections);

std::cout << cv::Mat(intersections);
cv::imshow("Canvas", img);
cv::waitKey();

more

Official site

GitHub

Wiki

Documentation