hisence-yolo-detection/XKRS.UI/Defect.cs
2024-06-17 11:02:28 +08:00

435 lines
11 KiB
C#

using Microsoft.VisualBasic.ApplicationServices;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Text;
using XKRS.UI.Helper;
using System.Threading.Tasks;
namespace XKRS.UI
{
public class Defect
{
public string File { get; private set; }
public bool Selected { get; private set; }
private Rectangle _rectangle { get; }
public GraphicsPath Path { get; set; }
public Bitmap OriBitMap { get; set; }
public PointF _defectCenterPoint { get; }
private float _wholeDScale = 1f;
private float _xDScale = 1f;
private float _yDScale = 1f;
private float[] _lastSetScale = new float[3] { 1f, 1f, 1f };
private double _dRoute;
private double _totalDRoute;
private PointF _initialPosition;
private PointF _lastSetPosition;
private float _xDMove;
private float _yDMove;
private Brush[] _brushDColors = new Brush[5]
{ Brushes.Red, Brushes.Yellow, Brushes.Blue, Brushes.Green, Brushes.Black };
private int _selectedColorIndex = 0;
private Matrix _matrix;
private PointF _upperLeftPoint;
/// <summary>
/// 每个缺陷自身上一次赋值的缩放值
/// </summary>
public float[] LastSetScale
{
get
{
return _lastSetScale;
}
set
{
_lastSetScale = value;
}
}
/// <summary>
/// 每个缺陷自身的整体缩放比例
/// </summary>
public float WholeDScale
{
get
{
return _wholeDScale;
}
set
{
_wholeDScale = value;
}
}
/// <summary>
/// 每个缺陷自身的横向缩放
/// </summary>
public float XDScale
{
get
{
return _xDScale;
}
set
{
_xDScale = value;
}
}
/// <summary>
/// 每个缺陷自身的竖向缩放
/// </summary>
public float YDScale
{
get
{
return _yDScale;
}
set
{
_yDScale = value;
}
}
/// <summary>
/// 每个缺陷自身旋转过程中的角度
/// </summary>
public double DRoute
{
get
{
return Math.Ceiling(_dRoute % 360);
}
set
{
_dRoute = value;
}
}
/// <summary>
/// 每个缺陷自身旋转的总角度
/// </summary>
public double TotalDRoute
{
get
{
if (_totalDRoute % 360 > 0)
{
return _totalDRoute % 360;
}
else
{
return (_totalDRoute + 360) % 360;
}
}
set
{
_totalDRoute = value;
}
}
/// <summary>
/// 每个缺陷自身开始加载时中心点在控件坐标系中的坐标
/// </summary
public PointF InitialPosition
{
get
{
return _initialPosition;
}
set
{
_initialPosition = value;
}
}
/// <summary>
/// 上一次赋值的位移终点
/// </summary>
public PointF LastSetPosition
{
get
{
return _lastSetPosition;
}
set
{
_lastSetPosition = value;
}
}
/// <summary>
/// 每个缺陷自身在控件坐标系X轴方向上的位移
/// </summary>
public float XDMove
{
get
{
return (int)_xDMove;
}
set
{
_xDMove = value;
}
}
/// <summary>
/// 每个缺陷自身在空间坐标系Y轴方向上的位移
/// </summary>
public float YDMove
{
get
{
return (int)_yDMove;
}
set
{
_yDMove = value;
}
}
/// <summary>
/// 每个缺陷自身的边框颜色
/// </summary>
public Brush[] BrushDColor
{
get
{
return _brushDColors;
}
}
/// <summary>
/// 每个缺陷自身在调整颜色控件中选择的边框颜色序列
/// </summary>
public int SelectedColorIndex
{
get
{
return _selectedColorIndex;
}
set
{
_selectedColorIndex = value;
}
}
/// <summary>
/// 每个缺陷自身的矩阵,该矩阵需要左乘背景图的矩阵才是最终看到的显示效果
/// </summary>
public Matrix PrivateMatrix
{
get
{
return _matrix;
}
private set
{
_matrix = value;
}
}
/// <summary>
/// 每个缺陷自身的左上角顶点坐标
/// </summary>
public PointF UpperLeftPoint
{
get
{
return _upperLeftPoint;
}
private set
{
_upperLeftPoint = value;
}
}
public Defect(string file) : this(file, new Matrix())
{
}
public Defect(string file, Matrix m)
{
File = file;
using (Stream s = System.IO.File.Open(file, FileMode.Open))
{
OriBitMap = (Bitmap)Image.FromStream(s);
}
PrivateMatrix = m;
UpperLeftPoint = new PointF(0, 0);
_defectCenterPoint = new PointF(UpperLeftPoint.X + OriBitMap.Width / 2, UpperLeftPoint.Y + OriBitMap.Height / 2);
_rectangle = new Rectangle((int)UpperLeftPoint.X, (int)UpperLeftPoint.Y, OriBitMap.Width, OriBitMap.Height);
Path = new GraphicsPath();
Path.AddRectangle(_rectangle);
Path.CloseAllFigures();
}
public Defect(string file, PointF[] points, Matrix m)
{
File = file;
using (Stream s = System.IO.File.Open(file, FileMode.Open))
{
Bitmap midBitmap = (Bitmap)Image.FromStream(s);
Path = new GraphicsPath();
Path.AddPolygon(points);
Path.CloseAllFigures();
OriBitMap = BitmapCrop(midBitmap, Path);
}
PrivateMatrix = m;
RectangleF rec = Path.GetBounds();
UpperLeftPoint = new PointF(rec.X, rec.Y);
_defectCenterPoint = new PointF(UpperLeftPoint.X + OriBitMap.Width / 2, UpperLeftPoint.Y + OriBitMap.Height / 2);
_rectangle = new Rectangle((int)rec.X, (int)rec.Y, OriBitMap.Width, OriBitMap.Height);
PrivateMatrix.Translate(-rec.X, -rec.Y);
}
public Defect(Rectangle rectangle, Matrix m)
{
Path = new GraphicsPath();
Path.AddRectangle(rectangle);
Path.CloseAllFigures();
OriBitMap = BitmapCropRect(Path);
PrivateMatrix = m;
RectangleF rec = Path.GetBounds();
UpperLeftPoint = new PointF(rec.X, rec.Y);
_defectCenterPoint = new PointF(UpperLeftPoint.X + OriBitMap.Width / 2, UpperLeftPoint.Y + OriBitMap.Height / 2);
_rectangle = new Rectangle((int)rec.X, (int)rec.Y, OriBitMap.Width, OriBitMap.Height);
PrivateMatrix.Translate(-rec.X, -rec.Y);
}
public Bitmap BitmapCropRect( GraphicsPath path)
{
RectangleF rec = path.GetBounds();
int left = (int)rec.Left;
int top = (int)rec.Top;
int width = (int)rec.Width;
int height = (int)rec.Height;
Bitmap imgCropped = new Bitmap(width, height);
//Bitmap img = (Bitmap)bitmap.Clone();
Region r = new Region(path);
for (int i = left; i < left + width; i++)
{
for (int j = top; j < top + height; j++)
{
if (!r.IsVisible(i, j))
{
imgCropped.SetPixel(i - left, j - top, Color.FromArgb(0, 255, 255, 255));
}
else
{
imgCropped.SetPixel(i - left, j - top, Color.FromArgb(0, 0, 255, 255));
}
}
}
return imgCropped;
}
public Bitmap BitmapCrop(Bitmap bitmap, GraphicsPath path)
{
RectangleF rec = path.GetBounds();
int left = (int)rec.Left;
int top = (int)rec.Top;
int width = (int)rec.Width;
int height = (int)rec.Height;
Bitmap imgCropped = new Bitmap(width, height);
Bitmap img = (Bitmap)bitmap.Clone();
Region r = new Region(path);
for (int i = left; i < left + width; i++)
{
for (int j = top; j < top + height; j++)
{
if (!r.IsVisible(i, j))
{
imgCropped.SetPixel(i - left, j - top, Color.FromArgb(0, 255, 255, 255));
}
else
{
imgCropped.SetPixel(i - left, j - top, img.GetPixel(i, j));
}
}
}
return imgCropped;
}
public void DrawPath(Graphics graphics)
{
graphics.DrawPath(new Pen(BrushDColor[SelectedColorIndex], 1f), Path);
}
public void Select()
{
Selected = true;
}
public void UnSelect()
{
Selected = false;
}
public bool ContainPoint(Point p)
{
return _rectangle.Contains(p);
}
//Prepend参数指先操作,Append参数指后操作
public void Move(float x, float y, MatrixOrder order = MatrixOrder.Prepend)
{
switch (order)
{
default:
case MatrixOrder.Prepend:
//设定平移矩阵,order默认为predend,平移矩阵左乘_matrix
_matrix.Translate(x, y);
break;
case MatrixOrder.Append:
_matrix.Translate(x, y, MatrixOrder.Append);
break;
}
}
public void Scale(float scaleX, float scaleY)
{
_matrix.Scale(scaleX, scaleY);
}
public void Rotate(float angle)
{
_matrix.Rotate(angle);
}
public void RotateAt(float angle, PointF point)
{
_matrix.RotateAt(angle, point);
}
}
}