#include <mil.h>
void OfflineExtendedDepthOfField(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_INT NbImages,
MIL_INT SizeBand,
MIL_INT SizeX,
MIL_INT SizeY,
MIL_INT Type,
MIL_INT64 Attribute,
MIL_CONST_TEXT_PTR ImageDirectory);
void OnlineExtendedDepthOfField(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_INT NbImages,
MIL_INT SizeBand,
MIL_INT SizeX,
MIL_INT SizeY,
MIL_INT Type,
MIL_INT64 Attribute,
MIL_CONST_TEXT_PTR ImageDirectory);
#define IMAGES_DIR_SOURCE_BOARD M_IMAGE_PATH MIL_TEXT("ExtendedDepthOfField/BoardFocusStackingImages")
#define IMAGES_DIR_SOURCE_BOTTLES M_IMAGE_PATH MIL_TEXT("ExtendedDepthOfField/BottlesFocusStackingImages")
const MIL_INT NB_IMG_BOTTLES = 30;
const MIL_INT SIZE_BAND_IMG_BOTTLES = 1;
const MIL_INT SIZE_X_IMG_BOTTLES = 672;
const MIL_INT SIZE_Y_IMG_BOTTLES = 512;
const MIL_INT TYPE_IMG_BOTTLES = 8 + M_UNSIGNED;
const MIL_INT64 ATTRIBUTE_IMG_BOTTLES = M_IMAGE + M_DISP + M_GRAB + M_PROC;
const MIL_INT NB_IMG_BOARD = 25;
const MIL_INT SIZE_BAND_IMG_BOARD = 1;
const MIL_INT SIZE_X_IMG_BOARD = 672;
const MIL_INT SIZE_Y_IMG_BOARD = 512;
const MIL_INT TYPE_IMG_BOARD = 8 + M_UNSIGNED;
const MIL_INT64 ATTRIBUTE_IMG_BOARD = M_IMAGE + M_DISP + M_GRAB + M_PROC;
const MIL_INT MODE = M_RECONSTRUCTION;
const MIL_DOUBLE TRANSLATION_TOTELANCE = 1;
const MIL_DOUBLE MAXIMUM_CIRCLE_OF_CONFUSION_RADIUS = 8;
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("ExtendedDepthOfField\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This program demonstrates how to combine (fuse) multiple\n")
MIL_TEXT("images taken at different focus distances using a liquid\n")
MIL_TEXT("lens to give a resulting in-focus image with an extended\n")
MIL_TEXT("depth of field.\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, system, display, buffer,\n")
MIL_TEXT("image processing, registration.\n\n"));
}
int MosMain()
{
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAlloc(MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay);
PrintHeader();
MosPrintf(MIL_TEXT("\nFirst method (offline operation) :\n")
MIL_TEXT("The extended depth of field image is calculated in a single \n")
MIL_TEXT("step using all the provided images.\n\n")
MIL_TEXT("Press <Enter> to load the sequence of images.\n"));
MosGetch();
OfflineExtendedDepthOfField(MilSystem,
MilDisplay,
NB_IMG_BOTTLES,
SIZE_BAND_IMG_BOTTLES,
SIZE_X_IMG_BOTTLES,
SIZE_Y_IMG_BOTTLES,
TYPE_IMG_BOTTLES,
ATTRIBUTE_IMG_BOTTLES,
IMAGES_DIR_SOURCE_BOTTLES);
MosPrintf(MIL_TEXT("\nSecond method (online operation) :\n")
MIL_TEXT("The images are sequentially provided and pre-fused into a\n")
MIL_TEXT("result buffer. Then the extended depth of field image is generated.\n")
MIL_TEXT("Press <Enter> to load the images and to pre-fuse them sequentially.\n\n"));
MosGetch();
OnlineExtendedDepthOfField(MilSystem,
MilDisplay,
NB_IMG_BOARD,
SIZE_BAND_IMG_BOARD,
SIZE_X_IMG_BOARD,
SIZE_Y_IMG_BOARD,
TYPE_IMG_BOARD,
ATTRIBUTE_IMG_BOTTLES,
IMAGES_DIR_SOURCE_BOARD);
MdispFree(MilDisplay);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}
void OfflineExtendedDepthOfField(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_INT NbImages,
MIL_INT SizeBand,
MIL_INT SizeX,
MIL_INT SizeY,
MIL_INT Type,
MIL_INT64 Attribute,
MIL_CONST_TEXT_PTR ImageDirectory)
{
MIL_ID FusionImage;
MIL_ID DigId;
MIL_ID* ImagesArray;
ImagesArray = new MIL_ID[NbImages];
for (MIL_INT NumImg = 0; NumImg < NbImages; NumImg++)
{
MbufAllocColor(MilSystem, SizeBand, SizeX, SizeY, Type, Attribute, &ImagesArray[NumImg]);
}
MbufClone(ImagesArray[0], M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT, &FusionImage);
DigId = MdigAlloc(MilSystem, M_DEFAULT, ImageDirectory, M_EMULATED, M_NULL);
MdigProcess(DigId, ImagesArray, NbImages, M_SEQUENCE, M_DEFAULT, M_NULL, M_NULL);
MdigFree(DigId);
DigId = M_NULL;
for (MIL_INT NumImg = 0; NumImg < NbImages; NumImg++)
{
MdispSelect(MilDisplay, ImagesArray[NumImg]);
MosSleep(80);
}
MosPrintf(MIL_TEXT("\nPress <Enter> to process the sequence.\n\n"));
MosGetch();
MosPrintf(MIL_TEXT("Calculation in progress..."));
MregCalculate(M_DEFAULT_EXTENDED_DEPTH_OF_FIELD_CONTEXT, ImagesArray, FusionImage, NbImages, M_FUSION);
MdispSelect(MilDisplay, FusionImage);
MosPrintf(MIL_TEXT("\nPress <Enter> to continue.\n\n"));
MosGetch();
MbufFree(FusionImage);
for (MIL_INT NumImg = 0; NumImg < NbImages; NumImg++)
{
MbufFree(ImagesArray[NumImg]);
}
delete[] ImagesArray;
}
typedef struct
{
MIL_ID RegContext;
MIL_ID RegResult;
MIL_ID Display;
} HookDataStruct;
MIL_INT MFTYPE ProcessingFunction(MIL_INT HookType,
MIL_ID HookId,
void *UserDataPtr)
{
HookDataStruct* UserStruct = (HookDataStruct*)UserDataPtr;
MIL_ID ModifiedBufferId;
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &ModifiedBufferId);
MdispSelect(UserStruct->Display, ModifiedBufferId);
MregCalculate(UserStruct->RegContext, &ModifiedBufferId, UserStruct->RegResult, 1, M_ACCUMULATE);
return 0;
}
void OnlineExtendedDepthOfField(MIL_ID MilSystem,
MIL_ID MilDisplay,
MIL_INT NbImages,
MIL_INT SizeBand,
MIL_INT SizeX,
MIL_INT SizeY,
MIL_INT Type,
MIL_INT64 Attribute,
MIL_CONST_TEXT_PTR ImageDirectory)
{
MIL_ID FusionImage,
RegContext,
RegResult,
DigId;
MIL_ID* ImagesArray;
HookDataStruct UserHookData;
RegContext = MregAlloc (MilSystem, M_EXTENDED_DEPTH_OF_FIELD , M_DEFAULT, M_NULL);
RegResult = MregAllocResult(MilSystem, M_EXTENDED_DEPTH_OF_FIELD_RESULT, M_NULL);
DigId = MdigAlloc(MilSystem, M_DEFAULT, ImageDirectory, M_EMULATED, M_NULL);
MregControl(RegContext, M_DEFAULT, M_TRANSLATION_TOLERANCE, TRANSLATION_TOTELANCE);
MregControl(RegContext, M_DEFAULT, M_CIRCLE_OF_CONFUSION_RADIUS_MAX, MAXIMUM_CIRCLE_OF_CONFUSION_RADIUS);
MregControl(RegContext, M_DEFAULT, M_MODE, MODE);
const MIL_INT ImageCount = 2;
ImagesArray = new MIL_ID[ImageCount];
for (MIL_INT NumImg = 0; NumImg < ImageCount; NumImg++)
{
MbufAllocColor(MilSystem, SizeBand, SizeX, SizeY, Type, Attribute, &ImagesArray[NumImg]);
}
MbufAllocColor(MilSystem, SizeBand, SizeX, SizeY, Type, Attribute, &FusionImage);
UserHookData.RegContext = RegContext;
UserHookData.RegResult = RegResult;
UserHookData.Display = MilDisplay;
MdigProcess(DigId, ImagesArray, ImageCount, M_SEQUENCE + M_COUNT(NbImages), M_DEFAULT, ProcessingFunction, &UserHookData);
MosPrintf(MIL_TEXT("\nPress <Enter> to generate the extended depth of field image.\n\n"));
MosGetch();
MregCalculate(RegContext, M_NULL, RegResult, 0, M_ACCUMULATE_AND_FUSION);
MregDraw(M_DEFAULT, RegResult, FusionImage, M_DRAW_EDOF_IMAGE, M_DEFAULT, M_DEFAULT);
MdispSelect(MilDisplay, FusionImage);
MosPrintf(MIL_TEXT("Image fusion result."));
MosPrintf(MIL_TEXT("\nPress <Enter> to end.\n\n"));
MosGetch();
MbufFree(FusionImage);
for (MIL_INT NumImg = 0; NumImg < ImageCount; NumImg++)
{
MbufFree(ImagesArray[NumImg]);
}
delete[] ImagesArray;
MdigFree(DigId);
MregFree(RegResult);
MregFree(RegContext);
}