コード例 #1
0
        /// <summary>
        /// Implements an 8-connected region growing algorithm based on histogram similarity (using back projection).
        /// Starts with the starting pixel and checks in the back projection all the four pixel around it.
        /// If the pixels pass some threshold, they are selected and the region is grown to contain them as well.
        /// </summary>
        public static void Fill(Point startPoint, CVImage outputImage, 
            RegionGrowingCriteria criteria, object criteriaCookie,
            LabelingFunc labelFunc, object labelingCookie)
        {
            // first, include the starting pixel in the region.
            labelFunc(outputImage, startPoint, labelingCookie);

            // go over all the connection points.
            foreach (Point connection in connections)
            {
                Point connectionPoint = new Point(
                        startPoint.X + (int)connection.X,
                        startPoint.Y + (int)connection.Y);

                if (!outputImage.Contains(connectionPoint))
                    continue;

                // consider this point only if it's not already in the region.
                if (outputImage[connectionPoint].GrayLevel != 0) continue;

                // only if growing criteria is met, recurse away.
                if (criteria(startPoint, connectionPoint, criteriaCookie))
                {
                    // recurse into the new connection point.
                    Fill(connectionPoint, outputImage, criteria, criteriaCookie, labelFunc, labelingCookie);
                }
            }
        }
コード例 #2
0
ファイル: RegionGrowing.cs プロジェクト: tdck/opencvdotnet
        /// <summary>
        /// Implements an 8-connected region growing algorithm based on histogram similarity (using back projection).
        /// Starts with the starting pixel and checks in the back projection all the four pixel around it.
        /// If the pixels pass some threshold, they are selected and the region is grown to contain them as well.
        /// </summary>
        public static void Fill(Point startPoint, CVImage outputImage,
                                RegionGrowingCriteria criteria, object criteriaCookie,
                                LabelingFunc labelFunc, object labelingCookie)
        {
            // first, include the starting pixel in the region.
            labelFunc(outputImage, startPoint, labelingCookie);

            // go over all the connection points.
            foreach (Point connection in connections)
            {
                Point connectionPoint = new Point(
                    startPoint.X + (int)connection.X,
                    startPoint.Y + (int)connection.Y);

                if (!outputImage.Contains(connectionPoint))
                {
                    continue;
                }

                // consider this point only if it's not already in the region.
                if (outputImage[connectionPoint].GrayLevel != 0)
                {
                    continue;
                }

                // only if growing criteria is met, recurse away.
                if (criteria(startPoint, connectionPoint, criteriaCookie))
                {
                    // recurse into the new connection point.
                    Fill(connectionPoint, outputImage, criteria, criteriaCookie, labelFunc, labelingCookie);
                }
            }
        }
コード例 #3
0
 /// <summary>
 /// Can be used in the call to Fill() as the default labeling function.
 /// This function merely colors the pixel in the color specified under labelCookie
 /// which should be a Color object.
 /// </summary>
 public static void DefaultLabeling(CVImage outputImage, Point pt, object labelCookie)
 {
     if (!outputImage.Contains(pt)) return;
     if (outputImage[pt].GrayLevel != 0) return;
     Color c = (Color)labelCookie;
     outputImage[pt] = new CVRgbPixel(c);
 }
コード例 #4
0
ファイル: ColorHistogram.cs プロジェクト: tdck/opencvdotnet
        public void ShowHistogram(CVImage image, CVImage mask)
        {
            if (image == null)
            {
                return;
            }

            // split image into channels (b,g,r)
            CVImage[] planes = image.Split();

            // we will create a 1D histogram for every channel. each histogram will have
            // 'numberOfBins' bins for its single dimension (ranged from 0 to 255).
            int[]    bins   = { BinsPerChannel };
            CVPair[] ranges = { new CVPair(0, 255) };

            // calculate histogram for red, green and blue channels (seperately).
            CVHistogram histoRed   = planes[0].CalcHistogram(bins, ranges, mask);
            CVHistogram histoBlue  = planes[1].CalcHistogram(bins, ranges, mask);
            CVHistogram histoGreen = planes[2].CalcHistogram(bins, ranges, mask);

            // dispose of plane images.
            foreach (CVImage img in planes)
            {
                img.Release();
            }

            // draw the three histograms.
            DrawHistogram(bluePanel, histoBlue, 1);
            DrawHistogram(greenPanel, histoGreen, 2);
            DrawHistogram(redPanel, histoRed, 0);

            histoBlue.Release();
            histoGreen.Release();
            histoRed.Release();
        }
