Resize image using nearest neighbor algorithm.

The class implements image resizing filter using nearest neighbor algorithm, which does not assume any interpolation.

The filter accepts 8 and 16 bpp grayscale images and 24, 32, 48 and 64 bpp color images for processing.

Sample usage:

// create filter ResizeNearestNeighbor filter = new ResizeNearestNeighbor( 400, 300 ); // apply the filter Bitmap newImage = filter.Apply( image );

Initial image:

Result image:

상속: AForge.Imaging.Filters.BaseResizeFilter
예제 #1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO get cams from DB
            pb = new PictureBox[2];
            cams = new Edge.IPCameras.IPCamera[2];
            streams = new MJPEGStream[2];
            detectors = new MotionDetector[2];

            // TODO determine width/height based on number of cams
            int width = 480, height = 360;

            // Reset filters with determined width/height
            filter = new ResizeNearestNeighbor(width, height);

            // Add picturebox for each cam
            pb[0] = new PictureBox() { Width = width, Height = height, ImageLocation = "Images/testbeeld.png" };
            flowLayoutPanel1.Controls.Add(pb[0]);
            pb[1] = new PictureBox() { Width = width, Height = height, ImageLocation = "Images/testbeeld.png" };
            flowLayoutPanel1.Controls.Add(pb[1]);

            // Add cams to array
            cams[0] = new Edge.IPCameras.Foscam()
            {
                Description = "Woonkamer",
                Host = "10.30.59.81",
                Port = 8081,
                Username = "******",
                Password = "******",
            };
            cams[1] = new Edge.IPCameras.Foscam()
            {
                Description = "Buiten",
                Host = "10.30.59.82",
                Port = 8082,
                Username = "******",
                Password = "******",
            };

            // Setup motion detection
            for (int i = 0; i < cams.Count(); i++)
            {
                detectors[i] = new MotionDetector(
                                   new SimpleBackgroundModelingDetector(),
                                   new BlobCountingObjectsProcessing());
            }

            // Start streaming
            for (int i = 0; i < cams.Count(); i++)
            {
                streams[i] = new MJPEGStream(cams[i].MJPEGStreamURL);
                streams[i].NewFrame += new NewFrameEventHandler(Form1_NewFrame);
                streams[i].Start();
            }
        }
예제 #2
0
        // 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);

        }
예제 #3
0
        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;
        }
예제 #4
0
파일: Camera.cs 프로젝트: Jaejoon/iSpy
        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);
            }
        }
예제 #5
0
        public void ConvertTest3()
        {
            double[] pixels = 
            {
                 0, 0, 0, 0,
                 0, 1, 1, 0,
                 0, 1, 1, 0,
                 0, 0, 0, 0,
            };


            ArrayToImage conv1 = new ArrayToImage(width: 4, height: 4);
            Bitmap image;
            conv1.Convert(pixels, out image);
            image = new ResizeNearestNeighbor(16, 16).Apply(image);


            // Obtain an image
            // Bitmap image = ...

            // Show on screen
            //ImageBox.Show(image, PictureBoxSizeMode.Zoom);

            // Create the converter to convert the image to a
            //  matrix containing only values between 0 and 1 
            ImageToMatrix conv = new ImageToMatrix(min: 0, max: 1);

            // Convert the image and store it in the matrix
            double[,] matrix; conv.Convert(image, out matrix);

            /*
                        // Show the matrix on screen as an image
                        ImageBox.Show(matrix, PictureBoxSizeMode.Zoom);


                        // Show the matrix on screen as a .NET multidimensional array
                        MessageBox.Show(matrix.ToString(CSharpMatrixFormatProvider.InvariantCulture));

                        // Show the matrix on screen as a table
                        DataGridBox.Show(matrix, nonBlocking: true)
                            .SetAutoSizeColumns(DataGridViewAutoSizeColumnsMode.Fill)
                            .SetAutoSizeRows(DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders)
                            .SetDefaultFontSize(5)
                            .WaitForClose();
            */

            Assert.AreEqual(0, matrix.Min());
            Assert.AreEqual(1, matrix.Max());
            Assert.AreEqual(16 * 16, matrix.Length);
        }
예제 #6
0
파일: Camera.cs 프로젝트: vmail/main
        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());
                }
            }
        }
