Ask Your Question
0

Arrange from one end point to another?

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

Santhosh1 gravatar image

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

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

Comments

1

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

LBerger gravatar imageLBerger ( 2017-11-14 03:54:39 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

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

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

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);

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

image description

    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);

    //Just adding some Visualization!!
    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);

image description

    waitKey();
    return 0;
}
edit flag offensive delete link more

Comments

@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 -0600 )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 -0600 )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 -0600 )edit

Yes we can!

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

Question Tools

1 follower

Stats

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

Seen: 146 times

Last updated: Nov 17