/// <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); }
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> /// 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); }
/// <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); }
/// <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); }
/// <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); }
private void Work() { CVImage frame; try { while (true) { ReopenCapture(); while ((frame = cap.QueryFrame()) != null) { 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. byte bwValue = pixel.BwValue; // accumulate the new value to the moving average. double val = (double)bwValue / 255.0; avg.Accumulate(row, col, val); // calculate the diff between the frame and the average up until now. double delta = val - avg[row, col]; // if delta is smaller than the threshold, eliminate the background. if (delta < Threshold) { // set the color of the background frame to black. frame[row, col] = backgroundColor; } else { // if monochrome is checked, set pixel to foreground color. if (monochrome.Checked) { frame[row, col] = foregroundColor; } } } } // display the updated frame on the window. cvPanel.Image = frame.ToBitmap(); if (vw != null) { vw.WriteFrame(frame); } // delay for 20ms. Thread.Sleep(20); frame.Release(); } } } catch (CVException e1) { MessageBox.Show(e1.Message); } catch (ThreadAbortException) { Console.WriteLine("Aborting thread..."); } finally { if (cap != null) { cap.Release(); } if (vw != null) { vw.Release(); } } }