コード例 #5
0
        private void Track()
        {
            if (image == null)
            {
                return;
            }

            // calculate the back projection of the current image on top of the initial histogram.
            using (CVImage bp = image.CalcBackProject(this.initialHistogram))
            {
                // show the back projection.
                backProjectImage.Image = bp.ToBitmap();

                // calculate mean shift with user parameters and region of interest.
                CVConnectedComp conn = bp.MeanShift(originalImage.SelectionRect, Iterations);

                // update region of interest according to mean shift result.
                originalImage.SelectionRect = conn.Rect;
            }

            // rotate update counter only if rate != 0.
            if (UpdateHistogramRate != 0)
            {
                updateHistoCounter = (updateHistoCounter + 1) % UpdateHistogramRate;

                // update histogram is counter reached 0.
                if (updateHistoCounter == 0)
                {
                    UpdateHistogram();
                }
            }

            // refresh images.
            RefreshImages();
        }
コード例 #6
0
        public void ShowHistogram(CVImage image, CVImage mask)
        {
            if (image == null) return;

            // split image into channels (b,g,r)
            CVImage[] planes = image.Split();

            // we will create a 1D histogram for every channel. each histogram will have
            // 'numberOfBins' bins for its single dimension (ranged from 0 to 255).
            int[] bins = { BinsPerChannel };
            CVPair[] ranges = { new CVPair(0, 255) };

            // calculate histogram for red, green and blue channels (seperately).
            CVHistogram histoRed = planes[0].CalcHistogram(bins, ranges, mask);
            CVHistogram histoBlue = planes[1].CalcHistogram(bins, ranges, mask);
            CVHistogram histoGreen = planes[2].CalcHistogram(bins, ranges, mask);

            // dispose of plane images.
            foreach (CVImage img in planes)
                img.Release();

            // draw the three histograms.
            DrawHistogram(bluePanel, histoBlue, 1);
            DrawHistogram(greenPanel, histoGreen, 2);
            DrawHistogram(redPanel, histoRed, 0);

            histoBlue.Release();
            histoGreen.Release();
            histoRed.Release();
        }
コード例 #7
0
ファイル: RegionGrowing.cs プロジェクト: tdck/opencvdotnet
 /// <summary>
 /// Can be used to implement flood fill.
 /// </summary>
 public static bool FloodFillCondition(CVImage frame, Point startPt, Point candidatePt, int tolerange)
 {
     if (!frame.Contains(candidatePt) || !frame.Contains(startPt))
     {
         return(false);
     }
     return(Math.Abs(frame[startPt].GrayLevel - frame[candidatePt].GrayLevel) <= tolerange);
 }
コード例 #8
0
 /// <summary>
 /// Label 8-connected points from each point with color.
 /// </summary>
 public static void EightConnectedLabeling(CVImage outputImage, Point pt, object colorCookie)
 {
     foreach (Point conn in connections)
     {
         Point connectPt = new Point(pt.X + conn.X, pt.Y + conn.Y);
         DefaultLabeling(outputImage, connectPt, colorCookie);
     }
 }
コード例 #9
0
ファイル: RegionGrowing.cs プロジェクト: tdck/opencvdotnet
 /// <summary>
 /// Label 8-connected points from each point with color.
 /// </summary>
 public static void EightConnectedLabeling(CVImage outputImage, Point pt, object colorCookie)
 {
     foreach (Point conn in connections)
     {
         Point connectPt = new Point(pt.X + conn.X, pt.Y + conn.Y);
         DefaultLabeling(outputImage, connectPt, colorCookie);
     }
 }
コード例 #10
0
ファイル: Program.cs プロジェクト: tdck/opencvdotnet
        private static void TestDrawContours()
        {
            CVImage image = new CVImage(100, 100, CVDepth.Depth8U, 1);

            image.DrawRectangle(new System.Drawing.Rectangle(10, 10, 20, 30), System.Drawing.Color.Red, 3);
            new BitmapViewer(image.ToBitmap()).ShowDialog();
            CVImage res = image.DrawContours();

            new BitmapViewer(res.ToBitmap()).ShowDialog();
        }
