Ask Your Question

Arrange from one end point to another?

asked 2017-11-14 01:35:07 -0500

Santhosh1 gravatar image

updated 2017-11-16 06:37:19 -0500

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.

Please note

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.

image description

image description

image description

edit retag flag offensive close merge delete



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

LBerger gravatar imageLBerger ( 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.

Santhosh1 gravatar imageSanthosh1 ( 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

LBerger gravatar imageLBerger ( 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.

Santhosh1 gravatar imageSanthosh1 ( 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)

LBerger gravatar imageLBerger ( 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?

Santhosh1 gravatar imageSanthosh1 ( 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)

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

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

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

1 answer

Sort by ยป oldest newest most voted

answered 2017-11-17 00:26:55 -0500

updated 2017-11-17 03:31:03 -0500

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[])

    Mat mSrc= imread("D:\\Conti\\Image Samples\\forum\\Test.png",0);

        cout<< "Invalid Input Image!";
        return 0;

image description

    Mat mSrc_Binary,mSrc_Contours,mEndPoints,mResult;

    //Creating a Binary Image

    //Make sure it is one Pixel width .i.e to replicate a contour Points

    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:
    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

    //We are interested in the pixels whicn are equal to 11

    //Just adding some Visualization!!
    dilate(mEndPoints, mEndPoints, Mat(), Point(-1, -1), 2, 1, 1);//Make it Visible


image description

    return 0;
edit flag offensive delete link 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.

Santhosh1 gravatar imageSanthosh1 ( 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.

Balaji R gravatar imageBalaji R ( 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.

Santhosh1 gravatar imageSanthosh1 ( 2017-11-17 07:08:11 -0500 )edit

Yes we can!

Balaji R gravatar imageBalaji R ( 2017-11-17 08:08:12 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-11-14 01:35:07 -0500

Seen: 584 times

Last updated: Nov 17 '17