71 lines
2.1 KiB
C#
71 lines
2.1 KiB
C#
using OpenCvSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace ThermoCamUtils
|
|
{
|
|
public class PerspectiveTransform
|
|
{
|
|
Point2f[] srcPoints = new Point2f[] {
|
|
new Point2f(0, 0),
|
|
new Point2f(0, 0),
|
|
new Point2f(0, 0),
|
|
new Point2f(0, 0),
|
|
};
|
|
|
|
Point2f[] dstPoints = new Point2f[] {
|
|
new Point2f(600, 0),
|
|
new Point2f(0, 0),
|
|
new Point2f(0, 400),
|
|
new Point2f(600, 400),
|
|
};
|
|
|
|
protected Mat OriginalImage;
|
|
|
|
|
|
|
|
public PerspectiveTransform(List<System.Drawing.Point> _srcPoints, List<System.Drawing.Point> _dstPoints)
|
|
{
|
|
srcPoints = convertPoints(_srcPoints);
|
|
dstPoints = convertPoints(_dstPoints);
|
|
}
|
|
|
|
protected Point2f[] convertPoints(List<System.Drawing.Point> origList)
|
|
{
|
|
Point2f[] answ = new Point2f[origList.Count];
|
|
for (int i = 0; i < origList.Count; i++)
|
|
{
|
|
answ[i] = new Point2f(origList[i].X, origList[i].Y);
|
|
}
|
|
return answ;
|
|
}
|
|
|
|
public Bitmap convertImage(Bitmap OrigImage, int dimX, int dimY)
|
|
{
|
|
Bitmap answ = OrigImage;
|
|
OriginalImage = OpenCvSharp.Extensions.BitmapConverter.ToMat(OrigImage);
|
|
// verifica siano 4 sorgente e 4 dest...
|
|
if (srcPoints.Length == 4 && dstPoints.Length == 4)
|
|
{
|
|
using var matrix = Cv2.GetPerspectiveTransform(srcPoints, dstPoints);
|
|
using var dst = new Mat(new OpenCvSharp.Size(dimX, dimY), MatType.CV_8UC3);
|
|
Cv2.WarpPerspective(OriginalImage, dst, matrix, dst.Size());
|
|
|
|
answ = MatToBitmap(dst);
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
// This is the function that converts IplImage image
|
|
// into Bitmap
|
|
public static Bitmap MatToBitmap(Mat image)
|
|
{
|
|
return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
|
|
}
|
|
}
|
|
}
|