using System;
using System.Collections.Generic;
using System.Text;
using Matrox.MatroxImagingLibrary;
namespace MBlob
{
class Program
{
private const string IMAGE_FILE = MIL.M_IMAGE_PATH + "BoltsNutsWashers.mim";
private const int IMAGE_THRESHOLD_VALUE = 26;
private const int MIN_BLOB_AREA = 50;
private const int MAX_BLOB_AREA = 50000;
private const int MIN_BLOB_RADIUS = 3;
private const double MIN_COMPACTNESS = 1.5;
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 MilGraphicList = MIL.M_NULL;
MIL_ID MilBinImage = MIL.M_NULL;
MIL_ID MilBlobResult = MIL.M_NULL;
MIL_ID MilBlobFeatureList = MIL.M_NULL;
MIL_INT TotalBlobs = 0;
MIL_INT BlobsWithHoles = 0;
MIL_INT BlobsWithRoughHoles = 0;
MIL_INT n = 0;
MIL_INT SizeX = 0;
MIL_INT SizeY = 0;
double[] CogX = null;
double[] CogY = null;
MIL.MappAllocDefault(MIL.M_DEFAULT, ref MilApplication, ref MilSystem, ref MilDisplay, MIL.M_NULL, MIL.M_NULL);
MIL.MbufRestore(IMAGE_FILE, MilSystem, ref MilImage);
MIL.MgraAllocList(MilSystem, MIL.M_DEFAULT, ref MilGraphicList);
MIL.MdispControl(MilDisplay, MIL.M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList);
MIL.MdispSelect(MilDisplay, MilImage);
MIL.MbufInquire(MilImage, MIL.M_SIZE_X, ref SizeX);
MIL.MbufInquire(MilImage, MIL.M_SIZE_Y, ref SizeY);
MIL.MbufAlloc2d(MilSystem, SizeX, SizeY, 1 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_PROC, ref MilBinImage);
Console.Write("\nBLOB ANALYSIS:\n");
Console.Write("--------------\n\n");
Console.Write("This program determines the number of bolts, nuts and washers\n");
Console.Write("in the image and finds their center of gravity.\n");
Console.Write("Press <Enter> to continue.\n\n");
Console.ReadKey();
MIL.MimBinarize(MilImage, MilBinImage, MIL.M_FIXED + MIL.M_GREATER_OR_EQUAL, IMAGE_THRESHOLD_VALUE, MIL.M_NULL);
MIL.MimOpen(MilBinImage, MilBinImage, MIN_BLOB_RADIUS, MIL.M_BINARY);
MIL.MimClose(MilBinImage, MilBinImage, MIN_BLOB_RADIUS, MIL.M_BINARY);
MIL.MblobAllocFeatureList(MilSystem, ref MilBlobFeatureList);
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_AREA);
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_CENTER_OF_GRAVITY);
MIL.MblobAllocResult(MilSystem, ref MilBlobResult);
MIL.MblobCalculate(MilBinImage, MIL.M_NULL, MilBlobFeatureList, MilBlobResult);
MIL.MblobSelect(MilBlobResult, MIL.M_EXCLUDE, MIL.M_AREA, MIL.M_LESS_OR_EQUAL, MIN_BLOB_AREA, MIL.M_NULL);
MIL.MblobGetNumber(MilBlobResult, ref TotalBlobs);
Console.Write("There are {0} objects ", TotalBlobs);
CogX = new double[TotalBlobs];
CogY = new double[TotalBlobs];
if (CogX != null && CogY != null)
{
MIL.MblobGetResult(MilBlobResult, MIL.M_CENTER_OF_GRAVITY_X, CogX);
MIL.MblobGetResult(MilBlobResult, MIL.M_CENTER_OF_GRAVITY_Y, CogY);
Console.Write("and their centers of gravity are:\n");
for (n = 0; n < TotalBlobs; n++)
{
Console.Write("Blob #{0}: X={1,5:0.0}, Y={2,5:0.0}\n", n, CogX[n], CogY[n]);
}
}
else
{
Console.Write("\nError: Not enough memory.\n");
}
MIL.MgraColor(MIL.M_DEFAULT, MIL.M_COLOR_RED);
MIL.MblobDraw(MIL.M_DEFAULT, MilBlobResult, MilGraphicList, MIL.M_DRAW_CENTER_OF_GRAVITY, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT);
MIL.MblobControl(MilBlobResult, MIL.M_FOREGROUND_VALUE, MIL.M_ZERO);
MIL.MblobSelectFeature(MilBlobFeatureList, MIL.M_COMPACTNESS);
MIL.MblobCalculate(MilBinImage, MIL.M_NULL, MilBlobFeatureList, MilBlobResult);
MIL.MblobSelect(MilBlobResult, MIL.M_EXCLUDE, MIL.M_AREA, MIL.M_OUT_RANGE, MIN_BLOB_AREA, MAX_BLOB_AREA);
MIL.MblobGetNumber(MilBlobResult, ref BlobsWithHoles);
MIL.MblobSelect(MilBlobResult, MIL.M_EXCLUDE, MIL.M_COMPACTNESS, MIL.M_LESS_OR_EQUAL, MIN_COMPACTNESS, MIL.M_NULL);
MIL.MblobGetNumber(MilBlobResult, ref BlobsWithRoughHoles);
Console.Write("\nIdentified objects:\n");
Console.Write("{0} bolts\n", TotalBlobs - BlobsWithHoles);
Console.Write("{0} nuts\n", BlobsWithHoles - BlobsWithRoughHoles);
Console.Write("{0} washers\n\n", BlobsWithRoughHoles);
Console.Write("Press <Enter> to end.\n\n");
Console.ReadKey();
MIL.MgraFree(MilGraphicList);
MIL.MblobFree(MilBlobResult);
MIL.MblobFree(MilBlobFeatureList);
MIL.MbufFree(MilBinImage);
MIL.MbufFree(MilImage);
MIL.MappFreeDefault(MilApplication, MilSystem, MilDisplay, MIL.M_NULL, MIL.M_NULL);
}
}
}