예제 #1
0
        public static Bitmap ToManagedAndDisposeThis(this UnmanagedImage thisImage)
        {
            var result = thisImage.ToManagedImage();

            thisImage.Dispose();
            return(result);
        }
        /// <summary>
        /// Apply filter to an image in unmanaged memory.
        /// </summary>
        ///
        /// <param name="image">Source image in unmanaged memory to apply filter to.</param>
        ///
        /// <returns>Returns filter's result obtained by applying the filter to
        /// the source image.</returns>
        ///
        /// <remarks>The method keeps the source image unchanged and returns
        /// the result of image processing filter as new image.</remarks>
        ///
        /// <exception cref="ApplicationException">No filters were added into the filters' sequence.</exception>
        ///
        public UnmanagedImage Apply(UnmanagedImage image)
        {
            int n = InnerList.Count;

            // check for empty sequence
            if (n == 0)
            {
                throw new ApplicationException("No filters in the sequence.");
            }

            UnmanagedImage dstImg = null;
            UnmanagedImage tmpImg = null;

            // apply the first filter
            dstImg = ((IFilter)InnerList[0]).Apply(image);

            // apply other filters
            for (int i = 1; i < n; i++)
            {
                tmpImg = dstImg;
                dstImg = ((IFilter)InnerList[i]).Apply(tmpImg);
                tmpImg.Dispose( );
            }

            return(dstImg);
        }
        /// <summary>
        /// Apply filter to an image in unmanaged memory.
        /// </summary>
        ///
        /// <param name="sourceImage">Source image in unmanaged memory to apply filter to.</param>
        /// <param name="destinationImage">Destination image in unmanaged memory to put result into.</param>
        ///
        /// <remarks><para>The method keeps the source image unchanged and puts result of image processing
        /// into destination image.</para>
        ///
        /// <para><note>The destination image must have width, height and pixel format as it is expected by
        /// the final filter in the sequence.</note></para>
        /// </remarks>
        ///
        /// <exception cref="ApplicationException">No filters were added into the filters' sequence.</exception>
        ///
        public void Apply(UnmanagedImage sourceImage, UnmanagedImage destinationImage)
        {
            int n = InnerList.Count;

            // check for empty sequence
            if (n == 0)
            {
                throw new ApplicationException("No filters in the sequence.");
            }

            if (n == 1)
            {
                ((IFilter)InnerList[0]).Apply(sourceImage, destinationImage);
            }
            else
            {
                UnmanagedImage tmpImg1 = null;
                UnmanagedImage tmpImg2 = null;

                // apply the first filter
                tmpImg1 = ((IFilter)InnerList[0]).Apply(sourceImage);

                // apply other filters, except the last one
                n--;
                for (int i = 1; i < n; i++)
                {
                    tmpImg2 = tmpImg1;
                    tmpImg1 = ((IFilter)InnerList[i]).Apply(tmpImg2);
                    tmpImg2.Dispose( );
                }

                ((IFilter)InnerList[n]).Apply(tmpImg1, destinationImage);
            }
        }
        /// <summary>
        /// Reset motion detector to initial state.
        /// </summary>
        ///
        /// <remarks><para>Resets internal state and variables of motion detection algorithm.
        /// Usually this is required to be done before processing new video source, but
        /// may be also done at any time to restart motion detection algorithm.</para>
        /// </remarks>
        ///
        public void Reset( )
        {
            lock ( sync )
            {
                if (backgroundFrame != null)
                {
                    backgroundFrame.Dispose( );
                    backgroundFrame = null;
                }

                if (motionFrame != null)
                {
                    motionFrame.Dispose( );
                    motionFrame = null;
                }

                if (tempFrame != null)
                {
                    tempFrame.Dispose( );
                    tempFrame = null;
                }

                framesCounter = 0;
            }
        }
        public void Apply(UnmanagedImage sourceImage, UnmanagedImage destinationImage)
        {
            int count = base.InnerList.Count;

            switch (count)
            {
            case 0:
                throw new ApplicationException("No filters in the sequence.");

            case 1:
                ((IFilter)base.InnerList[0]).Apply(sourceImage, destinationImage);
                return;
            }
            UnmanagedImage image  = null;
            UnmanagedImage image2 = null;

            image = ((IFilter)base.InnerList[0]).Apply(sourceImage);
            count--;
            for (int i = 1; i < count; i++)
            {
                image2 = image;
                image  = ((IFilter)base.InnerList[i]).Apply(image2);
                image2.Dispose();
            }
            ((IFilter)base.InnerList[count]).Apply(image, destinationImage);
        }
        // Reset motion detector to initial state
        private void Reset(bool force)
        {
            lock ( sync )
            {
                if (
                    (backgroundFrame != null) &&
                    ((force == true) || (manuallySetBackgroundFrame == false))
                    )
                {
                    backgroundFrame.Dispose( );
                    backgroundFrame = null;
                }

                if (motionFrame != null)
                {
                    motionFrame.Dispose( );
                    motionFrame = null;
                }

                if (tempFrame != null)
                {
                    tempFrame.Dispose( );
                    tempFrame = null;
                }
            }
        }
