#include <mil.h>
#define EXAMPLE_SCRIPT_PATH M_IMAGE_PATH MIL_TEXT("ScriptPreprocessing/")
static const MIL_INT NB_SCRIPT_LANGUAGE = 4;
static MIL_CONST_TEXT_PTR SCRIPT_INTERPRETERS[NB_SCRIPT_LANGUAGE] =
{
M_INTERPRETER_C_PYTHON34,
M_INTERPRETER_C_PYTHON27,
M_INTERPRETER_CSHARP,
M_INTERPRETER_VB_DOT_NET
};
static MIL_CONST_TEXT_PTR SCRIPT_INTERPRETER_NAMES[NB_SCRIPT_LANGUAGE] =
{
MIL_TEXT("Python 3.4"),
MIL_TEXT("Python 2.7"),
MIL_TEXT("C#"),
MIL_TEXT("VB.NET"),
};
static MIL_CONST_TEXT_PTR SCRIPT_PATHS[NB_SCRIPT_LANGUAGE] =
{
EXAMPLE_SCRIPT_PATH MIL_TEXT("ScriptPreprocessing.py"),
EXAMPLE_SCRIPT_PATH MIL_TEXT("ScriptPreprocessing.py"),
EXAMPLE_SCRIPT_PATH MIL_TEXT("ScriptPreprocessing.cs"),
EXAMPLE_SCRIPT_PATH MIL_TEXT("ScriptPreprocessing.vb")
};
static MIL_INT SCRIPT_OPCODES[NB_SCRIPT_LANGUAGE] =
{
M_SCRIPT_MODULE_1 + 1L,
M_SCRIPT_MODULE_1 + 2L,
M_SCRIPT_MODULE_1 + 3L,
M_SCRIPT_MODULE_1 + 4L
};
#define PREPROCESSING_FUNCTION_NAME MIL_TEXT("Preprocessing")
#define SCRIPT_FUNCTION_NAME MIL_TEXT("PreprocessingFunction")
#define BUFFERING_SIZE_MAX 22
MIL_INT MFTYPE HookFunction(MIL_INT HookType, MIL_ID HookId, void* HookDataPtr);
typedef struct
{
MIL_ID MilDisplay;
MIL_ID MilImageDisp;
MIL_INT ProcessedImageCount;
MIL_ID PreprocessedImage;
MIL_ID GraphicList;
MIL_ID MilBlobFeatureList;
MIL_ID MilBlobResult;
MIL_CONST_TEXT_PTR InterpreterPath;
MIL_CONST_TEXT_PTR ScriptPath;
MIL_INT ScriptOpcode;
} HookDataStruct;
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n")
MIL_TEXT("ScriptPreprocessing\n\n")
MIL_TEXT("[SYNOPSIS]\n")
MIL_TEXT("This example shows how to execute MIL code within a script language.\n")
MIL_TEXT("A sequence of preprocessing operations followed by a blob analysis\n")
MIL_TEXT("operation is applied to the grabbed image. The preprocessing step\n")
MIL_TEXT("is executed within a script (C#, VB, and Python based on availability).\n\n")
MIL_TEXT("[MODULES USED]\n")
MIL_TEXT("Modules used: application, system, display, buffer,\n")
MIL_TEXT("digitizer, image processing, blob analysis\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n"));
MosGetch();
}
MIL_INT64 Preprocessing(MIL_ID Source, MIL_ID Destination, MIL_CONST_TEXT_PTR InterpreterPath, MIL_CONST_TEXT_PTR ScriptPath, MIL_INT Opcode)
{
MIL_ID FuncId;
MIL_INT64 ReturnValue = -1;
MfuncAllocScript(MIL_TEXT("Preprocessing"),
3,
InterpreterPath,
ScriptPath,
SCRIPT_FUNCTION_NAME,
Opcode,
M_LOCAL+M_SYNCHRONOUS_FUNCTION,
&FuncId
);
MfuncParamMilId(FuncId, 1, Source, M_IMAGE, M_IN);
MfuncParamMilId(FuncId, 2, Destination, M_IMAGE, M_OUT);
MfuncParamArrayMilInt64(FuncId, 3, &ReturnValue, 1, M_OUT);
MfuncCall(FuncId);
MfuncFree(FuncId);
return ReturnValue;
}
int MosMain()
{
MIL_ID MilApplication;
MIL_ID MilSystem;
MIL_ID MilDisplay;
MIL_ID MilDigitizer;
MIL_ID MilImageDisp;
MIL_ID MilGrabBufferList[BUFFERING_SIZE_MAX] = { 0 };
MIL_INT MilGrabBufferListSize;
MIL_INT SizeX = 0, SizeY = 0, n = 0;
MIL_INT64 Success = -1;
MIL_INT ProcessFrameCount = 0;
MIL_DOUBLE ProcessFrameRate = 0;
HookDataStruct UserHookData;
bool InterpreterStatuses[NB_SCRIPT_LANGUAGE];
MappAlloc(MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilSystem);
MdigAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDigitizer);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT, &MilDisplay);
SizeX = MdigInquire(MilDigitizer, M_SIZE_X, M_NULL);
SizeY = MdigInquire(MilDigitizer, M_SIZE_Y, M_NULL);
MbufAllocColor(MilSystem,
3,
SizeX,
SizeY,
8 + M_UNSIGNED,
M_IMAGE + M_GRAB + M_DISP + M_PROC,
&MilImageDisp);
UserHookData.GraphicList = MgraAllocList(MilSystem, M_DEFAULT, M_NULL);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, UserHookData.GraphicList);
MdispControl(MilDisplay, M_UPDATE, M_DISABLE);
MappControl(M_DEFAULT, M_ERROR, M_PRINT_DISABLE);
for (MilGrabBufferListSize = 0; MilGrabBufferListSize < BUFFERING_SIZE_MAX; MilGrabBufferListSize++)
{
MbufAlloc2d(MilSystem,
SizeX,
SizeY,
8 + M_UNSIGNED,
M_IMAGE + M_GRAB + M_PROC,
&MilGrabBufferList[MilGrabBufferListSize]);
if (MilGrabBufferList[MilGrabBufferListSize])
MbufClear(MilGrabBufferList[MilGrabBufferListSize], 0xFF);
else
break;
}
MappControl(M_DEFAULT, M_ERROR, M_PRINT_ENABLE);
for (n = 0; n < 2 && MilGrabBufferListSize; n++)
{
MilGrabBufferListSize--;
MbufFree(MilGrabBufferList[MilGrabBufferListSize]);
}
UserHookData.MilBlobFeatureList = MblobAllocFeatureList(MilSystem, M_NULL);
UserHookData.MilBlobResult = MblobAllocResult(MilSystem, M_NULL);
MblobSelectFeature(UserHookData.MilBlobFeatureList, M_BOX);
MblobSelectFeature(UserHookData.MilBlobFeatureList, M_CENTER_OF_GRAVITY_X);
MblobSelectFeature(UserHookData.MilBlobFeatureList, M_CENTER_OF_GRAVITY_Y);
UserHookData.MilDisplay = MilDisplay;
UserHookData.ProcessedImageCount = 0;
UserHookData.MilImageDisp = MilImageDisp;
UserHookData.PreprocessedImage = MilGrabBufferList[--MilGrabBufferListSize];
PrintHeader();
MosPrintf(MIL_TEXT("Loading resources...\n\n"));
MdigGrab(MilDigitizer, MilGrabBufferList[0]);
for (n = 0; n < NB_SCRIPT_LANGUAGE; n++)
{
UserHookData.InterpreterPath = SCRIPT_INTERPRETERS[n];
UserHookData.ScriptPath = SCRIPT_PATHS[n];
UserHookData.ScriptOpcode = SCRIPT_OPCODES[n];
MappControl(M_DEFAULT, M_ERROR, M_PRINT_DISABLE);
Success = Preprocessing(MilGrabBufferList[0],
UserHookData.PreprocessedImage,
UserHookData.InterpreterPath,
UserHookData.ScriptPath,
UserHookData.ScriptOpcode);
MappControl(M_DEFAULT, M_ERROR, M_PRINT_ENABLE);
InterpreterStatuses[n] = (Success == M_NULL);
}
for (n = 0; n < NB_SCRIPT_LANGUAGE; n++)
{
if (InterpreterStatuses[n])
{
UserHookData.InterpreterPath = SCRIPT_INTERPRETERS[n];
UserHookData.ScriptPath = SCRIPT_PATHS[n];
UserHookData.ScriptOpcode = SCRIPT_OPCODES[n];
MosPrintf(MIL_TEXT("\nExecution using %s:\n"), SCRIPT_INTERPRETER_NAMES[n]);
MosPrintf(MIL_TEXT("---------------------------\n"));
MosPrintf(MIL_TEXT("The sequence of preprocessing operations is interpreted runtime within a \n%s script.\n\n"),
SCRIPT_INTERPRETER_NAMES[n]);
MosPrintf(MIL_TEXT("Press <Enter> to start. \n\n"));
MosGetch();
MdispSelect(MilDisplay, MilImageDisp);
MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, M_START, M_DEFAULT, HookFunction, &UserHookData);
MosPrintf(MIL_TEXT("Press <Enter> to stop. \n\n"));
MosGetch();
MdigProcess(MilDigitizer, MilGrabBufferList, MilGrabBufferListSize, M_STOP, M_DEFAULT, HookFunction, &UserHookData);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_COUNT, &ProcessFrameCount);
MdigInquire(MilDigitizer, M_PROCESS_FRAME_RATE, &ProcessFrameRate);
MosPrintf(MIL_TEXT("%d frames grabbed at %.1f frames/sec (%.1f ms/frame).\n\n"),
(int)ProcessFrameCount, ProcessFrameRate, 1000.0 / ProcessFrameRate);
}
}
if (UserHookData.ProcessedImageCount == 0)
{
MosPrintf(MIL_TEXT("No script interpreter could be initialized. To use the Python interpreter, you\n")
MIL_TEXT("must have a valid Python installation of the same version than the interpreter.\n")
MIL_TEXT("To use the C# and VB interpreter, you must install MIL for .NET.\n")
MIL_TEXT("Please refer to the MIL User Manual and www.python.org for more information.\n\n"));
}
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
while (MilGrabBufferListSize >= 0)
MbufFree(MilGrabBufferList[MilGrabBufferListSize--]);
MblobFree(UserHookData.MilBlobFeatureList);
MblobFree(UserHookData.MilBlobResult);
MgraFree(UserHookData.GraphicList);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, MilDigitizer, MilImageDisp);
return 0;
}
MIL_INT MFTYPE HookFunction(MIL_INT HookType, MIL_ID HookId, void* HookDataPtr)
{
MIL_INT64 PreprocReturnValue = -1;
MIL_ID CurrentImage = 0;
HookDataStruct *UserHookDataPtr = (HookDataStruct *)HookDataPtr;
MdigGetHookInfo(HookId, M_MODIFIED_BUFFER + M_BUFFER_ID, &CurrentImage);
UserHookDataPtr->ProcessedImageCount++;
if (Preprocessing(CurrentImage,
UserHookDataPtr->PreprocessedImage,
UserHookDataPtr->InterpreterPath,
UserHookDataPtr->ScriptPath,
UserHookDataPtr->ScriptOpcode) == M_NULL)
{
MblobCalculate(UserHookDataPtr->PreprocessedImage, M_NULL, UserHookDataPtr->MilBlobFeatureList, UserHookDataPtr->MilBlobResult);
MbufCopy(CurrentImage, UserHookDataPtr->MilImageDisp);
MgraClear(M_DEFAULT, UserHookDataPtr->GraphicList);
MIL_INT NbBlobs = MblobGetNumber(UserHookDataPtr->MilBlobResult, M_NULL);
if (NbBlobs > 0)
{
MgraColor(M_DEFAULT, M_COLOR_GREEN);
MblobDraw(M_DEFAULT, UserHookDataPtr->MilBlobResult, UserHookDataPtr->GraphicList, M_DRAW_BLOBS, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, M_COLOR_BLUE);
MblobDraw(M_DEFAULT, UserHookDataPtr->MilBlobResult, UserHookDataPtr->GraphicList, M_DRAW_CENTER_OF_GRAVITY, M_DEFAULT, M_DEFAULT);
}
MdispControl(UserHookDataPtr->MilDisplay, M_UPDATE, M_NOW);
}
else
{
MosPrintf(MIL_TEXT("An error was returned by the preprocessing script function.\n\n"));
}
return 0;
}