Пример #1
0
        public CVImage CalcBackProject(CVHistogram histogram)
        {
            CVImage[] planes = Split();

            CVImage backProjection =
                new CVImage(
                    planes[0].RegionOfInterest.Width,
                    planes[0].RegionOfInterest.Height,
                    planes[0].Depth,
                    planes[0].Channels);

            __IplImagePtr[] iplImages = new __IplImagePtr[planes.Length];
            for (int i = 0; i < planes.Length; ++i)
            {
                iplImages[i] = planes[i].Internal;
            }

            PInvoke.cvCalcBackProject(iplImages, backProjection.Internal, histogram.Internal);
            CVUtils.CheckLastError();

            for (int i = 0; i < planes.Length; ++i)
            {
                planes[i].Release();
            }

            return(backProjection);
        }
Пример #2
0
        public CVHistogram CalcHistogram(int[] binSizes, CVPair[] binRanges, CVImage mask)
        {
            CVHistogram h = new CVHistogram(binSizes, binRanges);

            __IplImagePtr[] images = new __IplImagePtr[this.Channels];
            if (this.Channels == 1)
            {
                images[0] = this.Internal;
            }
            else
            {
                CVImage[] planes = this.Split();
                for (int i = 0; i < planes.Length; ++i)
                {
                    images[i] = planes[i].Internal;
                }
            }

            __CvArrPtr maskArr = IntPtr.Zero;

            if (mask != null)
            {
                maskArr = mask.Array;
            }

            PInvoke.cvCalcHist(images, h.Internal, 0, maskArr);
            CVUtils.CheckLastError();
            return(h);
        }
Пример #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();
        }
        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);
        }
        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);
        }
Пример #6
0
        /// <summary>
        /// Calculate the histogram for the region of interest and saves it under 
        /// initialHistogram.
        /// </summary>
        private void UpdateHistogram()
        {
            // release the previous histogram if any.
            if (initialHistogram != null) initialHistogram.Release();

            // if there's no image, just return.
            if (image == null) return;

            // we use symmetric bins and entire range.
            int[] bpBins = { NumberOfBins, NumberOfBins, NumberOfBins };
            CVPair[] bpRanges = { new CVPair(0, 255), new CVPair(0, 255), new CVPair(0, 255) };

            // calculate histogram for region of interest and display it in panel.
            image.RegionOfInterest = originalImage.SelectionRect;
            this.initialHistogram = image.CalcHistogram(bpBins, bpRanges);
            initHisto.ShowHistogram(image);
            image.ResetROI();

            ResetSegmentation();

            RefreshImages();
        }
Пример #7
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();
        }
Пример #8
0
        public CVHistogram CalcHistogram(int[] binSizes, CVPair[] binRanges, CVImage mask)
        {
            CVHistogram h = new CVHistogram(binSizes, binRanges);

            __IplImagePtr[] images = new __IplImagePtr[this.Channels];
            if (this.Channels == 1)
            {
                images[0] = this.Internal;
            }
            else
            {
                CVImage[] planes = this.Split();
                for (int i = 0; i < planes.Length; ++i)
                {
                    images[i] = planes[i].Internal;
                }
            }

            __CvArrPtr maskArr = IntPtr.Zero;
            if (mask != null) maskArr = mask.Array;

            PInvoke.cvCalcHist(images, h.Internal, 0, maskArr);
            CVUtils.CheckLastError();
            return h;
        }
Пример #9
0
        public CVImage CalcBackProject(CVHistogram histogram)
        {
            CVImage[] planes = Split();

            CVImage backProjection =
                new CVImage(
                    planes[0].RegionOfInterest.Width,
                    planes[0].RegionOfInterest.Height,
                    planes[0].Depth,
                    planes[0].Channels);

            __IplImagePtr[] iplImages = new __IplImagePtr[planes.Length];
            for (int i = 0; i < planes.Length; ++i)
                iplImages[i] = planes[i].Internal;

            PInvoke.cvCalcBackProject(iplImages, backProjection.Internal, histogram.Internal);
            CVUtils.CheckLastError();

            for (int i = 0; i < planes.Length; ++i)
                planes[i].Release();

            return backProjection;
        }
Пример #10
0
        private void video_SelectionChanged(object sender, EventArgs e)
        {
            if (videoPlayer.LastFrame == null) return;

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

            ProcessMeanShift();
        }