using OpenCvSharp;
using System;
using System.Drawing;
using static PaintDotNet.Base.DedicatedAnalysis.Inclusions.InclusionsGlobalSettings;
using Point = System.Drawing.Point;
namespace PaintDotNet.Base.DedicatedAnalysis.Inclusions.Model
{
///
/// 微粒(particle)
/// 单个析出的沉淀物,一般为非金属。
///
public class Particle
{
#region 属性
///
/// 组成微粒的像素数组
///
public OpenCvSharp.Point[] points;
public Point[] points2;
///
/// 当前微粒判定所使用的标准
///
public InclusionsStandard standard;
///
/// 水平距离
///
public double horizontalDistance;
///
/// 垂直距离
///
public double verticalDistance;
///
/// 像素长度
///
public double pixelLength;
///
/// 像素宽度
///
public double pixelWidth;
///
/// 物理长度
///
public double physicalLength;
///
/// 物理宽度
///
public double physicalWidth;
///
/// 长宽比
///
public double lwRatio;
///
/// 形状
///
public ParticleShape shape;
///
/// 矩形轮廓
///
public Rectangle rectProfile;
///
/// 矩形轮廓(K法)
///
public Rectangle rectKMethodProfile;
public double areaK;
public int offsetX;
public int offsetY;
#endregion
///
/// 构造方法
///
/// 组成微粒的像素数组
/// 当前微粒判定所使用的标准
public Particle(OpenCvSharp.Point[] points, InclusionsStandard standard)
{
this.points = points;
this.points2 = new Point[points.Length];
this.standard = standard;
parametersCalculation();
this.areaK = Cv2.ContourArea(points);
}
///
/// 参数计算
///
private void parametersCalculation()
{
int minx = 0, miny = 0, maxx = 0, maxy = 0;
for (int i = 0; i < this.points.Length; i++)
{
this.points2[i] = new Point(this.points[i].X, this.points[i].Y);
if (i==0)
{
minx = this.points[i].X;
maxx = this.points[i].X;
miny = this.points[i].Y;
maxy = this.points[i].Y;
continue;
}
if (this.points[i].X < minx)
{
minx = this.points[i].X;
}
if (this.points[i].X > maxx)
{
maxx = this.points[i].X;
}
if (this.points[i].Y < miny)
{
miny = this.points[i].Y;
}
if (this.points[i].Y > maxy)
{
maxy = this.points[i].Y;
}
}
// 计算
this.horizontalDistance = maxx - minx;
this.verticalDistance = maxy - miny;
// 计算长度,宽度/直径,面积,长宽比
// 计算长度,宽度/直径,面积,形状系数,长宽比
if (this.standard.globalSettings.rollingDirection == RollingDirection.PORTRAIT)
{
this.pixelLength = maxy - miny;
this.pixelWidth = maxx - minx;
}
else
{
this.pixelWidth = maxy - miny;
this.pixelLength = maxx - minx;
}
this.rectProfile = new Rectangle(minx, miny, maxx - minx, maxy - miny);
this.rectKMethodProfile = this.rectProfile;
this.lwRatio = this.pixelLength / this.pixelWidth;
if (this.lwRatio < this.standard.globalSettings.lwRatioLimit)
{
this.shape = ParticleShape.Globular;
}
else
{
this.shape = ParticleShape.Elongated;
}
this.physicalLength = this.pixelLength * this.standard.globalSettings.pxPerUnit;
this.physicalWidth = this.pixelWidth * this.standard.globalSettings.pxPerUnit;
}
}
}