Emgu学习笔记(四)---Canny、线检测、圆检测

这篇博客介绍了如何使用Emgu库进行图像处理,包括Canny边缘检测,通过设置不同的阈值来获取边缘;线检测通过HoughLinesBinary方法,调整参数以检测不同条件下的直线;以及圆检测,详细阐述了HoughCircles方法的参数含义,并展示了实际应用效果。

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();
        }
    }

效果图:




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值