using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MEdge
{
class Program
{
private const string CONTOUR_IMAGE = MIL.M_IMAGE_PATH + "Seals.mim";
private const int CONTOUR_MAX_RESULTS = 100;
private const double CONTOUR_MAXIMUM_ELONGATION = 0.8;
private static readonly int CONTOUR_DRAW_COLOR = MIL.M_COLOR_GREEN;
private static readonly int CONTOUR_LABEL_COLOR = MIL.M_COLOR_RED;
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 GraphicList = MIL.M_NULL;
MIL_ID MilEdgeContext = MIL.M_NULL;
MIL_ID MilEdgeResult = MIL.M_NULL;
double EdgeDrawColor = CONTOUR_DRAW_COLOR;
double LabelDrawColor = CONTOUR_LABEL_COLOR;
MIL_INT NumEdgeFound = 0;
MIL_INT NumResults = 0;
int i = 0;
double[] MeanFeretDiameter = new double[CONTOUR_MAX_RESULTS];
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufRestore(CONTOUR_IMAGE, MilSystem, ref MilImage);
MIL.MdispSelect(MilDisplay, MilImage);
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, ref GraphicList);
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
Console.Write("\nEDGE MODULE:\n");
Console.Write("------------\n\n");
Console.Write("This program determines the outer seal diameters in the displayed image \n");
Console.Write("by detecting and analyzing contours with the Edge Finder module.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MedgeAlloc(MilSystem, MIL.M_CONTOUR, MIL.M_DEFAULT, ref MilEdgeContext);
MIL.MedgeAllocResult(MilSystem, MIL.M_DEFAULT, ref MilEdgeResult);
MIL.MedgeControl(MilEdgeContext, MIL.M_MOMENT_ELONGATION, MIL.M_ENABLE);
MIL.MedgeControl(MilEdgeContext, MIL.M_FERET_MEAN_DIAMETER + MIL.M_SORT1_DOWN, MIL.M_ENABLE);
MIL.MedgeCalculate(MilEdgeContext, MilImage, MIL.M_NULL, MIL.M_NULL, MIL.M_NULL, MilEdgeResult, MIL.M_DEFAULT);
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_NUMBER_OF_CHAINS + MIL.M_TYPE_MIL_INT, ref NumEdgeFound);
MIL.MgraColor(MIL.M_DEFAULT, EdgeDrawColor);
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT);
Console.Write("{0} edges were found in the image.\n", NumEdgeFound);
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MedgeSelect(MilEdgeResult, MIL.M_EXCLUDE, MIL.M_MOMENT_ELONGATION, MIL.M_LESS, CONTOUR_MAXIMUM_ELONGATION, MIL.M_NULL);
MIL.MedgeSelect(MilEdgeResult, MIL.M_EXCLUDE, MIL.M_INCLUDED_EDGES, MIL.M_INSIDE_BOX, MIL.M_NULL, MIL.M_NULL);
MIL.MgraClear(MIL.M_DEFAULT, GraphicList);
MIL.MgraColor(MIL.M_DEFAULT, EdgeDrawColor);
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_EDGES, MIL.M_DEFAULT, MIL.M_DEFAULT);
Console.Write("Elongated edges and inner edges of each seal were removed.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_NUMBER_OF_CHAINS + MIL.M_TYPE_MIL_INT, ref NumResults);
if ((NumResults >= 1) && (NumResults <= CONTOUR_MAX_RESULTS))
{
MIL.MgraColor(MIL.M_DEFAULT, LabelDrawColor);
MIL.MedgeDraw(MIL.M_DEFAULT, MilEdgeResult, GraphicList, MIL.M_DRAW_INDEX, MIL.M_DEFAULT, MIL.M_DEFAULT);
MIL.MedgeGetResult(MilEdgeResult, MIL.M_DEFAULT, MIL.M_FERET_MEAN_DIAMETER, MeanFeretDiameter);
Console.Write("Mean diameter of the {0} outer edges are:\n\n", NumResults);
Console.Write("Index Mean diameter \n");
for (i = 0; i < NumResults; i++)
{
Console.Write("{0,-11}{1,-13:0.00}\n", i, MeanFeretDiameter[i]);
}
}
else
{
Console.Write("Edges have not been found or the number of found edges is greater than\n");
Console.Write("the specified maximum number of edges !\n\n");
}
Console.Write("\nPress <Enter> to end.\n");
Console.ReadKey();
MIL.MgraFree(GraphicList);
MIL.MbufFree(MilImage);
MIL.MedgeFree(MilEdgeContext);
MIL.MedgeFree(MilEdgeResult);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}