예제 #1
0
        /// <summary>
        /// Creates a new image that will be used as a mask for the histogram calculation.
        /// All forground points will be colored blue and all background points will be colored red.
        /// </summary>
        public static CVImage PrepareMask(CVImage image, Point[] forgroundPoints, Point[] backgroundPoints, bool includeNeautral, int floodFillThreshold)
        {
            CVImage outputImage = image.Clone();

            outputImage.Zero();

            FloodFillParams ffp = new FloodFillParams();

            ffp.Frame     = image;
            ffp.Threshold = floodFillThreshold;

            // fill all forground points in FG_COLOR
            foreach (Point pt in forgroundPoints)
            {
                RegionGrowing.Fill(pt, outputImage, new RegionGrowingCriteria(FloodFill), ffp, new LabelingFunc(RegionGrowing.DefaultLabeling), FG_COLOR);
            }

            // fill all background points in BG_COLOR
            foreach (Point pt in backgroundPoints)
            {
                RegionGrowing.Fill(pt, outputImage, new RegionGrowingCriteria(FloodFill), ffp, new LabelingFunc(RegionGrowing.DefaultLabeling), TEMP_BG_COLOR);
            }

            // now colorize to actual colors.
            for (int row = 0; row < outputImage.Height; ++row)
            {
                for (int col = 0; col < outputImage.Width; ++col)
                {
                    int argb = outputImage[row, col].ToColor().ToArgb();

                    if (includeNeautral)
                    {
                        if (argb == Color.Black.ToArgb())
                        {
                            outputImage[row, col] = new CVRgbPixel(NEAUTRAL_COLOR);
                        }
                    }

                    if (argb == TEMP_BG_COLOR.ToArgb())
                    {
                        outputImage[row, col] = new CVRgbPixel(BG_COLOR);
                    }
                }
            }

            // return as a grayscale image.
            CVImage grayscale = outputImage.ToGrayscale();

            outputImage.Release();
            return(grayscale);
        }
예제 #2
0
        /// <summary>
        /// Handles the next video frame.
        /// </summary>
        private void HandleNextFrame()
        {
            if (capture == null)
            {
                return;
            }

            // release last frame.
            if (lastFrame != null)
            {
                lastFrame.Release();
            }

            // query next frame.
            lastFrame = capture.QueryFrame();

            if (lastFrame == null)
            {
                if (loop)
                {
                    capture.Restart();
                    lastFrame = capture.QueryFrame();
                }
                else
                {
                    Pause();
                    return;
                }
            }

            // put next frame to picture box, if defined.
            if (pictureBox != null)
            {
                pictureBox.Image = lastFrame.ToBitmap();
            }

            // fire event.
            if (NextFrame != null)
            {
                NextFrameEventArgs ea = new NextFrameEventArgs();
                ea.Frame   = lastFrame;
                ea.Capture = capture;
                NextFrame(this, ea);
            }
        }
예제 #3
0
        void DrawHistogram(PictureBox window, CVHistogram histo, int channelIdx)
        {
            int imageWidth  = window.Width;
            int imageHeight = window.Height;

            int bins     = histo.BinSizes[0];
            int binWidth = imageWidth / bins;

            if (binWidth <= 0)
            {
                binWidth = 1;
            }

            CVPair  minMax      = histo.MinMaxValue;
            CVImage outputImage = new CVImage(imageWidth, imageHeight, CVDepth.Depth8U, 3);

            outputImage.Zero();

            for (int bin = 0; bin < bins; bin++)
            {
                double binValue  = histo[bin];
                byte   level     = (byte)CVUtils.Round(binValue * 255 / minMax.Second);
                byte   binHeight = (byte)CVUtils.Round(binValue * imageHeight / minMax.Second);

                byte[] color = new byte[3];
                color[channelIdx] = (byte)(((double)bin / (double)bins) * 255);

                byte[] markerColor = new byte[3];
                markerColor[channelIdx] = level;

                Color colColor  = Color.FromArgb(color[2], color[1], color[0]);
                Color colMarker = Color.FromArgb(markerColor[2], markerColor[1], markerColor[0]);


                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, binHeight), colColor);
                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, binHeight), colMarker);
                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, 1, binHeight), colMarker);
            }

            window.Image = outputImage.ToBitmap();
            outputImage.Release();
        }
예제 #4
0
        void DrawHistogram(PictureBox window, CVHistogram histo, int channelIdx)
        {
            int imageWidth = window.Width;
            int imageHeight = window.Height;

            int bins = histo.BinSizes[0];
            int binWidth = imageWidth / bins;
            if (binWidth <= 0) binWidth = 1;

            CVPair minMax = histo.MinMaxValue;
            CVImage outputImage = new CVImage(imageWidth, imageHeight, CVDepth.Depth8U, 3);
            outputImage.Zero();

            for (int bin = 0; bin < bins; bin++)
            {
                double binValue = histo[bin];
                byte level = (byte)CVUtils.Round(binValue * 255 / minMax.Second);
                byte binHeight = (byte)CVUtils.Round(binValue * imageHeight / minMax.Second);

                byte[] color = new byte[3];
                color[channelIdx] = (byte)(((double)bin / (double)bins) * 255);

                byte[] markerColor = new byte[3];
                markerColor[channelIdx] = level;

                Color colColor = Color.FromArgb(color[2], color[1], color[0]);
                Color colMarker = Color.FromArgb(markerColor[2], markerColor[1], markerColor[0]);

                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, binHeight), Color.White /* colColor */);
                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, binWidth - 1, 1), colMarker);
                outputImage.DrawRectangle(new Rectangle(bin * binWidth, imageHeight - binHeight, 1, binHeight), colMarker);
            }

            window.Image = outputImage.ToBitmap();
            outputImage.Release();
        }