128 lines
4.8 KiB
C#
128 lines
4.8 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.Windows;
|
|
using System.Diagnostics;
|
|
|
|
namespace CogPattern {
|
|
/// <summary>
|
|
/// BrightnessRunParam 클래스
|
|
/// </summary>
|
|
public class BrightnessRunParam : IRunParam, ICloneable {
|
|
/// <summary>
|
|
/// BrightnessParamExt
|
|
/// </summary>
|
|
public BrightnessParamExt BrightnessParamExt { get; set; } = new BrightnessParamExt();
|
|
/// <summary>
|
|
/// object복사
|
|
/// </summary>
|
|
/// <returns>object</returns>
|
|
public object Clone() {
|
|
return new BrightnessRunParam() {
|
|
BrightnessParamExt = BrightnessParamExt.Clone() as BrightnessParamExt
|
|
};
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// CogBrightness 클래스
|
|
/// </summary>
|
|
internal static class CogBrightness {
|
|
//private static readonly Logger _Logger = new Logger(typeof(CogBrightness).Name);
|
|
|
|
/// <summary>
|
|
/// 패턴결과 찾기
|
|
/// </summary>
|
|
/// <param name="image">이미지</param>
|
|
/// <param name="findRoi">ROI정보</param>
|
|
/// <param name="paramExt">추가파라미터</param>
|
|
/// <returns>PatternResult</returns>
|
|
public static PatternResult Find(ICogImage image, ROI findRoi, BrightnessParamExt paramExt) {
|
|
if (CogEnvironment.IsColorImage(image)) {
|
|
var grayImageId = CogEnvironment.ConvertToGray(image);
|
|
var patternResult = FindInternal(grayImageId, findRoi, paramExt);
|
|
CogEnvironment.FreeImage(grayImageId);
|
|
|
|
return patternResult;
|
|
}
|
|
return FindInternal(image, findRoi, paramExt);
|
|
}
|
|
|
|
private static PatternResult FindInternal(ICogImage image, ROI findRoi, BrightnessParamExt paramExt) {
|
|
PatternResult finderResult;
|
|
BrightnessResult brightnessResult;
|
|
|
|
var timeFind = new Stopwatch();
|
|
timeFind.Start();
|
|
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
var imagePoint = CogEnvironment.GetImageCenter(image);
|
|
brightnessResult = CogEnvironment.GetBrightness(image, imagePoint);
|
|
finderResult = ExtractResults(brightnessResult, paramExt);
|
|
}
|
|
else {
|
|
brightnessResult = CogEnvironment.GetBrightnessWithCropRoi(image, findRoi);
|
|
finderResult = ExtractResults(brightnessResult, paramExt);
|
|
}
|
|
|
|
timeFind.Stop();
|
|
finderResult.TactTimeInMilliSec = timeFind.ElapsedMilliseconds;
|
|
|
|
return finderResult;
|
|
}
|
|
|
|
private static PatternResult ExtractResults(BrightnessResult brightness, BrightnessParamExt paramExt) {
|
|
var results = new List<BrightnessResult> {
|
|
brightness.Clone()
|
|
};
|
|
|
|
return new PatternResult() {
|
|
Status = (brightness.Avg > paramExt.AvgLow && brightness.Avg < paramExt.AvgHigh)? eFoundStatus.FoundOK : eFoundStatus.NotFound,
|
|
Results = results
|
|
};
|
|
}
|
|
/// <summary>
|
|
/// 결과없음 반환
|
|
/// </summary>
|
|
/// <returns>PatternResult</returns>
|
|
public static PatternResult GetNotFoundPatternResult() {
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
Results = new List<BrightnessResult>()
|
|
};
|
|
}
|
|
/// <summary>
|
|
/// 패턴 결과 그리기
|
|
/// </summary>
|
|
/// <param name="patternResult">패턴결과</param>
|
|
/// <param name="cogCameraDisplay">Display</param>
|
|
public static void Draw(PatternResult patternResult, ICogCameraDisplay cogCameraDisplay) {
|
|
CogColorConstants color = CogColorConstants.Magenta;
|
|
if (patternResult.Status != eFoundStatus.FoundOK) {
|
|
color = ColorConverter.ConvertColor(eColor.Red);
|
|
}
|
|
|
|
var results = patternResult.Results as List<BrightnessResult>;
|
|
foreach (var result in results) {
|
|
var msgs = new List<string> {
|
|
string.Format("Min : {0}", result.Min),
|
|
string.Format("Max : {0}", result.Max),
|
|
string.Format("Avg : {0}", result.Avg),
|
|
string.Format("TactTime : {0} ms", Math.Round(patternResult.TactTimeInMilliSec)),
|
|
string.Format("ID : {0}_{1}", patternResult.PatternIndex.PatternId, patternResult.PatternIndex.PatternSubId)
|
|
};
|
|
|
|
cogCameraDisplay.AddTextList(
|
|
color, new ImagePoint(result.ImagePosition.X, result.ImagePosition.Y), msgs);
|
|
}
|
|
}
|
|
}
|
|
}
|