// ========================================================================================================== private Bitmap MirrorFunct(Bitmap frame) { Mirror Mfilter = new Mirror(false, true); // apply the MirrFilter Mfilter.ApplyInPlace(frame); return (frame); }
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; }
/// <summary> /// Get movement samples from the specified image area /// </summary> /// <param name="x">Position of the upper left corner of the area on the x axis</param> /// <param name="y">Position of the upper left corner on of the area the y axis</param> /// <param name="width">Width of the area</param> /// <param name="height">Height of the area</param> /// <param name="maxCycles">Number of cycles to run</param> /// <param name="getImg">A delegate returning images to process</param> /// <param name="progEv">A delegate specifying code to run when progress of learning changes</param> /// <param name="complEv">A delegate specifying code to run when learning is complete</param> public void Learn(int x, int y, int width, int height, int maxCycles, Func<Bitmap> getImg, ProgressChangedEventHandler progEv, RunWorkerCompletedEventHandler complEv) { if (samples != null) { foreach(Bitmap b in samples) { b.Dispose(); } } ResizeBilinear resize = new ResizeBilinear(70, (int)(70 * ((float)height / width))); BackgroundWorker bw = new BackgroundWorker(); samples = new Bitmap[maxCycles, 10]; Crop cropFilter; Mirror mirror = new Mirror(false,true); HomogenityEdgeDetector edgeDetector = new HomogenityEdgeDetector(); // this allows our worker to report progress during work bw.WorkerReportsProgress = true; // what to do in the background thread bw.DoWork += new DoWorkEventHandler( delegate(object o, DoWorkEventArgs args) { BackgroundWorker b = o as BackgroundWorker; //Bitmap img; int xSize = 0, ySize = 0; int numImages = 0; for (int learningPoints = 0; learningPoints < maxCycles; learningPoints++) { b.ReportProgress((100 / maxCycles) * learningPoints); for (int tutorialBox = 0; tutorialBox < 100; tutorialBox++) { if (tutorialBox % 10 == 0) { numImages += 1; b.ReportProgress((100 / maxCycles) * learningPoints + (tutorialBox / maxCycles)); cropFilter = new Crop(new Rectangle(x - tutorialBox, y, width, height)); //img = resize.Apply(maxBlobFilter.Apply(cropFilter.Apply(getImg()))); xSize += width; ySize += height; samples[learningPoints, tutorialBox / 10] = resize.Apply(mirror.Apply(cropFilter.Apply(getImg()))); samples[learningPoints, tutorialBox / 10].Save("sample" + tutorialBox / 10 + ".bmp"); } System.Threading.Thread.Sleep(15); } } avgSize.x = (float)xSize / numImages; avgSize.y = (float)ySize / numImages; avgAspectRatio = (float)xSize / ySize; b.ReportProgress(100); args.Result = samples; }); // what to do when progress changed (update the progress bar for example) bw.ProgressChanged += progEv; // what to do when worker completes its task (notify the user) bw.RunWorkerCompleted += complEv; bw.RunWorkerAsync(); }
private void cam_NewFrameMirrortwo(object sender, NewFrameEventArgs eventArgs) { //Evento para espelhar espelhar try { Bitmap bitmirror = (Bitmap)eventArgs.Frame.Clone(); Mirror filter = new Mirror(false, true); filter.ApplyInPlace(bitmirror); pbCamera.Image = bitmirror; } catch (Exception ex) { System.Windows.MessageBox.Show(ex.Message); } }