// Process specified image trying to recognize counter's image public void Process(Bitmap image, IImageProcessingLog log) { log.AddMessage("Image size: " + image.Width + " x " + image.Height); //get image byte[] textData = GetImageData(); UnmanagedImage img = CreateImage(textData); log.AddImage("Raw Image", img.ToManagedImage()); //resize Image AForge.Imaging.Filters.ResizeNearestNeighbor resizeFilter = new AForge.Imaging.Filters.ResizeNearestNeighbor(500, (int)(500 / res)); UnmanagedImage resizedImage = resizeFilter.Apply(img); log.AddImage("Resized Image", resizedImage.ToManagedImage()); //filter floor UnmanagedImage floorFilteredImage = FilterFloor(resizedImage, textData); log.AddImage("Floor filtered", floorFilteredImage.ToManagedImage()); // 1- grayscale image Bitmap grayImage = Grayscale.CommonAlgorithms.BT709.Apply(resizedImage.ToManagedImage()); log.AddImage("Grayscale", resizedImage.ToManagedImage()); // 2 - Otsu thresholding OtsuThreshold threshold = new OtsuThreshold(); Bitmap binaryImage = threshold.Apply(grayImage); log.AddImage("Binary", binaryImage); log.AddMessage("Otsu threshold: " + threshold.ThresholdValue); //resive image // 3 - Blob counting BlobCounter blobCounter = new BlobCounter(); blobCounter.FilterBlobs = true; blobCounter.MinWidth = 1; blobCounter.MinWidth = 1; blobCounter.ProcessImage(binaryImage); Blob[] blobs = blobCounter.GetObjectsInformation(); log.AddMessage("Found blobs (min width/height = 24): " + blobs.Length); }
public Bitmap ProcessFrame(Bitmap inputBitmap, int x, int y) { // Create an image for AForge to process Bitmap workingImage = new Bitmap(inputBitmap.Width, inputBitmap.Height); workingImage = AForge.Imaging.Image.Clone(inputBitmap, PixelFormat.Format24bppRgb); // Create a mask for ROI selection Rectangle roi = new Rectangle(x - 30, y-30, 80, 80); Crop roicrop = new Crop(roi); Bitmap outimage = roicrop.Apply(workingImage); BlobCounter blobCounter = new BlobCounter(); blobCounter.ObjectsOrder = ObjectsOrder.Area; Blob[] blobs; // Find the blobs blobCounter.ProcessImage(outimage); blobs = blobCounter.GetObjectsInformation(); List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[0]); GrahamConvexHull grahamScan = new GrahamConvexHull(); List<IntPoint> hullPoints = grahamScan.FindHull(edgePoints); Graphics g = Graphics.FromImage(outimage); Pen redPen = new Pen(Color.Red, 2); g.DrawPolygon(redPen, ToPointsArray(hullPoints)); //g.Clear(Color.Black); //g.DrawImage(handImage, x, y); //g.DrawRectangle(redPen, roi); //g.DrawEllipse(redPen, x, y, 20, 20); ResizeNearestNeighbor resizeFilter = new ResizeNearestNeighbor(160, 160); Bitmap resizedImage = resizeFilter.Apply(outimage); return resizedImage; }
private void VideoNewFrame(object sender, NewFrameEventArgs e) { var nf = NewFrame; var f = e.Frame; if (_requestedToStop || nf==null || f==null) return; if (_lastframeEvent > DateTime.MinValue) { if ((Helper.Now<_nextFrameTarget)) { return; } CalculateFramerates(); } _lastframeEvent = Helper.Now; Bitmap bmOrig = null; bool bMotion = false; lock (_sync) { try { bmOrig = ResizeBmOrig(f); if (RotateFlipType != RotateFlipType.RotateNoneFlipNone) { bmOrig.RotateFlip(RotateFlipType); } _width = bmOrig.Width; _height = bmOrig.Height; if (ZPoint == Point.Empty) { ZPoint = new Point(bmOrig.Width / 2, bmOrig.Height / 2); } if (CW.NeedMotionZones) CW.NeedMotionZones = !SetMotionZones(CW.Camobject.detector.motionzones); if (Mask != null) { ApplyMask(bmOrig); } if (CW.Camobject.alerts.active && Plugin != null && Alarm!=null) { bmOrig = RunPlugin(bmOrig); } var bmd = bmOrig.LockBits(new Rectangle(0, 0, bmOrig.Width, bmOrig.Height), ImageLockMode.ReadWrite, bmOrig.PixelFormat); //this converts the image into a windows displayable image so do it regardless using (var lfu = new UnmanagedImage(bmd)) { if (_motionDetector != null) { bMotion = ApplyMotionDetector(lfu); } else { MotionDetected = false; } if (CW.Camobject.settings.FishEyeCorrect) { _feCorrect.Correct(lfu, CW.Camobject.settings.FishEyeFocalLengthPX, CW.Camobject.settings.FishEyeLimit, CW.Camobject.settings.FishEyeScale, ZPoint.X, ZPoint.Y); } if (ZFactor > 1) { var f1 = new ResizeNearestNeighbor(lfu.Width, lfu.Height); var f2 = new Crop(ViewRectangle); try { using (var imgTemp = f2.Apply(lfu)) { f1.Apply(imgTemp, lfu); } } catch (Exception ex) { ErrorHandler?.Invoke(ex.Message); } } } bmOrig.UnlockBits(bmd); PiP(bmOrig); AddTimestamp(bmOrig); } catch (UnsupportedImageFormatException ex) { CW.VideoSourceErrorState = true; CW.VideoSourceErrorMessage = ex.Message; bmOrig?.Dispose(); return; } catch (Exception ex) { bmOrig?.Dispose(); ErrorHandler?.Invoke(ex.Message); return; } if (MotionDetector != null && !CW.Calibrating && MotionDetector.MotionProcessingAlgorithm is BlobCountingObjectsProcessing && !CW.PTZNavigate && CW.Camobject.settings.ptzautotrack) { try { ProcessAutoTracking(); } catch (Exception ex) { ErrorHandler?.Invoke(ex.Message); } } } if (!_requestedToStop) { nf.Invoke(this, new NewFrameEventArgs(bmOrig)); } if (bMotion) { TriggerDetect(this); } }
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 void UpCam_TakeSnapshot() { SelectCamera(UpCamera); DisplayText("UpCam_TakeSnapshot()"); UpCamera.SnapshotRotation = Cnc.CurrentA; UpCamera.BuildMeasurementFunctionsList(UpcamSnapshot_dataGridView); UpCamera.TakeSnapshot(); DownCamera.SnapshotOriginalImage = new Bitmap(UpCamera.SnapshotImage); DownCamera.SnapshotImage = new Bitmap(UpCamera.SnapshotImage); // We need a copy of the snapshot to scale it, in 24bpp format. See http://stackoverflow.com/questions/2016406/converting-bitmap-pixelformats-in-c-sharp Bitmap Snapshot24bpp = new Bitmap(640, 480, System.Drawing.Imaging.PixelFormat.Format24bppRgb); using (Graphics gr = Graphics.FromImage(Snapshot24bpp)) { gr.DrawImage(UpCamera.SnapshotOriginalImage, new Rectangle(0, 0, 640, 480)); } // scale: double Xscale = Properties.Settings.Default.UpCam_XmmPerPixel / Properties.Settings.Default.DownCam_XmmPerPixel; double Yscale = Properties.Settings.Default.UpCam_YmmPerPixel / Properties.Settings.Default.DownCam_YmmPerPixel; double zoom = UpCamera.GetMeasurementZoom(); Xscale = Xscale / zoom; Yscale = Yscale / zoom; int SnapshotSizeX = (int)(Xscale * 640); int SnapshotSizeY = (int)(Yscale * 480); // SnapshotSize is the size (in pxls) of upcam snapshot, scaled so that it draws in correct size on downcam image. ResizeNearestNeighbor RezFilter = new ResizeNearestNeighbor(SnapshotSizeX, SnapshotSizeY); Bitmap ScaledShot = RezFilter.Apply(Snapshot24bpp); // and this is the scaled image // Mirror: Mirror MirrFilter = new Mirror(false, true); MirrFilter.ApplyInPlace(ScaledShot); // Clear DownCam image Graphics DownCamGr = Graphics.FromImage(DownCamera.SnapshotImage); DownCamGr.Clear(Color.Black); // Embed the ScaledShot to it. Upper left corner of the embedding is: int X = 320 - SnapshotSizeX / 2; int Y = 240 - SnapshotSizeY / 2; DownCamGr.DrawImage(ScaledShot, X, Y, SnapshotSizeX, SnapshotSizeY); DownCamera.SnapshotImage.MakeTransparent(Color.Black); // DownCam Snapshot is ok, copy it to original too DownCamera.SnapshotOriginalImage = new Bitmap(DownCamera.SnapshotImage); DownCamera.SnapshotRotation = Cnc.CurrentA; }
private void cameraTask_Completed(object sender, PhotoResult e) { if (e.TaskResult == TaskResult.OK) { BitmapImage bmp = new BitmapImage(); bmp.SetSource(e.ChosenPhoto); bmp.ImageOpened += ImageOpenedTrue; //imgPreview.Source = bmp; //bmp.DecodePixelHeight = 460; //Height="460" Margin="10,10,0,0" VerticalAlignment="Top" Width="684"> WriteableBitmap wbmp = new WriteableBitmap(bmp); //txtImage.Text = "Korkeus: " + wbmp.PixelHeight + " Leveys: " + wbmp.PixelWidth; Bitmap image = (Bitmap)wbmp; //AForge.Imaging.Filters.Grayscale //Grayscale gs = new Grayscale( // create filter ResizeNearestNeighbor filter = new ResizeNearestNeighbor(700, 480); // apply the filter Bitmap resizeImage = filter.Apply(image); txtImage.Text = "Korkeus: " + resizeImage.Height + " Leveys: " + resizeImage.Width; // create grayscale filter (BT709) Grayscale GSfilter = new Grayscale(0.2125, 0.7154, 0.0721); // apply the filter Bitmap grayImage = GSfilter.Apply(resizeImage); // create filter Threshold BIfilter = new Threshold(70); // apply the filter BIfilter.ApplyInPlace(grayImage); // create filter Invert Invertfilter = new Invert(); // apply the filter Invertfilter.ApplyInPlace(grayImage); AForge.Imaging.Filters.ConnectedComponentsLabeling Blobfilter = new AForge.Imaging.Filters.ConnectedComponentsLabeling(); Bitmap newImage = Blobfilter.Apply(grayImage); BlobCounter blobCounter = new BlobCounter(newImage); blobCounter.FilterBlobs = true; blobCounter.MinWidth = 5; blobCounter.MinHeight = 5; Blob[] blobs = blobCounter.GetObjectsInformation(); gos = new List<GameObject>(); List<Blob> blobList = blobs.ToList(); PointCollection extremePoints = new PointCollection(); System.Windows.Shapes.Rectangle r = new System.Windows.Shapes.Rectangle(); PointCollection edges; List<AForge.IntPoint> lst; List<int[]> intlist = new List<int[]>(); GameObject go = new GameObject(); foreach (Blob b in blobs) { lst = blobCounter.GetBlobsEdgePoints(b); edges = new PointCollection(); foreach (AForge.IntPoint p in lst) { edges.Add(new System.Windows.Point(p.X, p.Y)); } /* extremePoints.Add(new System.Windows.Point(b.Rectangle.X, b.Rectangle.Y)); extremePoints.Add(new System.Windows.Point(b.Rectangle.X+b.Rectangle.Width, b.Rectangle.Y)); extremePoints.Add(new System.Windows.Point(b.Rectangle.X, b.Rectangle.Y+b.Rectangle.Height)); extremePoints.Add(new System.Windows.Point(b.Rectangle.X + b.Rectangle.Width, b.Rectangle.Y + b.Rectangle.Height)); * */ gos.Add(new GameObject(b.Rectangle.Y,b.Rectangle.X,b.Rectangle.Width,b.Rectangle.Height,edges)); // x ja y käännettynä, jotta lopullisessa mallissa suunnat oikein. //GameObject go = new GameObject( //extremePoints.Clear(); //edges.Clear(); } //Serialize("LevelName.bin", gos); //SaveToLocalFolderAsync(e.ChosenPhoto, "LevelNameBG.jpg"); //writeJsonAsync(); fileName = txtBoxLevelName.Text; jsonWrite(fileName); WriteableBitmap wb; wb = (WriteableBitmap)resizeImage; using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication()) { if (iso.FileExists(fileName+"_bg.jpg")) { iso.DeleteFile(fileName+"_bg.jpg"); } using (IsolatedStorageFileStream isostream = iso.CreateFile(fileName+"_bg.jpg")) { Extensions.SaveJpeg(wb, isostream, wb.PixelWidth, wb.PixelHeight, 0, 85); isostream.Close(); } } //Rectangle[] rects = blobCounter.GetObjectsRectangles(); //txtImage.Text = txtImage.Text + "Number of rectangles:+ " + rects.Length; wbmp = (WriteableBitmap)newImage; imgImage.Source = wbmp; // BY PIXEL /* foreach (int pix in wbmp.Pixels) { int a = pix >> 24; int r = (pix & 0x00ff0000) >> 16; int g = (pix & 0x0000ff00) >> 8; int b = (pix & 0x000000ff); Color c = new Color(r, g, b, a); int intensity = (int)(c.R * 0.3 + c.G * 0.59 + c.B * 0.11); if (intensity > 127) ; // BLACK / SOLID else ; // WHITE / NON-SOLID }*/ //byte[] baLevel = CustomConvertToBytes(bmp); /* WriteableBitmap wbmp = new WriteableBitmap(bmp as BitmapSource); imgPreview.Source = wbmp;*/ } }
private void recoitframe() { textBox2.Clear(); string sourceURL = "http://192.168.1.110/image/jpeg.cgi"; byte[] buffer = new byte[100000]; int read, total = 0; try { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(sourceURL); req.Credentials = new NetworkCredential("admin", "robotexp"); WebResponse resp = req.GetResponse(); Stream stream = resp.GetResponseStream(); while ((read = stream.Read(buffer, total, 1000)) != 0) { total += read; } //traitement d'image bmp = (Bitmap)Bitmap.FromStream(new MemoryStream(buffer, 0, total)); ResizeNearestNeighbor filter2 = new ResizeNearestNeighbor(bmp.Width, bmp.Height); bmp = filter2.Apply(bmp); if (startProcessing) { traitementimage(ref bmp); } panel2.BackgroundImage = bmp; toolStripProgressBar2.Value = 100; } catch (Exception) { hist("Impossible de se connecter au caméra !!!"); toolStripProgressBar2.Value = 0; } }
private UnmanagedImage ZoomImage(UnmanagedImage lfu) { if (ZFactor > 1) { var f1 = new ResizeNearestNeighbor(lfu.Width, lfu.Height); var f2 = new Crop(ViewRectangle); try { lfu = f2.Apply(lfu); lfu = f1.Apply(lfu); } catch (Exception ex) { if (ErrorHandler != null) ErrorHandler(ex.Message); } } return lfu; }
/// <summary> /// Creates a preview image /// </summary> private Bitmap createImagePreview(Bitmap bmp) { ResizeNearestNeighbor filter = new ResizeNearestNeighbor(298, 298); return filter.Apply(bmp); }