Finding distance between two curves

简介: http://answers.opencv.org/question/129819/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.

http://answers.opencv.org/question/129819/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. 
image description
 
image description
 
优质解答:
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;
}
 
解读:
 
1、取反,比用threshthold方便;
     bitwise_not (mask1,masknot);
2、connectedComponents 寻找联通区域
 
官方解释和例子:
 
int cv::connectedComponents  ( InputArray  image,
    OutputArray  labels,
    int  connectivity = 8
    int  ltype = CV_32S 
  )    

computes the connected components labeled image of boolean image

image with 4 or 8 way connectivity - returns N, the total number of labels [0, N-1] where 0 represents the background label. ltype specifies the output label image type, an important consideration based on the total number of labels or alternatively the total number of pixels in the source image.

Parameters
image the 8-bit single-channel image to be labeled
labels destination labeled image
connectivity 8 or 4 for 8-way or 4-way connectivity respectively
ltype output image label type. Currently CV_32S and CV_16U are supported. 

 
# include  <opencv2 /core /utility.hpp >
# include  "opencv2/imgproc.hpp"
# include  "opencv2/imgcodecs.hpp"
# include  "opencv2/highgui.hpp"
# include  <iostream >
using  namespace cv;
using  namespace std;
Mat img;
int threshval  =  100;
static  void on_trackbar( intvoid *)
{
    Mat bw  = threshval  <  128  ? (img  < threshval)  : (img  > threshval);
    Mat labelImage(img.size(), CV_32S);
     int nLabels  = connectedComponents(bw, labelImage,  8);
    std : :vector <Vec3b > colors(nLabels);
    colors[ 0= Vec3b( 000); //background
     for( int label  =  1; label  < nLabels;  ++label){
        colors[label]  = Vec3b( (rand() & 255), (rand() & 255), (rand() & 255) );
    }
    Mat dst(img.size(), CV_8UC3);
     for( int r  =  0; r  < dst.rows;  ++r){
         for( int c  =  0; c  < dst.cols;  ++c){
             int label  = labelImage.at < int >(r, c);
            Vec3b  &pixel  = dst.at <Vec3b >(r, c);
            pixel  = colors[label];
        }
    }
    imshow(  "Connected Components", dst );
}
static  void help()
{
    cout  <<  "\n This program demonstrates connected components and use of the trackbar\n"
         "Usage: \n"
         "  ./connected_components <image(../data/stuff.jpg as default)>\n"
         "The image is converted to grayscale and displayed, another image has a trackbar\n"
         "that controls thresholding and thereby the extracted contours which are drawn in color\n";
}
const  char * keys  =
{
     "{help h||}{@image|../data/stuff.jpg|image for converting to a grayscale}"
};
int main(  int argc,  const  char * * argv )
{
    CommandLineParser parser(argc, argv, keys);
     if (parser.has( "help"))
    {
        help();
         return  0;
    }
    string inputImage  = "twolines.jpg";
    img  = imread(inputImage.c_str(),  0);
     if(img.empty())
    {
        cout  <<  "Could not read input image file: "  << inputImage  << endl;
         return  - 1;
    }
    namedWindow(  "Image"1 );
    imshow(  "Image", img );
    namedWindow(  "Connected Components"1 );
    createTrackbar(  "Threshold""Connected Components"&threshval,  255, on_trackbar );
    on_trackbar(threshval,  0);
    waitKey( 0);
     return  0;
}
但这个例子说不清楚,我自己做一个例子,3条线
         connectedComponents(img,labels, 8,CV_16U);
....
            Mat tmp  = labels  ==  1;
            Mat tmp2  = labels  ==  2;
            Mat tmp3  = labels  ==  3;
     
结果3个联通区域都能够找出来。这个函数之前我不知道,找联通区域是自己写函数实现的。
3、     Mat result(img.size(),CV_32FC1,Scalar::all(0));
这句就是生成全白的图片。应该说Mat::ones一直都不好用,采用直接定义的方式应该是正确的方式。
 补一下数据格式
• CV_8U - 8 -bit unsigned integers ( 0.. 255 )

• CV_8S - 8 -bit signed integers ( - 128.. 127 )

• CV_16U - 16 -bit unsigned integers ( 0.. 65535 )

• CV_16S - 16 -bit signed integers ( - 32768.. 32767 )

• CV_32S - 32 -bit signed integers ( - 2147483648.. 2147483647 )

• CV_32F - 32 -bit floating -point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )

