112 lines
4.5 KiB
C#
112 lines
4.5 KiB
C#
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Windows;
|
|
using Aurora.Api.DataType;
|
|
|
|
namespace MilPattern {
|
|
public class BrightnessRunParam : IRunParam, ICloneable {
|
|
public BrightnessParamExt BrightnessParamExt { get; set; } = new BrightnessParamExt();
|
|
|
|
public bool IsValidFindRoi(ImageSize imageSize) {
|
|
return (0 < BrightnessParamExt.FindROI.Width && BrightnessParamExt.FindROI.Width < imageSize.Width) &&
|
|
(0 < BrightnessParamExt.FindROI.Height && BrightnessParamExt.FindROI.Height < imageSize.Height);
|
|
}
|
|
|
|
public ROI GetFindRoi() {
|
|
return BrightnessParamExt.FindROI.Clone() as ROI;
|
|
}
|
|
|
|
public void SetFindROI(ROI findRoi) {
|
|
BrightnessParamExt.FindROI = findRoi;
|
|
}
|
|
|
|
public object Clone() {
|
|
return new BrightnessRunParam() {
|
|
BrightnessParamExt = BrightnessParamExt.Clone() as BrightnessParamExt
|
|
};
|
|
}
|
|
}
|
|
|
|
internal static class MilBrightness {
|
|
private static Logger _Logger = new Logger(typeof(MilBrightness).Name);
|
|
|
|
public static PatternResult Find(MIL_ID image, ROI findRoi, BrightnessParamExt paramExt) {
|
|
if (MilEnvironment.IsColorImage(image)) {
|
|
var grayImageId = MilEnvironment.ConvertToGray(image);
|
|
var patternResult = FindInternal(grayImageId, findRoi, paramExt);
|
|
MilEnvironment.FreeImage(grayImageId);
|
|
|
|
return patternResult;
|
|
}
|
|
return FindInternal(image, findRoi, paramExt);
|
|
}
|
|
|
|
private static PatternResult FindInternal(MIL_ID image, ROI findRoi, BrightnessParamExt paramExt) {
|
|
PatternResult finderResult;
|
|
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, MIL.M_NULL);
|
|
BrightnessResult brightnessResult;
|
|
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
var imagePoint = MilEnvironment.GetImageCenter(image);
|
|
brightnessResult = MilEnvironment.GetBrightness(image, imagePoint);
|
|
finderResult = ExtractResults(brightnessResult, paramExt);
|
|
}
|
|
else {
|
|
brightnessResult = MilEnvironment.GetBrightnessWithCropRoi(image, findRoi);
|
|
finderResult = ExtractResults(brightnessResult, paramExt);
|
|
}
|
|
|
|
double tactTime = 0;
|
|
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ + MIL.M_SYNCHRONOUS, ref tactTime);
|
|
finderResult.TactTimeInMilliSec = MathUtil.ConvertUnitSecToMilli(tactTime);
|
|
|
|
return finderResult;
|
|
}
|
|
|
|
private static PatternResult ExtractResults(BrightnessResult brightness, BrightnessParamExt paramExt) {
|
|
var results = new List<BrightnessResult>();
|
|
results.Add(brightness.Clone());
|
|
|
|
return new PatternResult() {
|
|
Status = (brightness.Avg > paramExt.AvgLow && brightness.Avg < paramExt.AvgHigh)? eFoundStatus.FoundOK : eFoundStatus.NotFound,
|
|
Results = results
|
|
};
|
|
}
|
|
|
|
public static PatternResult GetNotFoundPatternResult() {
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
Results = new List<BrightnessResult>()
|
|
};
|
|
}
|
|
|
|
public static void Draw(PatternResult patternResult, IMilCameraDisplay milCameraDisplay) {
|
|
MIL_INT color = MIL.M_NULL;
|
|
if (patternResult.Status != eFoundStatus.FoundOK) {
|
|
color = ColorConverter.ConvertColor(eColor.Red);
|
|
}
|
|
else {
|
|
color = ColorConverter.ConvertColor(eColor.Magenta);
|
|
}
|
|
|
|
var results = patternResult.Results as List<BrightnessResult>;
|
|
foreach (var result in results) {
|
|
var msgs = new List<string>();
|
|
msgs.Add(string.Format("Min : {0}", result.Min));
|
|
msgs.Add(string.Format("Max : {0}", result.Max));
|
|
msgs.Add(string.Format("Avg : {0}", result.Avg));
|
|
msgs.Add(string.Format("TactTime : {0} ms", Math.Round(patternResult.TactTimeInMilliSec)));
|
|
msgs.Add(string.Format("ID : {0}_{1}", patternResult.PatternIndex.PatternId, patternResult.PatternIndex.PatternSubId));
|
|
|
|
milCameraDisplay.AddTextList(
|
|
color, new ImagePoint(result.ImagePosition.X, result.ImagePosition.Y), msgs);
|
|
}
|
|
}
|
|
}
|
|
}
|