예제 #7
0
        /// <summary>
        /// Reset motion detector to initial state.
        /// </summary>
        ///
        /// <remarks><para>Resets internal state and variables of motion detection algorithm.
        /// Usually this is required to be done before processing new video source, but
        /// may be also done at any time to restart motion detection algorithm.</para>
        /// </remarks>
        ///
        public void Reset( )
        {
            lock ( _sync )
            {
                if (_backgroundFrame != null)
                {
                    _backgroundFrame.Dispose( );
                    _backgroundFrame = null;
                }

                if (_motionFrame != null)
                {
                    _motionFrame.Dispose( );
                    _motionFrame = null;
                }

                if (_tempFrame != null)
                {
                    _tempFrame.Dispose( );
                    _tempFrame = null;
                }

                _framesCounter = 0;
            }
        }
예제 #8
0
        /// <summary>
        /// Using the fill array calculated in passes 1 and 2, apply red-eye correction to the specified image.
        /// </summary>
        /// <param name="image"></param>
        /// <param name="ox"></param>
        /// <param name="oy"></param>
        public unsafe void CorrectRedEye(UnmanagedImage image, int ox, int oy)
        {
            int pixelSize = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;

            if (pixelSize > 4)
            {
                throw new Exception("Invalid pixel depth");
            }
            UnmanagedImage mask = GetBlurredMask();;

            try {
                long scan0  = (long)image.ImageData;
                long stride = image.Stride;
                // do the job
                byte *src;
                //Establish bounds
                int top    = oy;
                int bottom = oy + mask.Height;
                int left   = ox;
                int right  = ox + mask.Width;

                byte *fade;
                float fadeVal;
                float gray;

                //Scan region
                for (int y = top; y < bottom; y++)
                {
                    src = (byte *)(scan0 + y * stride + (left * pixelSize));
                    for (int x = left; x < right; x++, src += pixelSize)
                    {
                        if (src[RGB.R] == 0)
                        {
                            continue;                  //Because 0 will crash the formula
                        }
                        //Get ptr to mask pixel
                        fade = (byte *)((long)mask.ImageData + (y - top) * mask.Stride + x - left);
                        if (*fade == 0)
                        {
                            continue;
                        }

                        fadeVal = (float)*fade / 255.0F;
                        //Calculate monochrome alternative
                        gray = (byte)((float)src[RGB.G] * 0.5f + (float)src[RGB.B] * 0.5f);

                        //Apply monochrome alternative using mask
                        src[RGB.R] = (byte)((fadeVal * gray) + (1.0 - fadeVal) * src[RGB.R]);
                        src[RGB.G] = (byte)((fadeVal * gray) + (1.0 - fadeVal) * src[RGB.G]);
                        src[RGB.B] = (byte)((fadeVal * gray) + (1.0 - fadeVal) * src[RGB.B]);
                    }
                }
            } finally {
                if (mask != null)
                {
                    mask.Dispose();
                }
            }
        }
        public Bitmap Apply(BitmapData imageData)
        {
            UnmanagedImage image  = this.Apply(new UnmanagedImage(imageData));
            Bitmap         bitmap = image.ToManagedImage();

            image.Dispose();
            return(bitmap);
        }
예제 #10
0
        protected override void ProcessFilter(UnmanagedImage image)
        {
            UnmanagedImage unmanagedImage = opening.Apply(image);

            subtract.UnmanagedOverlayImage = unmanagedImage;
            subtract.ApplyInPlace(image);
            unmanagedImage.Dispose();
        }