コード例 #11
0
ファイル: History.cs プロジェクト: tdck/opencvdotnet
        private void ProcessBackgroundSubtraction()
        {
            CVImage frame = videoPlayer.LastFrame;

            // access last frame from video player.
            for (int row = 0; row < frame.Height; ++row)
            {
                for (int col = 0; col < frame.Width; ++col)
                {
                    CVRgbPixel pixel = frame[row, col];

                    // get black and white value for this pixel.
                    int bwValue = pixel.BwValue;

                    // accumulate the new value to the moving average.
                    double val = (double)bwValue / 255.0;

                    if (bgAccum[row, col] == -1)
                    {
                        bgAccum[row, col] = val;
                    }
                    else
                    {
                        bgAccum[row, col] = alpha * val + (1 - alpha) * bgAccum[row, col];
                    }

                    // calculate the diff between the frame and the average up until now.
                    double delta = val - bgAccum[row, col];

                    byte currentBgValue = bgFrame[row, col].BwValue;

                    if (currentBgValue > 0)
                    {
                        currentBgValue -= STEP_SIZE;
                    }

                    // if delta is smaller than the threshold, eliminate the background.
                    if (delta < threshold)
                    {
                        // set the color of the background frame to black.
                        //bgFrame[row, col] = new CVRgbPixel(0, 0, 0);
                    }
                    else
                    {
                        currentBgValue = 255;
                    }

                    bgFrame[row, col] = new CVRgbPixel(currentBgValue, currentBgValue, currentBgValue);
                }
            }

            // display the updated frame on the window.
            bs.Image = bgFrame.ToBitmap();
        }
コード例 #12
0
ファイル: Path.cs プロジェクト: tdck/opencvdotnet
        private void videoTimer_Tick(object sender, EventArgs e)
        {
            image = cap.QueryFrame();

            if (image == null)
            {
                cap.Restart();
            }

            Track();
        }
コード例 #13
0
ファイル: Path.cs プロジェクト: tdck/opencvdotnet
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();

            openFile.Title = "Select Image File";
            if (openFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            image = new CVImage(openFile.FileName);
            Track();
        }
コード例 #14
0
        private bool MyRGCriteria(Point startPt, Point candidatePt, object frameCookie)
        {
            CVImage frame = frameCookie as CVImage;

            int tolerance;

            if (!int.TryParse(grayscaleTolerance.Text, out tolerance))
            {
                tolerance = 1;
            }

            return(Math.Abs(frame[startPt].GrayLevel - frame[candidatePt].GrayLevel) <= tolerance);
        }
コード例 #15
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);
        }
コード例 #16
0
        private void video_SelectionChanged(object sender, EventArgs f)
        {
            if (videoPlayer.LastFrame == null)
            {
                return;
            }

            using (CVImage region = videoPlayer.LastFrame.CopyRegion(video.SelectionRect))
            {
                selectionHistogram = region.CalcHistogram(30);
            }

            ProcessRegionGrowing();
        }
コード例 #17
0
ファイル: RegionGrowing.cs プロジェクト: tdck/opencvdotnet
        /// <summary>
        /// Can be used in the call to Fill() as the default labeling function.
        /// This function merely colors the pixel in the color specified under labelCookie
        /// which should be a Color object.
        /// </summary>
        public static void DefaultLabeling(CVImage outputImage, Point pt, object labelCookie)
        {
            if (!outputImage.Contains(pt))
            {
                return;
            }
            if (outputImage[pt].GrayLevel != 0)
            {
                return;
            }
            Color c = (Color)labelCookie;

            outputImage[pt] = new CVRgbPixel(c);
        }
コード例 #18
0
        private void meanShiftNoMask_SelectionChanged(object sender, EventArgs e)
        {
            using (CVImage frame = videoPlayer.LastFrame.CopyRegion(meanShiftNoMask.SelectionRect))
            {
                if (histNoMask != null)
                {
                    histNoMask.Dispose();
                }
                histNoMask = frame.CalcHistogram(Bins);
                noMaskHist.BinsPerChannel = Bins;
                noMaskHist.ShowHistogram(frame);
            }

            NextFrame(false);
        }
