master
syneffort 12 months ago
parent 53b16d3288
commit 19af6a4038
  1. 26
      OpenCV/Forms/MainForm.Designer.cs
  2. 34
      OpenCV/Forms/MainForm.cs
  3. 1
      OpenCV/OpenCV.csproj
  4. 76
      OpenCV/OpenCV/Contour.cs
  5. 19
      OpenCV/OpenCV/OpenCVClass.cs

@ -31,6 +31,8 @@
this.btnCanny = new System.Windows.Forms.Button(); this.btnCanny = new System.Windows.Forms.Button();
this.btnSobel = new System.Windows.Forms.Button(); this.btnSobel = new System.Windows.Forms.Button();
this.btnLapace = 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(); this.SuspendLayout();
// //
// btnCanny // btnCanny
@ -63,11 +65,33 @@
this.btnLapace.UseVisualStyleBackColor = true; this.btnLapace.UseVisualStyleBackColor = true;
this.btnLapace.Click += new System.EventHandler(this.btnLapace_Click); 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 // MainForm
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F); this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(470, 365); 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.btnLapace);
this.Controls.Add(this.btnSobel); this.Controls.Add(this.btnSobel);
this.Controls.Add(this.btnCanny); this.Controls.Add(this.btnCanny);
@ -82,5 +106,7 @@
private System.Windows.Forms.Button btnCanny; private System.Windows.Forms.Button btnCanny;
private System.Windows.Forms.Button btnSobel; private System.Windows.Forms.Button btnSobel;
private System.Windows.Forms.Button btnLapace; private System.Windows.Forms.Button btnLapace;
private System.Windows.Forms.Button btnContour;
private System.Windows.Forms.Button btnScanContour;
} }
} }

@ -78,5 +78,39 @@ namespace OpenCV.Forms
converter.Dispose(); 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();
}
} }
} }

@ -84,6 +84,7 @@
<Compile Include="Forms\MainForm.Designer.cs"> <Compile Include="Forms\MainForm.Designer.cs">
<DependentUpon>MainForm.cs</DependentUpon> <DependentUpon>MainForm.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="OpenCV\Contour.cs" />
<Compile Include="OpenCV\Edge.cs" /> <Compile Include="OpenCV\Edge.cs" />
<Compile Include="OpenCV\OpenCVClass.cs" /> <Compile Include="OpenCV\OpenCVClass.cs" />
<Compile Include="Program.cs" /> <Compile Include="Program.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<CvPoint> 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<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(_cvt, contours, CvColor.Yellow, CvColor.Red, 1, 2, LineType.AntiAlias);
//}
foreach (CvSeq<CvPoint> 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;
}
}
}

@ -7,14 +7,16 @@ namespace OpenCV.OpenCV
partial class OpenCVClass : IDisposable partial class OpenCVClass : IDisposable
{ {
IplImage _cvt; IplImage _cvt;
IplImage _gray;
IplImage _bin;
IplConvKernel _convKernel; IplConvKernel _convKernel;
public IplImage GrayScale(IplImage src) public IplImage GrayScale(IplImage src)
{ {
_cvt = new IplImage(src.Size, BitDepth.U8, 1); _gray = new IplImage(src.Size, BitDepth.U8, 1);
Cv.CvtColor(src, _cvt, ColorConversion.BgrToGray); Cv.CvtColor(src, _gray, ColorConversion.BgrToGray);
return _cvt; return _gray;
} }
public IplImage InversionImage(IplImage src) public IplImage InversionImage(IplImage src)
@ -26,9 +28,9 @@ namespace OpenCV.OpenCV
public IplImage Binary(IplImage src, int threshold) public IplImage Binary(IplImage src, int threshold)
{ {
_cvt = GrayScale(src); _bin = GrayScale(src);
Cv.Threshold(_cvt, _cvt, threshold, 255, ThresholdType.Binary); Cv.Threshold(_bin, _bin, threshold, 255, ThresholdType.Binary);
return _cvt; return _bin;
} }
public IplImage Blur(IplImage src) public IplImage Blur(IplImage src)
@ -247,7 +249,10 @@ namespace OpenCV.OpenCV
public virtual void Dispose() public virtual void Dispose()
{ {
Release(_cvt); Release(
_cvt,
_gray,
_bin);
} }
public IplConvKernel ConvKernel public IplConvKernel ConvKernel

Loading…
Cancel
Save