diff --git a/OpenCV/Forms/MainForm.Designer.cs b/OpenCV/Forms/MainForm.Designer.cs index 829d745..f7cbad7 100644 --- a/OpenCV/Forms/MainForm.Designer.cs +++ b/OpenCV/Forms/MainForm.Designer.cs @@ -31,6 +31,8 @@ this.btnCanny = new System.Windows.Forms.Button(); this.btnSobel = new System.Windows.Forms.Button(); this.btnLapace = new System.Windows.Forms.Button(); + this.btnContour = new System.Windows.Forms.Button(); + this.btnScanContour = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnCanny @@ -63,11 +65,33 @@ this.btnLapace.UseVisualStyleBackColor = true; this.btnLapace.Click += new System.EventHandler(this.btnLapace_Click); // + // btnContour + // + this.btnContour.Location = new System.Drawing.Point(122, 13); + this.btnContour.Name = "btnContour"; + this.btnContour.Size = new System.Drawing.Size(102, 23); + this.btnContour.TabIndex = 0; + this.btnContour.Text = "Contour"; + this.btnContour.UseVisualStyleBackColor = true; + this.btnContour.Click += new System.EventHandler(this.btnContour_Click); + // + // btnScanContour + // + this.btnScanContour.Location = new System.Drawing.Point(122, 42); + this.btnScanContour.Name = "btnScanContour"; + this.btnScanContour.Size = new System.Drawing.Size(102, 23); + this.btnScanContour.TabIndex = 0; + this.btnScanContour.Text = "ScanContour"; + this.btnScanContour.UseVisualStyleBackColor = true; + this.btnScanContour.Click += new System.EventHandler(this.btnScanContour_Click); + // // MainForm // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(470, 365); + this.Controls.Add(this.btnScanContour); + this.Controls.Add(this.btnContour); this.Controls.Add(this.btnLapace); this.Controls.Add(this.btnSobel); this.Controls.Add(this.btnCanny); @@ -82,5 +106,7 @@ private System.Windows.Forms.Button btnCanny; private System.Windows.Forms.Button btnSobel; private System.Windows.Forms.Button btnLapace; + private System.Windows.Forms.Button btnContour; + private System.Windows.Forms.Button btnScanContour; } } \ No newline at end of file diff --git a/OpenCV/Forms/MainForm.cs b/OpenCV/Forms/MainForm.cs index b032017..26f690d 100644 --- a/OpenCV/Forms/MainForm.cs +++ b/OpenCV/Forms/MainForm.cs @@ -78,5 +78,39 @@ namespace OpenCV.Forms converter.Dispose(); } + + private void btnContour_Click(object sender, EventArgs e) + { + IplImage src = GetImage(); + if (src == null) + return; + + OpenCVClass converter = new OpenCVClass(); + IplImage converted = converter.FindContour(src); + + CVDialog dlg = new CVDialog(); + dlg.Source = src; + dlg.Converted = converted; + dlg.ShowDialog(); + + converter.Dispose(); + } + + private void btnScanContour_Click(object sender, EventArgs e) + { + IplImage src = GetImage(); + if (src == null) + return; + + OpenCVClass converter = new OpenCVClass(); + IplImage converted = converter.ScanContour(src); + + CVDialog dlg = new CVDialog(); + dlg.Source = src; + dlg.Converted = converted; + dlg.ShowDialog(); + + converter.Dispose(); + } } } diff --git a/OpenCV/OpenCV.csproj b/OpenCV/OpenCV.csproj index 5c1d047..c081a08 100644 --- a/OpenCV/OpenCV.csproj +++ b/OpenCV/OpenCV.csproj @@ -84,6 +84,7 @@ MainForm.cs + diff --git a/OpenCV/OpenCV/Contour.cs b/OpenCV/OpenCV/Contour.cs new file mode 100644 index 0000000..1ead02e --- /dev/null +++ b/OpenCV/OpenCV/Contour.cs @@ -0,0 +1,76 @@ +using OpenCvSharp; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OpenCV.OpenCV +{ + internal partial class OpenCVClass : IDisposable + { + + public IplImage FindContour(IplImage src) + { + _cvt = new IplImage(src.Size, BitDepth.U8, 3); + Cv.Copy(src, _cvt); + _bin = Binary(src, 150); + + CvMemStorage storage = new CvMemStorage(); + CvSeq contours; + + Cv.FindContours(_bin, storage, out contours, CvContour.SizeOf, ContourRetrieval.List, ContourChain.ApproxNone); + + //while (contours != null) + //{ + // for (int i = 0; i < contours.Total; i++) + // { + // Cv.DrawCircle(_cvt, contours[i].Value, 3, CvColor.Red); + + // } + + // contours = contours.HNext; + //} + + Cv.DrawContours(_cvt, contours, CvColor.Yellow, CvColor.Red, 1, 2, LineType.AntiAlias); + Cv.ReleaseMemStorage(storage); + Cv.ClearSeq(contours); + + return _cvt; + } + + public IplImage ScanContour(IplImage src) + { + _cvt = new IplImage(src.Size, BitDepth.U8, 3); + Cv.Copy(src, _cvt); + _bin = Binary(src, 150); + + CvMemStorage storage = new CvMemStorage(); + //CvSeq 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(_cvt, contours, CvColor.Yellow, CvColor.Red, 1, 2, LineType.AntiAlias); + //} + + foreach (CvSeq contour in scanner) + { + if (contour[0].Value == new CvPoint(1, 1)) + continue; + + _cvt.DrawContours(contour, CvColor.Yellow, CvColor.Red, 1, 2, LineType.AntiAlias); + } + + //Cv.EndFindContours(scanner); + + Cv.ReleaseMemStorage(storage); + + return _cvt; + } + } +} diff --git a/OpenCV/OpenCV/OpenCVClass.cs b/OpenCV/OpenCV/OpenCVClass.cs index fa4c0e3..12c07a7 100644 --- a/OpenCV/OpenCV/OpenCVClass.cs +++ b/OpenCV/OpenCV/OpenCVClass.cs @@ -7,14 +7,16 @@ namespace OpenCV.OpenCV partial class OpenCVClass : IDisposable { IplImage _cvt; + IplImage _gray; + IplImage _bin; IplConvKernel _convKernel; public IplImage GrayScale(IplImage src) { - _cvt = new IplImage(src.Size, BitDepth.U8, 1); - Cv.CvtColor(src, _cvt, ColorConversion.BgrToGray); - return _cvt; + _gray = new IplImage(src.Size, BitDepth.U8, 1); + Cv.CvtColor(src, _gray, ColorConversion.BgrToGray); + return _gray; } public IplImage InversionImage(IplImage src) @@ -26,9 +28,9 @@ namespace OpenCV.OpenCV public IplImage Binary(IplImage src, int threshold) { - _cvt = GrayScale(src); - Cv.Threshold(_cvt, _cvt, threshold, 255, ThresholdType.Binary); - return _cvt; + _bin = GrayScale(src); + Cv.Threshold(_bin, _bin, threshold, 255, ThresholdType.Binary); + return _bin; } public IplImage Blur(IplImage src) @@ -247,7 +249,10 @@ namespace OpenCV.OpenCV public virtual void Dispose() { - Release(_cvt); + Release( + _cvt, + _gray, + _bin); } public IplConvKernel ConvKernel