コード例 #19
0
ファイル: History.cs プロジェクト: tdck/opencvdotnet
        private void ProcessMeanShift()
        {
            if (selectionHistogram == null)
            {
                return;
            }

            using (CVImage lastFrameClone = videoPlayer.LastFrame.Clone())
            {
                using (CVImage bp = videoPlayer.LastFrame.CalcBackProject(selectionHistogram))
                {
                    CVConnectedComp cc = bp.MeanShift(video.SelectionRect, 100);
                    video.SelectionRect = cc.Rect;
                }
            }
        }
コード例 #20
0
ファイル: Path.cs プロジェクト: tdck/opencvdotnet
        private void openVideoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();

            openFile.Title  = "Select Video File";
            openFile.Filter = "AVI Files|*.avi";
            if (openFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            cap.Open(openFile.FileName);
            image = cap.QueryFrame();

            UpdateHistogram();
            Track();
        }
コード例 #21
0
        private void openVideoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();

            openFile.Title = "Select Video File";
            if (openFile.ShowDialog() != DialogResult.OK)
            {
                return;
            }

            CVCapture cap = new CVCapture(openFile.FileName);

            image = cap.QueryFrame();
            cap.Dispose();

            UpdateHistogram();
        }
コード例 #22
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);
            }
        }
コード例 #23
0
        private void ProcessRegionGrowing()
        {
            if (selectionHistogram == null)
            {
                return;
            }

            using (CVImage lastFrameClone = videoPlayer.LastFrame.Clone())
            {
                using (CVImage bp = videoPlayer.LastFrame.CalcBackProject(selectionHistogram))
                {
                    CVConnectedComp cc = bp.MeanShift(video.SelectionRect, 100);
                    video.SelectionRect = cc.Rect;

                    Point midPoint = new Point(
                        video.SelectionRect.Left + video.SelectionRect.Width / 2,
                        video.SelectionRect.Top + video.SelectionRect.Height / 2);

                    using (CVImage rg = videoPlayer.Capture.CreateCompatibleImage())
                    {
                        rg.Zero();
                        OpenCVDotNet.Algorithms.RegionGrowing.Fill(midPoint, rg, new OpenCVDotNet.Algorithms.RegionGrowingCriteria(MyRGCriteria), videoPlayer.LastFrame);
                        growingFrame.Image = rg.ToBitmap();

                        if (overlay.Checked)
                        {
                            // go over regions in rg and overlay on output image.
                            for (int row = 0; row < rg.Height; row++)
                            {
                                for (int col = 0; col < rg.Width; ++col)
                                {
                                    if (rg[row, col].GrayLevel != 0)
                                    {
                                        videoPlayer.LastFrame[row, col] = new CVRgbPixel(0, 100, 0);
                                    }
                                }
                            }
                        }

                        video.Image = videoPlayer.LastFrame.ToBitmap();
                    }
                }
            }
        }
コード例 #24
0
        public override void Process()
        {
            CVImage swap = FPrevious;

            FPrevious = FCurrent;
            FCurrent  = swap;

            FInput.Image.GetImage(TColorFormat.L8, FCurrent);

            Image <Gray, byte>  p  = FPrevious.GetImage() as Image <Gray, byte>;
            Image <Gray, byte>  c  = FCurrent.GetImage() as Image <Gray, byte>;
            Image <Gray, float> vx = FVelocityX.GetImage() as Image <Gray, float>;
            Image <Gray, float> vy = FVelocityY.GetImage() as Image <Gray, float>;

            OpticalFlow.HS(p, c, UsePrevious, vx, vy, FLambda, new MCvTermCriteria(FIterations));

            CopyToRgb();
            FOutput.Send();
        }
