Example #1
0
        //Overload for Aurigma bitmaps
        public static bool CheckForMovement(Aurigma.GraphicsMill.Bitmap aurigmaFile)
        {
            bool verdict = false;

            using (System.Drawing.Bitmap inputFile = aurigmaFile.ToGdiPlusBitmap())
            {
                verdict = AnalyzeImage(inputFile);
            }
            return(verdict);
        }
Example #2
0
        public static bool AnalyzeImage(System.Drawing.Bitmap inputBitmap)
        {
            Stopwatch stopWatch = new Stopwatch();

            stopWatch.Start();

            if (inputBitmap.Height < blocksize || inputBitmap.Width < blocksize)
            {
                return(false);
            }
            movementBitmap = BuildMovementBitmap();

            //Clone movement image to 24-bit small Image - Workaround to fix PixelFormat
            System.Drawing.Bitmap movementPattern = new System.Drawing.Bitmap(movementBitmap.Width, movementBitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            using (Graphics gr = Graphics.FromImage(movementPattern))
            {
                gr.DrawImage(movementBitmap, new Rectangle(0, 0, movementPattern.Width, movementPattern.Height));
            }

            bool IsThereMovement = false;

            Aurigma.GraphicsMill.Bitmap analyzeBitmap = new Aurigma.GraphicsMill.Bitmap(inputBitmap);

            //Get an area of interest
            analyzeBitmap = FindAreaOfInterest(analyzeBitmap);

            //Process area of interest bitmap into B&W we can analyze
            analyzeBitmap = GreyscaleAndThreshold(analyzeBitmap);

            //Have to turn it back into 24-bit or the pattern matching won't work
            analyzeBitmap.ColorManagement.Convert(Aurigma.GraphicsMill.PixelFormat.Format24bppRgb);

            //...Aaaaaaand back to a system Bitmap
            System.Drawing.Bitmap matchBitmap = analyzeBitmap.ToGdiPlusBitmap();

            // create template matching algorithm's instance
            // (We set similarity threshold at top of code to 90%)
            ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(THRESHOLD);

            // find all matchings with specified above similarity
            TemplateMatch[] matches = tm.ProcessImage(matchBitmap, movementPattern);

            //Find any matches?
            if (matches.Length > 0)
            {
                TemplateMatch match = matches[0];
                IsThereMovement = true;
            }

            movementBitmap.Dispose();
            analyzeBitmap.Dispose();
            matchBitmap.Dispose();
            movementPattern.Dispose();

            stopWatch.Stop();
            TimeSpan ts = stopWatch.Elapsed;

            MessageBox.Show("Processed in " + ts.TotalSeconds + " seconds.");

            return(IsThereMovement);
        }
Example #3
0
        protected override int _AddImageInternal(Aurigma.GraphicsMill.Bitmap image, int destinationIndex)
        {
            if (image == null)
            {
                return(-1);
            }

            int result = -1;

            lock (this)
            {
                if (_imageList == null)
                {
                    throw new Aurigma.GraphicsMill.UnexpectedException(StringResources.GetString("InternalImageListCannotBeNull"));
                }

                bool disposeImage = false;
                if (_imageList.ImageSize.Width != image.Width || _imageList.ImageSize.Height != image.Height)
                {
                    Aurigma.GraphicsMill.Bitmap imageCopy = ConvertToNonextendedRgb(image);
                    if (imageCopy == image)
                    {
                        imageCopy = new Aurigma.GraphicsMill.Bitmap(image);
                    }

                    imageCopy.Transforms.Resize(_imageList.ImageSize.Width, _imageList.ImageSize.Height, Transforms.ResizeInterpolationMode.High, Transforms.ResizeMode.Shrink);

                    int destX = (_imageList.ImageSize.Width - imageCopy.Width) / 2,
                        destY = (_imageList.ImageSize.Height - imageCopy.Height) / 2;

                    Aurigma.GraphicsMill.Bitmap resultImage = new Aurigma.GraphicsMill.Bitmap(
                        _imageList.ImageSize.Width,
                        _imageList.ImageSize.Height,
                        Aurigma.GraphicsMill.PixelFormat.Format32bppArgb,
                        new Aurigma.GraphicsMill.RgbColor(0xff, 0xff, 0xff, 0x0));

                    resultImage.Draw(imageCopy, destX, destY, image.Width, image.Height, Aurigma.GraphicsMill.Transforms.CombineMode.Copy, Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode.Low);
                    imageCopy.Dispose();

                    image        = resultImage;
                    disposeImage = true;
                }

                if (destinationIndex != -1)
                {
                    _imageList.Images[destinationIndex] = image.ToGdiPlusBitmap();
                    result = destinationIndex;
                }
                else
                {
                    _imageList.Images.Add(image.ToGdiPlusBitmap());
                    result = _imageList.Images.Count - 1;
                }

                if (disposeImage)
                {
                    image.Dispose();
                }
            }

            return(result);
        }
        protected override int _AddImageInternal(Bitmap image, int destinationIndex)
        {
            if (_imageList == null)
            {
                throw new Aurigma.GraphicsMill.UnexpectedException(StringResources.GetString("InternalImageListCannotBeNull"));
            }

            int result = -1;

            Aurigma.GraphicsMill.Bitmap imgResult = null;

            try
            {
                CreateListImage(_imageList.ImageSize, _listItemBackgroundColor, _listItemBackgroundImage, _listItemForegroundImage, image, out imgResult);

                try
                {
                    if (destinationIndex != -1)
                    {
                        _imageList.Images[destinationIndex] = imgResult.ToGdiPlusBitmap();
                        result = destinationIndex;
                    }
                    else
                    {
                        _imageList.Images.Add(imgResult.ToGdiPlusBitmap());
                        result = _imageList.Images.Count - 1;
                    }
                }
                catch (System.InvalidOperationException)
                {
                    if (_nextIndexToRewrite.Count > 0)
                    {
                        int index = (int)_nextIndexToRewrite.Dequeue();
                        ReplaceImage(index, imgResult.ToGdiPlusBitmap());
                        result = index;
                    }
                    else
                    {
                        throw;
                    }
                }

                if (result != -1 && result != 0)
                {
                    _nextIndexToRewrite.Enqueue(result);
                }
            }
            catch
            {
                result = -1;
            }
            finally
            {
                if (imgResult != null)
                {
                    imgResult.Dispose();
                }
            }

            return(result);
        }
Example #5
0
        private System.Drawing.Bitmap CreateDrawnImage()
        {
            var converter = new Aurigma.GraphicsMill.Transforms.ColorConverter();

            Aurigma.GraphicsMill.Bitmap interimImage = null;
            System.Drawing.Bitmap       result       = null;

            System.IntPtr dc          = NativeMethods.GetDC(System.IntPtr.Zero);
            float         monitorResX = NativeMethods.GetDeviceCaps(dc, NativeMethods.LOGPIXELSX);
            float         monitorResY = NativeMethods.GetDeviceCaps(dc, NativeMethods.LOGPIXELSY);

            NativeMethods.ReleaseDC(System.IntPtr.Zero, dc);

            float scaleX, scaleY;

            if (_scaleToActualSize)
            {
                float resX = _image.DpiX;
                float resY = _image.DpiY;

                scaleX = resX > VObject.Eps ? monitorResX / resX : 1.0f;
                scaleY = resY > VObject.Eps ? monitorResY / resY : 1.0f;
            }
            else
            {
                scaleX = 1.0f;
                scaleY = 1.0f;
            }
            bool performScale = System.Math.Abs(scaleX - 1.0f) > VObject.Eps || System.Math.Abs(scaleY - 1.0f) > VObject.Eps;

            try
            {
                if (_image.PixelFormat == Aurigma.GraphicsMill.PixelFormat.Format24bppRgb ||
                    _image.PixelFormat == Aurigma.GraphicsMill.PixelFormat.Format32bppRgb ||
                    _image.PixelFormat == Aurigma.GraphicsMill.PixelFormat.Format32bppArgb)
                {
                    if (!performScale)
                    {
                        result = _image.ToGdiPlusBitmap();
                    }
                    else
                    {
                        interimImage = new Aurigma.GraphicsMill.Bitmap();
                        using (Aurigma.GraphicsMill.Transforms.Resize resize = new Aurigma.GraphicsMill.Transforms.Resize())
                        {
                            resize.Width             = (int)(_image.Width * scaleX);
                            resize.Height            = (int)(_image.Height * scaleY);
                            resize.InterpolationMode = scaleToActualSizeInterpolationMode;
                            interimImage             = resize.Apply(_image);
                        }

                        result = interimImage.ToGdiPlusBitmap();
                    }
                }
                else
                {
                    if (_image.PixelFormat.IsIndexed || _image.HasAlpha)
                    {
                        converter.DestinationPixelFormat = Aurigma.GraphicsMill.PixelFormat.Format32bppArgb;
                    }
                    else
                    {
                        converter.DestinationPixelFormat = Aurigma.GraphicsMill.PixelFormat.Format24bppRgb;
                    }

                    interimImage = new Aurigma.GraphicsMill.Bitmap();
                    interimImage = converter.Apply(_image);

                    if (performScale)
                    {
                        interimImage.Transforms.Resize((int)(_image.Width * scaleX), (int)(_image.Height * scaleY));
                    }

                    result = interimImage.ToGdiPlusBitmap();
                }
            }
            catch
            {
                if (result != null)
                {
                    result.Dispose();
                }
                throw;
            }
            finally
            {
                converter.Dispose();

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

            result.SetResolution(monitorResX, monitorResY);
            return(result);
        }