diff --git a/OpenCV/Forms/MainForm.Designer.cs b/OpenCV/Forms/MainForm.Designer.cs index a5afd4d..829d745 100644 --- a/OpenCV/Forms/MainForm.Designer.cs +++ b/OpenCV/Forms/MainForm.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { this.btnCanny = new System.Windows.Forms.Button(); + this.btnSobel = new System.Windows.Forms.Button(); + this.btnLapace = new System.Windows.Forms.Button(); this.SuspendLayout(); // // btnCanny @@ -41,11 +43,33 @@ this.btnCanny.UseVisualStyleBackColor = true; this.btnCanny.Click += new System.EventHandler(this.btnCanny_Click); // + // btnSobel + // + this.btnSobel.Location = new System.Drawing.Point(13, 42); + this.btnSobel.Name = "btnSobel"; + this.btnSobel.Size = new System.Drawing.Size(75, 23); + this.btnSobel.TabIndex = 0; + this.btnSobel.Text = "Sobel"; + this.btnSobel.UseVisualStyleBackColor = true; + this.btnSobel.Click += new System.EventHandler(this.btnSobel_Click); + // + // btnLapace + // + this.btnLapace.Location = new System.Drawing.Point(13, 71); + this.btnLapace.Name = "btnLapace"; + this.btnLapace.Size = new System.Drawing.Size(75, 23); + this.btnLapace.TabIndex = 0; + this.btnLapace.Text = "Laplace"; + this.btnLapace.UseVisualStyleBackColor = true; + this.btnLapace.Click += new System.EventHandler(this.btnLapace_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.btnLapace); + this.Controls.Add(this.btnSobel); this.Controls.Add(this.btnCanny); this.Name = "MainForm"; this.Text = "MainForm"; @@ -56,5 +80,7 @@ #endregion private System.Windows.Forms.Button btnCanny; + private System.Windows.Forms.Button btnSobel; + private System.Windows.Forms.Button btnLapace; } } \ No newline at end of file diff --git a/OpenCV/Forms/MainForm.cs b/OpenCV/Forms/MainForm.cs index cce9f84..b032017 100644 --- a/OpenCV/Forms/MainForm.cs +++ b/OpenCV/Forms/MainForm.cs @@ -44,5 +44,39 @@ namespace OpenCV.Forms converter.Dispose(); } + + private void btnSobel_Click(object sender, EventArgs e) + { + IplImage src = GetImage(); + if (src == null) + return; + + OpenCVClass converter = new OpenCVClass(); + IplImage converted = converter.SobelEdge(src); + + CVDialog dlg = new CVDialog(); + dlg.Source = src; + dlg.Converted = converted; + dlg.ShowDialog(); + + converter.Dispose(); + } + + private void btnLapace_Click(object sender, EventArgs e) + { + IplImage src = GetImage(); + if (src == null) + return; + + OpenCVClass converter = new OpenCVClass(); + IplImage converted = converter.LaplaceEdge(src); + + CVDialog dlg = new CVDialog(); + dlg.Source = src; + dlg.Converted = converted; + dlg.ShowDialog(); + + converter.Dispose(); + } } } diff --git a/OpenCV/Libs/dll.zip b/OpenCV/Libs/dll.zip new file mode 100644 index 0000000..e467a2d Binary files /dev/null and b/OpenCV/Libs/dll.zip differ diff --git a/OpenCV/OpenCV.csproj b/OpenCV/OpenCV.csproj index 687d35f..5c1d047 100644 --- a/OpenCV/OpenCV.csproj +++ b/OpenCV/OpenCV.csproj @@ -84,7 +84,7 @@ MainForm.cs - + @@ -106,6 +106,7 @@ True Resources.resx + SettingsSingleFileGenerator Settings.Designer.cs diff --git a/OpenCV/OpenCV/Canny.cs b/OpenCV/OpenCV/Canny.cs deleted file mode 100644 index 6efb918..0000000 --- a/OpenCV/OpenCV/Canny.cs +++ /dev/null @@ -1,22 +0,0 @@ -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 - { - IplImage _canny; - - public IplImage CannyEdge(IplImage src) - { - _canny = new IplImage(src.Size, BitDepth.U8, 1); - Cv.Canny(src, _canny, 100, 255, ApertureSize.Size3); - - return _canny; - } - } -} diff --git a/OpenCV/OpenCV/Edge.cs b/OpenCV/OpenCV/Edge.cs new file mode 100644 index 0000000..74d7157 --- /dev/null +++ b/OpenCV/OpenCV/Edge.cs @@ -0,0 +1,38 @@ +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 CannyEdge(IplImage src) + { + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + Cv.Canny(src, _cvt, 100, 255, ApertureSize.Size3); + + return _cvt; + } + + public IplImage SobelEdge(IplImage src) + { + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + Cv.CvtColor(src, _cvt, ColorConversion.BgrToGray); + Cv.Sobel(_cvt, _cvt, 1, 1, ApertureSize.Size3); + + return _cvt; + } + + public IplImage LaplaceEdge(IplImage src) + { + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + Cv.CvtColor(src, _cvt, ColorConversion.BgrToGray); + Cv.Laplace(_cvt, _cvt, ApertureSize.Size3); + + return _cvt; + } + } +} diff --git a/OpenCV/OpenCV/OpenCVClass.cs b/OpenCV/OpenCV/OpenCVClass.cs index b0ea72f..fa4c0e3 100644 --- a/OpenCV/OpenCV/OpenCVClass.cs +++ b/OpenCV/OpenCV/OpenCVClass.cs @@ -1,76 +1,62 @@ using OpenCvSharp; using System; +using System.Collections.Generic; namespace OpenCV.OpenCV { partial class OpenCVClass : IDisposable { - IplImage _gray; - IplImage _inversion; - IplImage _bin; - IplImage _blur; - IplImage _zoomin; - IplImage _zoomout; - IplImage _resize; - IplImage _slice; - IplImage _symm; - IplImage _rotate; - IplImage _affine; - IplImage _perspective; - IplImage _draw; - IplImage _hsv; - IplImage _hsvRed; - IplImage _morp; + IplImage _cvt; IplConvKernel _convKernel; public IplImage GrayScale(IplImage src) { - _gray = new IplImage(src.Size, BitDepth.U8, 1); - Cv.CvtColor(src, _gray, ColorConversion.BgrToGray); - return _gray; + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + Cv.CvtColor(src, _cvt, ColorConversion.BgrToGray); + return _cvt; } public IplImage InversionImage(IplImage src) { - _inversion = new IplImage(src.Size, BitDepth.U8, 3); - Cv.Not(src, _inversion); - return _inversion; + _cvt = new IplImage(src.Size, BitDepth.U8, 3); + Cv.Not(src, _cvt); + return _cvt; } public IplImage Binary(IplImage src, int threshold) { - _bin = GrayScale(src); - Cv.Threshold(_bin, _bin, threshold, 255, ThresholdType.Binary); - return _bin; + _cvt = GrayScale(src); + Cv.Threshold(_cvt, _cvt, threshold, 255, ThresholdType.Binary); + return _cvt; } public IplImage Blur(IplImage src) { - _blur = new IplImage(src.Size, BitDepth.U8, 3); - Cv.Smooth(src, _blur, SmoothType.Blur, 9); // param1은 홀수, 중간 픽셀 선택을 위함 - return _blur; + _cvt = new IplImage(src.Size, BitDepth.U8, 3); + Cv.Smooth(src, _cvt, SmoothType.Blur, 9); // param1은 홀수, 중간 픽셀 선택을 위함 + return _cvt; } public IplImage ZoomIn(IplImage src) { - _zoomin = new IplImage(new CvSize(src.Width * 2, src.Height * 2) , BitDepth.U8, 3); - Cv.PyrUp(src, _zoomin, CvFilter.Gaussian5x5); - return _zoomin; + _cvt = new IplImage(new CvSize(src.Width * 2, src.Height * 2) , BitDepth.U8, 3); + Cv.PyrUp(src, _cvt, CvFilter.Gaussian5x5); + return _cvt; } public IplImage ZoomOut(IplImage src) { - _zoomout = new IplImage(new CvSize(src.Width / 2, src.Height / 2), BitDepth.U8, 3); - Cv.PyrDown(src, _zoomout, CvFilter.Gaussian5x5); - return _zoomout; + _cvt = new IplImage(new CvSize(src.Width / 2, src.Height / 2), BitDepth.U8, 3); + Cv.PyrDown(src, _cvt, CvFilter.Gaussian5x5); + return _cvt; } public IplImage Resize(IplImage src, double widthRate, double heightRate) { - _resize = new IplImage(new CvSize((int)(src.Width * widthRate), (int)(src.Height * heightRate)), BitDepth.U8, 3); - Cv.Resize(src, _resize, Interpolation.Linear); - return _resize; + _cvt = new IplImage(new CvSize((int)(src.Width * widthRate), (int)(src.Height * heightRate)), BitDepth.U8, 3); + Cv.Resize(src, _cvt, Interpolation.Linear); + return _cvt; } public IplImage Slice(IplImage src, int x, int y, int roiWidht, int roiHeight) @@ -89,24 +75,24 @@ namespace OpenCV.OpenCV //Cv.ResetImageROI(src); CvRect roi = new CvRect(x, y, roiWidht, roiHeight); - _slice = src.Clone(roi); + _cvt = src.Clone(roi); - return _slice; + return _cvt; } public IplImage Symmetry(IplImage src, FlipMode mode) { - _symm = new IplImage(src.Size, BitDepth.U8, 3); - Cv.Flip(src, _symm, mode); - return _symm; + _cvt = new IplImage(src.Size, BitDepth.U8, 3); + Cv.Flip(src, _cvt, mode); + return _cvt; } public IplImage Rotate(IplImage src, double angle) { - _rotate = new IplImage(src.Size, BitDepth.U8, 3); + _cvt = new IplImage(src.Size, BitDepth.U8, 3); CvMat matrix = Cv.GetRotationMatrix2D(new CvPoint2D32f(src.Width / 2, src.Height / 2), angle, 1); - Cv.WarpAffine(src, _rotate, matrix, Interpolation.Linear, CvScalar.ScalarAll(0)); - return _rotate; + Cv.WarpAffine(src, _cvt, matrix, Interpolation.Linear, CvScalar.ScalarAll(0)); + return _cvt; } public IplImage AffineImage(IplImage src, CvPoint2D32f[] srcPoints, CvPoint2D32f[] dstPoints) @@ -114,10 +100,10 @@ namespace OpenCV.OpenCV if (srcPoints.Length != 3 || dstPoints.Length != 3) return null; - _affine = new IplImage(src.Size, BitDepth.U8, 3); + _cvt = new IplImage(src.Size, BitDepth.U8, 3); CvMat matrix = Cv.GetAffineTransform(srcPoints, dstPoints); - Cv.WarpAffine(src, _affine, matrix, Interpolation.Linear, CvScalar.RealScalar(0)); - return _affine; + Cv.WarpAffine(src, _cvt, matrix, Interpolation.Linear, CvScalar.RealScalar(0)); + return _cvt; } public IplImage PerspectiveImage(IplImage src, CvPoint2D32f[] srcPoints, CvPoint2D32f[] dstPoints) @@ -125,36 +111,36 @@ namespace OpenCV.OpenCV if (srcPoints.Length != 4 || dstPoints.Length != 4) return null; - _perspective = new IplImage(src.Size, BitDepth.U8, 3); + _cvt = new IplImage(src.Size, BitDepth.U8, 3); CvMat matrix = Cv.GetPerspectiveTransform(srcPoints, dstPoints); - Cv.WarpPerspective(src, _perspective, matrix, Interpolation.Linear, CvScalar.RealScalar(0)); - return _perspective; + Cv.WarpPerspective(src, _cvt, matrix, Interpolation.Linear, CvScalar.RealScalar(0)); + return _cvt; } public IplImage DrawImage() { - _draw = new IplImage(new CvSize(640, 480), BitDepth.U8, 3); - Cv.DrawLine(_draw, new CvPoint(100, 100), new CvPoint(500, 200), CvColor.Blue, 20); - Cv.DrawCircle(_draw, new CvPoint(200, 200), 50, CvColor.Red, -1); - Cv.DrawRect(_draw, new CvPoint(300, 150), new CvPoint(500, 300), CvColor.Green, 5); - Cv.DrawEllipse(_draw, new CvPoint(150, 400), new CvSize(100, 50), 0, 90, 360, CvColor.Green, -1); - Cv.PutText(_draw, "Open CV", new CvPoint(400, 400), new CvFont(FontFace.HersheyComplex, 1, 1), CvColor.White); - - return _draw; + _cvt = new IplImage(new CvSize(640, 480), BitDepth.U8, 3); + Cv.DrawLine(_cvt, new CvPoint(100, 100), new CvPoint(500, 200), CvColor.Blue, 20); + Cv.DrawCircle(_cvt, new CvPoint(200, 200), 50, CvColor.Red, -1); + Cv.DrawRect(_cvt, new CvPoint(300, 150), new CvPoint(500, 300), CvColor.Green, 5); + Cv.DrawEllipse(_cvt, new CvPoint(150, 400), new CvSize(100, 50), 0, 90, 360, CvColor.Green, -1); + Cv.PutText(_cvt, "Open CV", new CvPoint(400, 400), new CvFont(FontFace.HersheyComplex, 1, 1), CvColor.White); + + return _cvt; } public IplImage HSV(IplImage src) { - _hsv = new IplImage(src.Size, BitDepth.U8, 3); + _cvt = new IplImage(src.Size, BitDepth.U8, 3); IplImage h = new IplImage(src.Size, BitDepth.U8, 1); IplImage s = new IplImage(src.Size, BitDepth.U8, 1); IplImage v = new IplImage(src.Size, BitDepth.U8, 1); - Cv.CvtColor(src, _hsv, ColorConversion.BgrToHsv); - Cv.Split(_hsv, h, s, v, null); + Cv.CvtColor(src, _cvt, ColorConversion.BgrToHsv); + Cv.Split(_cvt, h, s, v, null); - _hsv.SetZero(); + _cvt.SetZero(); // Hue //Cv.InRangeS(h, 20, 30, h); // Yellow @@ -167,7 +153,7 @@ namespace OpenCV.OpenCV Cv.InRangeS(h, 0, 10, lowerRed); Cv.InRangeS(h, 170, 179, upperRed); Cv.AddWeighted(lowerRed, 1.0, upperRed, 1.0, 0.0, red); - Cv.Copy(src, _hsv, red); + Cv.Copy(src, _cvt, red); // Saturation //Cv.InRangeS(s, 20, 30, s); @@ -177,27 +163,27 @@ namespace OpenCV.OpenCV //Cv.InRangeS(v, 20, 30, v); //Cv.Copy(src, hsv, v); - return _hsv; + return _cvt; } public IplImage HSVRed(IplImage src) { - _hsvRed = new IplImage(src.Size, BitDepth.U8, 3); + _cvt = new IplImage(src.Size, BitDepth.U8, 3); IplImage lowerRed = new IplImage(src.Size, BitDepth.U8, 1); IplImage upperRed = new IplImage(src.Size, BitDepth.U8, 1); IplImage red = new IplImage(src.Size, BitDepth.U8, 1); - Cv.CvtColor(src, _hsvRed, ColorConversion.BgrToHsv); + Cv.CvtColor(src, _cvt, ColorConversion.BgrToHsv); - Cv.InRangeS(_hsvRed, new CvScalar(0, 100, 100), new CvScalar(10, 255, 255), lowerRed); - Cv.InRangeS(_hsvRed, new CvScalar(170, 100, 100), new CvScalar(179, 255, 255), upperRed); + Cv.InRangeS(_cvt, new CvScalar(0, 100, 100), new CvScalar(10, 255, 255), lowerRed); + Cv.InRangeS(_cvt, new CvScalar(170, 100, 100), new CvScalar(179, 255, 255), upperRed); Cv.AddWeighted(lowerRed, 1.0, upperRed, 1.0, 0.0, red); - _hsvRed.SetZero(); - Cv.Copy(src, _hsvRed, red); + _cvt.SetZero(); + Cv.Copy(src, _cvt, red); - return _hsvRed; + return _cvt; } private void Release(params IplImage[] images) @@ -211,40 +197,40 @@ namespace OpenCV.OpenCV public IplImage DilateImage(IplImage src, int threshold, int iteration) { - _morp = new IplImage(src.Size, BitDepth.U8, 1); - _bin = Binary(src, threshold); - Cv.Dilate(_bin, _morp, _convKernel, iteration); + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + _cvt = Binary(src, threshold); + Cv.Dilate(_cvt, _cvt, _convKernel, iteration); - return _morp; + return _cvt; } public IplImage ErodeImage(IplImage src, int threshold, int iteration) { - _morp = new IplImage(src.Size, BitDepth.U8, 1); - _bin = Binary(src, threshold); - Cv.Erode(_bin, _morp, _convKernel, iteration); + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + _cvt = Binary(src, threshold); + Cv.Erode(_cvt, _cvt, _convKernel, iteration); - return _morp; + return _cvt; } public IplImage DEImage(IplImage src, int threshold, int iteration) { - _morp = new IplImage(src.Size, BitDepth.U8, 1); - _bin = Binary(src, threshold); - Cv.Dilate(_bin, _morp, _convKernel, iteration); - Cv.Erode(_morp, _morp, _convKernel, iteration); + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + _cvt = Binary(src, threshold); + Cv.Dilate(_cvt, _cvt, _convKernel, iteration); + Cv.Erode(_cvt, _cvt, _convKernel, iteration); - return _morp; + return _cvt; } public IplImage EDImage(IplImage src, int threshold, int iteration) { - _morp = new IplImage(src.Size, BitDepth.U8, 1); - _bin = Binary(src, threshold); - Cv.Erode(_bin, _morp, _convKernel, iteration); - Cv.Dilate(_morp, _morp, _convKernel, iteration); + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + _cvt = Binary(src, threshold); + Cv.Erode(_cvt, _cvt, _convKernel, iteration); + Cv.Dilate(_cvt, _cvt, _convKernel, iteration); - return _morp; + return _cvt; } public IplImage MorphologyImage(IplImage src, MorphologyOperation option, int iteration) @@ -252,34 +238,16 @@ namespace OpenCV.OpenCV if (_convKernel == null) return null; - _morp = new IplImage(src.Size, BitDepth.U8, 1); - _bin = Binary(src, 50); + _cvt = new IplImage(src.Size, BitDepth.U8, 1); + _cvt = Binary(src, 50); - Cv.MorphologyEx(_bin, _morp, _bin, _convKernel, option, iteration); - return _morp; + Cv.MorphologyEx(_cvt, _cvt, _cvt, _convKernel, option, iteration); + return _cvt; } public virtual void Dispose() { - Release( - _gray, - _inversion, - _bin, - _blur, - _zoomin, - _zoomout, - _resize, - _slice, - _symm, - _rotate, - _affine, - _perspective, - _draw, - _hsv, - _hsvRed, - _morp, - _canny - ); + Release(_cvt); } public IplConvKernel ConvKernel