254 lines
8.2 KiB
C#
254 lines
8.2 KiB
C#
/*
|
|
* Copyright (c) 2025 LG Electronics Inc.
|
|
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
|
|
*/
|
|
using Aurora.Api;
|
|
using CogCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Cognex.VisionPro;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using CogPattern;
|
|
using Cognex.VisionPro.PMAlign;
|
|
using Aurora.Api.DataType;
|
|
using Cognex.VisionPro.Display;
|
|
using System.Drawing;
|
|
|
|
namespace CogPattern {
|
|
public class ImageModelFinder : PatternBase, IPatternFinder, IPatternTrain, IPatternMask, IDisposable {
|
|
public string FILE_EXT_PATTERN { get; set; } = "_PatMax.vpp";
|
|
|
|
private Logger _logger;
|
|
private PatternInfo _patternInfo;
|
|
private CogPMAlignTool _CogPMAlignTool;
|
|
|
|
public ImageModelFinder(PatternInfo patternInfo) {
|
|
_patternInfo = patternInfo;
|
|
_logger = new Logger(
|
|
$"ImageModelFinder_{patternInfo.GetPatternIndex()}");
|
|
Init();
|
|
}
|
|
|
|
private void Init() {
|
|
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;
|
|
}
|
|
|
|
_CogPMAlignTool = MmfCommon.LoadTool(patternFilePath);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Save() {
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (!Directory.Exists(patternDirPath)) {
|
|
Directory.CreateDirectory(patternDirPath);
|
|
}
|
|
|
|
return MmfCommon.SaveTool(_CogPMAlignTool, patternFilePath);
|
|
}
|
|
|
|
public int Create() {
|
|
_CogPMAlignTool = MmfCommon.CreateTool();
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public int Remove() {
|
|
FreeTool();
|
|
|
|
var patternFilePath = GetPatternFilePath();
|
|
var patternDirPath = Path.GetDirectoryName(patternFilePath);
|
|
if (Directory.Exists(patternDirPath)) {
|
|
Directory.Delete(patternDirPath, recursive: true);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public ROI GetFindROI() {
|
|
return MmfCommon.GetFindROI(_CogPMAlignTool);
|
|
}
|
|
|
|
public int SetFindROI(ROI findRoi) {
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
return MmfCommon.SetFindROI(_CogPMAlignTool, findRoi);
|
|
}
|
|
|
|
public IRunParam GetRunParam() {
|
|
return MmfCommon.GetRunParam(_CogPMAlignTool);
|
|
}
|
|
|
|
public int SetRunParam(IRunParam runParam) {
|
|
return MmfCommon.SetRunParam(_CogPMAlignTool, runParam);
|
|
}
|
|
|
|
public PatternResult Find(object image) {
|
|
var findRoi = MmfCommon.GetFindROI(_CogPMAlignTool);
|
|
return Find(image, findRoi);
|
|
}
|
|
|
|
public PatternResult Find(object image, ROI findRoi) {
|
|
var patternResult = MmfCommon.Find(_CogPMAlignTool, (ICogImage)image, findRoi);
|
|
var modelFinderResultList = patternResult.Results as List<ModelFinderResult>;
|
|
|
|
if (modelFinderResultList?.Count > 0) {
|
|
var fixtureOrigin = MmfImage.GetFixtureOrigin(_CogPMAlignTool);
|
|
var fixture = new FixtureResult() {
|
|
Origin = modelFinderResultList[0].ImagePosition.Clone() as ImagePoint,
|
|
Offset = (modelFinderResultList[0].ImagePosition - fixtureOrigin.Origin) as ImagePoint,
|
|
Rotation = modelFinderResultList[0].Angle.Clone() as Angle
|
|
};
|
|
patternResult.MoreInfo = fixture;
|
|
}
|
|
|
|
return patternResult;
|
|
}
|
|
|
|
public PatternResult GetNotFoundPatternResult() {
|
|
return MmfCommon.GetNotFoundPatternResult();
|
|
}
|
|
|
|
public object GetTool() {
|
|
return _CogPMAlignTool;
|
|
}
|
|
|
|
public void Draw(
|
|
PatternResult patternResult, ICameraDisplay cameraDisplay, bool isFixturePattern = false) {
|
|
DrawTrainROI(patternResult, cameraDisplay as ICogCameraDisplay, isFixturePattern);
|
|
MmfCommon.Draw(patternResult, cameraDisplay as ICogCameraDisplay, isFixturePattern);
|
|
}
|
|
|
|
private void DrawTrainROI(
|
|
PatternResult patternResult, ICogCameraDisplay cogCameraDisplay, bool isFixturePattern) {
|
|
var trainParam = GetTrainParam() as ImageModelFinderTrainParam;
|
|
var cogDisplay = cogCameraDisplay.GetDisplay() as CogDisplay;
|
|
|
|
if (patternResult.Status != eFoundStatus.NotFound) {
|
|
foreach (BaseResult baseResult in patternResult.Results) {
|
|
var result = baseResult as ModelFinderResult;
|
|
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);
|
|
cogCameraDisplay.AddPatternResultFindRoi(cogDisplay, color, foundTrainRoi);
|
|
}
|
|
}
|
|
else {
|
|
var color = GraphicColors.NG;
|
|
cogCameraDisplay.AddPatternResultFindRoi(cogDisplay, color, trainParam.TrainRoi);
|
|
}
|
|
}
|
|
|
|
public Bitmap GetThumbnail() {
|
|
var bitmap = ThumbnailUtil.GetDefaultImage();
|
|
if (!IsTrained()) {
|
|
return bitmap;
|
|
}
|
|
|
|
ICogImage modelImage = null;
|
|
|
|
try {
|
|
modelImage = MmfImage.GetModelImage(_CogPMAlignTool);
|
|
if (modelImage != null) {
|
|
return modelImage.ToBitmap();
|
|
}
|
|
}
|
|
catch (Exception e) {
|
|
_logger.FuncE(e.Message);
|
|
}
|
|
finally {
|
|
CogEnvironment.FreeImage(modelImage);
|
|
}
|
|
|
|
return bitmap;
|
|
}
|
|
#endregion
|
|
|
|
#region IPatternTrain
|
|
public bool IsTrained() {
|
|
return MmfCommon.IsTrained(_CogPMAlignTool);
|
|
}
|
|
|
|
public ITrainParam GetTrainParam() {
|
|
return MmfImage.GetTrainParam(_CogPMAlignTool);
|
|
}
|
|
|
|
public object GetTrainTool() {
|
|
return _CogPMAlignTool;
|
|
}
|
|
|
|
public int Train(ITrainParam trainParam) {
|
|
return MmfImage.Train(_CogPMAlignTool, trainParam);
|
|
}
|
|
#endregion
|
|
|
|
#region IPatternMask
|
|
public ICogImage GetModelImage() {
|
|
return MmfImage.GetModelImage(_CogPMAlignTool);
|
|
}
|
|
|
|
public ICogImage GetMaskImage() {
|
|
return MmfImage.GetMaskImage(_CogPMAlignTool);
|
|
}
|
|
|
|
public int SetMaskImage(ICogImage maskImage) {
|
|
return MmfImage.SetMaskImage(_CogPMAlignTool, maskImage);
|
|
}
|
|
|
|
public int SetTrainOrigin(Cross trainOrigin) {
|
|
return MmfImage.SetTrainOrigin(_CogPMAlignTool, trainOrigin);
|
|
}
|
|
|
|
public int SetTrainOriginInModel(Cross trainOriginInModel) {
|
|
return MmfImage.SetTrainOriginInModel(_CogPMAlignTool, trainOriginInModel);
|
|
}
|
|
#endregion
|
|
|
|
#region IDisposible
|
|
public void Dispose() {
|
|
FreeTool();
|
|
}
|
|
|
|
private void FreeTool() {
|
|
MmfCommon.FreeTool(_CogPMAlignTool);
|
|
}
|
|
#endregion
|
|
|
|
public int GetFindNumber() {
|
|
var runParam = MmfCommon.GetRunParam(_CogPMAlignTool);
|
|
return runParam.FindNumber;
|
|
}
|
|
}
|
|
}
|