Exemple #1
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();
        }
Exemple #2
0
        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;
                }
            }
        }
Exemple #3
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();
                    }
                }
            }
        }
        private void NextFrame(bool track)
        {
            meanShiftNoMask.Image = videoPlayer.LastFrame.ToBitmap();

            if (!track)
            {
                vot.Reset();
            }

            if (hist == null)
            {
                return;
            }
            using (CVImage backProj = videoPlayer.LastFrame.CalcBackProject(hist))
            {
                CVConnectedComp cc = backProj.MeanShift(meanShift.SelectionRect, 1);
                vot.AddValue(cc.Area, Color.Blue);

                if (cc.Area > MIN_AREA && cc.Area < MAX_AREA)
                {
                    if (track)
                    {
                        meanShift.SelectionRect = cc.Rect;
                    }
                }

                bpWithMask.Image = backProj.ToBitmap();
            }

            OpenCVDotNet.UI.Statistics blueStats = vot.GetStatistics(Color.Blue);
            if (blueStats != null)
            {
                avgWithMask.Text  = ((int)blueStats.Average).ToString();
                lastWithMask.Text = ((int)blueStats.LastValue).ToString();
            }

            if (histNoMask == null)
            {
                return;
            }
            using (CVImage backProjNoMask = videoPlayer.LastFrame.CalcBackProject(histNoMask))
            {
                CVConnectedComp cc = backProjNoMask.MeanShift(meanShiftNoMask.SelectionRect, 1);
                vot.AddValue(cc.Area, Color.Red);

                if (cc.Area > MIN_AREA && cc.Area < MAX_AREA)
                {
                    if (track)
                    {
                        meanShiftNoMask.SelectionRect = cc.Rect;
                    }
                }

                bpNoMask.Image = backProjNoMask.ToBitmap();
            }

            OpenCVDotNet.UI.Statistics redStats = vot.GetStatistics(Color.Red);
            if (redStats != null)
            {
                avgNoMask.Text  = ((int)redStats.Average).ToString();
                lastNoMask.Text = ((int)redStats.LastValue).ToString();
            }
        }