207 lines
6.2 KiB
C#
207 lines
6.2 KiB
C#
using Aurora.Api.DataType;
|
|
using System;
|
|
using System.Drawing;
|
|
|
|
namespace VisionCommon {
|
|
public class ROI : ICloneable {
|
|
public double CenterX { get; set; } = 0;
|
|
public double CenterY { get; set; } = 0;
|
|
public double Width { get; set; } = 0;
|
|
public double Height { get; set; } = 0;
|
|
public Angle Angle { get; set; } = new Angle();
|
|
|
|
public ImagePoint Center {
|
|
get {
|
|
return new ImagePoint(CenterX, CenterY);
|
|
}
|
|
private set {
|
|
CenterX = value.X;
|
|
CenterY = value.Y;
|
|
}
|
|
}
|
|
|
|
#region Unrotated ROI Pos
|
|
public ImagePoint LeftTop {
|
|
get { return new ImagePoint(Left, Top); }
|
|
}
|
|
public ImagePoint RightBottom {
|
|
get { return new ImagePoint(Right, Bottom); }
|
|
}
|
|
public double Left {
|
|
get { return CenterX - (Width * 0.5); }
|
|
}
|
|
public double Right {
|
|
get { return CenterX + (Width * 0.5); }
|
|
}
|
|
public double Top {
|
|
get { return CenterY - (Height * 0.5); }
|
|
}
|
|
public double Bottom {
|
|
get { return CenterY + (Height * 0.5); }
|
|
}
|
|
#endregion
|
|
|
|
#region Rotated ROI Pos
|
|
public ImagePoint RotatedLeftTop {
|
|
get { return GetRotatedLeftTop(this); }
|
|
}
|
|
public ImagePoint RotatedRightBottom {
|
|
get { return GetRotatedRightBottom(this); }
|
|
}
|
|
public ImagePoint RotatedLeftCenter {
|
|
get { return GetRotatedLeftCenter(this); }
|
|
}
|
|
public ImagePoint RotatedRightCenter {
|
|
get { return GetRotatedRightCenter(this); }
|
|
}
|
|
public static ImagePoint GetRotatedLeftTop(ROI roi) {
|
|
return MathUtil.Rotate(roi.LeftTop, roi.Angle, roi.Center) as ImagePoint;
|
|
}
|
|
public static ImagePoint GetRotatedRightBottom(ROI roi) {
|
|
return MathUtil.Rotate(roi.RightBottom, roi.Angle, roi.Center) as ImagePoint;
|
|
}
|
|
public static ImagePoint GetRotatedLeftCenter(ROI roi) {
|
|
var leftCenter = new ImagePoint() {
|
|
X = roi.Left,
|
|
Y = roi.Top + (roi.Height / 2)
|
|
};
|
|
return MathUtil.Rotate(leftCenter, roi.Angle, roi.Center) as ImagePoint;
|
|
}
|
|
public static ImagePoint GetRotatedRightCenter(ROI roi) {
|
|
var rightCenter = new ImagePoint() {
|
|
X = roi.Right,
|
|
Y = roi.Bottom - (roi.Height / 2)
|
|
};
|
|
return MathUtil.Rotate(rightCenter, roi.Angle, roi.Center) as ImagePoint;
|
|
}
|
|
#endregion
|
|
|
|
public ROI() { }
|
|
|
|
public ROI(
|
|
ImagePoint center,
|
|
double width,
|
|
double height)
|
|
: this(center.X, center.Y, width, height) {
|
|
}
|
|
|
|
public ROI(
|
|
double centerX,
|
|
double centerY,
|
|
double width,
|
|
double height)
|
|
: this(centerX, centerY, width, height, new Angle()) {
|
|
}
|
|
|
|
public ROI(
|
|
ImagePoint center,
|
|
double width,
|
|
double height,
|
|
Angle angle)
|
|
: this(center.X, center.Y, width, height, angle) {
|
|
}
|
|
|
|
public ROI(
|
|
double centerX,
|
|
double centerY,
|
|
double width,
|
|
double height,
|
|
Angle angle) {
|
|
CenterX = centerX;
|
|
CenterY = centerY;
|
|
Width = width;
|
|
Height = height;
|
|
Angle = angle.Clone() as Angle;
|
|
}
|
|
|
|
public static bool IsNullOrEmpty(ROI roi) {
|
|
if (roi == null) {
|
|
return true;
|
|
}
|
|
|
|
return roi.Width == 0 || roi.Height == 0;
|
|
}
|
|
|
|
public object Clone() {
|
|
return new ROI(CenterX, CenterY, Width, Height, Angle.Clone() as Angle);
|
|
}
|
|
|
|
public override string ToString() {
|
|
return $"Left : {Left:F2}, Right : {Right:F2}, Center : {Center}, " +
|
|
$"Top : {Top:F2}, Bottom : {Bottom:F2}, " +
|
|
$"Width : {Width:F2}, Height : {Height:F2}, Angle : {Angle}";
|
|
}
|
|
|
|
public static ROI Translate(ROI roi, ImagePoint translation) {
|
|
return new ROI(
|
|
(roi.Center + translation) as ImagePoint,
|
|
roi.Width,
|
|
roi.Height,
|
|
roi.Angle.Clone() as Angle);
|
|
}
|
|
|
|
public static bool IsOutsideImage(ROI roi, ImageSize imageSize) {
|
|
if (roi.Left < 0) {
|
|
return true;
|
|
}
|
|
if (roi.Top < 0) {
|
|
return true;
|
|
}
|
|
if (imageSize.Width <= roi.Right) {
|
|
return true;
|
|
}
|
|
if (imageSize.Height <= roi.Bottom) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static ROI FitRoiToImage(ROI roi, ImageSize imageSize) {
|
|
double left = roi.Left;
|
|
double right = roi.Right;
|
|
double top = roi.Top;
|
|
double bottom = roi.Bottom;
|
|
|
|
if (roi.Left < 0) {
|
|
left = 0;
|
|
}
|
|
if (roi.Top < 0) {
|
|
top = 0;
|
|
}
|
|
if (imageSize.Width <= roi.Right) {
|
|
right = imageSize.Width - 1;
|
|
}
|
|
if (imageSize.Height <= roi.Bottom) {
|
|
bottom = imageSize.Height - 1;
|
|
}
|
|
|
|
var roiCenter = new ImagePoint(
|
|
(left + right) * 0.5,
|
|
(top + bottom) * 0.5);
|
|
|
|
return new ROI(
|
|
roiCenter,
|
|
right - left,
|
|
bottom - top,
|
|
roi.Angle);
|
|
}
|
|
|
|
public static Rectangle ToRectangle(ROI roi) {
|
|
return new Rectangle(
|
|
(int)roi.Left,
|
|
(int)roi.Top,
|
|
(int)roi.Width,
|
|
(int)roi.Height);
|
|
}
|
|
|
|
public static ROI FromRectangle(Rectangle rectangle) {
|
|
return new ROI(
|
|
rectangle.Left + (rectangle.Width * 0.5),
|
|
rectangle.Top + (rectangle.Height * 0.5),
|
|
rectangle.Width,
|
|
rectangle.Height,
|
|
new Angle());
|
|
}
|
|
}
|
|
}
|