# Finding distance between two curves

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.

(/upfiles/14878459581269807.jpg)

edit retag close merge delete

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

( 2017-02-23 04:53:53 -0600 )edit

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

( 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.

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

Sort by ยป oldest newest most voted

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[])
{
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 dist;
imshow("distance float", dist/255);

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

more

it works,sorry i cant upnote

( 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?

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

It gives length of green segment for each contour point

( 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

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

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

( 2018-07-06 01:44:06 -0600 )edit

@agroms Any update on doing this on python ?

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

Official site

GitHub

Wiki

Documentation