#include <mil.h>
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("Morphology\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This program performs various grayscale\n")
MIL_TEXT("and binary morphological operations.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, system, display, buffer,\n")
MIL_TEXT("graphic, image processing.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
static const MIL_TEXT_CHAR* TopHatFilteringFilename = M_IMAGE_PATH MIL_TEXT("/Preprocessing/Dust.tif");
void TopHatFiltering(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay);
static const MIL_TEXT_CHAR* ObjectSkeletonFilename = M_IMAGE_PATH MIL_TEXT("/Preprocessing/Circuit.tif");
void ObjectSkeleton(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay);
static const MIL_TEXT_CHAR* ObjectConnectMapFilename = M_IMAGE_PATH MIL_TEXT("/Preprocessing/CircuitPins.tif");
void ObjectConnectMap(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay);
static const MIL_TEXT_CHAR* ObjectSegmentationFilename = M_IMAGE_PATH MIL_TEXT("/Preprocessing/Connector.tif");
void ObjectSegmentation(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay);
static const MIL_TEXT_CHAR* MorphologicalReconstructionFilename = M_IMAGE_PATH MIL_TEXT("/Preprocessing/Retina.tif");
void MorphologicalReconstruction(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay);
void MorphoReconstruction(MIL_ID MilSystem,
MIL_ID SrcImage,
MIL_ID Seed,
MIL_ID DstImage,
MIL_INT MaxIter);
static const MIL_TEXT_CHAR* ObjectPerimeterFilename = M_IMAGE_PATH MIL_TEXT("/Cell.mim");
void ObjectPerimeter(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay);
void AllocDisplayImage(MIL_ID MilSystem,
MIL_ID SrcImage,
MIL_ID MilDisplay,
MIL_ID& DispProcImage,
MIL_ID& MilOverlay);
void AllocGenPseudoColorLUT(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_INT StartIndex,
MIL_INT EndIndex,
MIL_ID& PseudoColorLut);
int MosMain(void)
{
MIL_ID MilApplication = MappAlloc(M_NULL, M_DEFAULT, M_NULL);
MIL_ID MilSystem = MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, M_NULL);
MIL_ID MilDisplay = MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, M_NULL);
PrintHeader();
TopHatFiltering(TopHatFilteringFilename, MilSystem, MilDisplay);
ObjectSkeleton(ObjectSkeletonFilename, MilSystem, MilDisplay);
ObjectConnectMap(ObjectConnectMapFilename, MilSystem, MilDisplay);
ObjectSegmentation(ObjectSegmentationFilename, MilSystem, MilDisplay);
ObjectPerimeter(ObjectPerimeterFilename, MilSystem, MilDisplay);
MorphologicalReconstruction(MorphologicalReconstructionFilename, MilSystem, MilDisplay);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}
void TopHatFiltering(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay)
{
MosPrintf(MIL_TEXT("[TOP_HAT FILTERING]\n\n")
MIL_TEXT("In this example a top-hat filtering operation with\n")
MIL_TEXT("a dedicated structuring element is used to enhance\n")
MIL_TEXT("defects in a scene with non-uniform illumination.\n\n"));
MIL_ID MilSrcImage = MbufRestore(SrcFilename, MilSystem, M_NULL);
MIL_ID MilDispProcImage,
MilOverlayImage;
AllocDisplayImage(MilSystem, MilSrcImage, MilDisplay, MilDispProcImage, MilOverlayImage);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MIL_ID MilStructElement = MbufAlloc2d(MilSystem, 1, 8, 32, M_STRUCT_ELEMENT, M_NULL);
MbufClear(MilStructElement, 0);
MimMorphic(MilSrcImage, MilDispProcImage, MilStructElement, M_TOP_HAT, 1, M_GRAYSCALE);
MosPrintf(MIL_TEXT("The result of the top-hat filtering is displayed.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimBinarize(MilDispProcImage, MilDispProcImage, M_PERCENTILE_VALUE+M_GREATER, 95, M_NULL);
MosPrintf(MIL_TEXT("The 5%% brightest pixels are thresholded and displayed.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimOpen(MilDispProcImage, MilDispProcImage, 1, M_BINARY);
MosPrintf(MIL_TEXT("A morphological opening is applied to remove small noise.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilSrcImage);
MbufFree(MilStructElement);
MbufFree(MilDispProcImage);
}
void ObjectSkeleton(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay)
{
MosPrintf(MIL_TEXT("[OBJECT SKELETON]\n\n")
MIL_TEXT("In this example a binary thinning operation is used to extract\n")
MIL_TEXT("the paths in a network. The result of a distance transformation\n")
MIL_TEXT("is combined with the path in order to determine the thickness of\n")
MIL_TEXT("the network.\n")
MIL_TEXT("A pseudo color display is used to enhance the final result.\n\n"));
MIL_ID MilSrcImage = MbufRestore(SrcFilename, MilSystem, M_NULL);
MIL_ID MilDispProcImage,
MilOverlayImage;
AllocDisplayImage(MilSystem, MilSrcImage, MilDisplay, MilDispProcImage, MilOverlayImage);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MIL_INT SizeX, SizeY;
MbufInquire(MilSrcImage, M_SIZE_X, &SizeX);
MbufInquire(MilSrcImage, M_SIZE_Y, &SizeY);
MIL_ID MilDistanceImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MIL_ID MilSkeletonImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MimBinarize(MilSrcImage, MilSkeletonImage, M_FIXED+M_LESS, 25, M_NULL);
MimClose(MilSkeletonImage, MilSkeletonImage, 1, M_BINARY);
MbufCopy(MilSkeletonImage, MilOverlayImage);
MosPrintf(MIL_TEXT("The source image is thresholded and displayed.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimDistance(MilSkeletonImage, MilDistanceImage, M_CHAMFER_3_4);
MimThin(MilSkeletonImage, MilSkeletonImage, M_TO_SKELETON, M_BINARY3);
MbufCopy(MilSkeletonImage, MilOverlayImage);
MosPrintf(MIL_TEXT("A binary thinning is applied and the result skeleton is displayed.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimArith(MilSkeletonImage, MilDistanceImage, MilDistanceImage, M_AND);
MIL_ID MilStatResult = MimAllocResult(MilSystem, 1, M_STAT_LIST, M_NULL);
MimStat(MilDistanceImage, MilStatResult, M_MAX, M_NULL, M_NULL, M_NULL);
MIL_INT MaxValue;
MimGetResult(MilStatResult, M_MAX+M_TYPE_MIL_INT, &MaxValue);
MIL_ID MilPseudoColorLut;
AllocGenPseudoColorLUT(MilSystem, MilDisplay, 1, MaxValue-5, MilPseudoColorLut);
MimLutMap(MilDistanceImage, MilOverlayImage, MilPseudoColorLut);
MosPrintf(MIL_TEXT("The thickness of the object is retrieved by combining the object's skeleton\n"));
MosPrintf(MIL_TEXT("with a distance transform result of the object. The maximum distance value\n"));
MosPrintf(MIL_TEXT("is %d pixels. A LUT mapping is used to display the skeleton in pseudo colors\n"), (int)MaxValue);
MosPrintf(MIL_TEXT("based on the object's thickness: blue = thin sections to red = thick sections.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilSrcImage);
MbufFree(MilDistanceImage);
MbufFree(MilSkeletonImage);
MbufFree(MilPseudoColorLut);
MbufFree(MilDispProcImage);
MimFree(MilStatResult);
}
void ObjectConnectMap(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay)
{
MosPrintf(MIL_TEXT("[OBJECT CONNECT MAP]\n\n")
MIL_TEXT("In this example a binary thinning is combined with a connect map\n")
MIL_TEXT("operation in order to identify the breaks in and the bridges\n")
MIL_TEXT("between parallel circuit lines.\n\n"));
MIL_ID MilSrcImage = MbufRestore(SrcFilename, MilSystem, M_NULL);
MIL_ID MilDispProcImage,
MilOverlayImage;
AllocDisplayImage(MilSystem, MilSrcImage, MilDisplay, MilDispProcImage, MilOverlayImage);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MIL_INT SizeX, SizeY;
MbufInquire(MilSrcImage, M_SIZE_X, &SizeX);
MbufInquire(MilSrcImage, M_SIZE_Y, &SizeY);
MIL_ID MilProcImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L + M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MIL_ID MilBinaryImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 1L + M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MIL_ID MilSkeletonImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 1L + M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MimConvolve(MilSrcImage, MilProcImage, M_DERICHE_FILTER(M_SMOOTH, 50));
const MIL_INT ChildOffsetX = 30;
const MIL_INT ChildOffsetY = 50;
const MIL_INT ChildSizeX = 80;
const MIL_INT ChildSizeY = 300;
MIL_ID MilProcChild = MbufChild2d(MilProcImage, ChildOffsetX, ChildOffsetY, ChildSizeX, ChildSizeY, M_NULL);
MIL_INT ThresholdValue = MimBinarize(MilProcChild, M_NULL, M_BIMODAL+M_GREATER, M_NULL, M_NULL);
MimBinarize(MilProcImage, MilBinaryImage, M_FIXED+M_GREATER, (MIL_DOUBLE)ThresholdValue, M_NULL);
MimOpen(MilBinaryImage, MilBinaryImage, 1, M_BINARY);
MimThin(MilBinaryImage, MilSkeletonImage, M_TO_SKELETON, M_BINARY3);
MbufClearCond(MilOverlayImage, 255, 255, 255, MilSkeletonImage, M_NOT_EQUAL, 0);
MosPrintf(MIL_TEXT("The skeleton of the segmented source image is displayed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MIL_ID LutBufId = MbufAlloc1d(MilSystem, 512, 8L+M_UNSIGNED, M_LUT, M_NULL);
MbufClear(LutBufId, 0);
const MIL_UINT8 IsolatedPointCode = 1;
MbufPut1d(LutBufId, 256, 1, &IsolatedPointCode);
const MIL_UINT8 EndPointCode = 2;
MbufPut1d(LutBufId, 257, 1, &EndPointCode);
MbufPut1d(LutBufId, 258, 1, &EndPointCode);
MbufPut1d(LutBufId, 260, 1, &EndPointCode);
MbufPut1d(LutBufId, 264, 1, &EndPointCode);
MbufPut1d(LutBufId, 272, 1, &EndPointCode);
MbufPut1d(LutBufId, 288, 1, &EndPointCode);
MbufPut1d(LutBufId, 320, 1, &EndPointCode);
MbufPut1d(LutBufId, 384, 1, &EndPointCode);
MbufPut1d(LutBufId, 259, 1, &EndPointCode);
MbufPut1d(LutBufId, 262, 1, &EndPointCode);
MbufPut1d(LutBufId, 268, 1, &EndPointCode);
MbufPut1d(LutBufId, 280, 1, &EndPointCode);
MbufPut1d(LutBufId, 304, 1, &EndPointCode);
MbufPut1d(LutBufId, 352, 1, &EndPointCode);
MbufPut1d(LutBufId, 448, 1, &EndPointCode);
MbufPut1d(LutBufId, 385, 1, &EndPointCode);
const MIL_UINT8 TriplePointCode = 3;
MbufPut1d(LutBufId, 277, 1, &TriplePointCode);
MbufPut1d(LutBufId, 340, 1, &TriplePointCode);
MbufPut1d(LutBufId, 337, 1, &TriplePointCode);
MbufPut1d(LutBufId, 325, 1, &TriplePointCode);
MbufPut1d(LutBufId, 298, 1, &TriplePointCode);
MbufPut1d(LutBufId, 424, 1, &TriplePointCode);
MbufPut1d(LutBufId, 418, 1, &TriplePointCode);
MbufPut1d(LutBufId, 394, 1, &TriplePointCode);
MbufPut1d(LutBufId, 404, 1, &TriplePointCode);
MbufPut1d(LutBufId, 293, 1, &TriplePointCode);
MbufPut1d(LutBufId, 338, 1, &TriplePointCode);
MbufPut1d(LutBufId, 329, 1, &TriplePointCode);
MbufPut1d(LutBufId, 297, 1, &TriplePointCode);
MbufPut1d(LutBufId, 330, 1, &TriplePointCode);
MbufPut1d(LutBufId, 402, 1, &TriplePointCode);
MbufPut1d(LutBufId, 420, 1, &TriplePointCode);
const MIL_UINT8 CrossPointCode = 4;
MbufPut1d(LutBufId, 341, 1, &CrossPointCode);
MbufPut1d(LutBufId, 426, 1, &CrossPointCode);
MimConnectMap(MilSkeletonImage, MilProcImage, LutBufId);
MimDilate(MilProcImage, MilProcImage, 2, M_GRAYSCALE);
MbufClearCond(MilOverlayImage, 0, 0, 255, MilProcImage, M_EQUAL, IsolatedPointCode);
MbufClearCond(MilOverlayImage, 255, 0, 255, MilProcImage, M_EQUAL, EndPointCode );
MbufClearCond(MilOverlayImage, 255, 0, 0, MilProcImage, M_EQUAL, TriplePointCode );
MbufClearCond(MilOverlayImage, 255, 255, 0, MilProcImage, M_EQUAL, CrossPointCode );
MosPrintf(MIL_TEXT("The result of the connectivity analysis is displayed:\n"));
MosPrintf(MIL_TEXT(" - blue : isolated points\n"));
MosPrintf(MIL_TEXT(" - red : triple points\n"));
MosPrintf(MIL_TEXT(" - yellow : cross points\n"));
MosPrintf(MIL_TEXT(" - magenta: end points\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilSrcImage);
MbufFree(MilProcChild);
MbufFree(MilProcImage);
MbufFree(MilBinaryImage);
MbufFree(MilSkeletonImage);
MbufFree(MilDispProcImage);
MbufFree(LutBufId);
}
void ObjectSegmentation(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay)
{
MosPrintf(MIL_TEXT("[OBJECT SEGMENTATION]\n\n")
MIL_TEXT("In this example, combinations of binary morphological operations\n")
MIL_TEXT("are used to segment the object into its principal components.\n\n"));
MIL_ID MilSrcImage = MbufRestore(SrcFilename, MilSystem, M_NULL);
MIL_ID MilDispProcImage,
MilOverlayImage;
AllocDisplayImage(MilSystem, MilSrcImage, MilDisplay, MilDispProcImage, MilOverlayImage);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MIL_INT SizeX, SizeY;
MbufInquire(MilSrcImage, M_SIZE_X, &SizeX);
MbufInquire(MilSrcImage, M_SIZE_Y, &SizeY);
MIL_ID MilDstImage1 = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MIL_ID MilDstImage2 = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MimBinarize(MilSrcImage, MilSrcImage, M_BIMODAL+M_GREATER, M_NULL, M_NULL);
MIL_ID MilStructElement = MbufAlloc2d(MilSystem, 10, 1, 32, M_STRUCT_ELEMENT, M_NULL);
MbufControlNeighborhood(MilStructElement, M_OVERSCAN, M_MIRROR);
MbufClear(MilStructElement, 1);
MimMorphic(MilSrcImage, MilDstImage1, MilStructElement, M_OPEN, 1, M_BINARY);
MbufFree(MilStructElement);
MbufCopy(MilDstImage1, MilOverlayImage);
MosPrintf(MIL_TEXT("A horizontal opening is used to remove the vertical structures.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MilStructElement = MbufAlloc2d(MilSystem, 1, 40, 32, M_STRUCT_ELEMENT, M_NULL);
MbufControlNeighborhood(MilStructElement, M_OVERSCAN, M_MIRROR);
MbufClear(MilStructElement, 1);
MimMorphic(MilSrcImage, MilDstImage2, MilStructElement, M_OPEN, 1, M_BINARY);
MimDilate(MilDstImage2, MilDstImage2, 1, M_BINARY);
MbufFree(MilStructElement);
MbufCopy(MilDstImage2, MilOverlayImage);
MosPrintf(MIL_TEXT("A vertical opening is used to isolate the vertical structures.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MimShift(MilDstImage1, MilDstImage1, -2);
MimShift(MilDstImage2, MilDstImage2, -1);
MimArith(MilDstImage1, MilDstImage2, MilDstImage1, M_OR);
MbufCopy(MilDstImage1,MilDstImage2);
MbufCopyCond(MilSrcImage, MilDstImage1, MilDstImage2, M_EQUAL, 0);
MIL_ID MilPseudoColorLut;
AllocGenPseudoColorLUT(MilSystem, MilDisplay, 255>>2, 255, MilPseudoColorLut);
MimLutMap(MilDstImage1, MilOverlayImage, MilPseudoColorLut);
MosPrintf(MIL_TEXT("Results are combined and displayed using pseudo colors.\n"));
MosPrintf(MIL_TEXT("The twisted pin's sections appear with emphasis in red.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilSrcImage);
MbufFree(MilDispProcImage);
MbufFree(MilDstImage1);
MbufFree(MilDstImage2);
MbufFree(MilPseudoColorLut);
}
void ObjectPerimeter(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay)
{
MosPrintf(MIL_TEXT("[OBJECT PERIMETER]\n\n")
MIL_TEXT("In this example, the exoskeletons of the perimeters of\n")
MIL_TEXT("dark objects are extracted and displayed.\n\n"));
MIL_ID MilSrcImage = MbufRestore(SrcFilename, MilSystem, M_NULL);
MIL_ID MilDispProcImage,
MilOverlayImage;
AllocDisplayImage(MilSystem, MilSrcImage, MilDisplay, MilDispProcImage, MilOverlayImage);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MIL_INT SizeX, SizeY;
MbufInquire(MilSrcImage, M_SIZE_X, &SizeX);
MbufInquire(MilSrcImage, M_SIZE_Y, &SizeY);
MIL_ID BinImage,
DilBinImage;
const MIL_DOUBLE IMAGE_THRESHOLD_VALUE = 200;
const MIL_INT SMALL_PARTICLE_RADIUS = 2L;
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &BinImage);
MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + M_UNSIGNED, M_IMAGE + M_PROC, &DilBinImage);
MimBinarize(MilSrcImage, BinImage, M_FIXED + M_LESS_OR_EQUAL, IMAGE_THRESHOLD_VALUE, M_NULL);
MimOpen(BinImage, BinImage, SMALL_PARTICLE_RADIUS, M_BINARY);
MimDilate(BinImage, DilBinImage, 1L, M_BINARY);
MimArith(BinImage, DilBinImage, BinImage, M_XOR);
MbufClear(MilDispProcImage, 0);
MbufClearCond(MilOverlayImage, 255, 0, 255, BinImage, M_EQUAL, 1);
MosPrintf(MIL_TEXT("Exoskeletons of the object's perimeters are displaed.\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n"));
MosGetch();
MbufFree(MilSrcImage);
MbufFree(MilDispProcImage);
MbufFree(DilBinImage);
MbufFree(BinImage);
}
void MorphologicalReconstruction(const MIL_TEXT_CHAR* SrcFilename,
MIL_ID MilSystem,
MIL_ID MilDisplay)
{
MosPrintf(MIL_TEXT("[MORPHOLOGICAL RECONSTRUCTION]\n\n")
MIL_TEXT("In this example, a combination of morphological operations and image\n")
MIL_TEXT("arithmetics is used to perform a morphological reconstruction operation.\n\n"));
MIL_ID MilSrcImage = MbufRestore(SrcFilename, MilSystem, M_NULL);
MIL_ID MilDispProcImage,
MilOverlayImage;
AllocDisplayImage(MilSystem, MilSrcImage, MilDisplay, MilDispProcImage, MilOverlayImage);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MIL_INT SizeX, SizeY;
MbufInquire(MilSrcImage, M_SIZE_X, &SizeX);
MbufInquire(MilSrcImage, M_SIZE_Y, &SizeY);
MIL_ID MilSeedImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MIL_ID MilDstImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MimDilate(MilSrcImage, MilSeedImage, 5, M_GRAYSCALE);
MbufCopy(MilSeedImage, MilDispProcImage);
MosPrintf(MIL_TEXT("The reconstruction's seed image is displayed.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MorphoReconstruction(MilSystem, MilSrcImage, MilSeedImage, MilDstImage, 100);
MbufCopy(MilDstImage, MilDispProcImage);
MosPrintf(MIL_TEXT("The reconstruction is displayed.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
MbufFree(MilSrcImage);
MbufFree(MilSeedImage);
MbufFree(MilDstImage);
MbufFree(MilDispProcImage);
}
void MorphoReconstruction(MIL_ID MilSystem,
MIL_ID MilSrcImage,
MIL_ID MilSeedImage,
MIL_ID MilDstImage,
MIL_INT MaxIter)
{
MIL_ID MilCountResult = MimAllocResult(MilSystem, 1, M_COUNT_LIST, M_NULL);
MIL_INT SizeX, SizeY;
MbufInquire(MilSrcImage, M_SIZE_X, &SizeX);
MbufInquire(MilSrcImage, M_SIZE_Y, &SizeY);
MIL_ID MilCondImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MIL_ID MilPrevDstImage = MbufAlloc2d(MilSystem, SizeX, SizeY, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MbufCopy(MilSeedImage, MilDstImage);
MbufCopy(MilSeedImage, MilPrevDstImage);
MimArith(MilDstImage, MilSrcImage, MilCondImage, M_SUB+M_SATURATION);
MbufCopyCond(MilSrcImage, MilPrevDstImage, MilCondImage, M_EQUAL, 0);
MimErode(MilPrevDstImage, MilDstImage, 1, M_GRAYSCALE);
MIL_INT CountDiff=1;
for(MIL_INT ii=0; ii<MaxIter && (CountDiff>0); ii++)
{
MimArith(MilDstImage, MilSrcImage, MilCondImage, M_SUB+M_SATURATION);
MbufCopyCond(MilSrcImage, MilDstImage, MilCondImage, M_EQUAL, 0);
MimCountDifference(MilDstImage, MilPrevDstImage, MilCountResult);
MIL_INT CountDiff;
MimGetResult(MilCountResult, M_VALUE+M_TYPE_MIL_INT, &CountDiff);
if(CountDiff>0)
{
MbufCopy(MilDstImage, MilPrevDstImage);
MimErode(MilPrevDstImage, MilDstImage, 1, M_GRAYSCALE);
}
}
MbufFree(MilCondImage);
MbufFree(MilPrevDstImage);
MimFree(MilCountResult);
};
void AllocDisplayImage(MIL_ID MilSystem,
MIL_ID MilSrcImage,
MIL_ID MilDisplay,
MIL_ID& MilDispProcImage,
MIL_ID& MilOverlayImage)
{
MIL_INT SrcSizeX, SrcSizeY;
MbufInquire(MilSrcImage, M_SIZE_X, &SrcSizeX);
MbufInquire(MilSrcImage, M_SIZE_Y, &SrcSizeY);
MbufAlloc2d(MilSystem,
SrcSizeX,
SrcSizeY,
8L+M_UNSIGNED,
M_IMAGE+M_PROC+M_DISP,
&MilDispProcImage);
MbufCopy(MilSrcImage, MilDispProcImage);
MdispSelect(MilDisplay, MilDispProcImage);
MdispControl(MilDisplay, M_OVERLAY, M_ENABLE);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayImage);
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
}
void AllocGenPseudoColorLUT(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_INT StartIndex,
MIL_INT EndIndex,
MIL_ID& MilPseudoColorLut)
{
MIL_DOUBLE Slope = 160.0/(MIL_DOUBLE)(StartIndex-EndIndex);
MIL_DOUBLE Offset = -Slope*(MIL_DOUBLE)EndIndex;
MIL_UINT8 HLut[256];
for(MIL_INT ii=0; ii<256; ii++)
{
if(ii<StartIndex)
HLut[ii]=160;
else if(ii>EndIndex)
HLut[ii]=0;
else
HLut[ii]=static_cast<MIL_UINT8>(Slope*ii+Offset+0.5);
}
MIL_ID MilTmpBuffer = MbufAllocColor(MilSystem, 3, 256, 1, 8L+M_UNSIGNED, M_IMAGE+M_PROC, M_NULL);
MbufClear(MilTmpBuffer, M_RGB888(0, 230, 120));
MbufPutColor(MilTmpBuffer, M_SINGLE_BAND, 0, HLut);
MimConvert(MilTmpBuffer, MilTmpBuffer, M_HSL_TO_RGB);
MIL_INT KeyingColor;
MdispInquire(MilDisplay, M_TRANSPARENT_COLOR, &KeyingColor);
MIL_ID MilTmpChild = MbufChild2d(MilTmpBuffer, 0, 0, 1, 1, M_NULL);
MbufClear(MilTmpChild, (double) KeyingColor);
MilPseudoColorLut = MbufAllocColor(MilSystem, 3, 256, 1, 8L+M_UNSIGNED, M_LUT, M_NULL);
MbufCopy(MilTmpBuffer, MilPseudoColorLut);
MbufFree(MilTmpChild);
MbufFree(MilTmpBuffer);
};