207 lines
8.8 KiB
C#
207 lines
8.8 KiB
C#
using Aurora.Api;
|
|
using MilCommon;
|
|
using VisionCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using Aurora.Api.DataType;
|
|
|
|
namespace MilPattern {
|
|
public class BlobRunParam : IRunParam, ICloneable {
|
|
public BlobParamExt BlobParamExt { get; set; } = new BlobParamExt();
|
|
|
|
public bool IsValidFindRoi(ImageSize imageSize) {
|
|
return (0 < BlobParamExt.FindROI.Width && BlobParamExt.FindROI.Width < imageSize.Width) &&
|
|
(0 < BlobParamExt.FindROI.Height && BlobParamExt.FindROI.Height < imageSize.Height);
|
|
}
|
|
|
|
public ROI GetFindRoi() {
|
|
return BlobParamExt.FindROI.Clone() as ROI;
|
|
}
|
|
|
|
public void SetFindROI(ROI findRoi) {
|
|
BlobParamExt.FindROI = findRoi;
|
|
}
|
|
|
|
public object Clone() {
|
|
return new BlobRunParam() {
|
|
BlobParamExt = BlobParamExt.Clone() as BlobParamExt
|
|
};
|
|
}
|
|
}
|
|
|
|
internal static class MilBlob {
|
|
private static Logger _Logger = new Logger(typeof(MilBlob).Name);
|
|
|
|
public static MIL_ID CreateContext(MIL_ID milSystem) {
|
|
MIL_ID contextId = MIL.M_NULL;
|
|
MIL.MblobAlloc(milSystem, MIL.M_DEFAULT, MIL.M_DEFAULT, ref contextId);
|
|
|
|
return contextId;
|
|
}
|
|
|
|
public static MIL_ID LoadContext(MIL_ID milSystem, string filePath) {
|
|
MIL_ID contextId = MIL.M_NULL;
|
|
MIL.MblobRestore(filePath, milSystem, MIL.M_DEFAULT, ref contextId);
|
|
|
|
return contextId;
|
|
}
|
|
|
|
public static int SaveContext(MIL_ID contextId, string filePath) {
|
|
if (contextId != MIL.M_NULL && filePath != null) {
|
|
MIL.MblobSave(filePath, contextId, MIL.M_DEFAULT);
|
|
return E1.OK;
|
|
}
|
|
|
|
return E1.GENERIC;
|
|
}
|
|
|
|
public static void FreeContext(MIL_ID contextId) {
|
|
if (contextId != MIL.M_NULL) {
|
|
MIL.MblobFree(contextId);
|
|
}
|
|
}
|
|
|
|
public static MIL_ID CreateResultContext(MIL_ID contextId) {
|
|
var milSystem = GetSystemOfContext(contextId);
|
|
|
|
return MIL.MblobAllocResult(milSystem, MIL.M_DEFAULT, MIL.M_DEFAULT, MIL.M_NULL);
|
|
}
|
|
|
|
private static MIL_ID GetSystemOfContext(MIL_ID contextId) {
|
|
return (MIL_ID)MIL.MblobInquire(contextId, MIL.M_OWNER_SYSTEM, MIL.M_NULL);
|
|
}
|
|
|
|
public static int SetRunParam(MIL_ID contextId, IRunParam runParam) {
|
|
MIL.MblobControl(contextId, MIL.M_BOX, MIL.M_ENABLE);
|
|
MIL.MblobControl(contextId, MIL.M_MIN_AREA_BOX, MIL.M_ENABLE);
|
|
MIL.MblobControl(contextId, MIL.M_CHAINS, MIL.M_ENABLE);
|
|
MIL.MblobControl(contextId, MIL.M_CENTER_OF_GRAVITY + MIL.M_BINARY, MIL.M_ENABLE);
|
|
MIL.MblobControl(contextId, MIL.M_IDENTIFIER_TYPE, MIL.M_BINARY);
|
|
MIL.MblobControl(contextId, MIL.M_FOREGROUND_VALUE, MIL.M_NONZERO);
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
public static PatternResult Find(MIL_ID contextId, MIL_ID resultContextId, MIL_ID image, ROI findRoi, BlobParamExt paramExt) {
|
|
if (MilEnvironment.IsColorImage(image)) {
|
|
var grayImageId = MilEnvironment.ConvertToGray(image);
|
|
var patternResult = FindInternal(contextId, resultContextId, grayImageId, findRoi, paramExt);
|
|
MilEnvironment.FreeImage(grayImageId);
|
|
|
|
return patternResult;
|
|
}
|
|
return FindInternal(contextId, resultContextId, image, findRoi, paramExt);
|
|
}
|
|
|
|
private static PatternResult FindInternal(MIL_ID contextId, MIL_ID resultContextId, MIL_ID image, ROI findRoi, BlobParamExt paramExt) {
|
|
PatternResult finderResult;
|
|
MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET + MIL.M_SYNCHRONOUS, MIL.M_NULL);
|
|
|
|
if (ROI.IsNullOrEmpty(findRoi)) {
|
|
MIL.MblobCalculate(contextId, image, MIL.M_NULL, resultContextId);
|
|
finderResult = ExtractResults(resultContextId, MilEnvironment.GetImageCenter(image), findRoi, paramExt);
|
|
}
|
|
else {
|
|
var fittedFindRoi = FindWithCropImage(contextId, resultContextId, image, findRoi);
|
|
finderResult = ExtractResults(resultContextId, MilEnvironment.GetImageCenter(image), fittedFindRoi, 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 ROI FindWithCropImage(MIL_ID contextId, MIL_ID resultContextId, MIL_ID image, ROI findRoi) {
|
|
MIL_ID cropImage = MIL.M_NULL;
|
|
ROI fittedFindRoi = findRoi.Clone() as ROI;
|
|
|
|
try {
|
|
var imageSzie = MilEnvironment.GetImageSize(image);
|
|
if (ROI.IsOutsideImage(findRoi, imageSzie)) {
|
|
fittedFindRoi = ROI.FitRoiToImage(findRoi, imageSzie);
|
|
}
|
|
|
|
cropImage = MilEnvironment.CropImage(image, fittedFindRoi);
|
|
MIL.MblobCalculate(contextId, cropImage, MIL.M_NULL, resultContextId);
|
|
}
|
|
catch (Exception e) {
|
|
_Logger.E(e.Message);
|
|
}
|
|
finally {
|
|
MilEnvironment.FreeImage(cropImage);
|
|
}
|
|
|
|
return fittedFindRoi;
|
|
}
|
|
|
|
private static PatternResult ExtractResults(MIL_ID contextResult, ImagePoint imageCenter, ROI findRoi, BlobParamExt paramExt) {
|
|
List<BlobResult> results = new List<BlobResult>();
|
|
MIL_INT numOfResult = 0;
|
|
|
|
MIL.MblobSelect(contextResult, MIL.M_EXCLUDE_ONLY, MIL.M_AREA, MIL.M_OUT_RANGE, paramExt.AreaLow, paramExt.AreaHigh);
|
|
MIL.MblobGetResult(contextResult, MIL.M_GENERAL, MIL.M_NUMBER + MIL.M_TYPE_MIL_INT, ref numOfResult);
|
|
|
|
for (int resultIndex = 0; resultIndex < numOfResult; resultIndex++) {
|
|
double area = 0;
|
|
double gravityX = 0;
|
|
double gravityY = 0;
|
|
MIL.MblobGetResult(contextResult, MIL.M_BLOB_INDEX(resultIndex), MIL.M_AREA + MIL.M_TYPE_DOUBLE, ref area);
|
|
MIL.MblobGetResult(contextResult, MIL.M_BLOB_INDEX(resultIndex), MIL.M_CENTER_OF_GRAVITY_X + MIL.M_BINARY + MIL.M_TYPE_DOUBLE, ref gravityX);
|
|
MIL.MblobGetResult(contextResult, MIL.M_BLOB_INDEX(resultIndex), MIL.M_CENTER_OF_GRAVITY_Y + MIL.M_BINARY + MIL.M_TYPE_DOUBLE, ref gravityY);
|
|
|
|
var position = new ImagePoint(gravityX + findRoi.Left, gravityY + findRoi.Top);
|
|
results.Add(new BlobResult(
|
|
position,
|
|
(position - imageCenter) as ImagePoint,
|
|
area));
|
|
}
|
|
|
|
return new PatternResult() {
|
|
Status = (results.Count > 0) ? eFoundStatus.FoundOK : eFoundStatus.NotFound,
|
|
Results = results
|
|
};
|
|
}
|
|
|
|
public static PatternResult GetNotFoundPatternResult() {
|
|
return new PatternResult() {
|
|
Status = eFoundStatus.NotFound,
|
|
Results = new List<BlobResult>()
|
|
};
|
|
}
|
|
|
|
public static void Draw(PatternResult patternResult, IMilCameraDisplay milCameraDisplay) {
|
|
MIL_ID staticGraphicList = 0;
|
|
MIL.MdispInquire(milCameraDisplay.GetMilDisplayId(), MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, ref staticGraphicList);
|
|
|
|
if (patternResult.Status == eFoundStatus.FoundOK) {
|
|
var results = patternResult.Results as List<BlobResult>;
|
|
var color = GraphicColors.GetFindResultColor(patternResult.Status, isFixturePattern: false);
|
|
var crosses = new List<Cross>();
|
|
|
|
foreach (var result in results) {
|
|
var cross = new Cross(result.ImagePosition.Clone() as ImagePoint, 50);
|
|
crosses.Add(cross);
|
|
|
|
milCameraDisplay.SetPatternTracking(cross);
|
|
milCameraDisplay.AddCross(cross, color);
|
|
|
|
var msgs = new List<string>();
|
|
msgs.Add(string.Format("I({0:0.0}, {1:0.0})", result.ImagePosition.X, result.ImagePosition.Y));
|
|
msgs.Add(result.ConvertMaterialPosition());
|
|
msgs.Add(string.Format("Area : {0:0.00}", result.Area));
|
|
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);
|
|
}
|
|
milCameraDisplay.DrawPatternTrackings(crosses);
|
|
}
|
|
}
|
|
}
|
|
}
|