Canny边缘检测:
用法和opencv中的一致,
Image<Gray,Byte> Image<Gray,Byte>.Canny(double thresh,double threshLinging)
thresh、threshLinging为第一滞后阈值和第二滞后阈值。
private void button1_Click(object sender, EventArgs e)
{
Image<Bgr, Byte> image1 = new Image<Bgr, Byte>(Properties.Resources._2);
Image<Gray, Byte> grayImage = image1.Convert<Gray, Byte>();
Image<Gray, Byte> cannyGray = grayImage.Canny(100, 200);
pictureBox1.Image = cannyGray.ToBitmap();
}
线检测:
1.先调用HoughLinesBinary检测直线,返回一个LineSegment2D[]的数组
LineSegment2D[][] Image<Gray,Byte>.HoughLinesBinary(double rhoResolution,double thetaResolution,int threshold,double minLineWidth,double gapBetweenLines)
rhoResolution:累计像素的距离分辨率
thetaResolution:累计弧度的角度分辨率
minLineWidth:最小的线长度
gapBetweenLines:线段连接之间的距离
2.Draw,把线段画出来:
public void Draw(Cross2DF cross, TColor color, int thickness);
public void Draw(Ellipse ellipse, TColor color, int thickness = 1, LineType lineType = LineType.EightConnected, int shift = 0);
public void Draw(Point[] contours, TColor color, int thickness = 1, LineType lineType = LineType.EightConnected, Point offset = default(Point));
void Image<Bgr,Byte>.Draw(LineSegment2D line,Bgr color,int thicknes,[LineType lineType.EightConnected],[int shift=0])
//线检测
private void button2_Click(object sender, EventArgs e)
{
Image<Gray, Byte> cannyGray = new Image<Gray, Byte>(new Bitmap(pictureBox1.Image));
//调用HoughLinesBinary检测直线,返回一个LineSegment2D[]的数组
LineSegment2D[] lines = cannyGray.HoughLinesBinary(1, Math.PI / 45.0, 20, 30,10)[0];
//画线
Image<Bgr, Byte> imageLines = new Image<Bgr, Byte>(cannyGray.Width, cannyGray.Height);
foreach(LineSegment2D line in lines)
{
//在imageLines上将直线画出
imageLines.Draw(line, new Bgr(Color.DeepSkyBlue), 2);
}
//显示结果
pictureBox2.Image = imageLines.ToBitmap();
}
圆检测:
CircleF[][] HoughCircles(TColor cannyThreshold, TColor accumulatorThreshold, double dp, double minDist, int minRadius = 0, int maxRadius = 0)
TColor cannyThreshold:传递到Canny边缘检测器的两个阈值中较高的阈值
TColor accumlator:中心检测阶段的累加器阈值。它越小,就越能检测到假圆。
double dp:检测圆中心的累计分别率
double minDist:检测圆中心之间的最小距离。
//圆检测
private void button3_Click(object sender, EventArgs e)
{
Image<Bgr, Byte> image1 = new Image<Bgr, Byte>(Properties.Resources._7);
Image<Gray, Byte> gray1 = image1.Convert<Gray, Byte>();
//调用HoughCircles (Canny included)检测圆形
CircleF[] circles = gray1.HoughCircles(new Gray(180), new Gray(120), 2.0, gray1.Width / 8, 0, 0)[0];
//画图
Image<Bgr, Byte> imageCircles = image1.CopyBlank();
foreach(CircleF circle in circles)
{
imageCircles.Draw(circle, new Bgr(Color.Yellow), 5);
}
pictureBox3.Image = imageCircles.ToBitmap();
}
}
效果图: