How to print all coordinates inside Contour opencv

I work with image contour detection. I can easily print the outline coordinates, but I'm interested in printing the coordinates of all the pixels inside the outline. for example, if I get a 4 x 4 pixel outline, than I want to print not only the outline, but all the coordinates of the pixels that it encloses in opencv

+4
source share
4 answers

1- use findContours to extract the outlines of your image. (convert the image to shades of gray, apply the binary threshold and stone edge detection earlier to get better results.)

2- select a contour (by area, shape, moment, etc.)

3 - use pointPolygonTest for all points to check and save whether they are inside the specified path.

+4
source

I'm not sure what you are trying to do:
- draw a filled outline
- get the coordinates of the path and all the points inside it - If you are only interested in drawing, just use the drawContour function and set the thickness parameter to -1 . He will draw a contour and all the points inside it.
I'm not sure that in opencv you can just get all the points that lie inside some path - you can write it yourself or just draw a filled path and get all the non-black points from the image (just use a simple loop). This is not a very effective solution, but should work fine.

+2
source

A simple way (assuming there is no OpenCV function for it) is to find the bounding rectangle, then raster scan the rectangle tracking the winding number (or use pointPolygonTest if efficiency is not a concern).

0
source

A contour is a lot of points. For example, in C ++ (everything looks similar in java and python) Contours are stored in vectors:

 vector<vector<cv::Point>> 

So, for example, if you find the contours this way:

 vector<vector<cv::Point>> contours1; cv::findContours( input_img, contours1, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE ); 

you can get inside each point of the contour. This is an example code that displays the points of a particular contour.

 cv::Mat draw = cv::Mat::zeros( 500,500, CV_8UC3 ); int contour_id = 1; for(int i = 0; i< contour[contour_id].size(); i++) { cout << contour[contour_id][i] << endl; 

// Now draw each point in the form of a circle with a radius = 1

  cv::circle(draw,contour[contour_id][i],1,cv::Scalar(0,0,255)); } 

considers

0
source

Source: https://habr.com/ru/post/1484954/


All Articles