C# 霍夫峰检测
我已经实现了Hough峰值检测。结果如下: 我们可以看到红线与画布的两侧(左、右)相交 如何将检测到的行限制在源行长度之间? 比如说, 源代码C# 霍夫峰检测,c#,image-processing,hough-transform,C#,Image Processing,Hough Transform,我已经实现了Hough峰值检测。结果如下: 我们可以看到红线与画布的两侧(左、右)相交 如何将检测到的行限制在源行长度之间? 比如说, 源代码 公共类行 { 公共点开始{get;set;} 公共点结束{get;set;} 公共线路(点起点、点终点) { 开始=开始; 结束=结束; } } 公共类HoughLineTransform { 公共HoughMap累加器{get;set;} 公共HoughLineTransform() { } 公共列表获取行(int阈值) { if(累加器==nu
公共类行
{
公共点开始{get;set;}
公共点结束{get;set;}
公共线路(点起点、点终点)
{
开始=开始;
结束=结束;
}
}
公共类HoughLineTransform
{
公共HoughMap累加器{get;set;}
公共HoughLineTransform()
{
}
公共列表获取行(int阈值)
{
if(累加器==null)
{
抛出新异常(“HoughMap为空”);
}
int houghWidth=累加器宽度;
int houghHeight=累加器高度;
int imageWidth=累加器.Image.Width;
int imageHeight=累加器.Image.Height;
列表行=新列表();
if(累加器==null)
回流线;
对于(int-rho=0;rho=阈值)
{
int峰值=累加器[rho,θ];
对于(int-ly=-4;ly=0&&lx+theta峰值)
{
峰值=累加器[rho+ly,θ+lx];
ly=lx=5;
}
}
}
}
中频(峰值>(整数)累加器[rho,θ])
继续;
int-x1,y1,x2,y2;
x1=y1=x2=y2=0;
双rad=θ*Math.PI/180;
如果(θ>=45&&θ,一种简单的方法是在原始图像上覆盖生成的houghline,并检查像素重叠的位置(使用特定窗口)
如果起点或终点重叠,则线段的起点/终点为
顺便说一句:我没有检查过你的算法,但大约15年前我自己写了一个。我记得有一个迭代方法,你每次只找到一行(只是累积图像中的最大值)。
找到一条线后,删除该线的累积像素,然后重新开始查找最大值。
然后找到第二条最重要的线。依此类推。由于HT的性质,它将为没有线段信息的线提供峰值。(例如:虚线的HT贴图类似)。现在,如果要检测线段,可以尝试扫描接近度(取决于HT分辨率)并确定(大多数)点的位置。
public class Line
{
public Point Start { get; set; }
public Point End { get; set; }
public Line(Point start, Point end)
{
Start = start;
End = end;
}
}
public class HoughLineTransform
{
public HoughMap Accumulator { get; set; }
public HoughLineTransform()
{
}
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.Width;
int imageHeight = Accumulator.Image.Height;
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)
{
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)
{
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
{
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;
}
}