二値化された画像から図形の輪郭を抽出し、頂点の座標の取得を行っているのですが、画像のように四角形の輪郭の座標しか得られていません。三角形も輪郭を検出しているのはわかるのですが・・・。contoursの指定の仕方が悪いのでしょうか?よければ、教えていただきたいです。よろしくお願いいたします。
C#
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Text; 5using System.Threading.Tasks; 6using OpenCvSharp; 7 8namespace tyouten4 9{ 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 // 入力画像 15 using (IplImage img = new IplImage("C:\pictures\sample2.png", LoadMode.Color)) 16 // グレースケール画像 17 using (IplImage img2 = new IplImage("C:\pictures\sample2.png", LoadMode.GrayScale)) 18 // 二値化用画像 19 using (IplImage binary = img2.Clone()) 20 using (CvMemStorage storage = new CvMemStorage()) 21 { 22 // 二値化 23 Cv.Threshold(img2, binary, 0, 255, ThresholdType.Binary | ThresholdType.Otsu); 24 25 CvSeq<CvPoint> contours; 26 27 // 輪郭検出 28 Cv.FindContours(img2, storage, out contours, CvContour.SizeOf, ContourRetrieval.External, ContourChain.ApproxSimple); 29 30 // 座標から、頂点に点を描画 31 for (int i = 0; i <= contours.Total;) 32 { 33 Console.WriteLine(contours[i]); 34 Cv.Circle(img, new CvPoint(contours[i].Value.X, contours[i].Value.Y), 5, new CvScalar(0, 0, 255)); 35 i++; 36 } 37 38 // 輪郭の描画 39 Cv.DrawContours(img, contours, new CvScalar(0, 0, 255), new CvScalar(0, 255, 0), 3); 40 41 // 画像の表示 42 Cv.ShowImage("img", img); 43 Cv.ShowImage("img2", img2); 44 Cv.ShowImage("binary", binary); 45 Cv.WaitKey(); 46 } 47 48 49 50 51 52 53 } 54 } 55}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。