Ask Your Question
1

not rect ROI defined by 4 points

asked 2015-12-20 12:56:24 -0600

giuseppedes gravatar image

updated 2015-12-21 02:54:41 -0600

Hi, I would use the method countNonZero (InputArray src) on a ROI of my image; The ROI is not a rect, but it's defined by the points A, B, C, D like the red zone in the figure.

How can I use this method on this ROI?

image description

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2015-12-21 03:20:43 -0600

LorenaGdL gravatar image

One possible solution

Mat image = imread("ball.jpg", 0);
threshold(image, image, 127, 255, THRESH_BINARY);

//Set your ROI points
Point a = Point(image.cols, 20);
Point b = Point(image.cols, 50);
Point c = Point(image.cols/2, image.rows);
Point d = Point(image.cols/2+50, image.rows);
vector<Point> points;
points.push_back(a);
points.push_back(b);
points.push_back(c);
points.push_back(d);

//Find the corresponding ROI based on the poitns
convexHull(points, points);                         //to assure correct point order
Mat roi(image.rows, image.cols, CV_8U, Scalar(0));  //black image
fillConvexPoly(roi, points, Scalar(255));           //draw ROI in white

//Filter original image according to ROI
Mat filtered_image;             
image.copyTo(filtered_image, roi);          //alternative: bitwise_and(image, roi, filtered_image);
int non_zero = countNonZero(filtered_image);
edit flag offensive delete link more

Comments

@LorenaGdL really good trick use of convexHull to assure correct point order, +1!

pklab gravatar imagepklab ( 2015-12-21 03:53:45 -0600 )edit

This solution works fine, but I have to store points after convexhull in a different vector. Adding:

cerr << "A " << points.at(0).x << " - " << points.at(0).y << endl;
cerr << "B " << points.at(1).x << " - " << points.at(1).y << endl;
cerr << "C " << points.at(2).x << " - " << points.at(2).y << endl;
cerr << "D " << points.at(3).x << " - " << points.at(3).y << endl;

Before and after convexhull i have:

A 300 - 50 B 300 - 20 C 150 - 300 D 200 - 300

A 300 - 50 B 200 - 300 C 150 - 300 D 200 - 300

And the point (300,20) disappear. With:

 vector<Point> points2;


//Find the corresponding ROI based on the poitns
convexHull(points, points2);                         //to assure correct point order

It all works fine!

Thank you

giuseppedes gravatar imagegiuseppedes ( 2015-12-21 04:23:34 -0600 )edit

convexHull(points, points); this code may give wrong results because convexHull() is not suitable for inplace usage now. i think it's the time for feature request.

sturkmen gravatar imagesturkmen ( 2017-08-24 16:17:36 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-12-20 12:56:24 -0600

Seen: 1,755 times

Last updated: Dec 21 '15