1. 程式人生 > >Emgu學習筆記(四)---Canny、線檢測、圓檢測

Emgu學習筆記(四)---Canny、線檢測、圓檢測

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

效果圖: