Пример #1
0
        private static void CountObjects(MIL_ID MilDisplay, MIL_ID MilGraphicsList, MIL_ID MilGraphicsContext, MIL_ID MilBinImage, MIL_ID MilBlobFeatureList, MIL_ID MilBlobResult)
        {
            MIL_INT NumberOfBlobs      = 0;
            MIL_INT NumberOfPrimitives = 0;
            MIL_INT Index;

            string TextLabel;

            // Disable the display update for better performance.
            MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_DISABLE);

            // Remove all elements from the graphics list, except the rectangle
            // region primitive at index 0.
            MIL.MgraInquireList(MilGraphicsList, MIL.M_LIST, MIL.M_DEFAULT, MIL.M_NUMBER_OF_GRAPHICS, ref NumberOfPrimitives);
            for (Index = NumberOfPrimitives - 1; Index > 0; Index--)
            {
                MIL.MgraControlList(MilGraphicsList, MIL.M_GRAPHIC_INDEX(Index), MIL.M_DEFAULT, MIL.M_DELETE, MIL.M_DEFAULT);
            }

            // Set the input region. The blob analysis will be done
            // from the (filled) interactive rectangle.
            MIL.MbufSetRegion(MilBinImage, MilGraphicsList, MIL.M_DEFAULT, MIL.M_RASTERIZE + MIL.M_FILL_REGION, MIL.M_DEFAULT);

            // Calculate the blobs and their features.
            MIL.MblobCalculate(MilBinImage, MIL.M_NULL, MilBlobFeatureList, MilBlobResult);

            // Get the total number of blobs.
            MIL.MblobGetNumber(MilBlobResult, ref NumberOfBlobs);

            // Set the input units to display unit for the count annotations.
            MIL.MgraControl(MilGraphicsContext, MIL.M_INPUT_UNITS, MIL.M_DISPLAY);
            TextLabel = string.Format(" Number of blobs found: {0:00} ", NumberOfBlobs);

            MIL.MgraColor(MilGraphicsContext, MIL.M_COLOR_WHITE);
            MIL.MgraText(MilGraphicsContext, MilGraphicsList, 10, 10, TextLabel);

            // Restore the input units to pixel units for result annotations.
            MIL.MgraControl(MilGraphicsContext, MIL.M_INPUT_UNITS, MIL.M_PIXEL);

            // Draw blob center of gravity annotation.
            MIL.MgraColor(MilGraphicsContext, MIL.M_COLOR_RED);
            MIL.MblobDraw(MilGraphicsContext, MilBlobResult, MilGraphicsList, MIL.M_DRAW_CENTER_OF_GRAVITY, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT);

            // Draw blob bounding box annotations.
            MIL.MgraColor(MilGraphicsContext, MIL.M_COLOR_GREEN);
            MIL.MblobDraw(MilGraphicsContext, MilBlobResult, MilGraphicsList, MIL.M_DRAW_BOX, MIL.M_INCLUDED_BLOBS, MIL.M_DEFAULT);

            // Enable the display to update the drawings.
            MIL.MdispControl(MilDisplay, MIL.M_UPDATE, MIL.M_ENABLE);
        }