using MilCommon; using Hermes; using Matrox.MatroxImagingLibrary; using System; using VisionCommon; namespace MilPattern { public class ShapeModelFinderTrainParam : ITrainParam, ICloneCopy { 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 double InnerRadius { get; set; } = 10; public double OuterRadius { get; set; } = 15; 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, InnerRadius = InnerRadius, OuterRadius = OuterRadius, }; } 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; target.InnerRadius = this.InnerRadius; target.OuterRadius = this.OuterRadius; } } 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; double innerRadius = 0; double outerRadius = 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; case eModelType.Ring: MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_INNER_RADIUS, ref innerRadius); trainParam.InnerRadius = innerRadius; MIL.MmodInquire(contextId, MIL.M_DEFAULT, MIL.M_OUTER_RADIUS, ref outerRadius); trainParam.OuterRadius = outerRadius; 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; case eModelType.Ring: MIL.MmodDefine( contextId, (long)param.ModelType, (long)param.ForegroundColor, param.InnerRadius, param.OuterRadius, 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; } } }