コード例 #25
0
ファイル: History.cs プロジェクト: tdck/opencvdotnet
        private void videoPlayer_Opening(VideoPlayer sender, OpeningEventArgs args)
        {
            if (bgFrame != null)
            {
                bgFrame.Dispose();
            }

            // create accumulator image when a new video is opened.
            bgAccum = new double[args.NewCapture.Height, args.NewCapture.Width];
            for (int row = 0; row < args.NewCapture.Height; ++row)
            {
                for (int col = 0; col < args.NewCapture.Width; ++col)
                {
                    bgAccum[row, col] = -1.0;
                }
            }

            bgFrame = args.NewCapture.CreateCompatibleImage();
        }
コード例 #26
0
        public override void Process()
        {
            CVImage swap = FPrevious;

            FPrevious = FCurrent;
            FCurrent  = swap;

            FInput.Image.GetImage(TColorFormat.L8, FCurrent);

            Image <Gray, byte>  p  = FPrevious.GetImage() as Image <Gray, byte>;
            Image <Gray, byte>  c  = FCurrent.GetImage() as Image <Gray, byte>;
            Image <Gray, float> vx = FVelocityX.GetImage() as Image <Gray, float>;
            Image <Gray, float> vy = FVelocityY.GetImage() as Image <Gray, float>;

            OpticalFlow.LK(p, c, FWindowSize, vx, vy);

            CopyToRgb();
            FOutput.Send();
        }
コード例 #27
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();
        }
コード例 #28
0
        private void CalculateMaskedHistogram()
        {
            if (videoPlayer.LastFrame == null)
            {
                return;
            }

            using (CVImage frame = videoPlayer.LastFrame.CopyRegion(meanShift.SelectionRect))
            {
                // create a list of real fg and bg markers.
                List <Point> realFg = new List <Point>(meanShift.GetMarkerLocations(Color.Blue));
                List <Point> realBg = new List <Point>(meanShift.GetMarkerLocations(Color.Red));

                // create the histogram mask.
                using (CVImage mask = MaskedHistogram.PrepareMask(frame, realFg.ToArray(), realBg.ToArray(), includeNeautral.Checked, ffThresh.Value))
                {
                    maskPicture.Image = mask.ToBitmap();

                    // show mask histogram to user interface.
                    maskHistogram.BinsPerChannel = Bins;
                    maskHistogram.ShowHistogram(frame, mask);

                    // calculate new histogram (dispose old one if exist).
                    if (hist != null)
                    {
                        hist.Dispose();
                    }
                    hist = frame.CalcHistogram(Bins, mask);

                    // apply mask to overlay (just for ui).
                    using (CVImage masked = MaskedHistogram.ApplyMask(frame, mask))
                    {
                        maskOverlay.Image = masked.ToBitmap();
                    }
                }
            }

            NextFrame(false);
        }
コード例 #29
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();
        }
コード例 #30
0
        /// <summary>
        /// This function applies the mask to image in the following way:
        ///  - All pixels in 'mask' that are blue are copied to the output image in maximum intensity.
        ///  - All pixels in 'mask' that are black are copied to the output image in half intensity.
        ///  - All pixels in 'mask' that are red are not copied at all to output image.
        /// </summary>
        public static CVImage ApplyMask(CVImage image, CVImage mask)
        {
            CVImage outputImage = image.Clone();

            outputImage.Zero();

            for (int row = 0; row < image.Height; ++row)
            {
                for (int col = 0; col < image.Width; ++col)
                {
                    // copy everything that's not background.
                    if (mask[row, col].ToColor().ToArgb() != Color.Black.ToArgb())
                    {
                        outputImage[row, col] = image[row, col];
                    }
                    else
                    {
                        outputImage[row, col] = new CVRgbPixel(TEMP_BG_COLOR);
                    }
                }
            }

            return(outputImage);
        }
コード例 #31
0
ファイル: Negative.cs プロジェクト: tdck/opencvdotnet
        /// <summary>
        /// Called by the video player to handle the next video frame.
        /// </summary>
        private void videoPlayer1_NextFrame(OpenCVDotNet.UI.VideoPlayer sender, OpenCVDotNet.UI.NextFrameEventArgs args)
        {
            // args.Frame contains the frame to be handled.
            CVImage frame = args.Frame;

            // go over all the pixels (rows, cols)
            for (int y = 0; y < frame.Height; ++y)
            {
                for (int x = 0; x < frame.Width; ++x)
                {
                    CVRgbPixel pixel = frame[y, x];

                    // invert RGB colors.
                    frame[y, x] = new CVRgbPixel(
                        (byte)(255 - pixel.R),
                        (byte)(255 - pixel.G),
                        (byte)(255 - pixel.B));
                }
            }

            // assign resulting frame to picture box as a bitmap.
            pictureBox1.Image = frame.ToBitmap();
            writer.WriteFrame(frame);
        }
