using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MImSegment
{
class Program
{
private const string IMAGE_FILE = MIL.M_IMAGE_PATH + "pills.mim";
private const int WATERSHED_MINIMAL_GRADIENT_VARIATION = 45;
private const int WATERSHED_MINIMAL_DISTANCE_VARIATION = 2;
private const int PIXEL_FETCH_POSITION_X = 2;
private const int PIXEL_FETCH_POSITION_Y = 2;
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 MilImage = MIL.M_NULL;
MIL_ID MilImageWatershed = MIL.M_NULL;
int[] lFetchedValue = new int[1] { 0 };
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilImageWatershed);
MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilImage);
MIL.MdispSelect(MilDisplay, MilImage);
Console.Write("\nSEGMENTATION:\n");
Console.Write("-------------\n\n");
Console.Write("An edge detection followed by a watershed will be used to remove\n");
Console.Write("the background.\nPress <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MimEdgeDetect(MilImageWatershed, MilImageWatershed, MIL.M_NULL, MIL.M_SOBEL, MIL.M_REGULAR_EDGE_DETECT, MIL.M_NULL);
MIL.MimWatershed(MilImageWatershed, MIL.M_NULL, MilImageWatershed, WATERSHED_MINIMAL_GRADIENT_VARIATION, MIL.M_MINIMA_FILL + MIL.M_BASIN);
MIL.MbufGet2d(MilImageWatershed, PIXEL_FETCH_POSITION_X, PIXEL_FETCH_POSITION_Y, 1, 1, lFetchedValue);
MIL.MimClip(MilImageWatershed, MilImageWatershed, MIL.M_EQUAL, lFetchedValue[0], 0, 0, 0);
MIL.MimClip(MilImageWatershed, MilImage, MIL.M_NOT_EQUAL, 0, 0, 0xFF, 0);
Console.Write("A distance transformation followed by a watershed will be used \n");
Console.Write("to separate the touching pills.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MimDistance(MilImage, MilImageWatershed, MIL.M_CHAMFER_3_4);
MIL.MimWatershed(MilImageWatershed, MIL.M_NULL, MilImageWatershed, WATERSHED_MINIMAL_DISTANCE_VARIATION, MIL.M_STRAIGHT_WATERSHED + MIL.M_MAXIMA_FILL + MIL.M_SKIP_LAST_LEVEL + MIL.M_WATERSHED);
MIL.MimArith(MilImageWatershed, MilImage, MilImage, MIL.M_AND);
Console.Write("Here are the segmented pills.\n");
Console.Write("Press <Enter> to end.\n\n");
Console.ReadKey();
MIL.MbufFree(MilImageWatershed);
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}