|
|
|
@ -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 |
|
|
|
|