public DiffContainer GetDiffs(Bitmap newFrame, int compressRate) { IplImage Frame = BitmapConverter.ToIplImage(newFrame); if (_oldImage == null || _oldImage.Height != newFrame.Height) { _oldImage = new Bitmap(newFrame.Width, newFrame.Height); } IplImage Previous_Frame = BitmapConverter.ToIplImage(_oldImage); IplImage Difference = new IplImage(Previous_Frame.Width, Previous_Frame.Height, BitDepth.U8, 4); Cv.AbsDiff(Frame, Previous_Frame, Difference); IplImage gray = new IplImage(Previous_Frame.Width, Previous_Frame.Height, BitDepth.U8, 1); Cv.CvtColor(Difference, gray, ColorConversion.RgbToGray); DiffContainer container = new DiffContainer(); CvSeq <CvPoint> contours; CvSeq <CvPoint> contour; using (CvMemStorage storage = new CvMemStorage()) for (int i = gray.FindContours(storage, out contours, CvContour.SizeOf, ContourRetrieval.External, ContourChain.ApproxSimple); contours != null; contours = contours.HNext) { contour = Cv.ApproxPoly(contours, CvContour.SizeOf, storage, ApproxPolyMethod.DP, 0.2); if (contour.ContourPerimeter() > 100 && contour.ContourPerimeter() < 10000 ) { var r = contour.BoundingRect(); var im = Frame.GetSubImage(r); Rectangle rect = new Rectangle(r.X, r.Y, r.Width, r.Height); rect.X -= 1; rect.Y -= 1; rect.Width += 2; rect.Height += 2; var j = BitmapConverter.ToBitmap(im); container.Data.Add(rect, j); } } UpdateOldFrame(container); return(container); }
static IplImage GetCountours(IplImage src) { CvMemStorage mem = Cv.CreateMemStorage(0); CvSeq <CvPoint> firstContour = null; IplImage dst = GetThresholdImage(src); dst = GetThresholdImage(dst); int count = dst.FindContours(mem, out firstContour); //src.DrawContours(firstContour, CvColor.Green, CvColor.Blue, 2); //src.DrawRect(firstContour.BoundingRect(), CvColor.Red); src = src.GetSubImage(firstContour.BoundingRect()); mem.Dispose(); firstContour.Dispose(); dst.Dispose(); return(src); }
public static IplImage testContours(IplImage target) { if (g_storage == null) { g_gray = new IplImage(target.Size, BitDepth.U8, 1); g_binary = new IplImage(target.Size, BitDepth.U8, 1); g_storage = new CvMemStorage(0); } else { g_storage.Clear(); } CvSeq <CvPoint> contours; target.CvtColor(g_gray, ColorConversion.BgrToGray); g_gray.Threshold(g_gray, g_thresh, 255, ThresholdType.Binary); g_gray.Copy(g_binary); g_gray.FindContours(g_storage, out contours, CvContour.SizeOf, ContourRetrieval.CComp); g_gray.Zero(); if (contours != null) { contours.ApproxPoly(CvContour.SizeOf, g_storage, ApproxPolyMethod.DP, 3, true); g_gray.DrawContours(contours, new CvScalar(255), new CvScalar(128), 100); } //g_gray.Dilate(g_gray, null, 2); //g_gray.Erode(g_gray, null, 2); return(g_gray); }