You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openCV/OpenCV/OpenCVClass.cs

163 lines
5.1 KiB

3 years ago
using OpenCvSharp;
using System;
namespace OpenCV
{
class OpenCVClass : IDisposable
{
IplImage gray;
IplImage inversion;
IplImage bin;
3 years ago
IplImage blur;
3 years ago
IplImage zoomin;
IplImage zoomout;
3 years ago
IplImage resize;
IplImage slice;
3 years ago
IplImage symm;
IplImage rotate;
IplImage affine;
IplImage perspective;
IplImage draw;
3 years ago
public IplImage GrayScale(IplImage src)
{
gray = new IplImage(src.Size, BitDepth.U8, 1);
Cv.CvtColor(src, gray, ColorConversion.BgrToGray);
return gray;
}
public IplImage InversionImage(IplImage src)
{
inversion = new IplImage(src.Size, BitDepth.U8, 3);
Cv.Not(src, inversion);
return inversion;
}
public IplImage Binary(IplImage src)
{
bin = GrayScale(src);
Cv.Threshold(bin, bin, 150, 255, ThresholdType.Binary);
return bin;
}
3 years ago
public IplImage Blur(IplImage src)
{
blur = new IplImage(src.Size, BitDepth.U8, 3);
Cv.Smooth(src, blur, SmoothType.Blur, 9); // param1은 홀수, 중간 픽셀 선택을 위함
return blur;
}
3 years ago
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;
}
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;
}
3 years ago
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;
}
public IplImage Slice(IplImage src, int x, int y, int roiWidht, int roiHeight)
{
//slice = new IplImage(new CvSize(roiWidht, roiHeight), BitDepth.U8, 3);
//src.ROI = new CvRect(x, y, roiWidht, roiHeight);
//src.SetROI(new CvRect(x, y, roiWidht, roiHeight));
//Cv.SetImageROI(src, new CvRect(x, y, roiWidht, roiHeight));
//Cv.Copy(src, slice);
//Cv.Resize(src, slice);
//slice = src.Clone(); // 속성까지 복사되어 채널 등 값이 변경됨
//src.ResetROI();
//Cv.ResetImageROI(src);
CvRect roi = new CvRect(x, y, roiWidht, roiHeight);
slice = src.Clone(roi);
return slice;
}
3 years ago
public IplImage Symmetry(IplImage src, FlipMode mode)
{
symm = new IplImage(src.Size, BitDepth.U8, 3);
Cv.Flip(src, symm, mode);
return symm;
}
public IplImage Rotate(IplImage src, double angle)
{
rotate = 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;
}
public IplImage AffineImage(IplImage src, CvPoint2D32f[] srcPoints, CvPoint2D32f[] dstPoints)
{
if (srcPoints.Length != 3 || dstPoints.Length != 3)
return null;
affine = 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;
}
public IplImage PerspectiveImage(IplImage src, CvPoint2D32f[] srcPoints, CvPoint2D32f[] dstPoints)
{
if (srcPoints.Length != 4 || dstPoints.Length != 4)
return null;
perspective = 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;
}
public IplImage DrawImage()
{
draw = new IplImage(new CvSize(640, 480), BitDepth.U8, 3);
}
3 years ago
private void Release(params IplImage[] images)
{
for (int i = 0; i < images.Length; i++)
{
if (images[i] != null)
Cv.ReleaseImage(images[i]);
}
}
3 years ago
public void Dispose()
{
3 years ago
Release(
gray,
inversion,
bin,
blur,
zoomin,
zoomout,
resize,
slice,
symm,
rotate,
affine,
perspective,
draw
3 years ago
);
3 years ago
}
}
}