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.

( 2018-01-23 02:38:01 -0600 )edit

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

( 2018-01-23 02:44:43 -0600 )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

( 2018-01-23 03:53:38 -0600 )edit

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

( 2018-01-23 09:57:44 -0600 )edit

Sort by ยป oldest newest most voted
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

more

Why do you want exactly ? remove line :

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

( 2018-01-23 12:21:54 -0600 )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

( 2018-01-23 12:26:01 -0600 )edit

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