Ask Your Question
1

Finding distance between two curves

asked 2017-02-23 04:38:45 -0600

agroms gravatar image

Hello, Im trying to add tangents along the curve in the image below, like the red lines in the second picture. Then I would like to use the tangents to find the the 90 degrees normal line to the tangent(the green lines). The goal is to find the distance between the two white lines at different places. I use Python and if anyone have any suggestion on how I could do this, or have any suggestions of a better way, I would be very grateful. image description

image description(/upfiles/14878459581269807.jpg)

edit retag flag offensive close merge delete

Comments

what about cv::distanceTransform (like suggested by @sturkmen )

LBerger gravatar imageLBerger ( 2017-02-23 04:53:53 -0600 )edit

How would I implement the cv::distanceTransform? Cant get it to work :/

agroms gravatar imageagroms ( 2017-02-23 05:56:45 -0600 )edit

Actually, I don't think distance transform is what he wants. That's just the shortest distance, not the perpendicular distance.

I can't think of a particularly fast way of doing perpendicular distances. If you can accept using just the distance to the closest point on the other line, the distance transform is great.

Tetragramm gravatar imageTetragramm ( 2017-02-24 19:11:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-02-25 04:09:01 -0600

LBerger gravatar image

I think this example in C++ should work. Unfortunately I don't know Python DistanceTranform but I think you can use this tutorial in C++ and this one in python to translate it in python. SparseMatrix is only for fun. you don't need it result (in Mat result) is saved in an yml file.

int main(int argc, char* argv[])
{
    Mat img=imread("14878460214049233.jpg",IMREAD_GRAYSCALE);
    imshow("test",img);
    threshold(img,img,200,255,CV_THRESH_BINARY); // to delete some noise
    imshow("test", img);

    Mat labels;
    connectedComponents(img,labels,8,CV_16U);
    Mat result(img.size(),CV_32FC1,Scalar::all(0));
    for (int i = 0; i <= 1; i++)
    {
        Mat mask1 = labels == 1+i;
        Mat mask2 = labels == 1+(1-i);
        Mat masknot;
        bitwise_not(mask1,masknot);
        imshow("masknot", masknot);
        Mat dist;
        distanceTransform(masknot,dist, DIST_L2,5,CV_8U);
        imshow("distance float", dist/255);
        dist.copyTo(result,mask2);

    }
    imshow("distance 1",result);
    FileStorage fs("distCtr.yml",FileStorage::WRITE);
    fs<<"Image"<<result;
    fs.release();
    waitKey();
    SparseMat ms(result);
    SparseMatConstIterator_<float> it = ms.begin<float>(),it_end = ms.end<float>();
    Mat lig(result.rows,1,CV_8U,Scalar::all(0));
    for (; it != it_end; it ++)
    {
        // print element indices and the element value
        const SparseMat::Node* n = it.node();
        if (lig.at<uchar>(n->idx[0])==0)
        {
            cout<< "("<<n->idx[0]<<","<<n->idx[1]<<") = " <<it.value<float>()<<"\t";
            lig.at<uchar>(n->idx[0])=1;
        }
    }
     return 0;
}
edit flag offensive delete link more

Comments

it works,sorry i cant upnote

jsxyhelu gravatar imagejsxyhelu ( 2017-02-25 06:19:36 -0600 )edit

Thanks for the repsonse, I'll try and translate it to Python. But what result does this code give, is it the closest point between the two curves?

agroms gravatar imageagroms ( 2017-03-02 09:45:03 -0600 )edit
1

It gives length of green segment for each contour point

LBerger gravatar imageLBerger ( 2017-03-02 09:47:12 -0600 )edit

the code works. But I'm not getting the result as showed in the (/upfiles/14878459581269807.jpg) . I'm getting 4 images. But none of them are giving me the distances. Can you please help me how to get the distance between the curves

Mathu gravatar imageMathu ( 2017-08-31 15:27:32 -0600 )edit

can u explain clearly, how it is calculating the distance between those 2 curves.

agreat gravatar imageagreat ( 2018-07-06 01:44:06 -0600 )edit

@agroms Any update on doing this on python ?

Esh gravatar imageEsh ( 2018-11-17 22:39:01 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-02-23 04:38:45 -0600

Seen: 6,447 times

Last updated: Feb 25 '17