Ask Your Question

How to know the width of the line

asked 2018-01-23 02:26:15 -0500

yode gravatar image

updated 2018-01-23 02:28:33 -0500

I want to remove the line with opening. but I have to know the width of the line to set the kernel in the follow image

image description

Could anybody give some advice? I hope to use a algorithm to figure out the radius of the kernel.

edit retag flag offensive close merge delete


Try the distance transform on the image and the mean value along the line will give the width.

kbarni gravatar imagekbarni ( 2018-01-23 02:38:01 -0500 )edit

Actually I have done the distance transform, but I don't know how to pick the width from those distance

yode gravatar imageyode ( 2018-01-23 02:44:43 -0500 )edit

you can use thinning function to get sketon line.

If you've got problem with square you can mixed distance transform and thinning results using a mask to filter line width

LBerger gravatar imageLBerger ( 2018-01-23 03:53:38 -0500 )edit

@LBerger Hard to implement your solution.. See the flowing answer, If you have better thinking, tell me pleasing

yode gravatar imageyode ( 2018-01-23 09:57:44 -0500 )edit

3 answers

Sort by ยป oldest newest most voted

answered 2018-01-23 13:27:06 -0500

LBerger gravatar image

Using 0.8 instead of 3 :

#include <opencv2/opencv.hpp> 
#include <opencv2/ximgproc.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char **argv)
    Mat select = imread("line.png", IMREAD_GRAYSCALE);
    Mat thinning_select;
    ximgproc::thinning(select, thinning_select, ximgproc::THINNING_ZHANGSUEN);
    Mat select_float;
    distanceTransform(select, select_float, DIST_C, 5);
    Mat img;
    // Quantize the hue to 30 levels
    // and the saturation to 32 levels
    int bins = 256;
    int histSize[] = { bins};
    float range[] = { 0, 256 };
    const float* ranges[] = { range};
    // we compute the histogram from the 0-th and 1-st channels
    int channels[] = { 0 };
    Mat hist;
    calcHist(&select_float, 1, channels, thinning_select, hist, 1, histSize, ranges,true,false);
    double tailleSquelette = countNonZero(thinning_select);
    double ratio = 0;
    int i = 0;
    while (ratio < tailleSquelette*0.8 && i<hist.rows)
        ratio +=<float>(i, 0);

    Mat mask = select_float <= i;

    imshow("No line", mask);
return 0;
edit flag offensive delete link more

answered 2018-01-23 09:57:07 -0500

yode gravatar image
Mat thinning_select=imread("image.png",0);
ximgproc::thinning(select, thinning_select, ximgproc::THINNING_ZHANGSUEN);

vector<Vec4i> lines;
HoughLinesP(thinning_select, lines, 1, CV_PI / 180, 80, 30,3);

Mat show_line = thinning_select.clone() = 0;
for (size_t i = 0; i < lines.size(); i++)
    line(show_line, Point(lines[i][0], lines[i][1]),
        Point(lines[i][2], lines[i][3]), Scalar(255), 3);
Mat select_float;
distanceTransform(select, select_float, DIST_C, 3);
double t_min, t_max;
Point min_point, max_point;
minMaxLoc(select_float, &t_min, &t_max, &min_point, &max_point, show_line);
cout << t_max << endl;

I will get the result 4 as the above code

edit flag offensive delete link more


Why do you want exactly ? remove line :

 distanceTransform(select, select_float, DIST_C, 4);
 Mat mask = select_float < 3;
 imshow("No line", mask);
LBerger gravatar imageLBerger ( 2018-01-23 12:21:54 -0500 )edit

In your this comment, you just guess out a 3. But I hope to figure out this threshold to used in my opening kernel. @LBerger

yode gravatar imageyode ( 2018-01-23 12:26:01 -0500 )edit

answered 2018-01-24 06:16:00 -0500

moHe gravatar image

I've a simple idea, may this can help you:)

  1. Do some morphology to eliminate the noise.
  2. Straighten the line, correct its angle to make it horizontal: use radon_transformation or houghline to get the rotating angle then warpaffine it.
  3. Finally get the number of white pixels on one column which is just the width (or you can get the averge of thoses on the whole line or part of it to make the result more stable).
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2018-01-23 02:26:15 -0500

Seen: 1,413 times

Last updated: Jan 24 '18