using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MReg
{
class Program
{
private const int NUM_IMAGES_TO_REGISTER = 3;
private const string IMAGE_FILES_SOURCE = MIL.M_IMAGE_PATH + "CircuitBoardPart{0}.mim";
static void Main(string[] args)
{
MIL_ID MilApplication = MIL.M_NULL;
MIL_ID MilSystem = MIL.M_NULL;
MIL_ID MilDisplay = MIL.M_NULL;
MIL_ID MilGraphicList = MIL.M_NULL;
MIL_ID[] MilSourceImages = new MIL_ID[NUM_IMAGES_TO_REGISTER];
MIL_ID MilMosaicImage = MIL.M_NULL;
MIL_ID MilRegContext = MIL.M_NULL;
MIL_ID MilRegResult = MIL.M_NULL;
MIL_INT Result = 0;
MIL_INT MosaicSizeX = 0;
MIL_INT MosaicSizeY = 0;
MIL_INT MosaicSizeBand = 0;
MIL_INT MosaicType = 0;
string[] ImageFilesSource = new string[NUM_IMAGES_TO_REGISTER];
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
for (int i = 0; i < NUM_IMAGES_TO_REGISTER; i++)
{
ImageFilesSource[i] = String.Format(IMAGE_FILES_SOURCE, i);
}
Console.Write("\nREGISTRATION MODULE:\n");
Console.Write("---------------------\n\n");
Console.Write("This program will make a mosaic from many source images.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
for (int i = 0; i < NUM_IMAGES_TO_REGISTER; i++)
{
MIL.MbufRestore(ImageFilesSource[i], MilSystem, ref MilSourceImages[i]);
}
for (int i = 0; i < NUM_IMAGES_TO_REGISTER; i++)
{
MIL.MdispSelect(MilDisplay, MilSourceImages[i]);
Console.Write("image {0}.\n", i);
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
}
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, ref MilGraphicList);
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList);
MIL.MregAlloc(MilSystem, MIL.M_STITCHING, MIL.M_DEFAULT, ref MilRegContext);
MIL.MregAllocResult(MilSystem, MIL.M_DEFAULT, ref MilRegResult);
MIL.MregControl(MilRegContext, MIL.M_CONTEXT, MIL.M_TRANSFORMATION_TYPE, MIL.M_TRANSLATION);
MIL.MregControl(MilRegContext, MIL.M_CONTEXT, MIL.M_LOCATION_DELTA, 100);
MIL.MregCalculate(MilRegContext, MilSourceImages, MilRegResult, NUM_IMAGES_TO_REGISTER, MIL.M_DEFAULT);
MIL.MregGetResult(MilRegResult, MIL.M_GENERAL, MIL.M_RESULT + MIL.M_TYPE_MIL_INT, ref Result);
if (Result == MIL.M_SUCCESS)
{
MIL.MregGetResult(MilRegResult, MIL.M_GENERAL, MIL.M_MOSAIC_SIZE_X + MIL.M_TYPE_MIL_INT, ref MosaicSizeX);
MIL.MregGetResult(MilRegResult, MIL.M_GENERAL, MIL.M_MOSAIC_SIZE_Y + MIL.M_TYPE_MIL_INT, ref MosaicSizeY);
MIL.MbufInquire(MilSourceImages[0], MIL.M_SIZE_BAND, ref MosaicSizeBand);
MIL.MbufInquire(MilSourceImages[0], MIL.M_TYPE, ref MosaicType);
MIL.MbufAllocColor(MilSystem, MosaicSizeBand, MosaicSizeX, MosaicSizeY, MosaicType, MIL.M_IMAGE + MIL.M_PROC + MIL.M_DISP, ref MilMosaicImage);
MIL.MregTransformImage(MilRegResult, MilSourceImages, MilMosaicImage, NUM_IMAGES_TO_REGISTER, MIL.M_BILINEAR + MIL.M_OVERSCAN_CLEAR, MIL.M_DEFAULT);
MIL.MdispSelect(MilDisplay, MilMosaicImage);
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_RGB888(0, 0xFF, 0));
Console.Write("mosaic image.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MregDraw(MIL.M_DEFAULT, MilRegResult, MilGraphicList, MIL.M_DRAW_BOX, MIL.M_ALL, MIL.M_DEFAULT);
for (int i = 0; i < NUM_IMAGES_TO_REGISTER; i++)
{
double SourcePosX = 0.5 * (double)MIL.MbufInquire(MilSourceImages[i], MIL.M_SIZE_X, MIL.M_NULL);
double SourcePosY = 0.5 * (double)MIL.MbufInquire(MilSourceImages[i], MIL.M_SIZE_Y, MIL.M_NULL);
double MosaicPosX = 0;
double MosaicPosY = 0;
MIL.MregTransformCoordinate(MilRegResult, i, MIL.M_MOSAIC, SourcePosX, SourcePosY, ref MosaicPosX, ref MosaicPosY, MIL.M_DEFAULT);
int MosaicPosXMilInt = (int)(MosaicPosX + 0.5);
int MosaicPosYMilInt = (int)(MosaicPosY + 0.5);
MIL.MgraLine(MIL.M_DEFAULT, MilGraphicList, MosaicPosXMilInt - 4, MosaicPosYMilInt, MosaicPosXMilInt + 4, MosaicPosYMilInt);
MIL.MgraLine(MIL.M_DEFAULT, MilGraphicList, MosaicPosXMilInt, MosaicPosYMilInt - 4, MosaicPosXMilInt, MosaicPosYMilInt + 4);
}
Console.Write("The bounding boxes and the center of all source images\n");
Console.Write("have been drawn in the mosaic.\n");
}
else
{
Console.Write("Error: Registration was not successful.\n");
}
Console.Write("\nPress <Enter> to end.\n\n");
Console.ReadKey();
MIL.MgraFree(MilGraphicList);
if (MilMosaicImage != MIL.M_NULL)
{
MIL.MbufFree(MilMosaicImage);
}
MIL.MregFree(MilRegContext);
MIL.MregFree(MilRegResult);
for (int i = 0; i < NUM_IMAGES_TO_REGISTER; i++)
{
MIL.MbufFree(MilSourceImages[i]);
}
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}