| 1 | using System;
|
|---|
| 2 | using System.Collections.Generic;
|
|---|
| 3 | using System.Linq;
|
|---|
| 4 | using System.Text;
|
|---|
| 5 |
|
|---|
| 6 | namespace KwsmLab.OpenCvSharp.Test
|
|---|
| 7 | {
|
|---|
| 8 | /// <summary>
|
|---|
| 9 | /// CvContourScanner Sample
|
|---|
| 10 | /// </summary>
|
|---|
| 11 | class ContourScanner
|
|---|
| 12 | {
|
|---|
| 13 | public ContourScanner()
|
|---|
| 14 | {
|
|---|
| 15 | // create IplImages
|
|---|
| 16 | using (IplImage src = new IplImage(Const.IMAGE_LENNA, LoadMode.Color))
|
|---|
| 17 | using (IplImage gray = new IplImage(src.Size, BitDepth.U8, 1))
|
|---|
| 18 | using (IplImage canny = new IplImage(src.Size, BitDepth.U8, 1))
|
|---|
| 19 | using (IplImage result = src.Clone())
|
|---|
| 20 | {
|
|---|
| 21 | // detect edges
|
|---|
| 22 | Cv.CvtColor(src, gray, ColorConversion.BgrToGray);
|
|---|
| 23 | Cv.Canny(gray, canny, 50, 200);
|
|---|
| 24 |
|
|---|
| 25 | // find all contours
|
|---|
| 26 | using (CvMemStorage storage = new CvMemStorage())
|
|---|
| 27 | {
|
|---|
| 28 | // find contours by CvContourScanner
|
|---|
| 29 | CvContourScanner scanner = Cv.StartFindContours(canny, storage, CvContour<CvPoint>.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple);
|
|---|
| 30 | //CvContourScanner scanner = new CvContourScanner(canny, storage, CvContour<CvPoint>.SizeOf, ContourRetrieval.Tree, ContourChain.ApproxSimple);
|
|---|
| 31 | while (true)
|
|---|
| 32 | {
|
|---|
| 33 | CvSeq<CvPoint> c = Cv.FindNextContour(scanner);
|
|---|
| 34 | if (c == null)
|
|---|
| 35 | break;
|
|---|
| 36 | else
|
|---|
| 37 | result.DrawContours(c, CvColor.Red, CvColor.Green, 0, 3, LineType.AntiAlias);
|
|---|
| 38 | }
|
|---|
| 39 | Cv.EndFindContours(scanner);
|
|---|
| 40 | //scanner.Dispose();
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | // show canny and result
|
|---|
| 44 | using (CvWindow window1 = new CvWindow("ContourScanner canny", canny))
|
|---|
| 45 | using (CvWindow window2 = new CvWindow("ContourScanner result", result))
|
|---|
| 46 | {
|
|---|
| 47 | Cv.WaitKey();
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|