ハフ変換を使って直線を検出し、結果を描画するために情報を取り出したいのですが、なぜかハフ変換後のfor分の中にあるGetSeqElemでエラーが出ます。
コードは以下のようになっています。原因と改善策を教えていただけませんでしょうか?
C#
1using System; 2using System.Collections.Generic; 3using System.ComponentModel; 4using System.Data; 5using System.Drawing; 6using System.Text; 7using System.Windows.Forms; 8using OpenCvSharp; 9using System.Diagnostics; 10 11 12namespace tyouten 13{ 14 class Program 15 { 16 static void Main(string[] args) 17 { 18 19 IplImage imgSrc = new IplImage("C:\pictures\test1.jpg", LoadMode.Color); // 入力画像 20 IplImage imgGray = new IplImage("C:\pictures\test1.jpg", LoadMode.GrayScale); // グレー画像 21 IplImage imgBinary = new IplImage(imgSrc.Size, BitDepth.U8, 1); // 二値化用の画像 22 23 // 範囲外を黒く塗りつぶす 24 Cv.Rectangle(imgSrc, new CvPoint(0, 0), new CvPoint(160, 576), new CvScalar(0, 0, 0), -1); 25 Cv.Rectangle(imgSrc, new CvPoint(900, 0), new CvPoint(1024, 576), new CvScalar(0, 0, 0), -1); 26 27 // グレー画像へ変換 28 Cv.CvtColor(imgSrc, imgGray, ColorConversion.BgrToGray); 29 // 二値化 30 Cv.Threshold(imgGray, imgBinary, 0.01, 255, ThresholdType.Otsu); 31 32 // ノイズ除去 33 Cv.Dilate(imgBinary, imgBinary, null, 1); // 膨張処理 34 Cv.Erode(imgBinary, imgBinary, null, 1); // 収縮処理 35 Cv.Erode(imgBinary, imgBinary, null, 1); // 収縮処理 36 Cv.Dilate(imgBinary, imgBinary, null, 1); // 膨張処理 37 38 // エッジ検出 39 Cv.Canny(imgBinary, imgBinary, 3, 3); 40 41 // ハフ変換 42 CvMemStorage storage = new CvMemStorage(); 43 CvSeq lines = Cv.HoughLines2(imgBinary, storage, HoughLinesMethod.Standard, 1, Math.PI / 180, 120); 44 45 for (int i = 0; i < lines.Total; i++) 46 { 47 CvLineSegmentPolar elem = lines.GetSeqElem(i).Value; 48 float rho = elem.Rho; 49 float theta = elem.Theta; 50 double a = Math.Cos(theta); 51 double b = Math.Sin(theta); 52 double x0 = a * rho; 53 double y0 = b * rho; 54 CvPoint pt1 = new CvPoint(Cv.Round(x0 + 10000 * (-b)), Cv.Round(y0 + 10000 * (a))); 55 CvPoint pt2 = new CvPoint(Cv.Round(x0 - 10000 * (-b)), Cv.Round(y0 - 10000 * (a))); 56 imgSrc.Line(pt1, pt2, CvColor.Red, 1, LineType.AntiAlias, 0); 57 } 58 59 60 Cv.ShowImage("src", imgSrc); 61 Cv.ShowImage("binary", imgBinary); 62 Cv.ShowImage("gray", imgGray); 63 Cv.WaitKey(); 64 Cv.DestroyAllWindows(); 65 } 66 } 67}
回答1件
あなたの回答
tips
プレビュー