309 lines
10 KiB
C#
309 lines
10 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Drawing;
|
|
|
|
namespace MilPattern {
|
|
public class PatternMatching : PatternBase, IPatternFinder, IPatternTrain, IPatternMask, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = ".mpat";
|
|
private PmParamExt _paramExt = null;
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
private MIL_ID _contextId = MIL.M_NULL;
|
|
private MIL_ID _resultContextId = MIL.M_NULL;
|
|
|
|
public PatternMatching(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"PatternMatching_{patternInfo.GetPatternIndex()}");
|
|
InitContext();
|
|
}
|
|
|
|
private void InitContext() {
|
|
if (E1.OK != Load()) {
|
|
Create();
|
|
}
|
|
}
|
|
|
|
#region IPatternFinder
|
|
public string GetPatternFilePath(string patternName = PatternConstants.FILE_NAME_PATTERN) {
|
|
return Path.Combine(new string[] {
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
patternName + FILE_EXT_PATTERN
|
|
});
|
|
}
|
|
|
|
public int Load() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
|
|
if (!File.Exists(patternFilePath)) {
|
|
_logger.I($"Pattern File does NOT exist : {patternFilePath}");
|
|
return E2.NOT_FOUND;
|
|
}
|
|
|
|
FreeContexts();
|
|
_contextId = MilPatternMatching.LoadContext(_patternInfo.MilSystemId, patternFilePath);
|
|
_resultContextId = MilPatternMatching.CreateResultContext(_contextId);
|
|
_paramExt = LoadParamExt();
|
|
ExtractFindRoiFromMpat();
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private void ExtractFindRoiFromMpat() {
|
|
if (!IsParamExtExist()) {
|
|
_logger.I($"Extract Find ROI : {_patternInfo}");
|
|
var findRoiMpat = MilPatternMatching.GetFindRoiFromMpat(_contextId);
|
|
_logger.I($"findRoiMpat : {findRoiMpat}");
|
|
|
|
_paramExt.FindROI = findRoiMpat.Clone() as ROI;
|
|
MilPatternMatching.ClearFindRoi(_contextId);
|
|
Save();
|
|
}
|
|
}
|
|
|
|
public int Save() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (!Directory.Exists(patternDirPath)) {
|
|
Directory.CreateDirectory(patternDirPath);
|
|
}
|
|
|
|
SaveParamExt(_paramExt);
|
|
return MilPatternMatching.SaveContext(_contextId, patternFilePath);
|
|
}
|
|
|
|
public int Create() {
|
|
FreeContexts();
|
|
_contextId = MilPatternMatching.CreateContext(_patternInfo.MilSystemId);
|
|
_resultContextId = MilPatternMatching.CreateResultContext(_contextId);
|
|
_paramExt = LoadParamExt();
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Remove() {
|
|
FreeContexts();
|
|
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (Directory.Exists(patternDirPath)) {
|
|
Directory.Delete(patternDirPath, recursive: true);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public ROI GetFindROI() {
|
|
return _paramExt.FindROI.Clone() as ROI;
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
_paramExt.FindROI = findRoi.Clone() as ROI;
|
|
return E1.OK;
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
var runParam = MilPatternMatching.GetRunParam(_contextId);
|
|
runParam.PmParamExt = _paramExt.Clone() as PmParamExt;
|
|
|
|
return runParam;
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
_paramExt = ((PatternMatchingRunParam)runParam).PmParamExt.Clone() as PmParamExt;
|
|
return MilPatternMatching.SetRunParam(_contextId, runParam);
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
return Find(image, _paramExt.FindROI.Clone() as ROI);
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
var patternResult = MilPatternMatching.Find(_contextId, _resultContextId, (MIL_ID)image, findRoi);
|
|
var patternMatchingResultList = patternResult.Results as List<PatternMatchingResult>;
|
|
|
|
if (patternMatchingResultList?.Count > 0) {
|
|
var fixtureOrigin = MilPatternMatching.GetFixtureOrigin(_contextId);
|
|
var fixture = new FixtureResult() {
|
|
Origin = patternMatchingResultList[0].ImagePosition.Clone() as ImagePoint,
|
|
Offset = (patternMatchingResultList[0].ImagePosition - fixtureOrigin.Origin) as ImagePoint,
|
|
Rotation = patternMatchingResultList[0].Angle.Clone() as Angle
|
|
};
|
|
patternResult.MoreInfo = fixture;
|
|
}
|
|
|
|
return patternResult;
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return MilPatternMatching.GetNotFoundPatternResult();
|
|
}
|
|
|
|
public object GetTool() {
|
|
return _contextId;
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
DrawTrainROI(patternResult, cameraDisplay as IMilCameraDisplay, isFixturePattern);
|
|
MilPatternMatching.Draw(patternResult, cameraDisplay as IMilCameraDisplay, isFixturePattern);
|
|
}
|
|
|
|
private void DrawTrainROI(
|
|
PatternResult patternResult, IMilCameraDisplay milCameraDisplay, bool isFixturePattern) {
|
|
var trainParam = GetTrainParam() as PatternMatchingTrainParam;
|
|
|
|
if (patternResult.Status != eFoundStatus.NotFound) {
|
|
foreach (BaseResult baseResult in patternResult.Results) {
|
|
var result = baseResult as PatternMatchingResult;
|
|
var offset = result.ImagePosition - trainParam.TrainOrigin.Origin;
|
|
var transRoiCenter = trainParam.TrainRoi.Center + offset as ImagePoint;
|
|
var roiCenter = MathUtil.Rotate(transRoiCenter, -result.Angle, result.ImagePosition) as ImagePoint;
|
|
var foundTrainRoi = new ROI(
|
|
roiCenter,
|
|
trainParam.TrainRoi.Width,
|
|
trainParam.TrainRoi.Height,
|
|
result.Angle);
|
|
|
|
var color = GraphicColors.GetTrainRoiColor(patternResult.Status, isFixturePattern);
|
|
milCameraDisplay.AddPatternResultFindRoi(color, foundTrainRoi);
|
|
}
|
|
}
|
|
else {
|
|
var color = GraphicColors.NG;
|
|
milCameraDisplay.AddPatternResultFindRoi(color, trainParam.TrainRoi);
|
|
}
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
var bitmap = ThumbnailUtil.GetDefaultImage();
|
|
if (!IsTrained()) {
|
|
return bitmap;
|
|
}
|
|
|
|
MIL_ID colorModelImage = MIL.M_NULL;
|
|
|
|
try {
|
|
colorModelImage = MilPatternMatching.GetColorModelImage(_contextId);
|
|
DrawMaskImage(colorModelImage);
|
|
|
|
return MilEnvironment.ConvertToBitmap(colorModelImage);
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
finally {
|
|
MilEnvironment.FreeImage(colorModelImage);
|
|
}
|
|
|
|
return bitmap;
|
|
}
|
|
#endregion
|
|
|
|
#region IPatternTrain
|
|
public bool IsTrained() {
|
|
return MilPatternMatching.HasModel(_contextId);
|
|
}
|
|
|
|
public ITrainParam GetTrainParam() {
|
|
return MilPatternMatching.GetTrainParam(_contextId);
|
|
}
|
|
|
|
public int Train(ITrainParam trainParam) {
|
|
return MilPatternMatching.Train(_contextId, trainParam);
|
|
}
|
|
#endregion
|
|
|
|
#region IPatternMask
|
|
public MIL_ID GetGrayModelImage() {
|
|
return MilPatternMatching.GetGrayModelImage(_contextId);
|
|
}
|
|
|
|
public MIL_ID GetColorModelImage() {
|
|
return MilPatternMatching.GetColorModelImage(_contextId);
|
|
}
|
|
|
|
public MIL_ID GetMaskImage() {
|
|
return MilPatternMatching.GetMaskImage(_contextId);
|
|
}
|
|
|
|
public void DrawMaskImage(MIL_ID overlayId) {
|
|
MilPatternMatching.DrawMaskImage(_contextId, overlayId);
|
|
}
|
|
|
|
public int SetMaskImage(MIL_ID maskImage) {
|
|
return MilPatternMatching.SetMaskImage(_contextId, maskImage);
|
|
}
|
|
|
|
public int SetTrainOrigin(Cross trainOriginInModel) {
|
|
return MilPatternMatching.SetTrainOrigin(_contextId, trainOriginInModel);
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
FreeContexts();
|
|
}
|
|
|
|
private void FreeContexts() {
|
|
MilPatternMatching.FreeContext(_contextId);
|
|
MilPatternMatching.FreeContext(_resultContextId);
|
|
|
|
_contextId = MIL.M_NULL;
|
|
_resultContextId = MIL.M_NULL;
|
|
}
|
|
#endregion
|
|
|
|
#region PatternMatching Specific
|
|
internal bool IsParamExtExist() {
|
|
return PmParamExt.Exists(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal PmParamExt LoadParamExt() {
|
|
return PmParamExt.Load(PatternConstants.GetSubPatternDirPath(_patternInfo));
|
|
}
|
|
|
|
internal int SaveParamExt(PmParamExt pmParamExt) {
|
|
return PmParamExt.Save(
|
|
PatternConstants.GetSubPatternDirPath(_patternInfo),
|
|
pmParamExt);
|
|
}
|
|
#endregion
|
|
|
|
#region DFA Inspection
|
|
public MIL_ID GetContextId() {
|
|
return _contextId;
|
|
}
|
|
|
|
public MIL_ID GetResultContextId() {
|
|
return _resultContextId;
|
|
}
|
|
|
|
public Cross GetTrainOrigin() {
|
|
return MilPatternMatching.GetFixtureOrigin(_contextId);
|
|
}
|
|
#endregion
|
|
|
|
public int GetFindNumber() {
|
|
var runParam = MilPatternMatching.GetRunParam(_contextId);
|
|
return runParam.FindNumber;
|
|
}
|
|
}
|
|
}
|