コード例 #32
0
ファイル: Stitch.cs プロジェクト: kevinbs/VVVV.Packs.Image
        public void Evaluate(int SpreadMax)
        {
            for (int i = SpreadMax; i < FOutput.SliceCount; i++)
                FOutput[i].Dispose();
            while (FOutput.SliceCount < SpreadMax)
                FOutput.Add(new CVImageLink());

            FStatus.SliceCount = SpreadMax;

            for (int i = 0; i < SpreadMax; i++)
            {
                if (!FDo[i])
                    continue;

                var inputSpread = FInput[i];
                var output = FOutput[i];

                foreach(var image in inputSpread)
                {
                    image.LockForReading();
                }

                CVImage result = new CVImage();
                try
                {
                    int size = inputSpread.SliceCount;

                    Image<Bgr, Byte>[] images = new Image<Bgr, byte>[size];
                    List<CVImage> ToDispose = new List<CVImage>();

                    for (int j = 0; j < size; j++)
                    {
                        if (inputSpread[j].FrontImage.ImageAttributes.ColourFormat == TColorFormat.RGB8)
                        {
                            images[j] = inputSpread[j].FrontImage.GetImage() as Image<Bgr, Byte>;
                        }
                        else
                        {
                            var image = new CVImage();
                            ToDispose.Add(image);
                            image.Initialise(inputSpread[j].FrontImage.Size, TColorFormat.RGB8);
                            inputSpread[j].FrontImage.GetImage(image);
                            images[j] = image.GetImage() as Image<Bgr, Byte>;
                        }
                    }

                    result.SetImage(FStitcher.Stitch(images));

                    foreach (var image in ToDispose)
                    {
                        image.Dispose();
                    }

                    FStatus[i] = "OK";
                }
                catch (Exception e)
                {
                    FStatus[i] = e.Message;
                }
                finally
                {
                    foreach(var image in inputSpread)
                    {
                        image.ReleaseForReading();
                    }
                }

                output.Send(result);
            }
        }
コード例 #33
0
 /// <summary>
 /// Implements an 8-connected region growing algorithm based on histogram similarity (using back projection).
 /// Starts with the starting pixel and checks in the back projection all the four pixel around it.
 /// If the pixels pass some threshold, they are selected and the region is grown to contain them as well.
 /// </summary>
 public static void Fill(Point startPoint, CVImage outputImage, RegionGrowingCriteria criteria, object criteriaCookie)
 {
     Fill(startPoint, outputImage, criteria, criteriaCookie, Color.Red);
 }
コード例 #34
0
 public void ShowHistogram(CVImage image)
 {
     ShowHistogram(image, null);
 }
コード例 #35
0
ファイル: Detector.cs プロジェクト: thigiacmaytinh/IPSSedu
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        Bitmap DetectPlate(Bitmap bmp)
        {
            CVImage img = new CVImage(bmp);

            return(img.DetectPlate(true));
        }
