1. 利用Canny Method 直線偵測
2. 利用Hough Transform(霍夫轉換)來找線段候選人
3. 利用 GetExteriorAngleDegree 計算目前線段(line)與自訂義水平線(otherline)的夾角
1: private void CalExteriorAngles()
2: {
3: LineSegment2D[] lines = CvInvoke.HoughLinesP(
4: m_img_thresh_gauss.Canny(100, 64),
5: 1, //Distance resolution in pixel-related units
6: Math.PI / 360.0, //Angle resolution measured in radians.
7: 90, //threshold
8: 140, //min Line width
9: 120); //gap between lines
10:
11:
12: Color[] colorArray = new Color[13] { Color.Red, Color.Green, Color.Blue, Color.Honeydew, Color.AliceBlue,
13: Color.AliceBlue, Color.Pink, Color.Indigo, Color.IndianRed, Color.GreenYellow,
14: Color.Lavender, Color.LemonChiffon, Color.LightBlue};
15: Random r = new Random();
16: int ind;
17: //double ramp;
18: double theta = -1;
19: int cnt = 0;
20: m_img_copy = m_img_color.Copy();
21: imageBox11.Image = m_img_copy;
22: listBox1.Items.Clear();
23: LineSegment2D otherline = new LineSegment2D();
24: otherline.P1 = new Point(0, 0);
25: otherline.P2 = new Point(m_img_gray.Width, 0);
26: foreach (LineSegment2D line in lines)
27: {
28: ind = r.Next() % 11 +1;
29: theta = line.GetExteriorAngleDegree(otherline);
30:
31:
32: //this.Text = theta.ToString();
33: //if (theta > 0)
34: {
35: m_img_copy.Draw(line, new Bgr(colorArray[ind]), 12);
36: imageBox11.Image = m_img_copy;
37: cnt++;
38: listBox1.Items.Add(string.Format("#{0} Angle = {1:###.##}",cnt, theta));
39: //imageBox11.Image = m_img_copy;
40: Application.DoEvents();
41: Thread.Sleep(2000);
42: //if (cnt > 0) break;
43: }
44:
45:
46:
47:
48: }
49: imageBox11.Image = m_img_copy;
50:
51: //MessageBox.Show("Done");
52: }
測試圖片1
直線偵測
測試圖片2
直線偵測
1: LineSegment2D[] lines = CvInvoke.HoughLinesP(
2: m_img_thresh_gauss.Canny(100, 64),
3: 1, //Distance resolution in pixel-related units
4: Math.PI / 360.0, //Angle resolution measured in radians.
5: 210, //threshold
6: 260, //min Line width
7: 11); //gap between lines
影片https://youtu.be/Z5FAp9Mz7nw
參考資料:
全站熱搜