OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Thu, 21 Dec 2017 05:16:37 -0600Building contours from pointshttp://answers.opencv.org/question/180725/building-contours-from-points/ Hello !
One of my algorithms constructs points which should represent a contour. My problem with these points is the order.
There is no reason these points have the correct connection order in order to be able to build a contour (for example, passing these to cv::drawContours will result in a mess...).
How should I order these points to satisfy the contour definition of OpenCV ?
Is it even possible ?
Thanks !
Wed, 20 Dec 2017 07:42:58 -0600http://answers.opencv.org/question/180725/building-contours-from-points/Comment by moHe for <p>Hello !</p>
<p>One of my algorithms constructs points which should represent a contour. My problem with these points is the order.
There is no reason these points have the correct connection order in order to be able to build a contour (for example, passing these to cv::drawContours will result in a mess...).</p>
<p>How should I order these points to satisfy the contour definition of OpenCV ?
Is it even possible ?</p>
<p>Thanks !</p>
http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180787#post-id-180787Hey, you should set a rule for points to make up a contour, like: it is a possibly convex, since a set of points can get many valid contours without crossed line.Thu, 21 Dec 2017 05:16:37 -0600http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180787#post-id-180787Comment by amani for <p>Hello !</p>
<p>One of my algorithms constructs points which should represent a contour. My problem with these points is the order.
There is no reason these points have the correct connection order in order to be able to build a contour (for example, passing these to cv::drawContours will result in a mess...).</p>
<p>How should I order these points to satisfy the contour definition of OpenCV ?
Is it even possible ?</p>
<p>Thanks !</p>
http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180739#post-id-180739have you tried sort functions on the vector of points?Wed, 20 Dec 2017 09:37:57 -0600http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180739#post-id-180739Comment by Florent Tomi for <p>Hello !</p>
<p>One of my algorithms constructs points which should represent a contour. My problem with these points is the order.
There is no reason these points have the correct connection order in order to be able to build a contour (for example, passing these to cv::drawContours will result in a mess...).</p>
<p>How should I order these points to satisfy the contour definition of OpenCV ?
Is it even possible ?</p>
<p>Thanks !</p>
http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180734#post-id-180734I can't post any code (I am not allowed to...), but I'll try to give you an example.
Let's say you have found a contour (with cv::findContours for example).
If you shuffle the points in the contour, there is no more reason for it to be a valid contour as the connection order (ie the line segment) is important to draw this contour.
My goal would be to restore the order of the connections.
Hoping this is clearer :)Wed, 20 Dec 2017 09:20:56 -0600http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180734#post-id-180734Comment by amani for <p>Hello !</p>
<p>One of my algorithms constructs points which should represent a contour. My problem with these points is the order.
There is no reason these points have the correct connection order in order to be able to build a contour (for example, passing these to cv::drawContours will result in a mess...).</p>
<p>How should I order these points to satisfy the contour definition of OpenCV ?
Is it even possible ?</p>
<p>Thanks !</p>
http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180726#post-id-180726Can you post some code and examples?Wed, 20 Dec 2017 07:45:55 -0600http://answers.opencv.org/question/180725/building-contours-from-points/?comment=180726#post-id-180726Answer by Pedro Batista for <p>Hello !</p>
<p>One of my algorithms constructs points which should represent a contour. My problem with these points is the order.
There is no reason these points have the correct connection order in order to be able to build a contour (for example, passing these to cv::drawContours will result in a mess...).</p>
<p>How should I order these points to satisfy the contour definition of OpenCV ?
Is it even possible ?</p>
<p>Thanks !</p>
http://answers.opencv.org/question/180725/building-contours-from-points/?answer=180750#post-id-180750OpenCV expects a contour to be a list of points of connected pixels, in which the starting point is the top-most one (with lowest Y coordinate). Then, each point on the list has to be connected with the previous one in a counter-clockwise fashion.
Imagine a circle defined as list of points: the top-most point (lowest Y coordinate) is number one in the list, then follows each point in a counter-clockwise fashion.
Priority goes like this:
Number 1 in list -> top-most point (lowest Y coordinate pixel)
Number n in the list -> connected to the n-1 pixel with the following priority:
1. Lower X coordinate compared to previous point
2. Higher Y coordinate compared to previous point
3. Higher X coordinate compared to previous point
4. Lower Y coordinate compared to previous pointWed, 20 Dec 2017 11:19:13 -0600http://answers.opencv.org/question/180725/building-contours-from-points/?answer=180750#post-id-180750