コード例 #36
0
ファイル: Detector.cs プロジェクト: thigiacmaytinh/IPSSedu
        ////////////////////////////////////////////////////////////////////////////////////////////////////////////

        public string DetectChar(Bitmap bmp)
        {
            //giai đoạn nhận diện ký tự gồm 3 bước:
            //Bước 1: sắp xếp các ký tự từ trái qua phải
            //Bước 2: chuyển ảnh ký tự thành tập dữ liệu SVM hợp lệ
            //Bước 3: nhận diện các tập dữ liệu và nối thành chuỗi kết quả


            CVImage img   = new CVImage(bmp);
            string  chars = img.DetectChar();

            int nRects = (chars.Length - chars.Replace("_", "").Length + 1) / 4;

            if (nRects == 0)
            {
                return("");
            }

            List <Rectangle> rects = new List <Rectangle>(nRects);

            string[] points = chars.Split(new char[] { '_' });
            for (int i = 0; i < nRects; i += 1)
            {
                Rectangle rect = new Rectangle(int.Parse(points[i * 4 + 0]), int.Parse(points[i * 4 + 1]), int.Parse(points[i * 4 + 2]), int.Parse(points[i * 4 + 3]));
                rects.Add(rect);
            }


            //ảnh các ký tự
            List <Bitmap> imgkytu = new List <Bitmap>(9);
            Bitmap        Part1   = null;
            Bitmap        Part2   = null;
            Bitmap        Part3   = null;
            Bitmap        Part4   = null;
            int           p1      = 0;

            //lưu các toạ độ của khung chữ nhật chứa ký tự
            int[] toado = new int[10];

            //sắp xếp các ký tự từ trái qua phải, từ trên xuống dưới
            for (int i = 0; i < nRects; i++)
            {
                Rectangle rect = rects[i];
                if (rect.Y < 65 || rect.Y > 165)
                {
                    if (rect.Y > 170)
                    {
                        imgkytu.Add(Resize(Crop(bmp, rect), new Size(20, 48)));
                        toado[p1] = rect.X;
                        for (int k = 0; k <= p1 - 1; k++)
                        {
                            if (toado[p1] < toado[k])
                            {
                                Bitmap tempImage = imgkytu[p1];
                                imgkytu[p1] = imgkytu[k];
                                imgkytu[k]  = tempImage;
                                int temp = toado[p1];
                                toado[p1] = toado[k];
                                toado[k]  = temp;
                            }
                        }
                        p1 += 1;
                    }
                    else
                    {
                        if (rect.X > 50 && rect.X < 100)
                        {
                            Part1 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                        else if (rect.X > 100 && rect.X < bmp.Width / 2)
                        {
                            Part2 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                        else if (rect.X > bmp.Width / 2 && rect.X < 300)
                        {
                            Part3 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                        else
                        {
                            Part4 = Resize(Crop(bmp, rect), new Size(20, 48));
                        }
                    }
                }
            }

            string[] temp5 = new string[6];


            //chuyển thành tập SVM hợp lệ

            if (imgkytu != null)
            {
                for (int i = 0; i <= p1 - 1; i++)
                {
                    temp5[i] += ImageToSVMToBinaryString(imgkytu[i]);
                }
            }


            //dự đoán số
            string result = "";

            result += PredictSVM(Part1, "num");
            result += PredictSVM(Part2, "num");
            result += "-";
            result += PredictSVM(Part3, "charnum");
            result += PredictSVM(Part4, "charnum");

            result += "-";
            for (int j = 0; j <= p1 - 1; j++)
            {
                result += PredictSVM(temp5[j], g_modelNum);
            }

            return(result.Trim());
        }
コード例 #37
0
ファイル: SafeHandles.cs プロジェクト: tdck/opencvdotnet
 internal __CvImagePtr(CVImage img)
     : base(true)
 {
     SetHandle(img.Ptr);
 }
コード例 #38
0
ファイル: Path.cs プロジェクト: tdck/opencvdotnet
 private void ResetSegmentation()
 {
     segmentationImage = image.Clone();
     segPoints         = new List <Point>();
 }
コード例 #39
0
 /// <summary>
 /// Can be used to implement flood fill.
 /// </summary>
 public static bool FloodFillCondition(CVImage frame, Point startPt, Point candidatePt, int tolerange)
 {
     if (!frame.Contains(candidatePt) || !frame.Contains(startPt)) return false;
     return Math.Abs(frame[startPt].GrayLevel - frame[candidatePt].GrayLevel) <= tolerange;
 }
コード例 #40
0
 /// <summary>
 /// Implements an 8-connected region growing algorithm based on histogram similarity (using back projection).
 /// Starts with the starting pixel and checks in the back projection all the four pixel around it.
 /// If the pixels pass some threshold, they are selected and the region is grown to contain them as well.
 /// </summary>
 public static void Fill(Point startPoint, CVImage outputImage, RegionGrowingCriteria criteria, object criteriaCookie, Color c)
 {
     Fill(startPoint, outputImage, criteria, criteriaCookie, DefaultLabeling, c);
 }