123 lines
3.9 KiB
C#
123 lines
3.9 KiB
C#
using OpenCvSharp;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Thermo.Cam.Utils
|
|
{
|
|
public class PerspTrans
|
|
{
|
|
#region Private Fields
|
|
|
|
private Point2f[] dstPoints = new Point2f[] {
|
|
new Point2f(600, 0),
|
|
new Point2f(0, 0),
|
|
new Point2f(0, 400),
|
|
new Point2f(600, 400),
|
|
};
|
|
|
|
private Point2f[] srcPoints = new Point2f[] {
|
|
new Point2f(300, 0),
|
|
new Point2f(0, 0),
|
|
new Point2f(0, 200),
|
|
new Point2f(300, 200),
|
|
};
|
|
|
|
#endregion Private Fields
|
|
|
|
#region Protected Fields
|
|
|
|
protected Mat OriginalImage;
|
|
|
|
#endregion Protected Fields
|
|
|
|
#region Public Constructors
|
|
|
|
public PerspTrans(List<System.Drawing.Point> _srcPoints, List<System.Drawing.Point> _dstPoints)
|
|
{
|
|
srcPoints = convDraw2CV(_srcPoints);
|
|
dstPoints = convDraw2CV(_dstPoints);
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Protected Methods
|
|
|
|
/// <summary>
|
|
/// Conversione array di punti Point2f --> System.Drawings
|
|
/// </summary>
|
|
/// <param name="origList"></param>
|
|
/// <returns></returns>
|
|
protected List<System.Drawing.Point> convCV2Draw(Point2f[] origList)
|
|
{
|
|
List<System.Drawing.Point> answ = origList.Select(item => new System.Drawing.Point() { X = (int)item.X, Y = (int)item.Y }).ToList();
|
|
return answ;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Conversione array di punti System.Drawings --> Point2f
|
|
/// </summary>
|
|
/// <param name="origList"></param>
|
|
/// <returns></returns>
|
|
protected Point2f[] convDraw2CV(List<System.Drawing.Point> origList)
|
|
{
|
|
Point2f[] answ = origList.Select(item => new Point2f(item.X, item.Y)).ToArray();
|
|
return answ;
|
|
}
|
|
|
|
#endregion Protected Methods
|
|
|
|
#region Public Methods
|
|
|
|
// This is the function that converts IplImage image
|
|
// into Bitmap
|
|
public static Bitmap MatToBitmap(Mat image)
|
|
{
|
|
return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(image);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Calcola antitrasformata x il punto dallo spazio di destinazione a quello di origine (secondo matrice di trasformazione)
|
|
/// </summary>
|
|
/// <param name="reqPointDest">Punto nelle corrd dello spazio DEST</param>
|
|
/// <returns></returns>
|
|
public List<System.Drawing.Point> getReversedPoint(List<System.Drawing.Point> reqPointDest)
|
|
{
|
|
List<System.Drawing.Point> answ = new List<System.Drawing.Point>();
|
|
// trasformo
|
|
var puntiDest = convDraw2CV(reqPointDest);
|
|
if (srcPoints.Length == 4 && dstPoints.Length == 4)
|
|
{
|
|
using var matrix = Cv2.GetPerspectiveTransform(srcPoints, dstPoints);
|
|
var puntiOrig = Cv2.PerspectiveTransform(puntiDest, matrix);
|
|
if (puntiOrig.Count() > 0)
|
|
{
|
|
answ = convCV2Draw(puntiOrig);
|
|
}
|
|
}
|
|
|
|
return answ;
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |