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

优质解答:

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(0, 0, 0);//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 ?oating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )

? CV_64F - 64-bit ?oating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )

4、 distanceTransform(masknot,dist, DIST_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;
        }
    }

这段代码也非常棒!它的目的是每一行只取一个值,并且打印出来。

当然,如果资源不成问题的话,直接采用原图循环的方法也很直接。但是我详细稀疏矩阵应该有独特的应用吧。

整个解答,思路清晰,代码富有弹性。

时间: 2024-08-02 18:16:17

Finding distance between two curves的相关文章

Finding Similar Users-Euclidean Distance Score

Purpose: Finding Similar Users Method:  Euclidean Distance Score ex2.py critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5, 'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5, 'The Night Listener': 3.0},'Ge

Geographical distance

Introduction Calculating the distance between geographical coordinates is based on some level of abstraction; it does not provide an exact distance, which is unattainable if one attempted to account for every irregularity in the surface of the earth.

CodeForce 680B - Bear and Finding Criminals

Bear and Finding Criminals There are n cities in Bearland, numbered 1 through n. Cities are arranged in one long row. The distance between cities i and j is equal to |i - j|. Limak is a police officer. He lives in a city a. His job is to catch crimin

CF&amp;&amp;CC百套计划2 CodeChef December Challenge 2017 Chef and Hamming Distance of arrays

https://www.codechef.com/DEC17/problems/CHEFHAM #include<cstdio> #include<cstring> #include<iostream> using namespace std; #define N 100001 int a[N],b[N]; int sum[N],wh[N][2]; int num1[N],num2[N]; void read(int &x) { x=0; char c=getc

Leetcode solution 243: Shortest Word Distance

Problem Statement Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. Example: Assume that words = ["practice", "makes", "perfect", "coding", "

461.求两个数字转成二进制后的“汉明距离” Hamming Distance

public class Solution { public int HammingDistance(int x, int y) { int distance = 0; string sX = Convert.ToString(x, 2); string sY = Convert.ToString(y, 2); int maxLength = Math.Max(sX.Length, sY.Length); //填充0,使两个字符串右对齐 sX = sX.PadLeft(maxLength, '0

LeetCode 72 Edit Distance

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Repla

461. Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, calculate the Hamming distance. Note:0 ≤ x, y < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanation:

2016 ICPC青岛站---k题 Finding Hotels(K-D树)

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=5992 Problem Description There are N hotels all over the world. Each hotel has a location and a price. M guests want to find a hotel with an acceptable price and a minimum distance from their locations.