#include <mil.h>
void Filter (MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_CONST_TEXT_PTR SourceFile);
void SaltAndPepperFilter (MIL_ID MilSystem,
MIL_ID MilDisplay);
MIL_CONST_TEXT_PTR IMAGE_FOR_FILTER = M_IMAGE_PATH MIL_TEXT("MultipleTarget.mim");
#define NB_LOOP 10
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("AdaptiveFiltering\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This program demonstrates how to filter an image\n")
MIL_TEXT("using linear filtering and edge preserving methods.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, system, display, buffer,\n")
MIL_TEXT("image processing.\n\n"));
}
int MosMain(void)
{
PrintHeader();
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, M_NULL, M_NULL, M_NULL);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay);
MdispControl(MilDisplay, M_OVERLAY, M_ENABLE);
MdispControl(MilDisplay, M_SCALE_DISPLAY, M_ENABLE);
Filter(MilSystem, MilDisplay, IMAGE_FOR_FILTER);
SaltAndPepperFilter(MilSystem, MilDisplay);
MdispFree(MilDisplay);
MappFreeDefault(MilApplication, MilSystem, M_NULL, M_NULL, M_NULL);
return 0;
}
void Filter (MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_CONST_TEXT_PTR SourceFile)
{
MIL_ID MilImageId,
MilSubImage00,
MilSubImage01,
MilSubImage10,
MilSubImage11,
MilDstImage,
MilOverlayId;
MIL_INT SizeX, SizeY, SizeBand, Type;
MIL_DOUBLE Time;
MbufDiskInquire(SourceFile, M_SIZE_X , &SizeX );
MbufDiskInquire(SourceFile, M_SIZE_Y , &SizeY );
MbufDiskInquire(SourceFile, M_SIZE_BAND, &SizeBand);
MbufDiskInquire(SourceFile, M_TYPE , &Type );
MbufAlloc2d(MilSystem, 2 * SizeX, 2 * SizeY, Type, M_IMAGE + M_PROC + M_DISP, &MilImageId);
MbufChild2d(MilImageId, 0, 0, SizeX, SizeY, &MilSubImage00);
MbufChild2d(MilImageId, SizeX, 0, SizeX, SizeY, &MilSubImage01);
MbufChild2d(MilImageId, 0, SizeY, SizeX, SizeY, &MilSubImage10);
MbufChild2d(MilImageId, SizeX, SizeY, SizeX, SizeY, &MilSubImage11);
MbufAlloc2d(MilSystem, SizeX, SizeY, Type, M_IMAGE + M_PROC + M_DISP, &MilDstImage);
MbufLoad(SourceFile, MilSubImage00);
MosPrintf(MIL_TEXT("*****************\n"));
MosPrintf(MIL_TEXT("General filtering\n"));
MosPrintf(MIL_TEXT("*****************\n\n"));
MIL_ID MilLinearFilterIIRContext = MimAlloc(MilSystem, M_LINEAR_FILTER_IIR_CONTEXT, M_DEFAULT, M_NULL);
MimControl(MilLinearFilterIIRContext, M_FILTER_SMOOTHNESS, 40);
MimConvolve(MilSubImage00, MilSubImage01, MilLinearFilterIIRContext);
MimConvolve(MilSubImage00, MilDstImage, MilLinearFilterIIRContext);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
for (MIL_INT it = 0; it < NB_LOOP; ++it)
MimConvolve(MilSubImage00, MilDstImage, MilLinearFilterIIRContext);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MosPrintf(MIL_TEXT("Deriche filter: %f ms\n"), Time*1000/NB_LOOP);
MimFree(MilLinearFilterIIRContext);
MimFilterAdaptive(M_BILATERAL, MilSubImage00, MilSubImage10, 20, 0.1, 5, M_DEFAULT);
MimFilterAdaptive(M_BILATERAL, MilSubImage00, MilDstImage, 20, 0.1, 5, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
for (MIL_INT it = 0; it < NB_LOOP; ++it)
MimFilterAdaptive(M_BILATERAL, MilSubImage00, MilDstImage, 20, 0.1, 5, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MosPrintf(MIL_TEXT("Bilateral filter: %f ms\n"), Time*1000/NB_LOOP);
MimFilterAdaptive(M_NOISE_PEAK_REMOVAL, MilSubImage00, MilSubImage11, 5, 5, 15, M_DEFAULT);
MimFilterAdaptive(M_NOISE_PEAK_REMOVAL, MilSubImage00, MilDstImage, 5, 5, 15, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
for (MIL_INT it = 0; it < NB_LOOP; ++it)
MimFilterAdaptive(M_NOISE_PEAK_REMOVAL, MilSubImage00, MilDstImage, 5, 5, 15, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MosPrintf(MIL_TEXT("Noise peak removal filter: %f ms\n\n"), Time*1000/NB_LOOP);
MdispSelect(MilDisplay, MilImageId);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayId);
MgraText(M_DEFAULT, MilOverlayId, 0, 0, MIL_TEXT("Source image"));
MgraText(M_DEFAULT, MilOverlayId, SizeX, 0, MIL_TEXT("Deriche filter"));
MgraText(M_DEFAULT, MilOverlayId, 0, SizeY, MIL_TEXT("Bilateral filter"));
MgraText(M_DEFAULT, MilOverlayId, SizeX, SizeY, MIL_TEXT("Noise peak removal filter"));
MosPrintf(MIL_TEXT("The image has been filtered using various techniques. \n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilDstImage);
MbufFree(MilSubImage11);
MbufFree(MilSubImage10);
MbufFree(MilSubImage01);
MbufFree(MilSubImage00);
MbufFree(MilImageId);
}
#define SIZE_X 271
#define SIZE_Y 256
#define BUFFER_TYPE M_UNSIGNED+8
MIL_CONST_TEXT_PTR ORIGINAL_IMAGE = M_IMAGE_PATH MIL_TEXT("CircuitBoardPart0.mim");
#define SEED_VALUE 42
void AddSaltAndPepperNoise(MIL_ID MilSystem,
MIL_ID MilImageId)
{
MIL_ID AugmentContext;
MimAlloc(MilSystem, M_AUGMENTATION_CONTEXT, M_DEFAULT, &AugmentContext);
MimControl(AugmentContext, M_AUG_SEED_MODE, M_RNG_INIT_VALUE);
MimControl(AugmentContext, M_AUG_RNG_INIT_VALUE, SEED_VALUE);
MimControl(AugmentContext, M_AUG_NOISE_SALT_PEPPER_OP, M_ENABLE);
MimControl(AugmentContext, M_AUG_NOISE_SALT_PEPPER_OP_DENSITY, 0.035);
MimAugment(AugmentContext, MilImageId, MilImageId, M_DEFAULT, M_DEFAULT);
MimFree(AugmentContext);
}
void SaltAndPepperFilter (MIL_ID MilSystem,
MIL_ID MilDisplay)
{
MIL_ID MilImageId,
MilSubImage00,
MilSubImage01,
MilSubImage10,
MilSubImage11,
MilDstImage,
MilOverlayId;
MIL_DOUBLE Time;
MbufAlloc2d(MilSystem, 2 * SIZE_X, 2 * SIZE_Y, BUFFER_TYPE, M_IMAGE + M_PROC + M_DISP, &MilImageId);
MbufChild2d(MilImageId, 0, 0, SIZE_X, SIZE_Y, &MilSubImage00);
MbufChild2d(MilImageId, SIZE_X, 0, SIZE_X, SIZE_Y, &MilSubImage01);
MbufChild2d(MilImageId, 0, SIZE_Y, SIZE_X, SIZE_Y, &MilSubImage10);
MbufChild2d(MilImageId, SIZE_X, SIZE_Y, SIZE_X, SIZE_Y, &MilSubImage11);
MbufAlloc2d(MilSystem, SIZE_X, SIZE_Y, BUFFER_TYPE, M_IMAGE + M_PROC + M_DISP, &MilDstImage);
MbufClear(MilImageId, 0);
MbufLoad(ORIGINAL_IMAGE, MilSubImage00);
MbufCopy(MilSubImage00, MilSubImage01);
AddSaltAndPepperNoise(MilSystem, MilSubImage01);
MosPrintf(MIL_TEXT("*************************\n"));
MosPrintf(MIL_TEXT("Salt and pepper filtering\n"));
MosPrintf(MIL_TEXT("*************************\n\n"));
MimRank(MilSubImage01, MilSubImage10, M_3X3_RECT, M_MEDIAN, M_GRAYSCALE);
MimRank(MilSubImage01, MilSubImage10, M_3X3_RECT, M_MEDIAN, M_GRAYSCALE);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
for (MIL_INT it = 0; it < NB_LOOP; ++it)
MimRank(MilSubImage01, MilSubImage10, M_3X3_RECT, M_MEDIAN, M_GRAYSCALE);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MosPrintf(MIL_TEXT("Median rank: %f ms\n"), Time*1000/NB_LOOP);
MIL_DOUBLE NbIter = 3;
MIL_DOUBLE Gap = 0;
MIL_DOUBLE MinVariation = 30;
MimFilterAdaptive(M_NOISE_PEAK_REMOVAL, MilSubImage01, MilSubImage11, NbIter, Gap, MinVariation, M_DEFAULT);
MimFilterAdaptive(M_NOISE_PEAK_REMOVAL, MilSubImage01, MilDstImage, NbIter, Gap, MinVariation, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_RESET+M_SYNCHRONOUS, M_NULL);
for (MIL_INT it = 0; it < NB_LOOP; ++it)
MimFilterAdaptive(M_NOISE_PEAK_REMOVAL, MilSubImage01, MilDstImage, NbIter, Gap, MinVariation, M_DEFAULT);
MappTimer(M_DEFAULT, M_TIMER_READ+M_SYNCHRONOUS, &Time);
MosPrintf(MIL_TEXT("Noise peak removal filter: %f ms\n\n"), Time*1000/NB_LOOP);
MdispControl(MilDisplay, M_OVERLAY_CLEAR, M_DEFAULT);
MdispSelect(MilDisplay, MilImageId);
MdispInquire(MilDisplay, M_OVERLAY_ID, &MilOverlayId);
MgraText(M_DEFAULT, MilOverlayId, 0, 0, MIL_TEXT("Original image"));
MgraText(M_DEFAULT, MilOverlayId, SIZE_X, 0, MIL_TEXT("Noisy image"));
MgraText(M_DEFAULT, MilOverlayId, 0, SIZE_Y, MIL_TEXT("Median rank filter"));
MgraText(M_DEFAULT, MilOverlayId, SIZE_X, SIZE_Y, MIL_TEXT("Noise peak removal filter"));
MosPrintf(MIL_TEXT("The noise peak removal technique better preserves the edges.\n"));
MosPrintf(MIL_TEXT("Also, it only modifies pixels that fit the parameters; the median\nrank modifies all pixels.\n"));
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n\n"));
MosGetch();
MbufFree(MilDstImage);
MbufFree(MilSubImage11);
MbufFree(MilSubImage10);
MbufFree(MilSubImage01);
MbufFree(MilSubImage00);
MbufFree(MilImageId);
}