First time here? Check out the FAQ!

Ask Your Question
0

How to know the width of the line

asked Jan 23 '18

yode gravatar image

updated Jan 23 '18

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.

Preview: (hide)

Comments

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

kbarni gravatar imagekbarni (Jan 23 '18)edit

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

yode gravatar imageyode (Jan 23 '18)edit
1

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 (Jan 23 '18)edit

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

yode gravatar imageyode (Jan 23 '18)edit

3 answers

Sort by » oldest newest most voted
0

answered Jan 23 '18

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 += hist.at<float>(i, 0);
        i++;
    }


    Mat mask = select_float <= i;

    imshow("No line", mask);
    waitKey(0);
return 0;
}
Preview: (hide)
0

answered Jan 23 '18

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

Preview: (hide)

Comments

Why do you want exactly ? remove line :

 distanceTransform(select, select_float, DIST_C, 4);
 Mat mask = select_float < 3;
 imshow("No line", mask);
 waitKey(0);
LBerger gravatar imageLBerger (Jan 23 '18)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 (Jan 23 '18)edit
0

answered Jan 24 '18

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).
Preview: (hide)

Question Tools

1 follower

Stats

Asked: Jan 23 '18

Seen: 1,865 times

Last updated: Jan 24 '18