# How to know the width of the line

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 Could anybody give some advice? I hope to use a algorithm to figure out the radius of the kernel.

edit retag close merge delete

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

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

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 Hard to implement your solution.. See the flowing answer, If you have better thinking, tell me pleasing

Sort by » oldest newest most voted

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

waitKey(0);
return 0;
}

more
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], lines[i]),
Point(lines[i], lines[i]), 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

more

Why do you want exactly ? remove line :

 distanceTransform(select, select_float, DIST_C, 4);
Mat mask = select_float < 3;
waitKey(0);


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

Official site

GitHub

Wiki

Documentation