예제 #11
0
        public Bitmap Apply(BitmapData imageData)
        {
            UnmanagedImage unmanagedImage = Apply(new UnmanagedImage(imageData));
            Bitmap         result         = unmanagedImage.ToManagedImage();

            unmanagedImage.Dispose();
            return(result);
        }
예제 #12
0
        /// <summary>
        /// Reset motion detector to initial state.
        /// </summary>
        ///
        /// <remarks><para>The method resets motion detection and motion processing algotithms by calling
        /// their <see cref="IMotionDetector.Reset"/> and <see cref="IMotionProcessing.Reset"/> methods.</para>
        /// </remarks>
        ///
        public void Reset( )
        {
            // lock ( _sync )
            {
                _detector?.Reset( );
                _processor?.Reset( );

                _videoWidth  = 0;
                _videoHeight = 0;

                if (_zonesFrame != null)
                {
                    _zonesFrame.Dispose( );
                    _zonesFrame = null;
                }
            }
        }
        private void process()
        {
            stopwatch.Reset();
            stopwatch.Start();
            plates = new List <string>();
            frame  = contrastCorrectionFilter.Apply(originalImage);
            frame  = grayscaleFilter.Apply(frame);

            BitmapData     frameData = frame.LockBits(new Rectangle(0, 0, frame.Width, frame.Height), ImageLockMode.ReadWrite, frame.PixelFormat);
            UnmanagedImage data      = new UnmanagedImage(frameData);

            bradleyLocalFilter.ApplyInPlace(data);
            fillHoles.ApplyInPlace(data);
            openingFilter.ApplyInPlace(data);

            blobCounter.ProcessImage(data);

            data.Dispose();
            frame.UnlockBits(frameData);

            Graphics g = Graphics.FromImage(originalImage);

            Blob[] blobs = blobCounter.GetObjectsInformation();
            foreach (Blob blob in blobs)
            {
                List <IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);
                List <IntPoint> corners    = null;

                // da li je četverougao?
                if (!shapeChecker.IsQuadrilateral(edgePoints, out corners))
                {
                    continue;
                }

                if (FindNewCornersAndCheckAspectRatio(corners))
                {
                    SimpleQuadrilateralTransformation sqt = new SimpleQuadrilateralTransformation(corners, 300, 66);
                    Bitmap plate = sqt.Apply(originalImage);
                    plate = grayscaleFilter.Apply(plate);
                    otsuThresholdFilter.ApplyInPlace(plate);

                    if (!IsLicensePlate(plate))
                    {
                        continue;
                    }

                    String plateText;
                    if (FindText(plate, out plateText))
                    {
                        g.DrawPolygon(pen, ToPointsArray(corners));
                        frame = plate;
                        plates.Add(plateText);
                    }
                }
            }
            g.Dispose();
            stopwatch.Stop();
        }
        protected override void ProcessFilter(UnmanagedImage image)
        {
            UnmanagedImage image2 = image.Clone();

            this.closing.ApplyInPlace(image);
            this.subtract.UnmanagedOverlayImage = image2;
            this.subtract.ApplyInPlace(image);
            image2.Dispose();
        }
예제 #15
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="image">Source image data.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage image)
        {
            // perform opening on the source image
            UnmanagedImage openedImage = opening.Apply(image);

            // subtract opened image from source image
            subtract.UnmanagedOverlayImage = openedImage;
            subtract.ApplyInPlace(image);

            openedImage.Dispose( );
        }
 /// <summary>
 /// Reset motion detector to initial state.
 /// </summary>
 public void Reset()
 {
     lock (this)
     {
         if (_motionFrame != null)
         {
             _motionFrame.Dispose();
             _motionFrame = null;
         }
     }
 }
예제 #17
0
        public static UnmanagedImage Resize(this UnmanagedImage thisImage, int width, int height, bool disposeInput)
        {
            if (!disposeInput)
            {
                return(Resize(thisImage, width, height));
            }

            var result = Resize(thisImage, width, height);

            thisImage.Dispose();
            return(result);
        }
