2026-01-26 14:51:28 +09:00

98 lines
3.7 KiB
C#

/*
* Copyright (c) 2025 LG Electronics Inc.
* SPDX-License-Identifier: LicenseRef-LGE-Proprietary
*/
using VisionCommon;
using CogCommon;
using Hermes;
using Cognex.VisionPro;
namespace CogPattern {
public class ImgPreProcessor {
private static readonly int REPEAT_MAX_COUNT = 5;
public static ICogImage GetPreProcessedImage(ICogImage srcImage, PreProcessingParam preProcParam) {
ICogImage tempImage = null;
ICogImage preProcImage = null;
GetFilteredImage(srcImage, ref tempImage, preProcParam);
GetBinarizedImage(tempImage, ref preProcImage, preProcParam);
CogEnvironment.CopyImage(preProcImage, ref tempImage);
GetMorphedImage(tempImage, ref preProcImage, preProcParam);
CogEnvironment.FreeImage(tempImage);
return preProcImage;
}
#region Filtering
public static int GetFilteredImage(ICogImage srcImage, ref ICogImage dstImage, PreProcessingParam preProcParam) {
if (preProcParam.FilteringEnabled() && !preProcParam.FilParam.IsAllZero()) {
ICogImage tempImage = null;
CogEnvironment.CopyImage(srcImage, ref tempImage);
for (int j = 0; j < REPEAT_MAX_COUNT; j++) {
if (preProcParam.FilParam.Types[j] != eFilteringType.None) {
for (int i = 0; i < preProcParam.FilParam.RepeatCounts[j]; i++) {
CogFiltering.Filtering(tempImage, ref dstImage, preProcParam.FilParam.Types[j]);
CogEnvironment.CopyImage(dstImage, ref tempImage);
}
}
}
CogEnvironment.FreeImage(tempImage);
}
else {
CogEnvironment.CopyImage(srcImage, ref dstImage);
}
return E1.OK;
}
#endregion
#region Binarization
public static int GetBinarizedImage(ICogImage srcImage, ref ICogImage preProcImage, PreProcessingParam preProcParam) {
if (preProcParam.BinParam.Enabled) {
CogBinarization.Binarize(
srcImage,
ref preProcImage,
preProcParam.BinParam.ThresholdLow,
preProcParam.BinParam.ThresholdHigh,
preProcParam.BinParam.ForegroundColor);
}
else {
CogEnvironment.CopyImage(srcImage, ref preProcImage);
}
return E1.OK;
}
#endregion
#region Morphology
public static int GetMorphedImage(ICogImage srcImage, ref ICogImage dstImage, PreProcessingParam preProcParam) {
if (preProcParam.MorphEnabled() && !preProcParam.MorphParam.IsAllZero()) {
ICogImage tempImage = null;
CogEnvironment.CopyImage(srcImage, ref tempImage);
for (int j = 0; j < REPEAT_MAX_COUNT; j++) {
if (preProcParam.MorphParam.Types[j] != eMorphologyType.None) {
ConvertMorphology(tempImage, ref dstImage, preProcParam.MorphParam.Types[j], preProcParam.MorphParam.RepeatCounts[j]);
CogEnvironment.CopyImage(dstImage, ref tempImage);
}
}
CogEnvironment.FreeImage(tempImage);
}
else {
CogEnvironment.CopyImage(srcImage, ref dstImage);
}
return E1.OK;
}
private static void ConvertMorphology(ICogImage srcImage, ref ICogImage dstImage, eMorphologyType morphologyType, int iteration) {
CogMorphology.Morphology(srcImage, ref dstImage, morphologyType, iteration);
}
#endregion
}
}