• CV_64F - 64 -bit floating -point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )
 
4、  distanceTransform (masknot,distDIST_L2,5,CV_8U);
 首先对图像进行二值化处理,然后给每个像素赋值为离它最近的背景像素点与其距离(Manhattan距离or欧氏距离),得到distance metric(距离矩阵),那么离边界越远的点越亮。
这个图就能够很好地表示出来。注意在OpenCV中始终是以黑色作为0,也是作为背景的。
 

 

5、      dist . copyTo ( result , mask2 );
带有mask的copy,用法都是平时比较少用的。mask的含义就是只有在mask为255的地方,这个拷贝才有效。
 
6、最为核心的地方到了,这个地方写得非常巧妙
 
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);
    }
 
循环两次,只看一次,在第一次中
mask1 是左边这条线
mask2 是右边这条线
那么,直接mask1做bitwise_not翻转之后,这个时候,mask1上面的这条线是黑色的(0)而背景是白色的(255), distanceTransform计算,那么得到了图像上所有白色区域到这条线的距离。
 
为了把mask2这条线显示出来,直接以mask2为模板,把dist copyto到新的mat里面
 
去,那么留下来的就是mask2上所有到mask1的距离值。
 
非常巧妙,我领悟了半天才明白,赞叹赞叹!
 
7、 SparseMat 稀疏矩阵

 

如果是我做的话,做到这一步可能就直接打印了,但是回答者继续一步
稀疏矩阵意味着只有非0元素会被存储
    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;
        }
    }
 
这段代码也非常棒!它的目的是每一行只取一个值,并且打印出来。
当然,如果资源不成问题的话,直接采用原图循环的方法也很直接。但是我详细稀疏矩阵应该有独特的应用吧。
 
整个解答,思路清晰,代码富有弹性。
 

 

 

     



目前方向:图像拼接融合、图像识别 联系方式:jsxyhelu@foxmail.com
目录
相关文章
|
7月前
|
C++
hackerrank challenges median
只能说这题我想多了,使用普通的插入排序完全可以解决这道题,在查找的时候用二分加快查找速度。 正确解题报告 这道题的关键在于,不能用int,因为两个int相加可能会越界!因为这个WA了好多遍。所以改用long long。 对double,使用math.h中的函数ceil(double)可以取整,根据ceil(v) == v的结果可以判断v是否是整数。
28 0
1142. Maximal Clique (25) 19‘
#include #include #include #include #include using namespace std; int e[201][201]; vector temp; bool judge(){ int flag = 1; for(int i = 0; i < temp.
959 0
|
计算机视觉
How do I get the lowest value of all the non zero value pixels?
How do I get the lowest value of all the non zero value pixels? 问题就是题目:如何从图像中获得非零的最小值。 优质解答: You can use mask : You can use mask :       Mat im = imread("f:/lib/opencv/samples/data/lena.
659 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]PrI.6.1
Given a basis $U=(u_1,\cdots,u_n)$ not necessarily orthonormal, in $\scrH$, how would you compute the biorthogonal basis $\sex{v_1,\cdots,v_n}$? Find ...
654 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.8
Prove that for any matrices $A,B$ we have $$\bex |\per (AB)|^2\leq \per (AA^*)\cdot \per (B^*B). \eex$$ (The corresponding relation for determinants is an easy equality.
561 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.4
If $\dim \scrH=3$, then $\dim \otimes^3\scrH =27$, $\dim \wedge^3\scrH =1$ and $\dim \vee^3\scrH =10$.
683 0
|
资源调度
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.5.5
Show that the inner product $$\bex \sef{x_1\vee \cdots \vee x_k,y_1\vee \cdots\vee y_k} \eex$$ is equal to the permanent of the $k\times k$ matrix $\sex{\sef{x_i,y_j}}$.
533 0
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.2
Show that the following statements are equivalent: (1). $A$ is positive. (2). $A=B^*B$ for some $B$.
717 0
|
关系型数据库
[Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.2.4
(1). The singular value decomposition leads tot eh polar decomposition: Every operator $A$ can be written as $A=UP$, where $U$ is unitary and $P$ is positive.
816 0