2026-01-26 14:51:28 +09:00

150 lines
6.3 KiB
C#

/*
* Copyright (c) 2025 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using MilCommon;
using Hermes;
using Matrox.MatroxImagingLibrary;
using System;
using VisionCommon;
namespace MilPattern {
public class ShapeModelFinderTrainParam : ITrainParam, ICloneCopy<ShapeModelFinderTrainParam> {
public eModelType ModelType { get; set; } = eModelType.Circle;
public eMilForegroundColor ForegroundColor { get; set; } = eMilForegroundColor.Any;
public double Radius { get; set; } = 10;
public double Width { get; set; } = 20;
public double Height { get; set; } = 20;
public double HorizontalThickness { get; set; } = 3;
public double VerticalThickness { get; set; } = 3;
public ShapeModelFinderTrainParam Clone() {
return ((ICloneable)this).Clone() as ShapeModelFinderTrainParam;
}
object ICloneable.Clone() {
return new ShapeModelFinderTrainParam() {
ModelType = ModelType,
ForegroundColor = ForegroundColor,
Radius = Radius,
Width = Width,
Height = Height,
HorizontalThickness = HorizontalThickness,
VerticalThickness = VerticalThickness
};
}
public void CopyTo(ShapeModelFinderTrainParam target) {
target.ModelType = this.ModelType;
target.ForegroundColor = this.ForegroundColor;
target.Radius = this.Radius;
target.Width = this.Width;
target.Height = this.Height;
target.HorizontalThickness = this.HorizontalThickness;
target.VerticalThickness = this.VerticalThickness;
}
}
internal static class MmfShape {
public static ITrainParam GetTrainParam(MIL_ID contextId) {
var trainParam = new ShapeModelFinderTrainParam();
if (MmfCommon.HasModel(contextId)) {
trainParam.ModelType = (eModelType)(int)MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_MODEL_TYPE, MIL.M_NULL);
trainParam.ForegroundColor =
(eMilForegroundColor)(int)MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_FOREGROUND_VALUE, MIL.M_NULL);
double radius = 0;
double width = 0;
double height = 0;
double horizontalThickness = 0;
double verticalThickness = 0;
switch (trainParam.ModelType) {
case eModelType.Circle:
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_RADIUS, ref radius);
trainParam.Radius = radius;
break;
case eModelType.Cross:
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_WIDTH, ref width);
trainParam.Width = width;
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_HEIGHT, ref height);
trainParam.Height = height;
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_HORIZONTAL_THICKNESS, ref horizontalThickness);
trainParam.HorizontalThickness = horizontalThickness;
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_VERTICAL_THICKNESS, ref verticalThickness);
trainParam.VerticalThickness = verticalThickness;
break;
case eModelType.Diamond:
case eModelType.Ellipse:
case eModelType.Rectangle:
case eModelType.Triangle:
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_WIDTH, ref width);
trainParam.Width = width;
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_HEIGHT, ref height);
trainParam.Height = height;
break;
case eModelType.Square:
MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_WIDTH, ref width);
trainParam.Width = width;
break;
}
}
return trainParam;
}
public static int Train(MIL_ID contextId, ITrainParam trainParam) {
MmfCommon.DeleteAllModels(contextId);
if (trainParam is ShapeModelFinderTrainParam) {
var param = trainParam as ShapeModelFinderTrainParam;
return DefineModel(contextId, param);
}
return E1.GENERIC;
}
private static int DefineModel(MIL_ID contextId, ShapeModelFinderTrainParam param) {
switch (param.ModelType) {
case eModelType.Circle:
MIL.MmodDefine(
contextId, (long)param.ModelType, (long)param.ForegroundColor,
param.Radius, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT);
break;
case eModelType.Cross:
MIL.MmodDefine(
contextId, (long)param.ModelType, (long)param.ForegroundColor,
param.Width, param.Height, param.HorizontalThickness, param.VerticalThickness);
break;
case eModelType.Diamond:
case eModelType.Ellipse:
case eModelType.Rectangle:
case eModelType.Triangle:
MIL.MmodDefine(
contextId, (long)param.ModelType, (long)param.ForegroundColor,
param.Width, param.Height, MIL.M_DEFAULT, MIL.M_DEFAULT);
break;
case eModelType.Square:
MIL.MmodDefine(
contextId, (long)param.ModelType, (long)param.ForegroundColor,
param.Width, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_DEFAULT);
break;
default:
throw new NotImplementedException();
}
MIL.MmodPreprocess(contextId, MIL.M_DEFAULT);
if (!MmfCommon.IsPreprocessed(contextId)) {
return E1.GENERIC;
}
return E1.OK;
}
}
}