public void ToManagedImageTest(PixelFormat pixelFormat, int x, int y, byte red, byte green, byte blue) { UnmanagedImage image = UnmanagedImage.Create(320, 240, pixelFormat); image.SetPixel(new IntPoint(x, y), Color.FromArgb(255, red, green, blue)); Bitmap bitmap = image.ToManagedImage(); // check colors of pixels Assert.AreEqual(Color.FromArgb(255, red, green, blue), bitmap.GetPixel(x, y)); // make sure there are only 1 pixel UnmanagedImage temp = UnmanagedImage.FromManagedImage(bitmap); List <IntPoint> pixels = temp.CollectActivePixels(); Assert.AreEqual(1, pixels.Count); image.Dispose(); bitmap.Dispose(); temp.Dispose(); }
public void MeanTest3() { UnmanagedImage image = UnmanagedImage.FromManagedImage(new byte[, ] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }, }.ToBitmap()); { Rectangle rectangle = new Rectangle(0, 0, 1, 2); double expected = (1 + 4) / 2.0; double actual = Tools.Mean(image, rectangle); Assert.AreEqual(expected, actual); } { double expected = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9) / 9.0; double actual = Tools.Mean(image); Assert.AreEqual(expected, actual); } }
public void DetectionContour(int sueil) { /* Detecte les contours de l'image depuis l'image noir et blanc */ /* VERSION AFORGE => LONGUE * // 2 - Edge detection * DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector(); * imgContour = edgeDetector.Apply(imgNB); * * // 3 - Threshold edges * Threshold thresholdFilterGlyph = new Threshold(sueil); * * thresholdFilterGlyph.ApplyInPlace(imgContour); * */ Image <Gray, Byte> graySoft = imgRecu.Convert <Gray, Byte>(); imgNB = UnmanagedImage.FromManagedImage(graySoft.ToBitmap()); Image <Gray, Byte> cannyEdges = graySoft.Canny(new Gray(sueil), new Gray(149)); imgContour = UnmanagedImage.FromManagedImage(cannyEdges.ToBitmap()); }
public void ProcessImageTest() { UnmanagedImage image = UnmanagedImage.FromManagedImage(Accord.Imaging.Image.Clone(Resources.sample_black)); FastCornersDetector target = new FastCornersDetector(); target.Suppress = false; target.Threshold = 20; List <IntPoint> actual = target.ProcessImage(image); Assert.AreEqual(237, actual.Count); Assert.AreEqual(404, actual[0].X); Assert.AreEqual(35, actual[0].Y); Assert.AreEqual(407, actual[6].X); Assert.AreEqual(36, actual[6].Y); Assert.AreEqual(407, actual[11].X); Assert.AreEqual(38, actual[11].Y); Assert.AreEqual(55, actual[65].X); Assert.AreEqual(135, actual[65].Y); Assert.AreEqual(103, actual[73].X); Assert.AreEqual(137, actual[73].Y); }
public void ProcessImageTest2() { UnmanagedImage image = UnmanagedImage.FromManagedImage(Accord.Imaging.Image.Clone(Resources.lena512)); FastCornersDetector target = new FastCornersDetector(); target.Suppress = true; target.Threshold = 40; List <IntPoint> actual = target.ProcessImage(image); Assert.AreEqual(324, actual.Count); Assert.AreEqual(506, actual[0].X); Assert.AreEqual(4, actual[0].Y); Assert.AreEqual(152, actual[6].X); Assert.AreEqual(75, actual[6].Y); Assert.AreEqual(416, actual[11].X); Assert.AreEqual(115, actual[11].Y); Assert.AreEqual(140, actual[65].X); Assert.AreEqual(246, actual[65].Y); Assert.AreEqual(133, actual[73].X); Assert.AreEqual(253, actual[73].Y); }
private long InkDarkness(Bitmap OMark) { int darkestC = 255, lightestC = 0; UnmanagedImage mark = UnmanagedImage.FromManagedImage(OMark); for (int y = 0; y < OMark.Height; y++) { for (int x = 0; x < OMark.Width; x++) { Color c = mark.GetPixel(x, y); if (((c.R + c.G + c.B) / 3) > lightestC) { lightestC = ((c.R + c.G + c.B) / 3); } if (((c.R + c.G + c.B) / 3) < darkestC) { darkestC = ((c.R + c.G + c.B) / 3); } } } int dc = 0; for (int y = 0; y < OMark.Height; y++) { for (int x = 0; x < OMark.Width; x++) { Color c = mark.GetPixel(x, y); if (((c.R + c.G + c.B) / 3) < (lightestC + darkestC) / 2) { dc += 255; } } } return(dc); }
public void GetBlobs3(string inputFile, string outputPath) { UnmanagedImage skewedImg = null; { var bmp = new Bitmap(inputFile); var img = UnmanagedImage.FromManagedImage(bmp); var grayImg = Accord.Imaging.Filters.Grayscale.CommonAlgorithms.BT709.Apply(img); var bwImg = new Accord.Imaging.Filters.OtsuThreshold().Apply(grayImg); var skewChecker = new DocumentSkewChecker(); double angle = skewChecker.GetSkewAngle(bwImg); var rotateFilter = new Accord.Imaging.Filters.RotateBilinear(-angle); skewedImg = rotateFilter.Apply(img); bwImg.Dispose(); grayImg.Dispose(); img.Dispose(); bmp.Dispose(); } // create filter BlobsFiltering filter = new BlobsFiltering(); // configure filter filter.CoupledSizeFiltering = true; filter.MinHeight = 200; // 1" @ 200 DPI filter.MinWidth = 200; // 1" @ 200 DPI // apply the filter filter.ApplyInPlace(skewedImg); // save output file Bitmap finalImg = skewedImg.ToManagedImage(); skewedImg.Dispose(); System.IO.File.Delete(outputPath); finalImg.Save(outputPath); finalImg.Dispose(); }
public long InkDarkness(Bitmap OMark) //get the darkness of each circle area { int darkestC = 255, lightestC = 0; UnmanagedImage mark = UnmanagedImage.FromManagedImage(OMark); //image in unmanaged memory for (int y = 0; y < OMark.Height; y++) { for (int x = 0; x < OMark.Width; x++) { Color c = mark.GetPixel(x, y); if (((c.R + c.G + c.B) / 3) > lightestC) { lightestC = ((c.R + c.G + c.B) / 3); } if (((c.R + c.G + c.B) / 3) < darkestC) { darkestC = ((c.R + c.G + c.B) / 3); } } } int dc = 0; for (int y = 0; y < OMark.Height; y++) { for (int x = 0; x < OMark.Width; x++) { Color c = mark.GetPixel(x, y); if (((c.R + c.G + c.B) / 3) < (lightestC + darkestC) / 2) { dc += 255; } } } return(dc); }
private static RGB _GetAverageColor(Bitmap bitmap) { using (UnmanagedImage bmp = UnmanagedImage.FromManagedImage(bitmap)) { Color tempColor; double r = 0, g = 0, b = 0; for (int i = 0; i < bmp.Height; i++) { for (int j = 0; j < bmp.Width; j++) { tempColor = bmp.GetPixel(j, i); r += tempColor.R; g += tempColor.G; b += tempColor.B; } } int scale = bmp.Height * bmp.Width; r /= scale; g /= scale; b /= scale; return(new RGB((byte)r, (byte)g, (byte)b)); } }
/// <summary> /// Given a Document, this heuristic attempts to determine what the background is by finding the most common rgb /// colors in a border around the image /// </summary> /// <param name="document">Image to be analyzed</param> /// <param name="border">Size of the border in percent</param> /// <returns>Bgr color best-guess for background color</returns> public static Bgr DetectBackground(Bitmap document, int border = 10) { double border2 = (double)border / 100f; System.Drawing.Point tl = new System.Drawing.Point((int)(border2 * document.Width), (int)(border2 * document.Height)); System.Drawing.Point br = new System.Drawing.Point((int)(document.Width - (border2 * document.Width)), (int)(document.Height - (border2 * document.Height))); Rectangle rect = new Rectangle(tl.X, tl.Y, br.X - tl.X, br.Y - tl.Y); UnmanagedImage blackened = UnmanagedImage.FromManagedImage(document); AForge.Imaging.Drawing.FillRectangle(blackened, rect, Color.Black); Bitmap blacknew = blackened.ToManagedImage(); AForge.Imaging.ImageStatistics stat = new ImageStatistics(blacknew); Histogram red = stat.RedWithoutBlack; Histogram green = stat.GreenWithoutBlack; Histogram blue = stat.BlueWithoutBlack; int indexR = (int)red.Median; int indexB = (int)blue.Median; int indexG = (int)green.Median; Emgu.CV.Image <Bgra, Byte> blackcv = new Image <Bgra, byte>(blacknew); return(new Bgr((double)indexB, (double)indexG, (double)indexR)); }
static void ForAllPixels(Bitmap image, Action <double[]> m) { /* * //Slow (2x times) but safe * for (int i = 0; i < image.Width; i++) * for (int j = 0; j < image.Height; j++) * m(HSL.FromRGB(new RGB(image.GetPixel(i, j)))); */ using (var unmanagedImg = UnmanagedImage.FromManagedImage(image)) { CheckSourceFormat(unmanagedImg.PixelFormat); var pixelSize = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4; int pixelCnt = unmanagedImg.Height * unmanagedImg.Width; unsafe { var p = (byte *)unmanagedImg.ImageData.ToPointer(); for (int pixels = 0; pixels < pixelCnt; pixels++, p += pixelSize) { var hsl = HSL.FromRGB(new RGB(p[RGB.R], p[RGB.G], p[RGB.B])); m(new[] { hsl.Hue / 359.0, hsl.Saturation, hsl.Luminance }); } } } }
private void Device_NewFrame(object sender, NewFrameEventArgs args) { if (_regionSelectionMode) { return; } UnmanagedImage rawFrame = UnmanagedImage.FromManagedImage(args.Frame); UnmanagedImage normalized = _normalizatorFilters.Apply(rawFrame); _currentUnprocessed = normalized; // allows worker thread to do the job _frameRecievedEvent.Set(); if ((DateTime.Now - _lastFPSUpdate).TotalMilliseconds > 1000) { Invoke(() => FPSLabel.Text = string.Format("{0:F2} fps", _frameCount / (DateTime.Now - _lastFPSUpdate).TotalSeconds)); _lastFPSUpdate = DateTime.Now; _frameCount = 0; } }
private Bitmap HoughTransform(Bitmap originalImage) { var imgBitmap = new Bitmap(originalImage); var lineTransform = new Accord.Imaging.HoughLineTransformation(); // Convert it to binary and mark the possible lines // in white so it can be processed by the transform var sequence = new FiltersSequence( Grayscale.CommonAlgorithms.BT709, new NiblackThreshold(), new Invert() ); var contoursBitmap = ImageFilter.PrewittFilter(imgBitmap, true); // Apply the sequence of filters above: Bitmap binaryImage = sequence.Apply(contoursBitmap); lineTransform.ProcessImage(binaryImage); // Now, let's say we would like to retrieve the lines and use them // for further processing. First, the lines can be ordered by their // relative intensity using HoughLine[] lines = lineTransform.GetLinesByRelativeIntensity(1); // Then, let's plot them on top of the input image. Since we will // apply many operations to a single image, it is better to first // convert it to an UnmanagedImage object to avoid having to lock // the image into memory multiple times. UnmanagedImage unmanagedImage = UnmanagedImage.FromManagedImage(binaryImage); // Finally, plot them in order: foreach (HoughLine line in lines) { line.Draw(unmanagedImage, color: Color.Red); } return(unmanagedImage.ToManagedImage()); }
public ActionResult Upload(HttpPostedFileBase file) { string path = Server.MapPath("~/Areas/Admin/Files/" + file.FileName); string ext = System.IO.Path.GetExtension(file.FileName); string[] nameFielCV = file.FileName.Split('.'); if (String.Compare(ext.ToLower(), "pdf") == 0) { @ViewBag.Path = "Bạn chỉ có thể tải lên file PDF"; } else { file.SaveAs(path); @ViewBag.Path = path; //@ViewBag.Path = "Test"+ExtractTextFromPdf(path); Aspose.Pdf.Document pdfDoc = new Aspose.Pdf.Document(path); int pageNum = pdfDoc.Pages.Count; // Response.Write("Hello--" + pageNum); XImage[] image = new XImage[pageNum]; String NoiSoanCV, SoCV, TieuDeCV, NoiDungCV, NoiNhanCV, ChuKyCV, filePath; NoiSoanCV = SoCV = TieuDeCV = NoiDungCV = NoiNhanCV = ChuKyCV = filePath = ""; Color c; for (int i = 1; i <= pageNum; i++) { Bitmap bt; UnmanagedImage sourceimage; BitmapData cloneBitmapCopyC; filePath = rootPath + file.FileName + nameFielCV[0] + i + ".bmp"; if (!System.IO.File.Exists(filePath)) { using (FileStream imageStream = new FileStream(rootPath + nameFielCV[0] + i + ".bmp", FileMode.Create)) { // Create Resolution object Resolution resolution = new Resolution(300); // Create JPEG device with specified attributes (Width, Height, Resolution, Quality) // where Quality [0-100], 100 is Maximum JpegDevice jpegDevice = new JpegDevice(resolution, 100); // Convert a particular page and save the image to stream jpegDevice.Process(pdfDoc.Pages[i], imageStream); // Close stream imageStream.Close(); } // Thao tác với bt bt = (Bitmap)Bitmap.FromFile((rootPath + nameFielCV[0] + i + ".bmp")); bt.SetResolution(192f, 192f); cloneBitmapCopyC = bt.LockBits(new System.Drawing.Rectangle(0, 114, bt.Width, bt.Height - 114), System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); sourceimage = new UnmanagedImage(cloneBitmapCopyC); // chuyển thành ảnh cấp xám xong Nhi Phan (Đen thành đen , Trắng Thành Trắng) for (int m = 0; m < sourceimage.Width; m++) { for (int j = 0; j < sourceimage.Height; j++) { c = sourceimage.GetPixel(m, j); //c.R = 0; // màu đen int grayScale = (int)((c.R * 0.3) + (c.G * 0.59) + (c.B * 0.11)); sourceimage.SetPixel(m, j, Color.FromArgb(c.A, grayScale, grayScale, grayScale)); if (c.R < 140) { sourceimage.SetPixel(m, j, Color.Black); } else { sourceimage.SetPixel(m, j, Color.White); } } } bt = sourceimage.ToManagedImage(); bt = Crop(bt); filePath = rootPath + nameFielCV[0] + "output_grayCV" + i + ".bmp"; if (!System.IO.File.Exists(filePath)) { FileStream output_gray = new FileStream((rootPath + nameFielCV[0] + "output_grayCV" + i + ".bmp"), FileMode.Create); bt.Save(output_gray, System.Drawing.Imaging.ImageFormat.Jpeg); output_gray.Dispose(); output_gray.Close(); } } else { // Thao tác với bt bt = (Bitmap)Bitmap.FromFile((rootPath + nameFielCV[0] + "output_grayCV" + i + ".bmp")); cloneBitmapCopyC = bt.LockBits(new System.Drawing.Rectangle(0, 0, bt.Width, bt.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); sourceimage = new UnmanagedImage(cloneBitmapCopyC); } sourceimage = UnmanagedImage.FromManagedImage(bt); String nameFile = ""; if (i == 1) { int ht, hs; ht = hs = 0; //Cắt tiêu đề nameFile = nameFielCV[0] + "donviSoanCV"; Dictionary <string, int> dictionaryheightDV = CropImgae(cloneBitmapCopyC, bt, sourceimage, ht, nameFile); ht = dictionaryheightDV["height"] + (int)1.3 * dictionaryheightDV["heighd"]; Bitmap dvCV = (Bitmap)Bitmap.FromFile((rootPath + nameFile + ".bmp")); NoiSoanCV = GetText(dvCV); //cắt số công văn nameFile = nameFielCV[0] + "soCV"; Dictionary <string, int> dictionaryheightSoCV = CropImgae(cloneBitmapCopyC, bt, sourceimage, ht, nameFile); ht = ht + dictionaryheightSoCV["height"]; Bitmap soCV = (Bitmap)Bitmap.FromFile((rootPath + nameFile + ".bmp")); SoCV = GetText(soCV); //cắt tiêu đề công văn nameFile = nameFielCV[0] + "TieuDeCV"; Dictionary <string, int> dictionaryheightTieuDeCV = CropImgae(cloneBitmapCopyC, bt, sourceimage, ht, nameFile); Bitmap TieuDeCVBit = (Bitmap)Bitmap.FromFile((rootPath + nameFile + ".bmp")); ht = ht + dictionaryheightTieuDeCV["height"]; if (ht > 460) { ht = ht + (int)(2 * dictionaryheightTieuDeCV["heighd"]); } TieuDeCV = GetText(TieuDeCVBit); //cắt nội dung nameFile = nameFielCV[0] + "NoiDungCV"; BitmapData cloneBitmapNoiDungCV = bt.LockBits(new System.Drawing.Rectangle(0, ht, sourceimage.Width, sourceimage.Height - ht), System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); UnmanagedImage titileImage = new UnmanagedImage(cloneBitmapNoiDungCV); Bitmap imgTitile = titileImage.ToManagedImage(); filePath = rootPath + nameFile + ".bmp"; if (!System.IO.File.Exists(filePath)) { FileStream output_title = new FileStream((rootPath + nameFile + ".bmp"), FileMode.Create); imgTitile.Save(output_title, System.Drawing.Imaging.ImageFormat.Jpeg); output_title.Dispose(); output_title.Close(); } Bitmap NoiDungCVBit = (Bitmap)Bitmap.FromFile((rootPath + nameFile + ".bmp")); NoiDungCV = GetText(NoiDungCVBit); } if (i == pageNum) { //cắt nơi nhận, chữ ký bool kt = false; for (int k = (int)(0.98 * bt.Height); k < bt.Height; k++) { c = sourceimage.GetPixel((int)(0.5 * bt.Width), k); if (c.R == 0) { kt = true; break; } } if (kt) { BitmapData cloneBitmapPageEnd = bt.LockBits(new System.Drawing.Rectangle(0, 0, bt.Width, bt.Height - 114), System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); sourceimage = new UnmanagedImage(cloneBitmapPageEnd); bt = sourceimage.ToManagedImage(); bt = Crop(bt); nameFile = nameFielCV[0] + "PageEndCV"; filePath = rootPath + nameFile + ".bmp"; if (!System.IO.File.Exists(filePath)) { FileStream PageEndCV = new FileStream((rootPath + nameFile + ".bmp"), FileMode.Create); bt.Save(PageEndCV, System.Drawing.Imaging.ImageFormat.Jpeg); PageEndCV.Dispose(); PageEndCV.Close(); } sourceimage = UnmanagedImage.FromManagedImage(bt); //bt.UnlockBits(cloneBitmapPageEnd); } //cắt chữ ký nameFile = nameFielCV[0] + "ChuKyCV"; BitmapData cloneBitmapChuKyCV = bt.LockBits(new System.Drawing.Rectangle((int)(0.5 * sourceimage.Width), (int)(0.9 * sourceimage.Height), (int)(0.4 * sourceimage.Width), sourceimage.Height - (int)(0.9 * sourceimage.Height)), System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); UnmanagedImage ChuKyImage = new UnmanagedImage(cloneBitmapChuKyCV); Bitmap ChuKyTitile = ChuKyImage.ToManagedImage(); ChuKyTitile = Crop(ChuKyTitile); filePath = rootPath + nameFile + ".bmp"; if (!System.IO.File.Exists(filePath)) { FileStream output_ChuKy = new FileStream((rootPath + nameFile + ".bmp"), FileMode.Create, FileAccess.ReadWrite); ChuKyTitile.Save(output_ChuKy, System.Drawing.Imaging.ImageFormat.Jpeg); output_ChuKy.Dispose(); output_ChuKy.Close(); } bt.UnlockBits(cloneBitmapChuKyCV); Bitmap ChuKyCVBit = (Bitmap)Bitmap.FromFile((rootPath + nameFile + ".bmp")); ChuKyCV = GetText(ChuKyCVBit); // cắt nơi nhận nameFile = nameFielCV[0] + "NoiNhanCV"; BitmapData cloneBitmapNoiNhanCV; if (!kt) { cloneBitmapNoiNhanCV = bt.LockBits(new System.Drawing.Rectangle(0, (int)(0.8 * (sourceimage.Height)), (int)(0.5 * sourceimage.Width), sourceimage.Height - (int)(0.8 * sourceimage.Height) - 50), System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); } else { cloneBitmapNoiNhanCV = bt.LockBits(new System.Drawing.Rectangle(0, (int)(0.6 * (sourceimage.Height - 10)), (int)(0.5 * sourceimage.Width), sourceimage.Height - (int)(0.6 * sourceimage.Height) - 50), System.Drawing.Imaging.ImageLockMode.ReadWrite, bt.PixelFormat); } UnmanagedImage NoiNhanImage = new UnmanagedImage(cloneBitmapNoiNhanCV); Bitmap NoiNhanTitile = NoiNhanImage.ToManagedImage(); NoiNhanTitile = Crop(NoiNhanTitile); filePath = rootPath + nameFile + ".bmp"; if (!System.IO.File.Exists(filePath)) { FileStream output_NoiNhan = new FileStream((rootPath + nameFile + ".bmp"), FileMode.Create); NoiNhanTitile.Save(output_NoiNhan, System.Drawing.Imaging.ImageFormat.Jpeg); output_NoiNhan.Dispose(); output_NoiNhan.Close(); } Bitmap NoiNhanCVBit = (Bitmap)Bitmap.FromFile((rootPath + nameFile + ".bmp")); NoiNhanCV = GetText(NoiNhanCVBit); bt.UnlockBits(cloneBitmapChuKyCV); } } //Lưu thông tin vừa cắt vào cơ sở dữ liệu row data = new row(); //NoiSoanCV, SoCV, TieuDeCV, NoiDungCV, NoiNhanCV, ChuKyCV; data.from_org = NoiSoanCV; data.number_dispatch = SoCV; data.title = TieuDeCV; data._abstract = NoiDungCV; data.to_org = NoiNhanCV; data.name_signer = ChuKyCV; data.attach_file = file.FileName; try { db.rows.Add(data); db.SaveChanges(); return(RedirectToAction("List", "CongVan")); } catch (Exception e) { string x = e.InnerException.ToString(); return(RedirectToAction("Index")); } } return(View()); }
/// <summary> /// Looks for the brightest pixel after applying a redness filter. Narrows search first using a resampled copy of the image to eliminate edge dots. /// Expects an image that is already cropped to the interested area for faster processing. /// </summary> /// <param name="img"></param> /// <param name="mouse"></param> /// <param name="maxDistanceFromMouse"></param> /// <returns></returns> public unsafe Point FindMaxPixel(UnmanagedImage img, PointF mouse, float maxDistanceFromMouse) { int width = 15; int height = (int)Math.Ceiling((double)img.Height / (double)img.Width * width); if (width <= img.Width && height <= img.Height + 1) { width = img.Width; height = img.Height; } double scale = (double)img.Width / (double)width; UnmanagedImage lowRed = null; try { if (width != img.Width && height != img.Height) { using (Bitmap reduced = new Bitmap(width, height, PixelFormat.Format24bppRgb)) using (Graphics g = Graphics.FromImage(reduced)) using (ImageAttributes ia = new ImageAttributes()) { g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy; g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; ia.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY); g.DrawImage(img.ToManagedImage(false), new Rectangle(0, 0, width, height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia); //TODO: Not sure if ToManagedImage will stick around after the underlying image is disposed. I know that the bitmap data will be gone, guess that's most of it. using (UnmanagedImage rui = UnmanagedImage.FromManagedImage(reduced)) { lowRed = new RedEyeFilter(2).Apply(rui); // Make an copy using the red eye filter } } } else { //Don't resample unless needed lowRed = new RedEyeFilter(2).Apply(img); } Point max = GetMax(lowRed, new PointF(mouse.X / (float)scale, mouse.Y / (float)scale), maxDistanceFromMouse / scale); //We weren't scaling things? OK, cool... if (scale == 0) { return(max); } //Otherwise, let's get the unscaled pixel. //Calculate the rectangle surrounding the selected pixel, but in source coordinates. int tinySize = (int)Math.Ceiling(scale) + 1; Rectangle tinyArea = new Rectangle((int)Math.Floor(scale * (double)max.X), (int)Math.Floor(scale * (double)max.Y), tinySize, tinySize); if (tinyArea.Right >= img.Width) { tinyArea.Width -= img.Width - tinyArea.Right + 1; } if (tinyArea.Bottom >= img.Height) { tinyArea.Height -= img.Height - tinyArea.Bottom + 1; } //Filter it and look using (UnmanagedImage tiny = new Crop(tinyArea).Apply(img)) { using (UnmanagedImage tinyRed = new RedEyeFilter(2).Apply(tiny)) { max = GetMax(tinyRed); max.X += tinyArea.X; max.Y += tinyArea.Y; } } return(max); } finally { if (lowRed != null) { lowRed.Dispose(); } } }
/// <summary> /// Performs object detection on the given frame. /// </summary> /// public Rectangle[] ProcessFrame(Bitmap frame) { return(ProcessFrame(UnmanagedImage.FromManagedImage(frame))); }
private async Task CalibThread(FrameReadyEventArgs e) { Debug.WriteLine("Calibrating " + _calibrationStep); e.Frame.Bitmap.Save(@"C:\temp\aforge\src\img" + _calibrationStep + ".jpg"); //var stats = new ImageStatistics(e.Frame.Bitmap); // //var histogram = stats.Gray; ////Debug.WriteLine("Grey: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean + //// " Dev: " + histogram.StdDev + " Median: " + histogram.Median); //var histogram = stats.Green; //Debug.WriteLine("Green: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean + // " Dev: " + histogram.StdDev + " Median: " + histogram.Median); //histogram = stats.Blue; //Debug.WriteLine("Blue: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean + // " Dev: " + histogram.StdDev + " Median: " + histogram.Median); //histogram = stats.Red; //Debug.WriteLine("Red: Min: " + histogram.Min + " Max: " + histogram.Max + " Mean: " + histogram.Mean + //e.Frame.Bitmap.Save(@"C:\temp\aforge\src\img" + _calibrationStep + ".jpg"); // " Dev: " + histogram.StdDev + " Median: " + histogram.Median); //e.NewImage.Save(@"C:\temp\aforge\src\img" + _calibrationStep + ".jpg"); if (_errors > 100) { //calibration not possible return; } if (_calibrationStep == 3) { _cc.FrameReady -= BaseCalibration; // TODO //Grid.Calculate(); _vs.Close(); CalibrationCompleted(this, new EventArgs()); } else { //var diffBitmap = new Bitmap(1,1); //if(diffFilter.OverlayImage != null) // diffBitmap = diffFilter.Apply(e.NewImage); //diffBitmap.Save(@"C:\temp\aforge\diff\img" + _calibrationStep + ".jpg"); if (_calibrationStep > 2) { _vs.Clear(); FillRects(); //var gf = new ColorFiltering(new IntRange(0, 255), //(int) stats.Red.Mean), // new IntRange((int) (stats.Green.Mean + ColorDiff), 255), // new IntRange(0, 255));//(int) stats.Blue.Mean)); //var bf = new ColorFiltering(new IntRange(0, 255),//(int)stats.Red.Mean), // new IntRange(0, 255),//(int) stats.Green.Mean), // new IntRange((int) stats.Blue.Mean + ColorDiff, 255)); // create color Channel images var gbm = PartiallyApplyAvgFilter(e.Frame.Bitmap, Channels.Green, 8, 8, 8); gbm.Save(@"C:\temp\aforge\gimg\img" + _calibrationStep + ".jpg"); var bbm = PartiallyApplyAvgFilter(e.Frame.Bitmap, Channels.Blue, 8, 8, 8); bbm.Save(@"C:\temp\aforge\bimg\img" + _calibrationStep + ".jpg"); var gblobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.YX, MaxHeight = 30, MinHeight = 15, MaxWidth = 30, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false }; gblobCounter.ProcessImage(gbm); var bblobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.YX, MaxHeight = 30, MinHeight = 15, MaxWidth = 30, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false }; bblobCounter.ProcessImage(bbm); ProcessBlobs(gblobCounter, 0); ProcessBlobs(bblobCounter, 1); _calibrationStep++; } else { switch (_calibrationStep) { case 2: // identify screen bounds //var thresholdFilter = new Threshold(50); //thresholdFilter.ApplyInPlace(diffBitmap); //var cf = new ColorFiltering(new IntRange(0, 255), //red is bad // new IntRange((int) stats.Green.Mean + MeanDiff, 255), // new IntRange((int) stats.Blue.Mean + MeanDiff, 255)); //var bm = cf.Apply(e.NewImage); //var bm = PartiallyApplyAvgFilter(e.Frame.Bitmap, Channels.GreenAndBlue, 2, 2, MeanDiff); var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap); bm = diffFilter.Apply(bm); var gf = new GaussianBlur(9.0, 3); gf.ApplyInPlace(bm); var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255), new IntRange(20, 255)); cf.ApplyInPlace(bm); var blobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true }; blobCounter.ProcessImage(bm); bm.ToManagedImage().Save(@"C:\temp\aforge\diff.jpg"); var blobs = blobCounter.GetObjectsInformation(); int i = 0; List <IntPoint> corners; do { corners = PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++])); } while (corners.Count != 4); InPlaceSort(corners); Grid.TopLeft = new Point(corners[0].X, corners[0].Y); Grid.TopRight = new Point(corners[1].X, corners[1].Y); Grid.BottomLeft = new Point(corners[2].X, corners[2].Y); Grid.BottomRight = new Point(corners[3].X, corners[3].Y); if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 10 && Grid.BottomLeft.X > 10 && Grid.BottomRight.X < _vs.Width - 10 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 10 && Grid.BottomLeft.Y < _vs.Height - 10 && Grid.BottomRight.Y < _vs.Height - 10 && Grid.TopLeft.X < Grid.BottomRight.X && blobs[i - 1].Area > 60000 && Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X && Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y && Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y) { _calibrationStep++; _vs.Clear(); FillRects(); Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y)); Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y)); Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y)); Grid.AddPoint(new Point(_vs.Width, _vs.Height), new Point(corners[3].X, corners[3].Y)); } else { _calibrationStep = 0; _errors++; } break; case 1: diffFilter.OverlayImage = e.Frame.Bitmap; //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255)); _vs.Clear(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; case 0: Grid = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height); var thread = new Thread(() => { _vs.Show(); _vs.AddRect(0, 0, (int)_vs.Width, (int)_vs.Height, Color.FromArgb(255, 0, 0, 0)); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; } } } Debug.WriteLine("Releasing"); await Task.Delay(100); _sem.Release(); }
/// <summary> /// /// </summary> /// <param name="imageData"></param> /// <returns></returns> public virtual Bitmap Apply(BitmapData imageData) { return(Apply(UnmanagedImage.FromManagedImage(imageData)).ToManagedImage()); }
// On new frame private void VideoNewFrame(object sender, NewFrameEventArgs e) { var tsBrush = new SolidBrush(iSpyServer.Default.TimestampColor); var f = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular, GraphicsUnit.Pixel); var sbTs = new SolidBrush(Color.FromArgb(128, 0, 0, 0)); Bitmap bmOrig = null, bmp = null; Graphics g = null, gCam = null; try { if (e.Frame != null) { _width = e.Frame.Width; _height = e.Frame.Height; lock (this) { if (LastFrameUnmanaged != null) { LastFrameUnmanaged.Dispose(); } if (CW.Camobject.settings.resize && (CW.Camobject.settings.desktopresizewidth != e.Frame.Width || CW.Camobject.settings.desktopresizeheight != e.Frame.Height)) { var result = new Bitmap(CW.Camobject.settings.desktopresizewidth, CW.Camobject.settings.desktopresizeheight, PixelFormat.Format24bppRgb); using (Graphics g2 = Graphics.FromImage(result)) g2.DrawImage(e.Frame, 0, 0, result.Width, result.Height); e.Frame.Dispose(); bmOrig = result; } else { bmOrig = e.Frame; } if (CW.Camobject.flipx) { bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipX); } if (CW.Camobject.flipy) { bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipY); } if (Mask != null) { g = Graphics.FromImage(bmOrig); g.DrawImage(Mask, 0, 0, _width, _height); } LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmOrig); if (CW.Camobject.settings.timestamplocation != 0 && CW.Camobject.settings.timestampformatter != "") { bmp = LastFrameUnmanaged.ToManagedImage(); gCam = Graphics.FromImage(bmp); string timestamp = String.Format( CW.Camobject.settings.timestampformatter.Replace("{FPS}", string.Format("{0:F2}", CW.Framerate)), DateTime.Now); Size rs = gCam.MeasureString(timestamp, f).ToSize(); var p = new Point(0, 0); switch (CW.Camobject.settings.timestamplocation) { case 2: p.X = _width / 2 - (rs.Width / 2); break; case 3: p.X = _width - rs.Width; break; case 4: p.Y = _height - rs.Height; break; case 5: p.Y = _height - rs.Height; p.X = _width / 2 - (rs.Width / 2); break; case 6: p.Y = _height - rs.Height; p.X = _width - rs.Width; break; } var rect = new Rectangle(p, rs); gCam.FillRectangle(sbTs, rect); gCam.DrawString(timestamp, f, tsBrush, p); LastFrameUnmanaged.Dispose(); LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmp); } } } } catch (UnsupportedImageFormatException ex) { CW.VideoSourceErrorState = true; CW.VideoSourceErrorMessage = ex.Message; if (LastFrameUnmanaged != null) { try { lock (this) { LastFrameUnmanaged.Dispose(); LastFrameUnmanaged = null; } } catch { } } } catch (Exception ex) { MainForm.LogExceptionToFile(ex); } if (gCam != null) { gCam.Dispose(); } if (bmp != null) { bmp.Dispose(); } if (g != null) { g.Dispose(); } if (bmOrig != null) { bmOrig.Dispose(); } tsBrush.Dispose(); f.Dispose(); sbTs.Dispose(); if (NewFrame != null && LastFrameUnmanaged != null) { LastFrameNull = false; NewFrame(this, new EventArgs()); } }
private async Task CalibThread(FrameReadyEventArgs e) { try { if (_errors > MaxErrorCount) { _cc.FrameReady -= BaseCalibration; _vs.Close(); Console.WriteLine("Calibration impossible"); } if (_calibrationStep == CalibrationFrames) { _cc.FrameReady -= BaseCalibration; _vs.Close(); Console.WriteLine("Calibration complete"); CalibrationCompleted(this, new EventArgs()); } else { switch (_calibrationStep) { // get the corners from the difference image case 2: var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap); bm = _diffFilter.Apply(bm); var gf = new GaussianBlur(9.0, 3); gf.ApplyInPlace(bm); var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255), new IntRange(20, 255)); cf.ApplyInPlace(bm); var blobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true }; blobCounter.ProcessImage(bm); var blobs = blobCounter.GetObjectsInformation(); if (blobs.Any()) { int i = 0; List <IntPoint> corners; do { corners = PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++])); } while (corners.Count != 4); InPlaceSort(corners); Grid.TopLeft = new Point(corners[0].X, corners[0].Y); Grid.TopRight = new Point(corners[1].X, corners[1].Y); Grid.BottomLeft = new Point(corners[2].X, corners[2].Y); Grid.BottomRight = new Point(corners[3].X, corners[3].Y); if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 && Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 && Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 && Grid.TopLeft.X < Grid.BottomRight.X && //blobs[i - 1].Area > 60000 && Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X && Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y && Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y) { _calibrationStep++; _vs.Clear(); Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y)); Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y)); Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y)); Grid.AddPoint(new Point(_vs.Width, _vs.Height), new Point(corners[3].X, corners[3].Y)); } else { _calibrationStep = 0; _errors++; } } else { _calibrationStep = 0; _errors++; _vs.Draw(); } break; case 1: // draw second image, store the first _diffFilter.OverlayImage = e.Frame.Bitmap; _vs.Clear(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; //draw the first image case 0: Grid = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height) { ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height) }; var thread = new Thread(() => { _vs.Show(); _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0)); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; } } await Task.Delay(MillisecondsDelay); } finally { _sem.Release(); } }
private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image) { Invert inv = new Invert(); inv.ApplyInPlace(image); UnmanagedImage ui = UnmanagedImage.FromManagedImage(image); pictureBox1.Image = image; if (controller.Tracker.TrackingObject == null) { return; } if (controller.Tracker.TrackingObject.IsEmpty) { return; } var rect = controller.Tracker.TrackingObject.Rectangle; Crop crop = new Crop(rect); UnmanagedImage head = crop.Apply(ui); var points = new List <IntPoint>() { new IntPoint(head.Width / 2, head.Height / 2) }; var pps = head.Collect16bppPixelValues(points); double mean = Accord.Statistics.Tools.Mean(pps); double cutoff = mean + 15; Threshold t = new Threshold((int)cutoff); var mask = t.Apply(ui); LevelsLinear16bpp levels = new LevelsLinear16bpp(); levels.InGray = new IntRange((int)cutoff, 65535); levels.OutGray = new IntRange(0, 65535); levels.ApplyInPlace(ui); var mask8bit = AForge.Imaging.Image.Convert16bppTo8bpp(mask.ToManagedImage()); BlobCounter bc = new BlobCounter(); bc.ObjectsOrder = ObjectsOrder.Area; bc.ProcessImage(mask8bit); var blobs = bc.GetObjectsInformation(); inv.ApplyInPlace(image); Intersect intersect = new Intersect(); intersect.UnmanagedOverlayImage = mask; mask = intersect.Apply(ui); List <Rectangle> rects = new List <Rectangle>(); // Extract the uppermost largest blobs. for (int i = 0; i < blobs.Length; i++) { double dx = (blobs[i].Rectangle.Top - controller.Tracker.TrackingObject.Center.Y); double d = (dx * dx) / controller.Tracker.TrackingObject.Area; if (d < 2 && blobs[i].Area > 1000) { rects.Add(blobs[i].Rectangle); } } rects.Sort(compare); if (rects.Count > 0) { captureHand(mask, rects[0], pbLeftArm, pbLeftHand); } if (rects.Count > 1) { captureHand(mask, rects[1], pbRightArm, pbRightHand); } RectanglesMarker marker = new RectanglesMarker(rects); marker.MarkerColor = Color.White; marker.ApplyInPlace(mask8bit); image = mask.ToManagedImage(); }
unsafe static void Main() { Test test = new Test(); test.TestRunningWeightedVariance(); return; var resourceDir = Path.Combine(Directory.GetParent(Directory.GetCurrentDirectory()).FullName, "Resources"); var imgColor = Bitmap.FromFile(Path.Combine(resourceDir, "testColorBig.jpg")).ToImage <Bgr, byte>(); imgColor = imgColor.CorrectContrast(105); /*var bmp1 = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("nature-spring.jpg"); * var image1 = bmp1.ToImage<Gray, float>(); * * var res1 = ResizeNearsetNeighbur.Resize(image1, new Size(640, 480)); * ImageBox.Show("Interpolated image", res1.ToBitmap()); * * var res = new Image<Bgr, float>(320, 200); * image1.GetRectSubPix(new PointF(1.9f, 1.9f), res); * ImageBox.Show("Interpolated image", res.ToBitmap());*/ test.TestLKFlow(); return; //uncomment if you want execute functions below var bmp = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("nature-spring.jpg"); var image = bmp.ToImage <Bgr, byte>(); UnmanagedImage uIm = UnmanagedImage.FromManagedImage(bmp); /********************** Bitmap <-> Image<,> *************************/ //from Bitmap... IImage bmpImg1 = bmp.ToImage(); //generic image (dest format depends on bmp pixel format) (this case: <Color3, byte>) Image <Bgr, byte> bmpImg2 = bmp.ToImage <Bgr, byte>(); //in this case additional cast is performed (<Color3, byte> => <Bgr, byte>) (no data convert) //to Bitmap... Bitmap bmpFromImg = bmpImg2.ToBitmap(copyAlways: false /*do not copy if you do not have to*/); //<Bgr, byte> can be casted to <Color3, byte> therefore data is shared between Bitmap and bmPimg2 /********************** UnmanagedImage <-> Image<,> *************************/ //from UnmanagedImage... var im1FromUIm = uIm.AsImage(); //generic image (dest format depends on bmp pixel format) var im2FromUIm = uIm.ToImage <Bgr, byte>(); //in this case additional cast is performed (<Color3, byte> => <Bgr, byte>) (no data convert) //to UnmanagedImage... var uIm2 = im1FromUIm.ToAForgeImage(copyAlways: false, failIfCannotCast: false); /******************* some AForge filter recreation... ***********************/ /********************** Array <-> Image<,> (also eliminates need for Matrix, UnmanagedImage converters) ********************************/ int[,] arr = new int[480, 640]; //from Array... var image1FromArray = arr.ToImage(); //supported for all 2D/3D arrays var castedImageFromArray = arr.AsImage(); //supported only on 2D arrays (data is shared) //to Array ... var arrFromIm = image1FromArray.ToArray(); //output is 2D or 3D array (see function overloads) /**************** channel rotate *******************/ //Image<,> => flexible var channels = image.SplitChannels(); var dest = new Image <Bgr, byte>(new Image <Gray, byte>[] { channels[1], channels[0], channels[2] }); //AForge AForge.Imaging.Filters.RotateChannels rc = new AForge.Imaging.Filters.RotateChannels(); rc.Apply(uIm); /**************** channel extract *******************/ //Image<,> => simple var ch = image[0]; //AForge AForge.Imaging.Filters.ExtractChannel ec = new AForge.Imaging.Filters.ExtractChannel(0); ec.Apply(uIm); /**************** Max (see Min also) *******************/ //Image<,> image.Max(image, inPlace: true); //AForge AForge.Imaging.Filters.Merge m = new AForge.Imaging.Filters.Merge(uIm); m.Apply(uIm); /**************** Sobel *******************/ var bmpSquareGray = (System.Drawing.Bitmap)System.Drawing.Bitmap.FromFile("square.bmp"); var grayIm = bmpSquareGray.ToImage <Gray, float>(); //currently there are no overloads for magnitude for <byte>, will be fixed later //Image<,> => flexible var sobelX = grayIm.Sobel(1, 0, 3); var sobelY = grayIm.Sobel(0, 1, 3); var mag = sobelX.Magnitude(sobelY); //should use Threshold for values > 255 (not implemented yet) //mag.ToBitmap().Save("bla.bmp"); //img.Save(..) is available for <IColor3, byte> and <Gray, byte> (Bitmap compatible formats) should change ? //var mag = sobelX.Abs().Add(sobelY.Abs()).Scale(0, 255).Convert<Gray, byte>(); //should work later (it is not implemented) //AForge AForge.Imaging.Filters.SobelEdgeDetector sobel = new AForge.Imaging.Filters.SobelEdgeDetector(); var destSobel = sobel.Apply(grayIm.ToAForgeImage()); //destSobel.ToManagedImage().Save("sobelAForge.bmp"); return; }
/// <summary> /// /// </summary> /// <param name="image"></param> /// <returns></returns> public virtual UnmanagedImage Apply(UnmanagedImage image) { return(UnmanagedImage.FromManagedImage(Apply(image.ToManagedImage()))); }
private void VideoNewFrame(object sender, NewFrameEventArgs e) { if (_requestedToStop || NewFrame == null) { return; } if (_lastframeEvent > DateTime.MinValue) { if ((DateTime.Now < _nextFrameTarget)) { return; } double dMin = Mininterval; _nextFrameTarget = _nextFrameTarget.AddMilliseconds(dMin); if (_nextFrameTarget < DateTime.Now) { _nextFrameTarget = DateTime.Now.AddMilliseconds(dMin); } TimeSpan tsFr = DateTime.Now - _lastframeProcessed; _framerates.Enqueue(1000d / tsFr.TotalMilliseconds); if (_framerates.Count >= 30) { _framerates.Dequeue(); } Framerate = _framerates.Average(); } else { _lastframeEvent = DateTime.Now; } lock (_sync) { _lastframeProcessed = DateTime.Now; var tsBrush = new SolidBrush(MainForm.Conf.TimestampColor.ToColor()); var sbTs = new SolidBrush(Color.FromArgb(128, 0, 0, 0)); Bitmap bmOrig = null, bmp = null; Graphics g = null, gCam = null; bool err = false; try { // motionLevel = 0; if (e.Frame != null) { if (LastFrameUnmanaged != null) { LastFrameUnmanaged.Dispose(); } //resize? if (CW.Camobject.settings.resize && (CW.Camobject.settings.desktopresizewidth != e.Frame.Width || CW.Camobject.settings.desktopresizeheight != e.Frame.Height)) { var result = new Bitmap(CW.Camobject.settings.desktopresizewidth, CW.Camobject.settings.desktopresizeheight, PixelFormat.Format24bppRgb); using (Graphics g2 = Graphics.FromImage(result)) { g2.CompositingMode = CompositingMode.SourceCopy; g2.CompositingQuality = CompositingQuality.HighSpeed; g2.PixelOffsetMode = PixelOffsetMode.Half; g2.SmoothingMode = SmoothingMode.None; g2.InterpolationMode = InterpolationMode.Default; g2.DrawImage(e.Frame, 0, 0, result.Width, result.Height); } e.Frame.Dispose(); bmOrig = result; } else { bmOrig = e.Frame; } if (CW.Camobject.rotate90) { bmOrig.RotateFlip(RotateFlipType.Rotate90FlipNone); } if (CW.Camobject.flipx) { bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipX); } if (CW.Camobject.flipy) { bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipY); } _width = bmOrig.Width; _height = bmOrig.Height; if (CW.NeedMotionZones) { CW.NeedMotionZones = !SetMotionZones(CW.Camobject.detector.motionzones); } if (Mask != null) { g = Graphics.FromImage(bmOrig); g.DrawImage(Mask, 0, 0, _width, _height); } if (Plugin != null) { bool runplugin = true; if (CW.Camobject.alerts.processmode == "motion") { //run plugin if motion detected in last 5 seconds runplugin = _motionlastdetected > DateTime.Now.AddSeconds(-5); } if (runplugin) { bmOrig = (Bitmap) Plugin.GetType().GetMethod("ProcessFrame").Invoke(Plugin, new object[] { bmOrig }); var pluginAlert = (String)Plugin.GetType().GetField("Alert").GetValue(Plugin); if (pluginAlert != "") { Alarm(pluginAlert, EventArgs.Empty); } } } LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmOrig); if (_motionDetector != null) { if (Alarm != null) { _processFrameCount++; if (_processFrameCount >= CW.Camobject.detector.processeveryframe || CW.Calibrating) { _processFrameCount = 0; MotionLevel = _motionDetector.ProcessFrame(LastFrameUnmanaged, Filter); if (MotionLevel >= _alarmLevel) { if (MotionLevel <= _alarmLevelMax || _alarmLevelMax >= 0.1) { MotionDetected = true; _motionlastdetected = DateTime.Now; Alarm(this, new EventArgs()); } } else { MotionDetected = false; } } } else { MotionDetected = false; } } else { MotionDetected = false; } if (ZFactor > 1) { var f1 = new ResizeNearestNeighbor(LastFrameUnmanaged.Width, LastFrameUnmanaged.Height); var f2 = new Crop(ViewRectangle); LastFrameUnmanaged = f2.Apply(LastFrameUnmanaged); LastFrameUnmanaged = f1.Apply(LastFrameUnmanaged); } if (CW.Camobject.settings.timestamplocation != 0 && CW.Camobject.settings.timestampformatter != "") { bmp = LastFrameUnmanaged.ToManagedImage(); gCam = Graphics.FromImage(bmp); var ts = CW.Camobject.settings.timestampformatter.Replace("{FPS}", string.Format("{0:F2}", Framerate)); ts = ts.Replace("{CAMERA}", CW.Camobject.name); ts = ts.Replace("{REC}", CW.Recording ? "REC" : ""); var timestamp = "Invalid Timestamp"; try { timestamp = String.Format(ts, DateTime.Now.AddHours( Convert.ToDouble(CW.Camobject.settings.timestampoffset))).Trim(); } catch { } var rs = gCam.MeasureString(timestamp, Drawfont).ToSize(); var p = new Point(0, 0); switch (CW.Camobject.settings.timestamplocation) { case 2: p.X = _width / 2 - (rs.Width / 2); break; case 3: p.X = _width - rs.Width; break; case 4: p.Y = _height - rs.Height; break; case 5: p.Y = _height - rs.Height; p.X = _width / 2 - (rs.Width / 2); break; case 6: p.Y = _height - rs.Height; p.X = _width - rs.Width; break; } var rect = new Rectangle(p, rs); gCam.FillRectangle(sbTs, rect); gCam.DrawString(timestamp, Drawfont, tsBrush, p); LastFrameUnmanaged.Dispose(); LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmp); } } } catch (UnsupportedImageFormatException ex) { CW.VideoSourceErrorState = true; CW.VideoSourceErrorMessage = ex.Message; if (LastFrameUnmanaged != null) { try { lock (_sync) { LastFrameUnmanaged.Dispose(); LastFrameUnmanaged = null; } } catch { } } err = true; } catch (Exception ex) { if (LastFrameUnmanaged != null) { try { lock (_sync) { LastFrameUnmanaged.Dispose(); LastFrameUnmanaged = null; } } catch { } } Log.Error("", ex);//Log.Error("",ex);//MainForm.LogExceptionToFile(ex); err = true; } if (gCam != null) { gCam.Dispose(); } if (bmp != null) { bmp.Dispose(); } if (g != null) { g.Dispose(); } if (bmOrig != null) { bmOrig.Dispose(); } tsBrush.Dispose(); sbTs.Dispose(); if (err) { return; } if (MotionDetector != null && !CW.Calibrating && MotionDetector.MotionProcessingAlgorithm is BlobCountingObjectsProcessing) { try { var blobcounter = (BlobCountingObjectsProcessing)MotionDetector.MotionProcessingAlgorithm; //tracking var pCenter = new Point(Width / 2, Height / 2); if (!CW.PTZNavigate && CW.Camobject.settings.ptzautotrack && blobcounter.ObjectsCount > 0 && blobcounter.ObjectsCount < 4 && !CW.Ptzneedsstop) { List <Rectangle> recs = blobcounter.ObjectRectangles.OrderByDescending(p => p.Width * p.Height).ToList(); Rectangle rec = recs.First(); //get center point var prec = new Point(rec.X + rec.Width / 2, rec.Y + rec.Height / 2); double dratiomin = 0.6; prec.X = prec.X - pCenter.X; prec.Y = prec.Y - pCenter.Y; if (CW.Camobject.settings.ptzautotrackmode == 1) //vert only { prec.X = 0; dratiomin = 0.3; } if (CW.Camobject.settings.ptzautotrackmode == 2) //horiz only { prec.Y = 0; dratiomin = 0.3; } double angle = Math.Atan2(-prec.Y, -prec.X); if (CW.Camobject.settings.ptzautotrackreverse) { angle = angle - Math.PI; if (angle < 0 - Math.PI) { angle += 2 * Math.PI; } } double dist = Math.Sqrt(Math.Pow(prec.X, 2.0d) + Math.Pow(prec.Y, 2.0d)); double maxdist = Math.Sqrt(Math.Pow(Width / 2, 2.0d) + Math.Pow(Height / 2, 2.0d)); double dratio = dist / maxdist; if (dratio > dratiomin) { CW.PTZ.SendPTZDirection(angle, 1); CW.LastAutoTrackSent = DateTime.Now; CW.Ptzneedsstop = true; } } } catch (Exception ex) { Log.Error("", ex);//Log.Error("",ex);//MainForm.LogExceptionToFile(ex); } } if (NewFrame != null && LastFrameUnmanaged != null) { LastFrameNull = false; NewFrame(this, new EventArgs()); } } }
private async Task CalibThread(FrameReadyEventArgs e) { //Debug.WriteLine("Calibrating " + _calibrationStep + " " + _drawing); //e.Frame.Bitmap.Save(@"C:\temp\daforge\src\img" + (_calibrationStep<10?"0":"") + _calibrationStep + "-" + (_drawing?"1":"0") + "-" + _errors + ".jpg", ImageFormat.Jpeg); if (_errors > 100) { //calibration not possible return; } if (_calibrationStep == CalibrationFrames) { _cc.FrameReady -= BaseCalibration; // TODO //Grid.Calculate(); //Grid.PredictFromCorners(); _vs.Close(); Console.WriteLine("Calibration complete"); CalibrationCompleted(this, new EventArgs()); } else { switch (_calibrationStep) { // get the corners from the difference image case 2: var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap); //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\src" + _errors + ".jpg", ImageFormat.Jpeg); bm = diffFilter.Apply(bm); var gf = new GaussianBlur(9.0, 3); gf.ApplyInPlace(bm); var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255), new IntRange(20, 255)); cf.ApplyInPlace(bm); var blobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true }; blobCounter.ProcessImage(bm); //bm.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.Jpeg); var blobs = blobCounter.GetObjectsInformation(); if (blobs.Any()) { int i = 0; List <IntPoint> corners; do { corners = PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++])); } while (corners.Count != 4); RecursiveAForgeCalibrator.GridBlobs.InPlaceSort(corners); Grid.TopLeft = new Point(corners[0].X, corners[0].Y); Grid.TopRight = new Point(corners[1].X, corners[1].Y); Grid.BottomLeft = new Point(corners[2].X, corners[2].Y); Grid.BottomRight = new Point(corners[3].X, corners[3].Y); if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 && Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 && Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 && Grid.TopLeft.X < Grid.BottomRight.X && //blobs[i - 1].Area > 60000 && Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X && Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y && Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y) { _calibrationStep++; _vs.Clear(); Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y)); Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y)); Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y)); Grid.AddPoint(new Point(_vs.Width, _vs.Height), new Point(corners[3].X, corners[3].Y)); } else { _calibrationStep = 0; _errors++; } } else { _calibrationStep = 0; _errors++; _vs.Draw(); } break; case 1: // draw second image, store the first diffFilter.OverlayImage = e.Frame.Bitmap; //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\srcf" + _errors + ".jpg", ImageFormat.Jpeg); //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255)); _vs.Clear(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; //draw the first image case 0: Grid = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height); Grid.ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height); var thread = new Thread(() => { _vs.Show(); _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0)); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; } } Debug.WriteLine("Releasing"); await Task.Delay(500); _sem.Release(); }
private async Task CalibThread(FrameReadyEventArgs e) { Debug.WriteLine("Calibrating " + _calibrationStep + " " + _drawing); e.Frame.Bitmap.Save(@"C:\temp\daforge\src\img" + (_calibrationStep < 10?"0":"") + _calibrationStep + "-" + (_drawing?"1":"0") + "-" + _errors + ".jpg", ImageFormat.Jpeg); if (_errors > 100) { //calibration not possible return; } if (_calibrationStep == CalibrationFrames && !_drawing) { _cc.FrameReady -= BaseCalibration; // TODO //Grid.Calculate(); _vs.Close(); CalibrationCompleted(this, new EventArgs()); } else { if (_calibrationStep > 2) { if (_drawing) { // draw diffimage _drawing = false; _vs.Clear(); FillRects(); diffFilter.OverlayImage = e.Frame.Bitmap; } else { // analyse diffimage _calibrationStep++; _drawing = true; _vs.Clear(); FillRects(); _calibrationStep--; var gbm = diffFilter.Apply(e.Frame.Bitmap); gbm.Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg); #if DEBUG actImg = (Bitmap)gbm.Clone(); #endif var d = UnmanagedImage.FromManagedImage(gbm); var stats = new ImageStatistics(d); var gcf = new ColorFiltering(new IntRange(0, 255), new IntRange((int)(stats.GreenWithoutBlack.Mean + stats.GreenWithoutBlack.StdDev + 5), 255), new IntRange(0, 255)); var bcf = new ColorFiltering(new IntRange(0, 255), new IntRange(0, 255), new IntRange((int)(stats.BlueWithoutBlack.Mean + stats.BlueWithoutBlack.StdDev), 255)); //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Median + " Blue: " + stats.BlueWithoutBlack.Median); //Debug.WriteLine("Green: " + stats.GreenWithoutBlack.Mean + " Blue: " + stats.BlueWithoutBlack.Mean); var bf = new Difference(gcf.Apply(d)); bcf.ApplyInPlace(d); bf.ApplyInPlace(d); d.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg); stats = new ImageStatistics(d); gcf = new ColorFiltering(new IntRange(0, 255), new IntRange((int)stats.GreenWithoutBlack.Mean, 255), new IntRange(0, 255)); bcf = new ColorFiltering(new IntRange(0, 255), new IntRange(0, 255), new IntRange((int)stats.BlueWithoutBlack.Mean, 255)); // split channels var bbm = bcf.Apply(d); bbm.ToManagedImage().Save(@"C:\temp\daforge\bimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp); gcf.ApplyInPlace(d); d.ToManagedImage().Save(@"C:\temp\daforge\gimg\img" + _calibrationStep + ".jpg", ImageFormat.Bmp); var gblobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.YX, MaxHeight = 60, MinHeight = 15, MaxWidth = 60, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false }; gblobCounter.ProcessImage(d); var bblobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.YX, MaxHeight = 60, MinHeight = 15, MaxWidth = 60, MinWidth = 15, FilterBlobs = true, CoupledSizeFiltering = false }; bblobCounter.ProcessImage(bbm); ProcessBlobs(gblobCounter, 0); ProcessBlobs(bblobCounter, 1); #if DEBUG actImg.Save(@"C:\temp\daforge\squares\img" + _calibrationStep + ".jpg", ImageFormat.Jpeg); #endif _calibrationStep++; } } else { switch (_calibrationStep) { case 2: var bm = UnmanagedImage.FromManagedImage(e.Frame.Bitmap); //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\src" + _errors + ".jpg", ImageFormat.Jpeg); bm = diffFilter.Apply(bm); var gf = new GaussianBlur(9.0, 3); gf.ApplyInPlace(bm); var cf = new ColorFiltering(new IntRange(10, 255), new IntRange(20, 255), new IntRange(20, 255)); cf.ApplyInPlace(bm); var blobCounter = new BlobCounter { ObjectsOrder = ObjectsOrder.Size, BackgroundThreshold = Color.FromArgb(255, 15, 20, 20), FilterBlobs = true }; blobCounter.ProcessImage(bm); bm.ToManagedImage().Save(@"C:\temp\daforge\diff\img" + _calibrationStep + "-" + _errors + ".jpg", ImageFormat.Jpeg); var blobs = blobCounter.GetObjectsInformation(); if (blobs.Any()) { int i = 0; List <IntPoint> corners; do { corners = PointsCloud.FindQuadrilateralCorners(blobCounter.GetBlobsEdgePoints(blobs[i++])); } while (corners.Count != 4); RecursiveAForgeCalibrator.GridBlobs.InPlaceSort(corners); Grid.TopLeft = new Point(corners[0].X, corners[0].Y); Grid.TopRight = new Point(corners[1].X, corners[1].Y); Grid.BottomLeft = new Point(corners[2].X, corners[2].Y); Grid.BottomRight = new Point(corners[3].X, corners[3].Y); if (Grid.TopLeft.X > 10 && Grid.TopRight.X < _vs.Width - 5 && Grid.BottomLeft.X > 5 && Grid.BottomRight.X < _vs.Width - 5 && Grid.TopLeft.Y > 10 && Grid.TopRight.Y > 5 && Grid.BottomLeft.Y < _vs.Height - 5 && Grid.BottomRight.Y < _vs.Height - 5 && Grid.TopLeft.X < Grid.BottomRight.X && blobs[i - 1].Area > 60000 && Grid.BottomLeft.X < Grid.TopRight.X && Grid.BottomLeft.X < Grid.BottomRight.X && Grid.TopLeft.Y < Grid.BottomLeft.Y && Grid.TopLeft.Y < Grid.BottomRight.Y && Grid.TopRight.Y < Grid.BottomLeft.Y && Grid.TopRight.Y < Grid.BottomRight.Y) { _calibrationStep++; _drawing = true; _vs.Clear(); FillRects(); Grid.AddPoint(new Point(), new Point(corners[0].X, corners[0].Y)); Grid.AddPoint(new Point(0, _vs.Height), new Point(corners[1].X, corners[1].Y)); Grid.AddPoint(new Point(_vs.Width, 0), new Point(corners[2].X, corners[2].Y)); Grid.AddPoint(new Point(_vs.Width, _vs.Height), new Point(corners[3].X, corners[3].Y)); //_mapper.PredictFromCorners(); } else { _calibrationStep = 0; _errors++; } } else { _calibrationStep = 0; _errors++; _vs.Draw(); } break; case 1: diffFilter.OverlayImage = e.Frame.Bitmap; //diffFilter.OverlayImage.Save(@"C:\temp\daforge\diff\srcf" + _errors + ".jpg", ImageFormat.Jpeg); //_vs.AddRect(0, 0, (int) _vs.Width, (int) _vs.Height, Color.FromArgb(255, 255, 255, 255)); _vs.Clear(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (!(y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1)) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; case 0: Grid = new Grid(e.Frame.Bitmap.Width, e.Frame.Bitmap.Height); Grid.ScreenSize = new Rectangle(0, 0, _vs.Width, _vs.Height); _mapper = new CornerBarycentricMapper(Grid); var thread = new Thread(() => { _vs.Show(); _vs.AddRect(0, 0, _vs.Width, _vs.Height, Color.FromArgb(255, 0, 0, 0)); }); thread.SetApartmentState(ApartmentState.STA); thread.Start(); thread.Join(); for (int y = 0; y < Columncount; y++) { for (int x = 0; x < Rowcount; x++) { if (y % 2 == 0 && x % 2 == 0 || y % 2 == 1 && x % 2 == 1) { _vs.AddRect((int)(x * _sqrwidth), (int)(y * _sqrheight), (int)_sqrwidth, (int)_sqrheight, Color.FromArgb(255, 255, 255, 255)); } } } _vs.Draw(); _calibrationStep++; break; } } } Debug.WriteLine("Releasing"); await Task.Delay(500); _sem.Release(); }
// New frame received by the player private void videoSourcePlayer_NewFrame(object sender, NewFrameEventArgs args) { var direccion = "Centro"; int direccionServo = 2; if (!detecting && !tracking) { return; } lock (this) { if (detecting) { detecting = false; tracking = false; UnmanagedImage im = UnmanagedImage.FromManagedImage(args.Frame); float xscale = im.Width / 160f; float yscale = im.Height / 120f; ResizeNearestNeighbor resize = new ResizeNearestNeighbor(160, 120); UnmanagedImage downsample = resize.Apply(im); Rectangle[] regions = detector.ProcessFrame(downsample); if (regions.Length > 0) { tracker.Reset(); // Will track the first face found Rectangle face = regions[0]; // Reduce the face size to avoid tracking background Rectangle window = new Rectangle( (int)((regions[0].X + regions[0].Width / 2f) * xscale), (int)((regions[0].Y + regions[0].Height / 2f) * yscale), 1, 1); Console.Write("x:" + (int)((regions[0].X + regions[0].Width / 2f) * xscale)); Console.Write("y:" + (int)((regions[0].X + regions[0].Height / 2f) * xscale)); window.Inflate( (int)(0.2f * regions[0].Width * xscale), (int)(0.4f * regions[0].Height * yscale)); // Initialize tracker tracker.SearchWindow = window; tracker.ProcessFrame(im); marker = new RectanglesMarker(window); marker.ApplyInPlace(im); args.Frame = im.ToManagedImage(); tracking = true; //detecting = true; } else { detecting = true; } } else if (tracking) { UnmanagedImage im = UnmanagedImage.FromManagedImage(args.Frame); // Track the object tracker.ProcessFrame(im); // Get the object position var obj = tracker.TrackingObject; var wnd = tracker.SearchWindow; //if (displayBackprojectionToolStripMenuItem.Checked) //{ // var backprojection = tracker.GetBackprojection(PixelFormat.Format24bppRgb); // im = UnmanagedImage.FromManagedImage(backprojection); //} //if (drawObjectAxisToolStripMenuItem.Checked) //{ // LineSegment axis = obj.GetAxis(); // // Draw X axis // if (axis != null) // Drawing.Line(im, axis.Start.Round(), axis.End.Round(), Color.Red); // else detecting = true; //} if (obj.Rectangle.Width < (args.Frame.Width / 3) * 2) { if (obj.Rectangle.X < args.Frame.Width / 3) { direccion = "Izquierda"; direccionServo = 1; } else if (obj.Rectangle.X > (args.Frame.Width / 3) * 2) { direccion = "Derecha"; direccionServo = 3; } } try { this.Invoke((MethodInvoker) delegate { if (textBox1 != null) { textBox1.Text = obj.Rectangle.X.ToString(); textBox2.Text = obj.Rectangle.Y.ToString(); label1.Text = direccion; } }); ComunicacionPuertoSerie.Instance.enviarEvento(direccionServo.ToString()); } catch (Exception e) { } if (/*drawObjectBoxToolStripMenuItem.Checked && drawTrackingWindowToolStripMenuItem.Checked*/ false) { marker = new RectanglesMarker(new Rectangle[] { wnd, obj.Rectangle }); } else if (/*drawObjectBoxToolStripMenuItem.Checked*/ true) { marker = new RectanglesMarker(obj.Rectangle); } else if (/*drawTrackingWindowToolStripMenuItem.Checked*/ true) { marker = new RectanglesMarker(wnd); } else { marker = null; } if (marker != null) { marker.ApplyInPlace(im); } args.Frame = im.ToManagedImage(); } else { if (marker != null) { args.Frame = marker.Apply(args.Frame); } } } }
//eventhandler if new frame is ready private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) { Bitmap img = (Bitmap)eventArgs.Frame.Clone(); if (counterImg == 10) { double delaisImage = DateTime.Now.TimeOfDay.TotalMilliseconds - _mill_last_pic; _mill_last_pic = DateTime.Now.TimeOfDay.TotalMilliseconds; double FPS = 1 / delaisImage * 1000 * counterImg + 1; // txt_nb_fps.Text = FPS.ToString() ; //txt_resolution.Text = "" + videoSource.DesiredFrameSize.Height + " * " + videoSource.DesiredFrameSize.Width; string resolutionTxt = "" + img.Width + " * " + img.Height; if (this != null && (!this.IsDisposed)) { try { this.Invoke((ProcessNewFPS)UpdateNewFPS, FPS); this.Invoke((ProcessNewResolution)UpdateNewResolution, resolutionTxt); } catch (ObjectDisposedException) // La fenetre était en train de se fermée { } } counterImg = 0; } counterImg++; //Rectangle rect = new Rectangle(0,0,eventArgs.Frame.Width,eventArgs.Frame.Height); // 1 - grayscaling UnmanagedImage image = UnmanagedImage.FromManagedImage(img); UnmanagedImage imageRouge = image.Clone(); UnmanagedImage imageBleu = image.Clone(); UnmanagedImage imageVert = image.Clone(); UnmanagedImage grayImage = null; Color colorPoint = image.GetPixel(posX, posY); this.Invoke((ProcessLalbelText)ChangeLabelText, new object[] { colorPoint.GetHue().ToString(), lbl_hue }); this.Invoke((ProcessLalbelText)ChangeLabelText, new object[] { colorPoint.GetBrightness().ToString(), lbl_lum }); this.Invoke((ProcessLalbelText)ChangeLabelText, new object[] { colorPoint.GetSaturation().ToString(), lbl_sat }); if (image.PixelFormat == PixelFormat.Format8bppIndexed) { grayImage = image; } else { grayImage = UnmanagedImage.Create(image.Width, image.Height, PixelFormat.Format8bppIndexed); Grayscale.CommonAlgorithms.BT709.Apply(image, grayImage); } // 2 - Edge detection DifferenceEdgeDetector edgeDetector = new DifferenceEdgeDetector(); UnmanagedImage edgesImage = edgeDetector.Apply(grayImage); // 3 - Threshold edges Threshold thresholdFilterGlyph = new Threshold((short)numericUpDown3.Value); Threshold thresholdFilterCouleur = new Threshold((short)numericUpDown2.Value); thresholdFilterGlyph.ApplyInPlace(edgesImage); /* * * Bitmap image = (Bitmap)eventArgs.Frame.Clone(); * * //Reference : http://www.aforgenet.com/framework/docs/html/743311a9-6c27-972d-39d2-ddc383dd1dd4.htm * * private HSLFiltering filter = new HSLFiltering(); * // set color ranges to keep red-orange * filter.Hue = new IntRange(0, 20); * filter.Saturation = new DoubleRange(0.5, 1); * * // apply the filter * filter.ApplyInPlace(image); * */ /*RGB colorRed = new RGB(215, 30, 30); * RGB colorBlue = new RGB(10, 10, 215); * RGB colorVert = new RGB(30, 215, 30); * RGB colorBlanc = new RGB(225, 219, 160);*/ HSLFiltering filter = new HSLFiltering(); // create filter // EuclideanColorFiltering filter = new EuclideanColorFiltering(); //filter.Radius = (short)numericUpDown1.Value; filter.Hue = new IntRange(40, 140); filter.Saturation = new Range(0.5f, 1.0f); filter.Luminance = new Range(0.2f, 1.0f); //filter.CenterColor = colorRed; filter.ApplyInPlace(imageRouge); filter.Hue = new IntRange(100, 180); //filter.CenterColor = colorBlanc; filter.ApplyInPlace(imageVert); filter.Hue = new IntRange(0, 40); //filter.CenterColor = colorBlue; filter.ApplyInPlace(imageBleu); Grayscale filterRouge = new Grayscale(0.800, 0.200, 0.200); Grayscale filterVert = new Grayscale(0.200, 0.800, 0.200); Grayscale filterBleu = new Grayscale(0.200, 0.200, 0.800); UnmanagedImage grayRougeImage = filterRouge.Apply(imageRouge); UnmanagedImage grayBleuImage = filterBleu.Apply(imageBleu); UnmanagedImage edgesRougeImage = edgeDetector.Apply(grayRougeImage); UnmanagedImage edgesBleuImage = edgeDetector.Apply(grayBleuImage); thresholdFilterCouleur.ApplyInPlace(edgesRougeImage); thresholdFilterCouleur.ApplyInPlace(edgesBleuImage); // All the image processing is done here... // pictureBox1.Image = image.ToManagedImage(); if (this != null && (!this.IsDisposed)) // Si on est pas en train de suppirmer la fenetre { try { this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { image, pic_ImageNormal }); this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { edgesImage, pic_ImageEdge }); this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { imageRouge, pic_ImageRouge }); this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { imageBleu, pic_ImageBleu }); this.Invoke((ProcessNewImage)DisplayNewImage, new object[] { imageVert, pic_ImageVert }); } catch (ObjectDisposedException) // La fenetre était en train de se fermée { } } /*pictureBox2.Image = grayImage.ToManagedImage(); * pictureBox3.Image = edgesImage.ToManagedImage(); * pictureBox4.Image = imageRouge.ToManagedImage();*/ }
// New frame received by the player private void videoSourcePlayer_NewFrame(object sender, ref Bitmap image) { if (!detecting && !tracking) { return; } lock (this) { if (detecting) { detecting = false; tracking = false; UnmanagedImage im = UnmanagedImage.FromManagedImage(image); float xscale = image.Width / 160f; float yscale = image.Height / 120f; ResizeNearestNeighbor resize = new ResizeNearestNeighbor(160, 120); UnmanagedImage downsample = resize.Apply(im); Rectangle[] regions = detector.ProcessFrame(downsample); if (regions.Length > 0) { tracker.Reset(); // Will track the first face found Rectangle face = regions[0]; // Reduce the face size to avoid tracking background Rectangle window = new Rectangle( (int)((regions[0].X + regions[0].Width / 2f) * xscale), (int)((regions[0].Y + regions[0].Height / 2f) * yscale), 1, 1); window.Inflate( (int)(0.2f * regions[0].Width * xscale), (int)(0.4f * regions[0].Height * yscale)); // Initialize tracker tracker.SearchWindow = window; tracker.ProcessFrame(im); marker = new RectanglesMarker(window); marker.ApplyInPlace(im); image = im.ToManagedImage(); tracking = true; //detecting = true; } else { detecting = true; } } else if (tracking) { UnmanagedImage im = UnmanagedImage.FromManagedImage(image); // Track the object tracker.ProcessFrame(im); // Get the object position var obj = tracker.TrackingObject; var wnd = tracker.SearchWindow; if (displayBackprojectionToolStripMenuItem.Checked) { var backprojection = tracker.GetBackprojection(PixelFormat.Format24bppRgb); im = UnmanagedImage.FromManagedImage(backprojection); } if (drawObjectAxisToolStripMenuItem.Checked) { LineSegment axis = obj.GetAxis(); // Draw X axis Drawing.Line(im, axis.Start.Round(), axis.End.Round(), Color.Red); } if (drawObjectBoxToolStripMenuItem.Checked && drawTrackingWindowToolStripMenuItem.Checked) { marker = new RectanglesMarker(new Rectangle[] { wnd, obj.Rectangle }); } else if (drawObjectBoxToolStripMenuItem.Checked) { //InteractionPoints p = new InteractionPoints(); //p.setHead(obj.Rectangle); marker = new RectanglesMarker(obj.Rectangle); } else if (drawTrackingWindowToolStripMenuItem.Checked) { marker = new RectanglesMarker(wnd); } else { marker = null; } if (marker != null) { marker.ApplyInPlace(im); } image = im.ToManagedImage(); } else { if (marker != null) { image = marker.Apply(image); } } } }
public UnmanagedImage LoadFile() { using (Bitmap bitmap = Accord.Imaging.Image.FromFile(SourceFilePath)) return(UnmanagedImage.FromManagedImage(bitmap)); }