/// <summary> /// Refresh all images. /// </summary> private void RefreshImages() { image.RegionOfInterest = originalImage.SelectionRect; regionOfInterest.Image = image.ToBitmap(); roiHisto.ShowHistogram(image); image.ResetROI(); originalImage.Image = image.ToBitmap(); // update rectangle values into status bar. statusBar.Text = originalImage.SelectionRect.ToString() + " Update Rate: " + (UpdateHistogramRate == 0 ? "Never" : UpdateHistogramRate.ToString()); }
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(); }
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(); }
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(); }
/// <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); } }
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(); } } } }
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); }
/// <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); }
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 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(); } }
private static void TestBitmapConversion() { CVImage image = new CVImage(new System.Drawing.Bitmap(@"D:\Users\Yoav HaCohen\Pictures\temp\hair_res.bmp")); new BitmapViewer(image.ToBitmap()).ShowDialog(); }
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"; }