예제 #18
0
        private void dispose()
        {
            if (pictureBox1.Image != null)
            {
                pictureBox1.Image.Dispose();
            }

            if (img != null)
            {
                img.Dispose();
            }
        }
        public UnmanagedImage Apply(UnmanagedImage image)
        {
            UnmanagedImage image2 = this.baseFilter.Apply(image);

            for (int i = 1; i < this.iterations; i++)
            {
                UnmanagedImage image3 = image2;
                image2 = this.baseFilter.Apply(image3);
                image3.Dispose();
            }
            return(image2);
        }
예제 #20
0
        public UnmanagedImage Apply(UnmanagedImage image)
        {
            UnmanagedImage unmanagedImage = baseFilter.Apply(image);

            for (int i = 1; i < iterations; i++)
            {
                UnmanagedImage unmanagedImage2 = unmanagedImage;
                unmanagedImage = baseFilter.Apply(unmanagedImage2);
                unmanagedImage2.Dispose();
            }
            return(unmanagedImage);
        }
        public static System.Drawing.Image ResizeImage(System.Drawing.Image SourceImage)
        {
            try {
//				// binarization filtering sequence
//	            FiltersSequence filter = new FiltersSequence(
//	                new Crop(rect),
//	                new Median(),
//	                new ContrastCorrection(),
//	                //new Mean(),
//	                new AForge.Imaging.Filters.Blur(),
//	                new GrayscaleBT709(),
//	                //new Threshold(),
//	                new Threshold(),
//	                new Invert()
//
//	            );


                // load image
                Bitmap image = (Bitmap)SourceImage;

                // format image
                AForge.Imaging.Image.Clone(image, image.PixelFormat);
                //            AForge.Imaging.Image.FormatImage(ref image);

                // lock the source image
                BitmapData sourceData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);

                // create filter
                ResizeBilinear filter = new ResizeBilinear(image.Width / 2, image.Height / 2);
                // apply the filter
//	            Bitmap newImage = filter.Apply( image );

                UnmanagedImage binarySource = filter.Apply(new UnmanagedImage(sourceData));


                Bitmap binarizedImage = binarySource.ToManagedImage();


                // unlock source image
                image.UnlockBits(sourceData);

                // dispose temporary binary source image
                binarySource.Dispose();

                System.Drawing.Image img = (System.Drawing.Image)binarizedImage;

                return(img);
            } catch (Exception ex) {
                throw ex;
            }
        }
예제 #22
0
        /// <summary>
        /// Process the filter on the specified image.
        /// </summary>
        ///
        /// <param name="image">Source image data.</param>
        ///
        protected override unsafe void ProcessFilter(UnmanagedImage image)
        {
            // copy source image
            UnmanagedImage sourceImage = image.Clone( );

            // perform closing on the source image
            closing.ApplyInPlace(image);
            // subtract source image from the closed image
            subtract.UnmanagedOverlayImage = sourceImage;
            subtract.ApplyInPlace(image);

            sourceImage.Dispose( );
        }
예제 #23
0
        /// <summary>
        /// Reset motion detector to initial state.
        /// </summary>
        ///
        /// <remarks><para>The method resets motion detection and motion processing algotithms by calling
        /// their <see cref="IMotionDetector.Reset"/> and <see cref="IMotionProcessing.Reset"/> methods.</para>
        /// </remarks>
        ///
        public void Reset( )
        {
            lock ( sync )
            {
                if (detector != null)
                {
                    detector.Reset( );
                }
                if (processor != null)
                {
                    processor.Reset( );
                }

                videoWidth  = 0;
                videoHeight = 0;

                if (zonesFrame != null)
                {
                    zonesFrame.Dispose( );
                    zonesFrame = null;
                }
            }
        }
