示例#1
0
        static void ColorSegmentationExample(MIL_ID MilSystem, MIL_ID MilDisplay)
        {
            MIL_ID SourceChild  = MIL.M_NULL;           // Source image buffer identifier.
            MIL_ID DestChild    = MIL.M_NULL;           // Dest image buffer identifier.
            MIL_ID MatchContext = MIL.M_NULL;           // Color matching context identifier.
            MIL_ID MatchResult  = MIL.M_NULL;           // Color matching result identifier.
            MIL_ID DisplayImage = MIL.M_NULL;           // Display image buffer identifier.

            MIL_INT SourceSizeX = 0;
            MIL_INT SourceSizeY = 0;                    // Source image sizes
            MIL_INT SampleIndex = 0;
            MIL_INT SpacesIndex = 0;                    // Indices

            double MatchScore = 0.0;                    // Color matching score.

            // Blank spaces to align the samples names evenly.
            string[] Spaces = { "", " ", "  ", "   " };

            // Color samples names.
            string[] SampleNames = { "Green", "Red", "Yellow", "Purple", "Blue", "Pink" };

            // Color samples position: {OffsetX, OffsetY}
            double[,] SamplesROI = new double[, ] {
                { 58, 143 }, { 136, 148 }, { 217, 144 }, { 295, 142 }, { 367, 143 }, { 442, 147 }
            };

            // Color samples size.
            const double SampleSizeX = 36, SampleSizeY = 32;

            // Array for match sample colors.
            MIL_INT[,] SampleMatchColor = new MIL_INT[NUM_SAMPLES, 3];

            Console.Write("\nCOLOR SEGMENTATION:\n");
            Console.Write("-------------------\n");

            // Allocate the parent display image.
            MIL.MbufDiskInquire(CANDY_SAMPLE_IMAGE_FILE, MIL.M_SIZE_X, ref SourceSizeX);
            MIL.MbufDiskInquire(CANDY_SAMPLE_IMAGE_FILE, MIL.M_SIZE_Y, ref SourceSizeY);
            MIL.MbufAllocColor(MilSystem, 3, 2 * SourceSizeX + DISPLAY_CENTER_MARGIN_X, SourceSizeY, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_DISP + MIL.M_PROC, ref DisplayImage);
            MIL.MbufClear(DisplayImage, MIL.M_COLOR_BLACK);

            // Create a source and dest child in the display image.
            MIL.MbufChild2d(DisplayImage, 0, 0, SourceSizeX, SourceSizeY, ref SourceChild);
            MIL.MbufChild2d(DisplayImage, SourceSizeX + DISPLAY_CENTER_MARGIN_X, 0, SourceSizeX, SourceSizeY, ref DestChild);

            // Load the source image into the source child.
            MIL.MbufLoad(CANDY_SAMPLE_IMAGE_FILE, SourceChild);

            // Allocate a color matching context.
            MIL.McolAlloc(MilSystem, MIL.M_COLOR_MATCHING, MIL.M_RGB, MIL.M_DEFAULT, MIL.M_DEFAULT, ref MatchContext);

            // Define each color sample in the context.
            for (int i = 0; i < NUM_SAMPLES; i++)
            {
                MIL.McolDefine(MatchContext, SourceChild, MIL.M_SAMPLE_LABEL(i + 1), MIL.M_IMAGE, SamplesROI[i, 0], SamplesROI[i, 1], SampleSizeX, SampleSizeY);
            }

            // Set the color matching parameters.
            MIL.McolSetMethod(MatchContext, MATCH_MODE, DISTANCE_TYPE, MIL.M_DEFAULT, MIL.M_DEFAULT);
            MIL.McolControl(MatchContext, MIL.M_CONTEXT, MIL.M_DISTANCE_TOLERANCE_MODE, TOLERANCE_MODE);
            MIL.McolControl(MatchContext, MIL.M_ALL, MIL.M_DISTANCE_TOLERANCE, TOLERANCE_VALUE);

            // Adjust tolerances for the red, yellow and pink samples.
            MIL.McolControl(MatchContext, MIL.M_SAMPLE_INDEX(1), MIL.M_DISTANCE_TOLERANCE, RED_TOLERANCE_VALUE);
            MIL.McolControl(MatchContext, MIL.M_SAMPLE_INDEX(2), MIL.M_DISTANCE_TOLERANCE, YELLOW_TOLERANCE_VALUE);
            MIL.McolControl(MatchContext, MIL.M_SAMPLE_INDEX(5), MIL.M_DISTANCE_TOLERANCE, PINK_TOLERANCE_VALUE);

            // Preprocess the context.
            MIL.McolPreprocess(MatchContext, MIL.M_DEFAULT);

            // Fill the samples colors array.
            for (int i = 0; i < NUM_SAMPLES; i++)
            {
                MIL.McolInquire(MatchContext, MIL.M_SAMPLE_LABEL(i + 1), MIL.M_MATCH_SAMPLE_COLOR_BAND_0 + MIL.M_TYPE_MIL_INT, ref SampleMatchColor[i, 0]);
                MIL.McolInquire(MatchContext, MIL.M_SAMPLE_LABEL(i + 1), MIL.M_MATCH_SAMPLE_COLOR_BAND_1 + MIL.M_TYPE_MIL_INT, ref SampleMatchColor[i, 1]);
                MIL.McolInquire(MatchContext, MIL.M_SAMPLE_LABEL(i + 1), MIL.M_MATCH_SAMPLE_COLOR_BAND_2 + MIL.M_TYPE_MIL_INT, ref SampleMatchColor[i, 2]);
            }

            // Draw the samples.
            DrawSampleColors(DestChild, SampleMatchColor, SampleNames, NUM_SAMPLES, CANDY_SAMPLES_XSPACING, CANDY_SAMPLES_YOFFSET);

            // Select the image buffer for display.
            MIL.MdispSelect(MilDisplay, DisplayImage);

            // Pause to show the original image.
            Console.Write("Color samples are defined for each possible candy color.\n");
            Console.Write("Press <Enter> to do color matching.\n\n");
            Console.ReadKey();

            // Load the target image.*/
            MIL.MbufClear(DisplayImage, MIL.M_COLOR_BLACK);
            MIL.MbufLoad(CANDY_TARGET_IMAGE_FILE, SourceChild);

            // Allocate a color matching result buffer.
            MIL.McolAllocResult(MilSystem, MIL.M_COLOR_MATCHING_RESULT, MIL.M_DEFAULT, ref MatchResult);

            // Enable controls to draw the labeled color image.
            MIL.McolControl(MatchContext, MIL.M_CONTEXT, MIL.M_GENERATE_PIXEL_MATCH, MIL.M_ENABLE);
            MIL.McolControl(MatchContext, MIL.M_CONTEXT, MIL.M_GENERATE_SAMPLE_COLOR_LUT, MIL.M_ENABLE);

            // Match with target image.
            MIL.McolMatch(MatchContext, SourceChild, MIL.M_DEFAULT, MIL.M_NULL, MatchResult, MIL.M_DEFAULT);

            // Retrieve and display results.
            Console.Write("Each pixel of the mixture is matched " +
                          "with one of the color samples.\n");
            Console.Write("\nColor segmentation results:\n");
            Console.Write("---------------------------\n");

            for (SampleIndex = 0; SampleIndex < NUM_SAMPLES; SampleIndex++)
            {
                MIL.McolGetResult(MatchResult, MIL.M_DEFAULT, MIL.M_SAMPLE_INDEX((int)SampleIndex), MIL.M_SCORE, ref MatchScore);
                SpacesIndex = 6 - SampleNames[SampleIndex].Length;
                Console.Write("Ratio of {0}{1} sample = {2,5:0.00}%\n", SampleNames[SampleIndex], Spaces[SpacesIndex], MatchScore);
            }
            Console.Write("\nResults reveal the low proportion of Blue candy.\n");

            // Draw the colored label image in the destination child.
            MIL.McolDraw(MIL.M_DEFAULT, MatchResult, DestChild, MIL.M_DRAW_PIXEL_MATCH_USING_COLOR, MIL.M_ALL, MIL.M_ALL, MIL.M_DEFAULT);

            // Pause to show the result image.
            Console.Write("\nPress <Enter> to end.\n\n");
            Console.ReadKey();

            // Free all allocations.
            MIL.MbufFree(DestChild);
            MIL.MbufFree(SourceChild);
            MIL.MbufFree(DisplayImage);
            MIL.McolFree(MatchResult);
            MIL.McolFree(MatchContext);
        }