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(); }
/// <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(); }
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(); }
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); }
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); }
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(); }
private void UpdateHistogram() { int numberOfBins; if (!int.TryParse(binSize.Text, out numberOfBins)) { statusBar.Text = string.Format("Number of bins '{0}' is not an integer.", binSize.Text); return; } if (image == null) { return; } //image.RegionOfInterest = originalImage.SelectionRect; // 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 = { numberOfBins }; CVPair[] ranges = { new CVPair(0, 255) }; // calculate histogram for red, green and blue channels (seperately). CVHistogram histoRed = planes[0].CalcHistogram(bins, ranges); CVHistogram histoBlue = planes[1].CalcHistogram(bins, ranges); CVHistogram histoGreen = planes[2].CalcHistogram(bins, ranges); // draw the three histograms. DrawHistogram(bluePanel, histoBlue, 1); DrawHistogram(greenPanel, histoGreen, 2); DrawHistogram(redPanel, histoRed, 0); // resize & put original image onto form. CVImage output = new CVImage(image.Width, image.Height, CVDepth.Depth8U, 3); CVImage emptyPlane = new CVImage(image.Width, image.Height, CVDepth.Depth8U, 1); emptyPlane.Zero(); CVImage[] images = new CVImage[3]; images[0] = images[1] = images[2] = emptyPlane; if (blueCheck.Checked) { images[0] = planes[0]; } if (greenCheck.Checked) { images[1] = planes[1]; } if (redCheck.Checked) { images[2] = planes[2]; } output.Merge(images); originalImage.Image = output.ToBitmap(); // dispose of plane images. foreach (CVImage img in planes) { img.Dispose(); } statusBar.Text = "Ready"; }