# Arrange from one end point to another?

The points found on these contour filtered image are not arrange in order from one end point to the other. They are jumbled up, I want to arrange them from one point to the other.

I have these contour detected by cv2.findContours

I have sliced one of the contour detected to get only points after a particular interval in the numpy array contour[::6]

Here are few images to give you idea of what I mean.

edit retag close merge delete

1

dilate and use thinning. It should be available in python)

( 2017-11-14 03:54:39 -0500 )edit

@LBerger I don't think dilate or thinning is required here. I already have my discrete points(x,y) which I'm interested in. I just need the two end points.

( 2017-11-14 04:27:01 -0500 )edit

If you know the answer to your question why do you ask a question? I see on your image that end points got only one neighbour for a distance less than a threshold. If you have another definition post it

You can use voronoi, dilate and thinning or flann

( 2017-11-14 04:35:27 -0500 )edit

@LBerger I have just selected every 6 consecutive point select[::6] a particular detected contour and plotted them here. The contour detected is not in order from one end to the other end point. It could have been detected from the middle of the contour or any other part of the contour and end at any other point.

( 2017-11-14 05:07:20 -0500 )edit

Ok so try to remove all double points in your array (double point= cross twice in your list)

( 2017-11-14 06:15:04 -0500 )edit

@LBerger I have already removed the duplicate points by using np.unique before select[::6]. Could you please explain what this double point logic is?

( 2017-11-14 06:41:36 -0500 )edit

When you iterate through your array add 1 to an empty image. Some points will be iterate twice : remove this points: end points only one. If thickness line is not one some points will be iterate only one time : use thinin algorithm (you must fill your shape first)

( 2017-11-14 06:47:45 -0500 )edit

@LBerger Points iterated twice have already been remove by using np.unique I still am confused.

( 2017-11-16 05:44:43 -0500 )edit

Sort by ยป oldest newest most voted

Hello Santhosh! Here is a Sample Implementation to find the Edge points of a Contour! Hope this helps!

#include <opencv2\opencv.hpp>
#include "opencv2\opencv_modules.hpp"

using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

if(mSrc.empty())
{
cout<< "Invalid Input Image!";
return 0;
}


    Mat mSrc_Binary,mSrc_Contours,mEndPoints,mResult;

//Creating a Binary Image
threshold(mSrc,mSrc_Binary,1,255,THRESH_BINARY);

//Make sure it is one Pixel width .i.e to replicate a contour Points
thinning(mSrc_Binary,mSrc_Contours);

Mat_<int> Kernal(3,3);
Kernal <<   1, 1, 1,
1,10, 1,
1, 1, 1;

//Multiply the Binary Image with the above Kernal
//Credits & Original Author:
https://stackoverflow.com/questions/26537313/how-can-i-find-endpoints-of-binary-skeleton-image-in-opencv
/*
0   0   1   0  * Kernal 1, 1, 1 => 1    3   12  2
0   1   0   0           1,10, 1    1    11  2   1
0   0   0   0           1, 1, 1    1    1   1   0
*/
filter2D(mSrc_Contours,mEndPoints,CV_8UC1,Kernal);

//We are interested in the pixels whicn are equal to 11
inRange(mEndPoints,11,11,mEndPoints);

cvtColor(mSrc_Binary,mResult,COLOR_GRAY2BGR);
dilate(mEndPoints, mEndPoints, Mat(), Point(-1, -1), 2, 1, 1);//Make it Visible
mResult.setTo(Scalar(0,0,255),mEndPoints);

imshow("mResult",mResult);


    waitKey();
return 0;
}

more

@Balaji R Thanks for the response. Can you please write down the logic so that I can try it out? It would take a long time to just understand the program.

( 2017-11-17 00:49:35 -0500 )edit

I've removed the unnecessary thinning part of the code! The main logic that you are interested is the convolution applied to the Binary Image with the Kernel! Just Imaging you have a line which is represented by 1's and you convolve with the kernel as shown below. Only pixels that have 1 connected white pixel will have the value 11.

( 2017-11-17 03:42:28 -0500 )edit

@Balaji R In short, this can be generalised as finding any point on the contour that doesn't continue further.

( 2017-11-17 07:08:11 -0500 )edit

Yes we can!

( 2017-11-17 08:08:12 -0500 )edit

Official site

GitHub

Wiki

Documentation