How to create rotated rectangular or polygonal ROI/mask?

1.1k views Asked by At

Let's say i have the following image:

enter image description here

And my region of interest looks like this:

ROI

And i want to have the following result:

enter image description here

How can i achieve this knowing that the ROI is denoted by four points:

 Point pt1(129,9);
 Point pt2(284,108);
 Point pt3(223,205);
 Point pt4(67,106);
1

There are 1 answers

0
herohuyongtao On BEST ANSWER

The idea is to use fillPoly() to fill all the pixels inside the rotated-rectangle/polygon to 0, 255 otherwise:

Mat mask = cv::Mat(img.size(), CV_8UC1, Scalar(255));     // suppose img is your image Mat

vector<vector<Point>> pts = { { pt1, pt2, pt3, pt4 } };
fillPoly(mask, pts, Scalar(0));                           // <- do it here