如何确定线条的宽度? [英] How to determine the width of the lines?
问题描述
我需要检测这些线的宽度:
I need to detect the width of these lines:
这些线是平行的,并且在它们上有一些噪声.
These lines are parallel and have some noise on them.
当前,我要做的是:
1.使用细化(ZhangSuen)查找中心
1.Find the center using thinning (ZhangSuen)
ZhanSuenThinning(binImage, thin);
2.计算距离变换
cv::distanceTransform(binImage, distImg, CV_DIST_L2, CV_DIST_MASK_5);
3.累计绕中心的一半距离
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.最后,获取实际宽度
4.Finally, get the actual width
width = halfWidth / count * 2;
结果不是很好,在1-2像素左右是错误的.在更大的Image上,结果甚至更糟,有什么建议吗?
The result, isn't quite good, where it's wrong around 1-2 pixels. On bigger Image, the result is even worse, Any suggestion?
推荐答案
您可以采用条形码阅读器算法,这是实现此目的的更快方法.
You can adapt barcode reader algorithms which is the faster way to do it.
扫描水平和垂直线. 令X为与黑线的水平相交的长度,而Y为与垂直相交的长度的Y(如果有一些噪声,则可以计算多个X和Y的中值).
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
所以:宽度= 2 *面积/斜边
So : width = 2 * area / hypotenuse
您也可以使用PCA轻松找到角度.
EDIT : You can also easily find the angle by using PCA.
这篇关于如何确定线条的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!