예제 #7
0
        public void ConvertTest2()
        {
            // Create a matrix representation 
            // of a 4x4 image with a inner 2x2
            // square drawn in the middle

            double[,] pixels = 
            {
                { 0, 0, 0, 0 },
                { 0, 1, 1, 0 },
                { 0, 1, 1, 0 },
                { 0, 0, 0, 0 },
            };

            // Create the converter to convert the matrix to a image
            MatrixToImage conv = new MatrixToImage(min: 0, max: 1);

            // Declare an image and store the pixels on it
            Bitmap image; conv.Convert(pixels, out image);

            // Show the image on screen
            image = new ResizeNearestNeighbor(320, 320).Apply(image);
            // ImageBox.Show(image, PictureBoxSizeMode.Zoom);

            Assert.AreEqual(0, conv.Min);
            Assert.AreEqual(1, conv.Max);
            Assert.AreEqual(320, image.Height);
            Assert.AreEqual(320, image.Width);
        }
예제 #8
0
        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;
        }
        public void ConvertTest3()
        {
            double[] pixels = 
            {
                 0, 0, 0, 0,
                 0, 1, 1, 0,
                 0, 1, 1, 0,
                 0, 0, 0, 0,
            };

            ArrayToImage conv1 = new ArrayToImage(width: 4, height: 4);
            Bitmap image;
            conv1.Convert(pixels, out image);
            image = new ResizeNearestNeighbor(16, 16).Apply(image);


            // Obtain a 16x16 bitmap image
            // Bitmap image = ...

            // Show on screen
            // ImageBox.Show(image, PictureBoxSizeMode.Zoom);

            // Create the converter to convert the image to an
            //   array containing only values between 0 and 1 
            ImageToArray conv = new ImageToArray(min: 0, max: 1);

            // Convert the image and store it in the array
            double[] array; conv.Convert(image, out array);

            // Show the array on screen
            // ImageBox.Show(array, 16, 16, PictureBoxSizeMode.Zoom);

            Assert.AreEqual(0, array.Min());
            Assert.AreEqual(1, array.Max());
            Assert.AreEqual(16 * 16, array.Length);
        }
예제 #10
0
        public void ConvertTest4()
        {
            // Create an array representation 
            // of a 4x4 image with a inner 2x2
            // square drawn in the middle

            Color[] pixels = 
            {
                 Color.Black, Color.Black,       Color.Black, Color.Black, 
                 Color.Black, Color.Transparent, Color.Red,   Color.Black, 
                 Color.Black, Color.Green,       Color.Blue,  Color.Black, 
                 Color.Black, Color.Black,       Color.Black,  Color.Black, 
            };

            // Create the converter to create a Bitmap from the array
            ArrayToImage conv = new ArrayToImage(width: 4, height: 4);

            // Declare an image and store the pixels on it
            Bitmap image; conv.Convert(pixels, out image);

            // Show the image on screen
            image = new ResizeNearestNeighbor(320, 320).Apply(image);
            // Accord.Controls.ImageBox.Show(image, PictureBoxSizeMode.Zoom);

            Assert.AreEqual(0, conv.Min);
            Assert.AreEqual(1, conv.Max);
            Assert.AreEqual(320, image.Height);
            Assert.AreEqual(320, image.Width);
        }
예제 #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="T:System.Object"/> class.
        /// </summary>
        /// <remarks></remarks>
        public TileOCR(string trainingPath)
        {
            classifier = new KNearestClassifier(1, Metric.EuclideanDistance, WeightMode.InverseDistance);
            training = LoadInstancesFromBitmaps(trainingPath);
            
            classifier.Train(training);

            results = new List<OCRResult>();

            grayscale = new Grayscale(0, 0.85, 0.15);
            invert = new Invert();
            resize = new ResizeNearestNeighbor(32, 32);
            floodFill = new PointedColorFloodFill(Color.Black);
            dilate = new BinaryDilatation3x3();
            blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinWidth = 4;
            blobCounter.MinHeight = 14;
            blobCounter.MaxWidth = 30;
            blobCounter.MaxHeight = 30;
            blobCounter.ObjectsOrder = ObjectsOrder.XY;
            threshold = new BradleyLocalThresholding();
            threshold.PixelBrightnessDifferenceLimit = 0;
            //Threshold.WindowSize = 20;
            threshold.WindowSize = 24;
        }
예제 #12
0
        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;*/
            }
        }
예제 #13
0
 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;
     }
     
 }
예제 #14
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;
        }
예제 #15
0
파일: Form1.cs 프로젝트: huyinjie/x3dom
 /// <summary>
 /// Creates a preview image
 /// </summary>
 private Bitmap createImagePreview(Bitmap bmp) {
     ResizeNearestNeighbor filter = new ResizeNearestNeighbor(298, 298);
     return filter.Apply(bmp);
 }