【发布时间】:2018-08-17 00:31:02
【问题描述】:
我想 detect longest line 使用霍夫变换在图像中。
输入图像
预期输出
当前输出
我们可以看到它检测到了错误的行。
在下面的代码中,我应该在哪里寻找错误?
不过有一个问题。如果我将阈值从 50 增加到 150,源代码似乎会产生正确的输出。但是,对我来说,这没有任何意义,因为增加的阈值意味着排除低投票的行。
。
源代码
HoughLineTransform.cs
public class Line
{
public Point Start { get; set; }
public Point End { get; set; }
public int Length
{
get
{
return (int)Math.Sqrt(Math.Pow(End.X - Start.X, 2) + Math.Pow(End.Y - Start.Y, 2)); ;
}
}
public Line()
{
}
public Line(Point start, Point end)
{
Start = start;
End = end;
}
}
public class HoughLineTransform
{
public HoughMap Accumulator { get; set; }
public HoughLineTransform() {}
public Line GetLongestLine()
{
List<Line> lines = GetLines(50);
int maxIndex = 0;
double maxLength = -1.0;
for (int i = 0; i < lines.Count; i++)
{
if (maxLength < lines[i].Length)
{
maxIndex = i;
maxLength = lines[i].Length;
}
}
return lines[maxIndex];
}
public List<Line> GetLines(int threshold)
{
if (Accumulator == null)
{
throw new Exception("HoughMap is null");
}
int houghWidth = Accumulator.Width;
int houghHeight = Accumulator.Height;
int imageWidth = Accumulator.Image.GetLength(0);
int imageHeight = Accumulator.Image.GetLength(1);
List<Line> lines = new List<Line>();
if (Accumulator == null)
return lines;
for (int rho = 0; rho < houghWidth; rho++)
{
for (int theta = 0; theta < houghHeight; theta++)
{
if ((int)Accumulator[rho, theta] > threshold)
{
//Is this point a local maxima (9x9)
int peak = Accumulator[rho, theta];
for (int ly = -4; ly <= 4; ly++)
{
for (int lx = -4; lx <= 4; lx++)
{
if ((ly + rho >= 0 && ly + rho < houghWidth) && (lx + theta >= 0 && lx + theta < houghHeight))
{
if ((int)Accumulator[rho + ly, theta + lx] > peak)
{
peak = Accumulator[rho + ly, theta + lx];
ly = lx = 5;
}
}
}
}
if (peak > (int)Accumulator[rho, theta])
continue;
int x1, y1, x2, y2;
x1 = y1 = x2 = y2 = 0;
double rad = theta * Math.PI / 180;
if (theta >= 45 && theta <= 135)
{
//y = (r - x Math.Cos(t)) / Math.Sin(t)
x1 = 0;
y1 = (int)(((double)(rho - (houghWidth / 2)) - ((x1 - (imageWidth / 2)) * Math.Cos(rad))) / Math.Sin(rad) + (imageHeight / 2));
x2 = imageWidth - 0;
y2 = (int)(((double)(rho - (houghWidth / 2)) - ((x2 - (imageWidth / 2)) * Math.Cos(rad))) / Math.Sin(rad) + (imageHeight / 2));
}
else
{
//x = (r - y Math.Sin(t)) / Math.Cos(t);
y1 = 0;
x1 = (int)(((double)(rho - (houghWidth / 2)) - ((y1 - (imageHeight / 2)) * Math.Sin(rad))) / Math.Cos(rad) + (imageWidth / 2));
y2 = imageHeight - 0;
x2 = (int)(((double)(rho - (houghWidth / 2)) - ((y2 - (imageHeight / 2)) * Math.Sin(rad))) / Math.Cos(rad) + (imageWidth / 2));
}
lines.Add(new Line(new Point(x1, y1), new Point(x2, y2)));
}
}
}
return lines;
}
}
【问题讨论】:
-
这是您的实现还是您从其他地方获得的?
-
c/c++代码在哪里?
-
我懒得阅读所有代码。您是否将像素数作为线长(即累加器中 bin 的值)?像素数不能很好地替代线长。但是由于您也知道角度,因此您可以根据该计数进行补偿并获得相当准确的线长度估计值。
标签: c# image-processing hough-transform