예제 #24
0
        private void SetUnmanagedLastFrame(Bitmap frame)
        {
            lock (this)
            {
                try
                {
                    if (_lastFrame != null)
                    {
                        _lastFrame.Dispose();
                    }

                    _lastFrame = UnmanagedImage.FromManagedImage(frame);
                }
                catch { }
            }
        }
        /// <summary>
        /// Apply filter to an image.
        /// </summary>
        ///
        /// <param name="imageData">Source image to apply filter to.</param>
        ///
        /// <returns>Returns filter's result obtained by applying the filter to
        /// the source image.</returns>
        ///
        /// <remarks>The filter accepts bitmap data as input and returns the result
        /// of image processing filter as new image. The source image data are kept
        /// unchanged.</remarks>
        ///
        /// <exception cref="ApplicationException">No filters were added into the filters' sequence.</exception>
        ///
        public Bitmap Apply(BitmapData imageData)
        {
            // to increase performance the method passes execution to the method, which
            // operates with unmanaged images - this saves time, because redundant managed
            // locks/unlocks are eliminated

            // get result as an unmanaged image
            UnmanagedImage dstUnmanagedImage = Apply(new UnmanagedImage(imageData));
            // convert unmanaged image to managed
            Bitmap dstImage = dstUnmanagedImage.ToManagedImage( );

            // dispose unmanaged mage
            dstUnmanagedImage.Dispose( );

            return(dstImage);
        }
        /// <summary>
        /// Apply corrections and crop a specific part of an image in order to perform OCR
        /// </summary>
        /// <param name="SourceImage"></param>
        /// <param name="rect"></param>
        /// <returns></returns>
        public static Bitmap PreprocessOCR(Bitmap SourceImage, Rectangle rect)
        {
            try {
                // binarization filtering sequence
                FiltersSequence filter = new FiltersSequence(
                    new Crop(rect),
                    new Median(),
                    new ContrastCorrection(),
                    //new Mean(),
                    new AForge.Imaging.Filters.Blur(),
                    new GrayscaleBT709(),
                    //new Threshold(),
                    new Threshold(),
                    new Invert()

                    );


                // load image
                Bitmap image = SourceImage;

                // format image
                AForge.Imaging.Image.Clone(image, image.PixelFormat);
                //            AForge.Imaging.Image.FormatImage(ref image);

                // lock the source image
                BitmapData sourceData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);

                // apply filters and binarize the image
                UnmanagedImage binarySource = filter.Apply(new UnmanagedImage(sourceData));


                Bitmap binarizedImage = binarySource.ToManagedImage();


                // unlock source image
                image.UnlockBits(sourceData);

                // dispose temporary binary source image
                binarySource.Dispose();

                return(binarizedImage);
            } catch (Exception ex) {
                throw ex;
            }
        }//preprocess
예제 #27
0
        public void Apply(UnmanagedImage sourceImage, UnmanagedImage destinationImage)
        {
            if (iterations == 1)
            {
                baseFilter.Apply(sourceImage, destinationImage);
                return;
            }
            UnmanagedImage unmanagedImage = baseFilter.Apply(sourceImage);

            iterations--;
            for (int i = 1; i < iterations; i++)
            {
                UnmanagedImage unmanagedImage2 = unmanagedImage;
                unmanagedImage = baseFilter.Apply(unmanagedImage2);
                unmanagedImage2.Dispose();
            }
            baseFilter.Apply(unmanagedImage, destinationImage);
        }
 public void Apply(UnmanagedImage sourceImage, UnmanagedImage destinationImage)
 {
     if (this.iterations == 1)
     {
         this.baseFilter.Apply(sourceImage, destinationImage);
     }
     else
     {
         UnmanagedImage image = this.baseFilter.Apply(sourceImage);
         this.iterations--;
         for (int i = 1; i < this.iterations; i++)
         {
             UnmanagedImage image2 = image;
             image = this.baseFilter.Apply(image2);
             image2.Dispose();
         }
         this.baseFilter.Apply(image, destinationImage);
     }
 }
        public UnmanagedImage Apply(UnmanagedImage image)
        {
            int count = base.InnerList.Count;

            if (count == 0)
            {
                throw new ApplicationException("No filters in the sequence.");
            }
            UnmanagedImage image2 = null;
            UnmanagedImage image3 = null;

            image2 = ((IFilter)base.InnerList[0]).Apply(image);
            for (int i = 1; i < count; i++)
            {
                image3 = image2;
                image2 = ((IFilter)base.InnerList[i]).Apply(image3);
                image3.Dispose();
            }
            return(image2);
        }
        // Reset motion detector to initial state
        private void Reset(bool force)
        {
            lock ( _sync )
            {
                if (
                    (_backgroundFrame != null) &&
                    (force || (_manuallySetBackgroundFrame == false))
                    )
                {
                    _backgroundFrame.Dispose( );
                    _backgroundFrame = null;
                }


                _motionFrame?.Dispose( );
                _motionFrame = null;

                _tempFrame?.Dispose( );
                _tempFrame = null;
            }
        }