Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine the width of the lines?

I need to detect the width of these lines:

enter image description here

These lines are parallel and have some noise on them.

Currently, what I do is:

1.Find the center using thinning (ZhangSuen)

ZhanSuenThinning(binImage, thin);

2.Compute the distance transform

cv::distanceTransform(binImage, distImg, CV_DIST_L2, CV_DIST_MASK_5);

3.Accumulate the half distance around the center

double halfWidth = 0.0;
int count = 0;
for(int a = 0; a < thinImg.cols; a++)
    for(int b = 0; b < thinImg.rows; b++)
        if(thinImg.ptr<uchar>(b, a)[0] > 0)
        {
            halfWidth += distImg.ptr<float>(b, a)[0];
            count ++;
        }

4.Finally, get the actual width

width = halfWidth / count * 2;

The result, isn't quite good, where it's wrong around 1-2 pixels. On bigger Image, the result is even worse, Any suggestion?

like image 695
azer89 Avatar asked Feb 27 '14 22:02

azer89


People also ask

What is the width of a line?

The line width (ΔU) is defined as the distance between the points at which the absorption curve slope is maximal.

Does line have a width?

A line is a one-dimensional figure, which has length but no width.


1 Answers

You can adapt barcode reader algorithms which is the faster way to do it.

image

Scan horizontal and vertical lines. Lets X the length of the horizontal intersection with black line an Y the length of the vertical intersection (you can have it be calculating the median value of several X and Y if there are some noise).

X * Y / 2 = area
X²+Y² = hypotenuse²
hypotenuse * width / 2 = area

So : width = 2 * area / hypotenuse

EDIT : You can also easily find the angle by using PCA.

like image 188
Olivier A Avatar answered Oct 07 '22 21:10

Olivier A