108 lines
4.1 KiB
C#
108 lines
4.1 KiB
C#
using VisionCommon;
|
|
using MilCommon;
|
|
using Hermes;
|
|
using Matrox.MatroxImagingLibrary;
|
|
|
|
namespace MilPattern {
|
|
public class ImgPreProcessor {
|
|
private static readonly int REPEAT_MAX_COUNT = 5;
|
|
|
|
public static MIL_ID GetPreProcessedImage(MIL_ID srcImage, PreProcessingParam preProcParam) {
|
|
MIL_ID preProcImage = MilEnvironment.GetCompatibleGrayImageBuffer(srcImage);
|
|
MIL_ID tempImage = MilEnvironment.GetCompatibleGrayImageBuffer(srcImage);
|
|
GetFilteredImage(srcImage, tempImage, preProcParam);
|
|
GetBinarizedImage(tempImage, preProcImage, preProcParam);
|
|
MilEnvironment.CopyImage(preProcImage, tempImage);
|
|
GetMorphedImage(tempImage, preProcImage, preProcParam);
|
|
|
|
MilEnvironment.FreeImage(tempImage);
|
|
return preProcImage;
|
|
}
|
|
|
|
#region Filtering
|
|
public static int GetFilteredImage(MIL_ID srcImage, MIL_ID dstImage, PreProcessingParam preProcParam) {
|
|
if (preProcParam.FilteringEnabled() && !preProcParam.FilParam.IsAllZero()) {
|
|
MIL_ID tempImage = MilEnvironment.GetCompatibleGrayImageBuffer(srcImage);
|
|
|
|
MilEnvironment.CopyImage(srcImage, 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++) {
|
|
MilFiltering.Filtering(tempImage, dstImage, ConvertToMilId(preProcParam.FilParam.Types[j]));
|
|
MilEnvironment.CopyImage(dstImage, tempImage);
|
|
}
|
|
}
|
|
}
|
|
|
|
MilEnvironment.FreeImage(tempImage);
|
|
}
|
|
else {
|
|
MilEnvironment.CopyImage(srcImage, dstImage);
|
|
}
|
|
return E1.OK;
|
|
}
|
|
|
|
private static MIL_ID ConvertToMilId(eFilteringType filteringType) {
|
|
if (filteringType == eFilteringType.Sharpen_4) {
|
|
return MIL.M_SHARPEN_4;
|
|
}
|
|
else if (filteringType == eFilteringType.Sharpen_8) {
|
|
return MIL.M_SHARPEN_8;
|
|
}
|
|
else {
|
|
return MIL.M_SMOOTH;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Binarization
|
|
public static int GetBinarizedImage(MIL_ID srcImage, MIL_ID preProcImage, PreProcessingParam preProcParam) {
|
|
if (preProcParam.BinaryEnabled()) {
|
|
MilBinarization.Binarize(
|
|
srcImage,
|
|
preProcImage,
|
|
preProcParam.BinParam.ThresholdLow,
|
|
preProcParam.BinParam.ThresholdHigh,
|
|
preProcParam.BinParam.ForegroundColor);
|
|
}
|
|
else {
|
|
MilEnvironment.CopyImage(srcImage, preProcImage);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
#endregion
|
|
|
|
#region Morphology
|
|
public static int GetMorphedImage(MIL_ID srcImage, MIL_ID dstImage, PreProcessingParam preProcParam) {
|
|
if (preProcParam.MorphEnabled() && !preProcParam.MorphParam.IsAllZero()) {
|
|
MIL_ID tempImage = MilEnvironment.GetCompatibleGrayImageBuffer(srcImage);
|
|
|
|
MilEnvironment.CopyImage(srcImage, tempImage);
|
|
|
|
for (int j = 0; j < REPEAT_MAX_COUNT; j++) {
|
|
if (preProcParam.MorphParam.Types[j] != eMorphologyType.None) {
|
|
ConvertMorphology(tempImage, dstImage, preProcParam.MorphParam.Types[j], preProcParam.MorphParam.RepeatCounts[j]);
|
|
MilEnvironment.CopyImage(dstImage, tempImage);
|
|
}
|
|
}
|
|
|
|
MilEnvironment.FreeImage(tempImage);
|
|
}
|
|
else {
|
|
MilEnvironment.CopyImage(srcImage, dstImage);
|
|
}
|
|
|
|
return E1.OK;
|
|
}
|
|
|
|
private static MIL_ID ConvertMorphology(MIL_ID srcImage, MIL_ID dstImage, eMorphologyType morphologyType, int iteration) {
|
|
MilMorphology.Morphology(srcImage, dstImage, morphologyType, iteration);
|
|
|
|
return dstImage;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|