public IplImage ScannerContour(IplImage src) { con = new IplImage(src.Size, BitDepth.U8, 3); Cv.Copy(src, con); bin = this.Binary(src, 150); CvMemStorage Storage = new CvMemStorage(); //CvSeq<CvPoint> contours; CvContourScanner scanner = Cv.StartFindContours(bin, Storage, CvContour.SizeOf, ContourRetrieval.List, ContourChain.ApproxNone); //while ((contours = Cv.FindNextContour(scanner)) != null) //{ // if (contours[0].Value == new CvPoint(1, 1)) continue; // Cv.DrawContours(con, contours, CvColor.Yellow, CvColor.Red, 1, 4, LineType.AntiAlias); //} //Cv.EndFindContours(scanner); foreach (CvSeq <CvPoint> contours in scanner) { if (contours[0].Value == new CvPoint(1, 1)) { continue; } con.DrawContours(contours, CvColor.Yellow, CvColor.Red, 1, 4, LineType.AntiAlias); } return(con); }
public ContourScanner() { // create IplImages using (IplImage src = new IplImage(Const.ImageLenna, LoadMode.Color)) using (IplImage gray = new IplImage(src.Size, BitDepth.U8, 1)) using (IplImage canny = new IplImage(src.Size, BitDepth.U8, 1)) using (IplImage result = src.Clone()) { // detect edges Cv.CvtColor(src, gray, ColorConversion.BgrToGray); Cv.Canny(gray, canny, 50, 200); // find all contours using (CvMemStorage storage = new CvMemStorage()) { // find contours by CvContourScanner // native style /* * CvContourScanner scanner = Cv.StartFindContours(canny, storage, CvContour.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple); * while (true) * { * CvSeq<CvPoint> c = Cv.FindNextContour(scanner); * if (c == null) * break; * else * Cv.DrawContours(result, c, CvColor.Red, CvColor.Green, 0, 3, LineType.AntiAlias); * } * Cv.EndFindContours(scanner); * //*/ // wrapper style using (CvContourScanner scanner = new CvContourScanner(canny, storage, CvContour.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple)) { foreach (CvSeq <CvPoint> c in scanner) { result.DrawContours(c, CvColor.Red, CvColor.Green, 0, 3, LineType.AntiAlias); } } } // show canny and result using (new CvWindow("ContourScanner canny", canny)) using (new CvWindow("ContourScanner result", result)) { Cv.WaitKey(); } } }
public static IplImage testContours(IplImage target) { if (g_storage == null) { g_gray = new IplImage(target.Size, BitDepth.U8, 1); g_binary = new IplImage(target.Size, BitDepth.U8, 1); g_storage = new CvMemStorage(0); } else { g_storage.Clear(); } CvSeq <CvPoint> contours; target.CvtColor(g_gray, ColorConversion.BgrToGray); g_gray.Threshold(g_gray, g_thresh, 255, ThresholdType.Binary); g_gray.Copy(g_binary); g_gray.FindContours(g_storage, out contours, CvContour.SizeOf, ContourRetrieval.CComp); g_gray.Zero(); if (contours != null) { contours.ApproxPoly(CvContour.SizeOf, g_storage, ApproxPolyMethod.DP, 3, true); g_gray.DrawContours(contours, new CvScalar(255), new CvScalar(128), 100); } //g_gray.Dilate(g_gray, null, 2); //g_gray.Erode(g_gray, null, 2); return(g_gray); }