Ejemplo n.º 1
1
 private void btnCrop_Click(object sender, EventArgs e)
 {
     IFilter imgeFilter = new Crop(new Rectangle(rectCropArea.X, rectCropArea.Y, rectCropArea.Width, rectCropArea.Height));
     Bitmap imgEffect = new Bitmap(img, panelImage.Width, panelImage.Height);
     img = imgeFilter.Apply(imgEffect);
     panelImage.BackgroundImage = img;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Applies the blob extraction feature of Aforge
        /// </summary>
        /// <param name="Mask">Mask from the flood-fill step</param>
        /// <param name="Source">Source image (full image)</param>
        /// <returns>A list of tuples(blob from mask, rectangle from source)</returns>
        private static List <Tuple <Bitmap, Bitmap> > ApplyBlobExtractor(Bitmap Mask, Bitmap Source)
        {
            List <Tuple <Bitmap, Bitmap> > BlobSrcblock = new List <Tuple <Bitmap, Bitmap> >();

            log.Debug("Using AForge Blob Counter to Process Mask");
            AForge.Imaging.BlobCounter blobCounter = new AForge.Imaging.BlobCounter();

            // Sort order
            blobCounter.ObjectsOrder = AForge.Imaging.ObjectsOrder.XY;
            blobCounter.ProcessImage(Mask);
            AForge.Imaging.Blob[] blobs = blobCounter.GetObjects(Mask, false);

            log.Info("Use the Blob Extraction Results to reverse extract blobs from images");
            // Adding images into the image list
            AForge.Imaging.UnmanagedImage currentImg;
            foreach (AForge.Imaging.Blob blob in blobs)
            {
                Rectangle myRect = blob.Rectangle;
                currentImg = blob.Image;
                Bitmap exBlob = currentImg.ToManagedImage();
                AForge.Imaging.Filters.Crop filter = new AForge.Imaging.Filters.Crop(myRect);
                Bitmap exSrc = filter.Apply(Source);
                BlobSrcblock.Add(new Tuple <Bitmap, Bitmap>(exBlob, exSrc));
            }
            log.Info("Extraction Complete: returning List of ( blob bitmap, src bitmap)");
            return(BlobSrcblock);
        }
Ejemplo n.º 3
0
 public static Bitmap CropImage(this Bitmap source)
 {
     const int widthCrop = 80;
     const int heightCrop = 50;
     Crop filter = new Crop(new Rectangle(widthCrop, heightCrop, source.Width- 2* widthCrop -10, source.Height - 10 - 2 * heightCrop));
     return filter.Apply(source);
 }
Ejemplo n.º 4
0
        public List<Bitmap> PickSlices(IReadOnlyList<Bitmap> inputImages, Size sliceSize)
        {
            var edgeImages = inputImages.Select(grayscaleFilter.Apply).ToList();
             edgeImages.ForEach(edgeDetectionFilter.ApplyInPlace);

             var edgeDensityHistograms = edgeImages.Select(x => new HorizontalIntensityStatistics(x).Gray).ToList();

             var imagesAndHistograms = Enumerable.Range(0, inputImages.Count).Select(index => {
            return new ImagesAndHistogram {
               OriginalImage = inputImages[index],
               EdgeImage = edgeImages[index],
               EdgeDensityHistogram = edgeDensityHistograms[index],
               Rating = ratingCalculator.ComputeRating(inputImages[index], edgeDensityHistograms[index], sliceSize.Width, sliceSize.Height)
            };
             }).OrderBy(x => x.Rating).ToList();

             var sliceAspect = sliceSize.Width / (double)sliceSize.Height;
             foreach (var imagesAndHistogram in imagesAndHistograms) {
            var desiredWidth = (int)(imagesAndHistogram.OriginalImage.Height * sliceAspect);
            if (desiredWidth > imagesAndHistogram.OriginalImage.Width) {
               continue;
            }
            var range = thumbnailGeneratorUtilities.GetRangeOfWidth(imagesAndHistogram.EdgeDensityHistogram, desiredWidth);

            var horizontalCrop = new Crop(new Rectangle(range.Min, 0, range.Max - range.Min, imagesAndHistogram.OriginalImage.Height)).Apply(imagesAndHistogram.OriginalImage);
            var horizontalCrop24bpp = horizontalCrop.Clone(new Rectangle(Point.Empty, horizontalCrop.Size), PixelFormat.Format24bppRgb);
            imagesAndHistogram.SliceImage = horizontalCrop24bpp;
            var resizer = new ResizeBicubic(sliceSize.Width, sliceSize.Height);
            imagesAndHistogram.SliceImageResized = resizer.Apply(horizontalCrop24bpp);
             }

             return imagesAndHistograms.Where(x => x.SliceImageResized != null).Select(x => x.SliceImageResized).ToList();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Looks for the brightest pixel after applying a redness filter. Narrows search first using a resampled copy of the image to eliminate edge dots. 
        /// Expects an image that is already cropped to the interested area for faster processing.
        /// </summary>
        /// <param name="img"></param>
        /// <param name="mouse"></param>
        /// <param name="maxDistanceFromMouse"></param>
        /// <returns></returns>
        public unsafe Point FindMaxPixel(UnmanagedImage img, PointF mouse, float maxDistanceFromMouse)
        {
            int width = 15;
            int height = (int)Math.Ceiling((double)img.Height / (double)img.Width * width);

            if (width <= img.Width && height <= img.Height + 1) {
                width = img.Width;
                height = img.Height;
            }

            double scale = (double)img.Width / (double)width;

            UnmanagedImage lowRed = null;
            try {
                if (width != img.Width && height != img.Height) {
                    using (Bitmap reduced = new Bitmap(width, height, PixelFormat.Format24bppRgb))
                    using (Graphics g = Graphics.FromImage(reduced))
                    using (ImageAttributes ia = new ImageAttributes()) {
                        g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
                        g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                        g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                        ia.SetWrapMode(System.Drawing.Drawing2D.WrapMode.TileFlipXY);
                        g.DrawImage(img.ToManagedImage(false), new Rectangle(0, 0, width, height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);
                        //TODO: Not sure if ToManagedImage will stick around after the underying image is disposed. I know that the bitmap data will be gone, guess that's most of it.
                        using (UnmanagedImage rui = UnmanagedImage.FromManagedImage(reduced)) {
                            lowRed = new RedEyeFilter(2).Apply(rui); // Make an copy using the red eye filter
                        }
                    }
                } else {
                    //Don't resample unless needed
                    lowRed = new RedEyeFilter(2).Apply(img);
                }

                Point max = GetMax(lowRed, new PointF(mouse.X / (float)scale, mouse.Y / (float)scale), maxDistanceFromMouse / scale);

                //We weren't scaling things? OK, cool...
                if (scale == 0) return max;

                //Otherwise, let's get the unscaled pixel.
                //Calculate the rectangle surrounding the selected pixel, but in source coordinates.
                int tinySize = (int)Math.Ceiling(scale) + 1;
                Rectangle tinyArea = new Rectangle((int)Math.Floor(scale * (double)max.X), (int)Math.Floor(scale * (double)max.Y), tinySize, tinySize);
                if (tinyArea.Right >= img.Width) tinyArea.Width -= img.Width - tinyArea.Right + 1;
                if (tinyArea.Bottom >= img.Height) tinyArea.Height -= img.Height - tinyArea.Bottom + 1;
                //Filter it and look
                using (UnmanagedImage tiny = new Crop(tinyArea).Apply(img)) {
                    using (UnmanagedImage tinyRed = new RedEyeFilter(2).Apply(tiny)) {
                        max = GetMax(tinyRed);
                        max.X += tinyArea.X;
                        max.Y += tinyArea.Y;
                    }
                }
                return max;
            } finally {
                if (lowRed != null) lowRed.Dispose();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Celem metody jest przykadrowanie otrzymanego obrazka, tak aby widok zawierał jedynie kartkę papieru, oraz zwrócenie go do metody UploadFile
        /// </summary>
        /// <param name="img"></param>
        /// <returns></returns>
        public System.Drawing.Image CropImage(System.Drawing.Image img)
        {
            //DocumentSkewChecker skewChecker = new DocumentSkewChecker();
            //double angle = skewChecker.GetSkewAngle(image);
            //RotateBilinear rotationFilter = new RotateBilinear(-angle);
            //rotationFilter.FillColor = Color.White;
            //Bitmap rotatedImage = rotationFilter.Apply(image);

            Bitmap image = new Bitmap(img);

            UnmanagedImage grayImage = null;

            if (image.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                grayImage = UnmanagedImage.FromManagedImage(image);
            }
            else
            {
                grayImage = UnmanagedImage.Create(image.Width, image.Height,
                    PixelFormat.Format8bppIndexed);
                Grayscale.CommonAlgorithms.BT709.Apply(UnmanagedImage.FromManagedImage(image), grayImage);
            }

            CannyEdgeDetector edgeDetector = new CannyEdgeDetector();
            UnmanagedImage edgesImage = edgeDetector.Apply(grayImage);

            OtsuThreshold thresholdFilter = new OtsuThreshold();
            thresholdFilter.ApplyInPlace(edgesImage);

            Dilatation DilatationFilter = new Dilatation();
            DilatationFilter.Apply(edgesImage);

            Opening OpeningFilter = new Opening();
            OpeningFilter.Apply(edgesImage);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.MinHeight = 32;
            blobCounter.MinWidth = 32;
            blobCounter.FilterBlobs = true;
            blobCounter.ObjectsOrder = ObjectsOrder.Size;

            blobCounter.ProcessImage(edgesImage);
            Blob[] blobs = blobCounter.GetObjectsInformation();

            ExtractBiggestBlob BiggestBlob = new ExtractBiggestBlob();
            Bitmap biggestBlobsImage = BiggestBlob.Apply(edgesImage.ToManagedImage());
            AForge.IntPoint BiggestBlogCorners = BiggestBlob.BlobPosition;

            Crop cropFilter = new Crop(new Rectangle(BiggestBlogCorners.X, BiggestBlogCorners.Y, biggestBlobsImage.Width, biggestBlobsImage.Height));
            Bitmap croppedImage = cropFilter.Apply(image);
            return croppedImage;
        }
Ejemplo n.º 7
0
        public static List<Ipoint> fillFeatures(Bitmap bmp, int com_x, int com_y, double[] input_i)
        {
            /*int cropx = Math.Max(com_x - 12, 0);
            int cropy = Math.Max(com_y - 12, 0);
            bmp = new Crop(new Rectangle(cropx, cropy, 24, 24)).Apply(bmp);*/
            bmp = new Crop(new Rectangle(16, 16, 48, 48)).Apply(bmp);
            bmp = new ResizeBicubic(12, 12).Apply(bmp);

            //bmp.Save(Constants.base_folder + "hasat\\zzz\\" + DateTime.Now.Ticks + ".bmp");

            for (int yy = 0; yy < 12; yy++)
            {
                for (int xx = 0; xx < 12; xx++)
                {
                    input_i[yy * 12 + xx] = 1d - Constants.getColorValForLabeling(bmp.GetPixel(xx, yy));
                }
            }

            return null;
        }
Ejemplo n.º 8
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;
        }
Ejemplo n.º 9
0
        public static void fillFeatures_SURF(Bitmap inbmp, int com_x, int com_y, Node[] nds)
        {
            int radius = 16;
            if ("circle".Equals(Constants.CIRCLE_TRIANGLE))
                radius = 25;
            int diameter = 2 * radius + 1;
            List<Ipoint> featureList = null;
            if ("SVM_SURF".Equals(Constants.NN_SVM_SURF))
            {
                //inbmp.Save(Constants.base_folder + "hasat\\zzz\\0_" + inbmp.Tag);
                int cropx = Math.Max(com_x - radius, 0);
                int cropy = Math.Max(com_y - radius, 0);

                int x_min = 100, x_max = 0;
                int y_min = 100, y_max = 0;

                Bitmap bmp = new Crop(new Rectangle(cropx, cropy, diameter, diameter)).Apply(inbmp);
                //bmp.Save(Constants.base_folder + "hasat\\zzz\\1_" + inbmp.Tag);

                for (int yy = 0; yy < diameter; yy++)
                {
                    for (int xx = 0; xx < diameter; xx++)
                    {
                        if (bmp.GetPixel(xx, yy).R < 128)
                        {
                            x_min = Math.Min(x_min, xx);
                            y_min = Math.Min(y_min, yy);
                            x_max = Math.Max(x_max, xx);
                            y_max = Math.Max(y_max, yy);
                        }
                    }
                }
                x_min--; y_min--; x_max++; y_max++;
                x_min = Math.Max(x_min, 0);
                y_min = Math.Max(y_min, 0);

                int x_length = x_max - x_min;
                int y_length = y_max - y_min;
                if (x_length > y_length)
                    bmp = new Crop(new Rectangle(x_min, Math.Max((radius - x_length / 2), 0), x_length, x_length)).Apply(bmp);
                else
                    bmp = new Crop(new Rectangle(Math.Max((radius - y_length / 2), 0), y_min, y_length, y_length)).Apply(bmp);

                //bmp.Save(Constants.base_folder + "hasat\\zzz\\2_" + inbmp.Tag);
                bmp = new ResizeBicubic(128, 128).Apply(bmp);
                //bmp.Save(Constants.base_folder + "hasat\\zzz\\3_" + inbmp.Tag);
                bmp = new ImageFilters.ColorLabelFilter(new Color[] { Color.White, Color.Black }, true).Apply(bmp);
                //bmp.Save(Constants.base_folder + "hasat\\zzz\\4_" + inbmp.Tag);

                ByteTools.imageCoM(bmp, ref com_x, ref com_y);

                long start = DateTime.Now.Ticks / 10000;
                Hashtable feat_hash = new Hashtable();
                for (int i = 0; i < NNTrain.surf_array.Length && NNTrain.surf_array[i] != null; i++)
                {
                    start = DateTime.Now.Ticks / 10000;
                    List<Ipoint> featureList_i = NNTrain.surf_array[i].getDescriptors(bmp);
                    // Console.WriteLine("TIME_2_" + i + "[" + featureList_i.Count + "]: " + (DateTime.Now.Ticks / 10000 - start));
                    start = DateTime.Now.Ticks / 10000;
                    if (featureList_i == null) continue;
                    for (int x = 0; x < featureList_i.Count; x++)
                    {
                        if (bmp.GetPixel((int)featureList_i[x].x, (int)featureList_i[x].y).R > 128)
                            continue;
                        if (!feat_hash.ContainsKey(Math.Round(featureList_i[x].x) + "_" + Math.Round(featureList_i[x].y)))
                        {
                            feat_hash.Add(Math.Round(featureList_i[x].x) + "_" + Math.Round(featureList_i[x].y), featureList_i[x]);
                        }
                    }
                }

                featureList = new List<Ipoint>(feat_hash.Values.Count);
                IEnumerator enm = feat_hash.GetEnumerator();
                while (enm.MoveNext())
                    featureList.Add((Ipoint)((DictionaryEntry)enm.Current).Value);
                featureList = featureList.GetRange(0, (int)Math.Min(featureList.Count, NNTrain.NUM_MAX_INTEREST_POINTS));

                /*
                 // bmp.Save(Constants.base_folder + "hasat\\zzz\\5_" + inbmp.Tag);
                 Graphics g = Graphics.FromImage(bmp);
                 g.DrawRectangle(Pens.Cyan, com_x-1, com_y-1, 2, 2);
                 SURF.paintSURFPoints(bmp, featureList);
                 bmp.Save(Constants.base_folder + "hasat\\zzz\\6_" + inbmp.Tag);
                */
                int CURRENT_POINT_INDEX = 0;

                for (int xx = 0; xx < nds.Length; xx++)
                {
                    nds[xx] = new Node(xx + 1, 0);
                }

                float com_linear_pos = 128 * com_y + com_x;
                for (int j = 0; j < NNTrain.NUM_MAX_INTEREST_POINTS; j++, CURRENT_POINT_INDEX++)
                {
                    if (CURRENT_POINT_INDEX == featureList.Count)
                        CURRENT_POINT_INDEX = 0;

                    if (featureList.Count == 0)
                    {
                        // nds[j] = new Node(j + 1, 0);
                    }
                    else
                    {
                        float xx = featureList[CURRENT_POINT_INDEX].x;
                        float yy = featureList[CURRENT_POINT_INDEX].y;

                        int y_quantized = (int)Math.Round((yy - com_y) / NNTrain.quantizer);
                        int x_quantized = (int)Math.Round((xx - com_x) / NNTrain.quantizer);
                        y_quantized += 4;
                        x_quantized += 4;
                        int indx = y_quantized * NNTrain.num_surf_quants + x_quantized;
                        if (indx >= 0 && indx < (NNTrain.num_surf_quants * NNTrain.num_surf_quants))
                            nds[indx].Value += 1d / NNTrain.NUM_MAX_INTEREST_POINTS;
                        else
                            Console.Write("");
                    }
                }
            }
            else if ("SVM_12SIMPLE".Equals(Constants.NN_SVM_SURF))
            {
                string filename = (string)inbmp.Tag;
                int cropx = Math.Max(com_x - radius, 0);
                int cropy = Math.Max(com_y - radius, 0);

                /*
                Graphics g = Graphics.FromImage(inbmp);
                g.DrawRectangle(Pens.Cyan, com_x-1, com_y-1, 2, 2);
                g.DrawRectangle(Pens.Red, cropx, cropy, diameter, diameter);
                inbmp.Save(Constants.base_folder + "hasat\\zzz\\0_" + filename);
                 */

                inbmp = new Crop(new Rectangle(cropx, cropy, diameter, diameter)).Apply(inbmp);

                int x_min = 100, x_max = 0;
                int y_min = 100, y_max = 0;
                for (int yy = 0; yy < diameter; yy++)
                {
                    for (int xx = 0; xx < diameter; xx++)
                    {
                        if (inbmp.GetPixel(xx, yy).R < 128)
                        {
                            x_min = Math.Min(x_min, xx);
                            y_min = Math.Min(y_min, yy);
                            x_max = Math.Max(x_max, xx);
                            y_max = Math.Max(y_max, yy);
                        }
                    }
                }
                x_min--; y_min--; x_max++; y_max++;
                x_min = Math.Max(x_min, 0);
                y_min = Math.Max(y_min, 0);

                int x_length = x_max - x_min;
                int y_length = y_max - y_min;
                if (x_length > y_length)
                {
                    inbmp = new Crop(new Rectangle(x_min, Math.Max((radius - x_length / 2), 0), x_length, x_length)).Apply(inbmp);
                }
                else
                {
                    inbmp = new Crop(new Rectangle(Math.Max((radius - y_length / 2), 0), y_min, y_length, y_length)).Apply(inbmp);
                }
                // inbmp.Save(Constants.base_folder + "hasat\\zzz\\8_" + filename);

                inbmp = new ResizeBicubic(12, 12).Apply(inbmp);

                //inbmp.Save(Constants.base_folder + "hasat\\zzz\\9_" + filename);

                for (int yy = 0; yy < 12; yy++)
                {
                    for (int xx = 0; xx < 12; xx++)
                    {
                        nds[yy * 12 + xx] = new Node(yy * 12 + xx + 1, 1d - Constants.getColorValForLabeling(inbmp.GetPixel(xx, yy)));
                    }
                }
            }

            //Graphics g = Graphics.FromImage(bmp);
            //g.DrawRectangle(Pens.Cyan, com_x, com_y, 2, 2);
        }
Ejemplo n.º 10
0
 public static Bitmap Crop(Bitmap bmp, int x, int y, int x2, int y2)
 {
     // create filter
     Crop filter = new Crop(new Rectangle(x, y, x2, y2));
     // apply the filter
     Bitmap newImage = filter.Apply(bmp);
     return newImage;
 }
Ejemplo n.º 11
0
Archivo: Camera.cs Proyecto: 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());
                }
            }
        }
        /// <summary>
        /// Recognize hands gesture.
        /// </summary>
        /// 
        /// <param name="imageData">Source image data to recognize hands gesture on.</param>
        /// <param name="bodyImageOnly">Specifies if the passed image data contain only human's body or not.</param>
        /// 
        /// <returns>Returns gesture structure, which specifies position of both hands.</returns>
        /// 
        /// <remarks><para>The <b>bodyImageOnly</b>> parameter specifies if human's body occupies the
        /// passes image from top to down and from left to rigth. If the value is set to <b>false</b>,
        /// then humans' body may occupy only part of the image, what will require image shrinking.</para></remarks>
        /// 
        public Gesture Recognize( BitmapData imageData, bool bodyImageOnly )
        {
            // check source image format
            if ( imageData.PixelFormat != PixelFormat.Format8bppIndexed )
            {
                throw new ArgumentException( "Source image can be binary (8 bpp indexed) only" );
            }

            // recognized gesture
            Gesture gesture = new Gesture( HandPosition.NotRaised, HandPosition.NotRaised );

            Bitmap bodyImage = null;
            BitmapData bodyImageData = null;

            if ( bodyImageOnly == false )
            {
                // use shrink filter to extract only body image
                Shrink shrinkFilter = new Shrink( );
                bodyImage = shrinkFilter.Apply( imageData );

                // lock body image for further processing
                bodyImageData = bodyImage.LockBits(
                    new Rectangle( 0, 0, bodyImage.Width, bodyImage.Height ),
                    ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed );
            }
            else
            {
                // use passed image as body image
                bodyImageData = imageData;
            }

            int bodyWidth = bodyImageData.Width;
            int bodyHeight = bodyImageData.Height;

            // get statistics about horizontal pixels distribution
            HorizontalIntensityStatistics his = new HorizontalIntensityStatistics( bodyImageData );
            int[] hisValues = (int[]) his.Gray.Values.Clone( );

            // build map of hands (0) and torso (1)
            double torsoLimit = torsoCoefficient * bodyHeight;

            for ( int i = 0; i < bodyWidth; i++ )
            {
                hisValues[i] = ( (double) hisValues[i] / 255 > torsoLimit ) ? 1 : 0;
            }

            // get hands' length
            int leftHand = 0;
            while ( ( hisValues[leftHand] == 0 ) && ( leftHand < bodyWidth ) )
                leftHand++;

            int rightHand = bodyWidth - 1;
            while ( ( hisValues[rightHand] == 0 ) && ( rightHand > 0 ) )
                rightHand--;
            rightHand = bodyWidth - ( rightHand + 1 );

            // get torso's width
            int torsoWidth = bodyWidth - leftHand - rightHand;

            // process left hand
            if ( ( (double) leftHand / torsoWidth ) >= handsMinProportion )
            {
                // extract left hand's image
                Crop cropFilter = new Crop( new Rectangle( 0, 0, leftHand, bodyHeight ) );
                Bitmap leftHandImage = cropFilter.Apply( bodyImageData );

                // get left hand's position
                gesture.LeftHand = GetHandPosition( leftHandImage );
            }

            // process right hand
            if ( ( (double) rightHand / torsoWidth ) >= handsMinProportion )
            {
                // extract right hand's image
                Crop cropFilter = new Crop( new Rectangle( bodyWidth - rightHand, 0, rightHand, bodyHeight ) );
                Bitmap rightHandImage = cropFilter.Apply( bodyImageData );

                // get right hand's position
                gesture.RightHand = GetHandPosition( rightHandImage );
            }

            if ( !bodyImageOnly )
            {
                // unlock body image and dispose it
                bodyImage.UnlockBits( bodyImageData );
                bodyImage.Dispose( );
            }

            return gesture;
        }
Ejemplo n.º 13
0
        private Bitmap CropDiff(Bitmap pixelDiff)
        {
            if (selection != null)
            {
                var crop = new Crop(selection.Value);

                return crop.Apply(pixelDiff);
            }

            return pixelDiff;
        }
Ejemplo n.º 14
0
 /// <summary>
 ///   Constructs a new <see cref="MatchingTracker"/> object tracker.
 /// </summary>
 /// 
 public MatchingTracker()
 {
     matcher = new ExhaustiveTemplateMatching(0);
     crop = new Crop(Rectangle.Empty);
     trackingObject = new TrackingObject();
 }
Ejemplo n.º 15
0
        public void TakePhotoOfPartAtCurrentLocation(TapeObj t)
        {
            if (t == null) return;
            var dir = Global.BaseDirectory + @"\images\";
            Directory.CreateDirectory(dir);

            var size = t.GetComponentSize();
            if (size == null) return;
            var s = new PartLocation(size.Width, size.Height); //assumes 0 degree rotation
            s = (1.5 * s) / MainForm.cameraView.downVideoProcessing.mmPerPixel; //convert to pixels and ad extra 25%
            s.Rotate(t.PartAngle * Math.PI / 180d); //and rotate to final position
            s.X = Math.Abs(s.X); //correct for sign changes
            s.Y = Math.Abs(s.Y);

            MainForm.cameraView.SetDownCameraFunctionSet("ComponentPhoto");
            Global.DoBackgroundWork(); //let new images be processed

            var topleft = MainForm.cameraView.downVideoProcessing.FrameCenter - (.5 * s);
            var rect = new Rectangle(topleft.ToPoint(), s.ToSize());
            var filter = new Crop(rect);

            using (var image = MainForm.cameraView.downVideoProcessing.GetMeasurementFrame()) {
                using (var cropped = filter.Apply(image)) {
                    var filename = dir + t.ID.Replace(" ", "_") + ".jpg";
                    if (File.Exists(filename)) File.Delete(filename);
                    cropped.Save(filename, ImageFormat.Jpeg);
                    t.TemplateFilename = filename;
                }
            }
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Detect hand on a given frame
        /// </summary>
        /// <param name="image">Image to detect the hand palm within</param>
        /// <param name="cycles">Number of snapshot cycles to check against</param>
        /// <param name="similarityRate">The threshold percent of pixels that have to match for a positive result</param>
        /// <param name="trainingPic">A reference to secondary frame field. Temporary.</param>
        /// <returns>IntPoint point at the centre of detected hand palm</returns>
        public IntPoint DetectHand(Bitmap image, int cycles, int similarityRate, PictureBox trainingPic)
        {
            IntPoint dimensions = new IntPoint(image.Width, image.Height);
            BlobCounterBase bc = new BlobCounter();
            Crop cropFilter;

            bc.FilterBlobs = true;
            bc.MinWidth = (int)(avgSize.x * 0.6);
            bc.MinHeight = (int)(avgSize.y * 0.6);
            bc.ObjectsOrder = ObjectsOrder.Size;
            bc.ProcessImage(image);
            Blob[] blobs = bc.GetObjectsInformation();

            int blobWidth = 0;
            int blobHeight = 0;
            float blobAspectRatio;
            double angle;
            RotateBilinear rotate;
            ResizeBilinear resize = new ResizeBilinear((int)avgSize.x, (int)avgSize.y);
            Bitmap bmp;
            List<IntPoint> edges = new List<IntPoint>();

            for (int b = 0; b < blobs.Length; b++)
            {
                Rectangle br = blobs[b].Rectangle;
                cropFilter = new Crop(br);
                image = cropFilter.Apply(image);
                bmp = AForge.Imaging.Image.Clone(image);
                //bc.ExtractBlobsImage(image, blobs[b], false);
                //bmp = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);
                edges = bc.GetBlobsEdgePoints(blobs[b]);
                IntPoint circleCenter = FindLargestCircleCenter(edges, blobs[b].Rectangle);
                //bmp = ImageFilters.Draw(bmp, edges);

                //Highlight circle center
                //Graphics gfx = Graphics.FromImage(bmp);
                //gfx.DrawRectangle(new Pen(Color.Red, 3), circleCenter.X - 10, circleCenter.Y - 10, 20, 20);

                blobWidth = blobs[b].Rectangle.Width;
                blobHeight = blobs[b].Rectangle.Height;
                blobAspectRatio = (float)blobWidth / blobHeight;
                if((blobAspectRatio) > avgAspectRatio + 0.08)
                {
                    float test3 = avgSize.y / avgSize.x;
                    if ((blobAspectRatio - avgAspectRatio) <= 1)
                    {
                        angle = ((blobAspectRatio - avgAspectRatio) / (test3 - avgAspectRatio)) * 90 + (1 - (blobAspectRatio - avgAspectRatio)) * 30;
                    }
                    else
                    {
                        angle = ((blobAspectRatio - avgAspectRatio) / (test3 - avgAspectRatio)) * 90;
                    }
                    rotate = new RotateBilinear(angle, false);
                    image = rotate.Apply(image);
                    //bmp = rotate.Apply(bmp);
                }
                image = resize.Apply(image);
                //bmp = resize.Apply(bmp);

                //image = cropFilter.Apply(image);
                //image = maxBlobFilter.Apply(image);
                int scannedPixels = 0;
                int matches = 0;
                float test = 0;
                float threshold = (float)similarityRate / 100;
                int imgHeight, imgWidth;
                
                for (int i = 0; i < cycles; i++)
                {
                    for (int smpl = 0; smpl < 10; smpl++)
                    {
                        if (image.Size.Width < samples[i, smpl].Size.Width)
                            imgWidth = image.Size.Width;
                        else
                            imgWidth = samples[i, smpl].Size.Width;
                        if (image.Size.Height < samples[i, smpl].Size.Height)
                            imgHeight = image.Size.Height;
                        else
                            imgHeight = samples[i, smpl].Size.Height;
                        for (int y = 0; y < imgHeight; y += 3)
                            for (int x = 0; x < imgWidth; x += 3)
                            {
                                scannedPixels++;
                                lock (samples[i, smpl])
                                {
                                    lock (image)
                                    {
                                        if (image.GetPixel(x, y).Equals(samples[i, smpl].GetPixel(x, y)))
                                        {
                                            matches++;
                                        }
                                    }
                                }
                            }
                        test = (float)matches / (float)scannedPixels;
                        if (test >= threshold)
                        {
                            //bc.ExtractBlobsImage(image, blobs[b], false);
                            //bmp.Save("detect" + detectedNum + "o.bmp");
                            //image.Save("detect" + detectedNum + "r.bmp");
                            detectedNum++;
                            return new IntPoint(dimensions.X - circleCenter.X, circleCenter.Y);

                            //return new IntPoint(image.Width - (int)blobs[b].CenterOfGravity.X, (int)blobs[b].CenterOfGravity.Y);
                        }
                        matches = 0;
                        scannedPixels = 0;
                    }
                }
            }
            return new IntPoint();
        }
Ejemplo n.º 17
0
 public Bitmap GetTopRightPart()
 {
     if (image == null)
         return null;
     Crop crop = new Crop(new Rectangle(0, 17, 15, 20));//TODO card identity
     var img = crop.Apply(image);
     return img;
 }
Ejemplo n.º 18
0
 public Bitmap GetPart(Rectangle rect)
 {
     if (image == null)
         return null;
     Crop crop = new Crop(rect);//TODO card identity
     var img = crop.Apply(image);
     return img;
 }
        private Bitmap ProcessImage(Bitmap bitmap)
        {
            ///CROP SYSTEM

            //if (cropx1 != 0 && cropy1 != 0 && cropx2 != 0 && cropy2 != 0 && Worker.selected_square != 0)
            //{
            //    Crop crop_filter = new Crop(new Rectangle(cropx1, cropy1, cropx2, cropy2));
            //     bitmap= crop_filter.Apply(bitmap);
            //    Console.WriteLine(">Crop Cutter:> Cropped\n({0},{1}),({2},{3})", cropx1, cropy1, cropx2, cropy2);
            //}

            //if (Worker.selected_square == 0)
            //{
            //    Crop crop_filter = new Crop(new Rectangle(0, 0, bitmap.Width,bitmap.Height));
            //    bitmap = crop_filter.Apply(bitmap);
            //    //Console.WriteLine(">Crop Cutter:> RESET");
            //}
            //// step 2 - locating objects

            if (cropx1 == 0 && cropy1 == 0 && cropx2 == 0 && cropy2 == 0)
            {
                cropx1 = 0;
                cropy1 = 0;
                cropx2 = bitmap.Width;
                cropy2 = bitmap.Height;

            }
            Bitmap old = bitmap;
            Crop crop_filter = new Crop(new Rectangle(cropx1, cropy1, cropx2, cropy2));

            bitmap = crop_filter.Apply(bitmap);

            // lock image
            BitmapData bitmapData = bitmap.LockBits(
                new Rectangle(0, 0, bitmap.Width, bitmap.Height),
                ImageLockMode.ReadWrite, bitmap.PixelFormat);

            //  ////invert
            // Invert inv = new Invert();
            //  // //apply the filter
            //inv.ApplyInPlace(bitmapData);

            //// create filter
            //HSLFiltering filter = new HSLFiltering();
            //// set color ranges to keep
            //filter.Hue = new IntRange(90, 300);
            //filter.Saturation = new Range(0.0f, .5f);
            //filter.Luminance = new Range(0f, 1);
            //// apply the filter
            //filter.ApplyInPlace(bitmapData);

            //EuclideanColorFiltering filter = new EuclideanColorFiltering();
            //// set center colol and radius
            //filter.CenterColor = new RGB(209, 206, 196);
            //filter.Radius = 150;
            //// apply the filter
            //filter.ApplyInPlace(bitmapData);

            //// create filter
            ChannelFiltering filter = new ChannelFiltering();
            //// set channels' ranges to keep

            Invert inv = new Invert();

            //// //apply the filter

            inv.ApplyInPlace(bitmapData);

            if (red_max > 255)
            {
                red_max = 255;
                Console.WriteLine("1THRESHOLD OVERIDE");
            }
            if (blue_max > 255)
            {
                blue_max = 255;
                Console.WriteLine("2THRESHOLD OVERIDE");
            }
            if (green_max > 255)
            {
                green_max = 255;
                Console.WriteLine("3THRESHOLD OVERIDE");
            }
            if (red > 255)
            {
                red = 254;
                Console.WriteLine("4THRESHOLD OVERIDE");
            }
            if (blue > 255)
            {
                blue = 254;
                Console.WriteLine("5THRESHOLD OVERIDE");
            }
            if (green > 255)
            {
                green = 254;
                Console.WriteLine("6THRESHOLD OVERIDE");
            }

            if (red < 0)
            {
                red = 1;
                Console.WriteLine("7THRESHOLD UNDERIDE");
            }
            if (blue < 0)
            {
                blue = 1;
                Console.WriteLine("8THRESHOLD UNDERIDE");
            }
            if (green < 0)
            {
                green = 1;
                Console.WriteLine("9THRESHOLD UNDERIDE");
            }

            if (red_max < 0)
            {
                red_max = 255;
                Console.WriteLine("10THRESHOLD UNDERIDE");
            }
            if (blue_max < 0)
            {
                blue_max = 255;
                Console.WriteLine("11THRESHOLD UNDERIDE");
            }
            if (green_max < 0)
            {
                green_max = 255;
                Console.WriteLine("13THRESHOLD UNDERIDE");
            }

            if (red_max < red)
            {
                Console.WriteLine("14THRESHOLD UNION ERROR");
                red = 0;
                red_max = 255;
            }

            if (blue_max < blue)
            {
                blue = 0;
                Console.WriteLine("15THRESHOLD UNION ERROR");
                blue_max = 255;
            }
            if (green_max < green)
            {
                green = 0;
                green_max = 255;
                Console.WriteLine("16THRESHOLD UNION ERROR");
            }

            file.WriteLine("R: " + red + " R_M: " + red_max);
            file.WriteLine("G: " + green + " G_M: " + green_max);
            file.WriteLine("B: " + blue + " B_M: " + blue_max);
            file.WriteLine("EOE");
            //file.Close();
            filter.Red = new IntRange(red, red_max);
            filter.Green = new IntRange(green, green_max);
            filter.Blue = new IntRange(blue, blue_max);
            //// apply the filter
            filter.ApplyInPlace(bitmapData);

            ////invert

            //edge

            //CannyEdgeDetector edge_filter = new CannyEdgeDetector();
            //// apply the filter
            //edge_filter.ApplyInPlace(bitmapData);

            BlobCounter blobCounter = new BlobCounter();
            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = blob_size * 3;
            blobCounter.MinWidth = blob_size * 4;

            blobCounter.ProcessImage(bitmapData);
            Blob[] blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // step 3 - check objects' type and highlight
            SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
            Graphics g = Graphics.FromImage(bitmap);
            Pen yellowPen = new Pen(Color.Yellow, 2); // circles
            Pen redPen = new Pen(Color.Red, 2);       // quadrilateral
            Pen brownPen = new Pen(Color.Brown, 2);   // quadrilateral with known sub-type
            Pen greenPen = new Pen(Color.Green, 2);   // known triangle
            Pen bluePen = new Pen(Color.Blue, 2);     // triangle
            double curdist = 0;
            double leastdist = 100;
            //List<IntPoint> bestcorn;//promoted to super
            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                List<IntPoint> corners; // the list of x,y coordinates.  corners(list)->corner(intpoint)->[x,y]

                // is triangle or quadrilateral

                if (shapeChecker.IsConvexPolygon(edgePoints, out corners))
                {
                    cornide = corners;
                    // get sub-type

                    PolygonSubType subType = shapeChecker.CheckPolygonSubType(corners);
                    Pen pen;
                    if (subType == PolygonSubType.Rectangle || subType == PolygonSubType.Trapezoid || subType == PolygonSubType.Parallelogram || subType == PolygonSubType.Rhombus || subType == PolygonSubType.Square || subType == PolygonSubType.Unknown)
                    {

                        if (corners.Count == 4)
                        {
                            pen = redPen;
                            IntPoint[] array_of_corners = corners.ToArray();//array_of_corners is now an array of corners
                            if (((732 / avdisty(array_of_corners)) + .222) < ((989 / avdistx(array_of_corners)) + 3.37))
                            {
                                curdist = ((732 / avdisty(array_of_corners)) + .222);//from our graph
                            }
                            else
                            {
                                curdist = ((989 / avdistx(array_of_corners)) + 3.37);//from our graph
                            }

                            //if (Worker.selected_square == 1)
                            //{
                            //    Console.WriteLine("called\n");
                            //    IntPoint one = array_of_corners[0];
                            //    IntPoint two = array_of_corners[3];
                            //    cropx1 = one.X - 5;
                            //    cropy1 = one.Y - 5;
                            //    cropx2 = two.X + 5;
                            //    cropy2 = two.Y + 5;
                            //}
                            //else if (i == Worker.selected_square - 1 && Worker.selected_square != 1)
                            //{
                            //    Console.WriteLine("called\n");
                            //    IntPoint one = array_of_corners[0];
                            //    IntPoint two = array_of_corners[3];
                            //    cropx1 = one.X - 5;
                            //    cropy1 = one.Y - 5;
                            //    cropx2 = two.X + 5;
                            //    cropy2 = two.Y + 5;
                            //}
                            //if (Worker.selected_square == 0)
                            //{
                            //    cropx1 = 0;
                            //    cropy1 = 0;
                            //    cropx2 = 0;
                            //    cropy2 = 0;
                            //}

                            if (Worker.selected_square == 0)//PREDATOR VISION
                            {

                                if (curdist < leastdist)
                                {
                                    //  Console.WriteLine((abs(array_of_corners[0].Y - array_of_corners[1].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)));
                                    //if ((abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) < 1.25 && (abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) > .5)
                                    if (true)
                                    {

                                        leastdist = curdist;
                                        if (leastdist < 55 && leastdist > 2)
                                        {

                                            double angle = (array_of_corners[0].X + array_of_corners[3].X) / 2;
                                            Console.WriteLine("0: " + array_of_corners[0].X + " 1: " + array_of_corners[1].X + " 2: " + array_of_corners[2].X + " 3: " + array_of_corners[3].X);
                                            Server.SetData(((array_of_corners[0].X + array_of_corners[1].X) / 2), 1);
                                            Console.WriteLine("PREDATOR: " + Server.GetData(1));
                                            Console.WriteLine(leastdist);
                                            Server.SetData(leastdist, 0);

                                            bestcorn = corners;//superify?
                                            //g.DrawPolygon(pen, ToPointsArray(bestcorn));//leave here as a comment

                                            IntPoint ones = array_of_corners[0];
                                            PointF string_draw = new PointF(ones.X, ones.Y);
                                            SolidBrush tbrush = new SolidBrush(Color.Red);
                                            try
                                            {
                                                if (string_draw.X != 0 && string_draw.Y != 0)
                                                {
                                                    g.DrawString(n.ToString(), this.Font, tbrush, string_draw);
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                Console.WriteLine("{0} in {1} ({2},{3})", e.Message, e.StackTrace, string_draw.X, string_draw.Y);
                                            }

                                        }

                                    }
                                }
                                try
                                {

                                    Graphics global_graphics_predator = Graphics.FromImage(bitmap);
                                    Pen penc = new Pen(Color.YellowGreen, 2);

                                    //g.DrawPolygon(penc, ToPointsArray(bestcorn));
                                    global_graphics_predator.DrawPolygon(penc, ToPointsArray(bestcorn));
                                    global_graphics_predator.DrawLine(penc, new System.Drawing.Point(320, 1), new System.Drawing.Point(320, 479));
                                    //bitmap = global_bitmap;

                                }
                                catch (Exception e)
                                {
                                }

                                yellowPen.Dispose();
                                redPen.Dispose();
                                greenPen.Dispose();
                                bluePen.Dispose();
                                brownPen.Dispose();
                                g.Dispose();

                                return bitmap;
                            }
                            else
                            {

                                int sel_least_dist = Worker.selected_square;
                                sel_least_dist *= 3;//threefeet
                                sel_least_dist -= 1;
                                int sel_max_dist = sel_least_dist + 5;
                                Console.WriteLine(sel_least_dist);
                                Console.WriteLine(sel_max_dist);
                                //Console.WriteLine(sel_least_dist + " " + sel_max_dist);

                                if (curdist < leastdist && curdist > sel_least_dist && curdist < sel_max_dist)
                                {
                                    //  Console.WriteLine((abs(array_of_corners[0].Y - array_of_corners[1].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)));
                                    //if ((abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) < 1.25 && (abs(array_of_corners[0].Y - array_of_corners[3].Y)) / (abs(array_of_corners[0].X - array_of_corners[1].X)) > .5)
                                    if (true)//todo
                                    {

                                        leastdist = curdist;
                                        if (leastdist < 55 && leastdist > 2)
                                        {

                                            // Console.WriteLine("0: " + array_of_corners[0].X + " 1: " + array_of_corners[1].X + " 2: " + array_of_corners[2].X + " 3: " + array_of_corners[3].X);

                                            Server.SetData(((array_of_corners[0].X + array_of_corners[1].X) / 2), 1);
                                            Console.WriteLine(Server.GetData(1));
                                            //Console.WriteLine(leastdist);
                                            Server.SetData(leastdist, 0);
                                            bestcorn = corners;//superify?
                                            //g.DrawPolygon(pen, ToPointsArray(bestcorn));//leave here as a comment

                                            IntPoint ones = array_of_corners[0];
                                            PointF string_draw = new PointF(ones.X, ones.Y);
                                            SolidBrush tbrush = new SolidBrush(Color.Red);
                                            try
                                            {
                                                if (string_draw.X != 0 && string_draw.Y != 0)
                                                {
                                                    g.DrawString(n.ToString(), this.Font, tbrush, string_draw);
                                                    // glob.DrawLine(penc, new System.Drawing.Point(10, 10), new System.Drawing.Point(20, 20));
                                                    //g.DrawLine(pen, new System.Drawing.Point(10, 10), new System.Drawing.Point(20, 20));
                                                }
                                            }
                                            catch (Exception e)
                                            {
                                                Console.WriteLine("{0} in {1} ({2},{3})", e.Message, e.StackTrace, string_draw.X, string_draw.Y);
                                            }

                                        }

                                    }
                                }
                            }

                            //pen = (corners.Count == 4) ? redPen : bluePen;
                        }//TODO get rid of anything that ISNT drawn, eg corner
                    }

                    //else
                    //{

                    //    pen = (corners.Count == 4) ? brownPen : greenPen;

                    //}

                }

            }
            //Console.WriteLine(leastdist); how dod oyo

            //double leastdist = 0;
            //for (int i = 0, n = blobs.Length; i < n; i++)
            //{
            //    try
            //    {
            //        List<IntPoint> filterlist;
            //        filterlist = cornide;

            //        IntPoint[] filter_corners = filterlist.ToArray();

            //        double currentdist = 0;
            //        for (int corner_count = 0; corner_count < filter_corners.Length; corner_count++)
            //        {

            //            IntPoint one_f = filter_corners[corner_count];
            //            corner_count++;
            //            IntPoint two_f = filter_corners[corner_count];
            //            corner_count++;
            //            IntPoint three_f = filter_corners[corner_count];
            //            corner_count++;
            //            IntPoint four_f = filter_corners[corner_count];

            //            double y_average_a = ((double)one_f.Y + (double)two_f.Y) / 2;
            //            double y_average_b = ((double)three_f.Y + (double)four_f.Y) / 2;
            //            double averaged_dist = y_average_b - y_average_a;
            //            currentdist = ((732 / averaged_dist) + .222);//from our graph
            //            if (currentdist <= leastdist)
            //            {
            //                leastdist = currentdist;
            //                Pen pencil = redPen;

            //                g.DrawPolygon(pencil, ToPointsArray(filterlist));
            //            }

            //        }

            //    }
            //    catch
            //    {
            //        //do nothING
            //    }

            //}
            //Console.WriteLine(leastdist);

            try
            {

                Graphics global_graphics = Graphics.FromImage(global_bitmap);
                Pen penc = new Pen(Color.YellowGreen, 2);
                {
                    //g.DrawPolygon(penc, ToPointsArray(bestcorn));
                    global_graphics.DrawPolygon(penc, ToPointsArray(bestcorn));
                    global_graphics.DrawLine(penc, new System.Drawing.Point(320, 1), new System.Drawing.Point(320, 479));

                    bitmap = global_bitmap;
                }
            }
            catch (Exception e)
            {
            }
            try
            {
                g.DrawLine(redPen, 220, 1, 220, 100);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
                Console.WriteLine(e.InnerException);

            }
            yellowPen.Dispose();
            redPen.Dispose();
            greenPen.Dispose();
            bluePen.Dispose();
            brownPen.Dispose();

            g.Dispose();

            return bitmap;
        }
Ejemplo n.º 20
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="image"></param>
        /// <param name="start"></param>
        /// <param name="maxEyeRadius">Should be 2-3 percent of max(width/height)</param>
        /// <param name="maxPointSearchDistance">In source pixels, the max distance from 'start' from which to look for the starting point. Good default: roughly 24 display pixels.</param>
        public static void MarkEye(UnmanagedImage image, Point start, int maxEyeRadius, float maxPointSearchDistance = 0)
        {
            int maxRadius = maxEyeRadius * 2 + (int)Math.Ceiling(maxPointSearchDistance);
            //Find subset
            Rectangle subset = new Rectangle(start.X - maxRadius,start.Y - maxRadius,maxRadius * 2, maxRadius * 2);
            if (subset.X < 0) { subset.Width += subset.X; subset.X = 0; }
            if (subset.Y < 0) { subset.Height += subset.Y; subset.Y = 0; }
            if (subset.Right >= image.Width) subset.Width -= (subset.Right - image.Width + 1);
            if (subset.Bottom >= image.Height) subset.Height -= (subset.Bottom - image.Height + 1);

            start.X -= subset.X;
            start.Y -= subset.Y;

            //Skip processing if we're slightly out of bounds
            if (subset.X < 0 || subset.Y < 0 || subset.Width < 0 || subset.Height < 0 || subset.Right >= image.Width || subset.Bottom >= image.Height) return;

            UnmanagedImage red = null;
            try{
                Point startAt = start;
                using (UnmanagedImage c = new Crop(subset).Apply(image)) {
                    red = new RedEyeFilter(2).Apply(c);
                    if (maxPointSearchDistance > 0) startAt = new ManualSearcher().FindMaxPixel(c,start,maxPointSearchDistance);
                }

                var fill = new AdaptiveCircleFill(red, startAt, start, maxEyeRadius * 2);
                fill.FirstPass();
                fill.SecondPass();
                fill.CorrectRedEye(image, subset.X, subset.Y);
                //fill.MarkFilledPixels(image, subset.X, subset.Y, new byte[] { 0, 255, 0, 0 });

                //fill.SetPixel(image, startAt.X + subset.X, startAt.Y + subset.Y, new byte[] { 255, 255, 0, 0 });
            }finally{
                if (red != null) red.Dispose();
            }
        }
Ejemplo n.º 21
0
 public Bitmap GetSelectedPicture()
 {
     var crop = new Crop(Rect);
     var newBmp = crop.Apply(pictureBox1.Image as Bitmap);
     return newBmp;
 }
Ejemplo n.º 22
0
        // =========================================================
        private void ZoomFunct(ref Bitmap frame, double Factor)
        {
            if (Factor < 0.1)
            {
                return;
            }
            int centerX = frame.Width / 2;
            int centerY = frame.Height / 2;
            int OrgSizeX = frame.Width;
            int OrgSizeY = frame.Height;

            int fromX = centerX - (int)(centerX / Factor);
            int fromY = centerY - (int)(centerY / Factor);
            int SizeX = (int)(OrgSizeX / Factor);
            int SizeY = (int)(OrgSizeY / Factor);
            Crop CrFilter = new Crop(new Rectangle(fromX, fromY, SizeX, SizeY));
            frame = CrFilter.Apply(frame);
            ResizeBilinear RBfilter = new ResizeBilinear(OrgSizeX, OrgSizeY);
            frame = RBfilter.Apply(frame);
        }
        /// <summary>
        /// Scans dominant color on image and returns it.
        /// Crops rank part on image and analyzes suit part on image
        /// </summary>
        /// <param name="bmp">Bitmap to be scanned</param>
        /// <returns>Returns 'B' for black , 'R' for red</returns>
        private char ScanColor(Bitmap bmp)
        {
            char color = 'B';
            //Crop rank part
            Crop crop = new Crop(new Rectangle(0, bmp.Height / 2, bmp.Width, bmp.Height / 2));
            bmp = crop.Apply(bmp);
            Bitmap temp = commonSeq.Apply(bmp); //Apply filters

            //Find suit blob on image
            BlobCounter counter = new BlobCounter();
            counter.ProcessImage(temp);
            Blob[] blobs = counter.GetObjectsInformation();

            if (blobs.Length > 0) //If blobs found
            {
                Blob max = blobs[0]; 
                //Find blob whose size is biggest 
                foreach (Blob blob in blobs)
                {
                    if (blob.Rectangle.Height > max.Rectangle.Height)
                        max = blob;
                    else if (blob.Rectangle.Height == max.Rectangle.Height)
                        max = blob.Rectangle.Width > max.Rectangle.Width ? blob : max;
                }
                QuadrilateralTransformation trans = new QuadrilateralTransformation();
                trans.SourceQuadrilateral = PointsCloud.FindQuadrilateralCorners(counter.GetBlobsEdgePoints(max));
                bmp = trans.Apply(bmp); //Extract suit
            }
            //Lock Bits for processing
            BitmapData imageData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
               ImageLockMode.ReadOnly, bmp.PixelFormat);
            int totalRed = 0;
            int totalBlack = 0;

            unsafe
            {
                //Count red and black pixels
                try
                {
                    UnmanagedImage img = new UnmanagedImage(imageData);
     
                    int height = img.Height;
                    int width = img.Width;
                    int pixelSize = (img.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
                    byte* p = (byte*)img.ImageData.ToPointer();

                    // for each line
                    for (int y = 0; y < height; y++)
                    {
                        // for each pixel
                        for (int x = 0; x < width; x++, p += pixelSize)
                        {
                            int r = (int)p[RGB.R]; //Red pixel value
                            int g = (int)p[RGB.G]; //Green pixel value
                            int b = (int)p[RGB.B]; //Blue pixel value

                            if (r > g + b) //If red component is bigger then total of green and blue component
                                totalRed++;  //then its red

                            if (r <= g + b && r < 50 && g < 50 && b < 50) //If all components less 50
                                totalBlack++; //then its black
                        }
                    }

                }
                finally
                {
                    bmp.UnlockBits(imageData); //Unlock
                }
            }
            if (totalRed > totalBlack) //If red is dominant
                color = 'R'; //Set color as Red

            return color;
        }
        /// <summary>
        /// Function that crops top-right part of card image , with size 30x60
        /// </summary>
        /// <returns>Cropped image</returns>
        public Bitmap GetTopRightPart()
        {
            if (image == null)
                return null;
            Crop crop = new Crop(new Rectangle(image.Width - 37, 10, 30, 60));  

            return crop.Apply(image);
        }
Ejemplo n.º 25
0
        /// <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();

        }
        // New frame received by the player
        private void videoSourcePlayer_NewFrame( object sender, ref Bitmap image )
        {
            BitmapData imageData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, image.PixelFormat);

            try
            {
                UnmanagedImage unmanagedImg = new UnmanagedImage( imageData );

                int x = (int)(unmanagedImg.Width / 3.19); // 443
                int y = (int)(unmanagedImg.Height / 2.2); // 490
                int w = (int)(unmanagedImg.Width / 2.7); // 553
                int h = (int)(unmanagedImg.Height / 4); // 270
                int s = (int)(unmanagedImg.Width / 10.2); // 141

                // Crop the player scroll window.  Speeds up the next couple operations
                Crop onePlayer = new Crop(new Rectangle(x, y, w, h));
                UnmanagedImage img = onePlayer.Apply(unmanagedImg);

                // Use a quadrilateral transformation to make the scroller a big square.
                List<IntPoint> corners = new List<IntPoint>();
                corners.Add(new IntPoint(s, 0));
                corners.Add(new IntPoint(img.Width - s, 0)); ;
                corners.Add(new IntPoint(img.Width, img.Height));
                corners.Add(new IntPoint(0, img.Height));

                QuadrilateralTransformation filter =
                    new QuadrilateralTransformation(corners, img.Width, img.Height);
                img = filter.Apply(img);

                // Crop the bottom half since it appears to have the best imagery
                Crop bottom = new Crop(new Rectangle(0, img.Height / 2, img.Width, img.Height / 2));
                img = bottom.Apply(img);

                UnmanagedImage grayImg = UnmanagedImage.Create(img.Width, img.Height, PixelFormat.Format8bppIndexed);
                Grayscale.CommonAlgorithms.BT709.Apply(img, grayImg);

                OtsuThreshold threshold = new OtsuThreshold();
                threshold.ApplyInPlace(grayImg);

                // Divide the square into 5 peices.  One for each color.
                UnmanagedImage[] colorImg = new UnmanagedImage[5];
                for(int i=0; i < 5; i++) {
                    int colorWidth = grayImg.Width / 5;
                    int colorHeight = grayImg.Height;
                    Crop colorCrop = new Crop(new Rectangle(colorWidth * i, 0, colorWidth, colorHeight));
                    colorImg[i] = colorCrop.Apply(grayImg);
                }

                greenCol.Image = colorImg[GREEN].ToManagedImage();
                redCol.Image = colorImg[RED].ToManagedImage();
                yellowCol.Image = colorImg[YELLOW].ToManagedImage();
                blueCol.Image = colorImg[BLUE].ToManagedImage();
                orangeCol.Image = colorImg[ORANGE].ToManagedImage();

                VerticalIntensityStatistics[] hist = new VerticalIntensityStatistics[5];

                for (int i = 0; i < 5; i++)
                {
                    hist[i] = new VerticalIntensityStatistics(colorImg[i]);
                }

                findPucks(hist);

                greenHist.Values = hist[GREEN].Gray.Values;
                redHist.Values = hist[RED].Gray.Values;
                yellowHist.Values = hist[YELLOW].Gray.Values;
                blueHist.Values = hist[BLUE].Gray.Values;
                orangeHist.Values = hist[ORANGE].Gray.Values;

                pictureBox1.Image = img.ToManagedImage();
            }
            finally
            {
                image.UnlockBits( imageData );
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        /// Scans dominant color on image and returns it.
        /// Crops rank part on image and analyzes suit part on image
        /// </summary>
        /// <param name="bmp">Bitmap to be scanned</param>
        /// <returns>Returns 'B' for black , 'R' for red</returns>
        private char ScanColor(Bitmap bmp)
        {
            //System.Diagnostics.Debug.Flush();
            //System.Diagnostics.Debug.Print("I'm here");
            char color = 'B';
            //Crop rank part


            Crop crop = new Crop(new Rectangle(0, bmp.Height / 2, bmp.Width, bmp.Height / 2));
            bmp = crop.Apply(bmp);
            Bitmap temp = commonSeq.Apply(bmp); //Apply filters

            //Find suit blob on image
            BlobCounter counter = new BlobCounter();
            counter.ProcessImage(temp);
            Blob[] blobs = counter.GetObjectsInformation();

            if (blobs.Length > 0) //If blobs found
            {
                Blob max = blobs[0];
                //Find blob whose size is biggest 
                foreach (Blob blob in blobs)
                {
                    if (blob.Rectangle.Height > max.Rectangle.Height)
                        max = blob;
                    else if (blob.Rectangle.Height == max.Rectangle.Height)
                        max = blob.Rectangle.Width > max.Rectangle.Width ? blob : max;
                }
                QuadrilateralTransformation trans = new QuadrilateralTransformation();
                trans.SourceQuadrilateral = PointsCloud.FindQuadrilateralCorners(counter.GetBlobsEdgePoints(max));
                bmp = trans.Apply(bmp); //Extract suit
            }
            //Lock Bits for processing

            //int bitsPerPixel = ((int)bmp.PixelFormat & 0xff00) >> 8;
            //int bytesPerPixel = (bitsPerPixel + 7) / 8;
            //int stride = 4 * ((bmp.Width * bytesPerPixel + 3) / 4);


           // BitmapData imageData=new BitmapData(bmp.Width, bmp.Height, bmp. )
          // BitmapData imageData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),ImageLockMode.ReadOnly, bmp.PixelFormat);
            int totalRed = 0;
            int totalBlack = 0;

            WriteableBitmap wbmp = (WriteableBitmap)bmp;

            wbmp.ForEach((x, y, pcolor) =>
            {
                int r=Convert.ToInt32(pcolor.R);
                int g=Convert.ToInt32(pcolor.G);
                int b=Convert.ToInt32(pcolor.B);

                if (r > g + b)
                    totalRed++;
                else if (r <= g + b && (r < 50 && g < 50))
                    totalBlack++;

                return pcolor;

            });

            

            if (totalRed > totalBlack) //If red is dominant
                color = 'R'; //Set color as Red

            return color;
        }
Ejemplo n.º 28
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            IVideoSource videoSource1 = videoSourcePlayer1.VideoSource;
            Bitmap currentVideoFrame = videoSourcePlayer1.GetCurrentVideoFrame();
            pictureBox1.Image = currentVideoFrame;

            if (currentVideoFrame != null)
            {
                Crop filter1 = new Crop(new Rectangle(1, 1, 319, 479));
                Crop filter2 = new Crop(new Rectangle(321, 1, 639, 479));
                Bitmap leftimage = filter1.Apply(currentVideoFrame);
                Bitmap rightimage = filter2.Apply(currentVideoFrame);

                // get grayscale image
                IFilter grayscaleFilter = new GrayscaleRMY();
                leftimage = grayscaleFilter.Apply(leftimage);
                rightimage = grayscaleFilter.Apply(rightimage);

                // apply threshold filter
                Threshold th = new Threshold(trackBar1.Value);
                Bitmap filteredImage1 = th.Apply(leftimage);
                pictureBox2.Image = filteredImage1;
                Bitmap filteredImage2 = th.Apply(rightimage);
                pictureBox3.Image = filteredImage2;
                label6.Text = trackBar1.Value.ToString();

                ImageStatistics lftstat = new ImageStatistics(filteredImage1);
                int lftpxlcntwthoutblck = lftstat.PixelsCountWithoutBlack;
                ImageStatistics rghtstat = new ImageStatistics(filteredImage2);
                int rghtpxlcntwthoutblck = rghtstat.PixelsCountWithoutBlack;

                int val = trackBar1.Value;

                if (((lftpxlcntwthoutblck - rghtpxlcntwthoutblck) > val) || ((rghtpxlcntwthoutblck - lftpxlcntwthoutblck) > val))
                     {
                        if ((lftpxlcntwthoutblck-rghtpxlcntwthoutblck) >val)
                            {
                                //label4.Text = "left";
                                label4.Text = "right";
                             }
                         if ((rghtpxlcntwthoutblck-lftpxlcntwthoutblck) >val)
                            {
                                //label4.Text = "right";
                                label4.Text = "left";
                            }
                     }
                else if ((lftpxlcntwthoutblck == 0) && (rghtpxlcntwthoutblck == 0))
                {
                    label4.Text = "Stop!! No  space ahead";
                }
                else    {
                    label4.Text = "Forward";

                        }

               }
        }
        public Bitmap CutWhiteSpaces(Bitmap bmp)
        {
            Bitmap tempBlob = bmp;

            int x, y;

            x = 0;
            y = 0;
            while (true)
            {
                if (tempBlob.GetPixel(x, y).R < 127) break;
                y++;
                if (y >= tempBlob.Height)
                {
                    y = 0;
                    x++;
                    if (x >= tempBlob.Width) break;
                }
            }
            int xMin = x;

            x = tempBlob.Width - 1;
            y = 0;
            while (true)
            {
                if (tempBlob.GetPixel(x, y).R < 127) break;
                y++;
                if (y >= tempBlob.Height)
                {
                    y = 0;
                    x--;
                    if (x == 0) break;
                }
            }
            int xMax = x;

            x = 0;
            y = 0;
            while (true)
            {
                if (tempBlob.GetPixel(x, y).R < 127) break;
                x++;
                if (x >= tempBlob.Width)
                {
                    x = 0;
                    y++;
                    if (y >= tempBlob.Height) break;
                }
            }
            int yMin = y;

            x = 0;
            y = tempBlob.Height - 1;
            while (true)
            {
                if (tempBlob.GetPixel(x, y).R < 127) break;
                x++;
                if (x >= tempBlob.Width)
                {
                    x = 0;
                    y--;
                    if (y >= tempBlob.Height) break;
                }
            }
            int yMax = y;

            Crop crop = new Crop(new Rectangle(xMin, yMin, xMax - xMin+1, yMax - yMin+1));
            tempBlob = crop.Apply(bmp);

            return tempBlob;
        }
Ejemplo n.º 30
0
        /// <summary>
        /// Function that crops top-right part of card image , with size 30x60
        /// </summary>
        /// <returns>Cropped image</returns>
        public Bitmap GetTopLeftPart()
        {
            if (image == null)
                return null;
            Crop crop = new Crop(new Rectangle(0, 0, 30, 84));

            Bitmap im2 = crop.Apply(image);
            //im2.Save("cropped", System.Drawing.Imaging.ImageFormat.Jpeg);
            return crop.Apply(image);
        }
Ejemplo n.º 31
0
        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);
            }
        }
Ejemplo n.º 32
0
        public static Image ApplyImageProperties(byte[] blobContent, ImageProperties properties)
        {
            Bitmap image = null;

            try {
                using (var ms = new MemoryStream(blobContent)) {
                    image = (Bitmap)System.Drawing.Image.FromStream(ms, false, false);
                    image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
                    if (properties.Crop != null)
                    {
                        AForge.Imaging.Filters.Crop filter = new AForge.Imaging.Filters.Crop(new Rectangle(properties.Crop.XOffset, properties.Crop.YOffset, properties.Crop.CropWidth, properties.Crop.CropHeight));
                        image = filter.Apply(image);
                    }
                    if (properties.ImageWidth != properties.OriginalWidth || properties.ImageHeight != properties.OriginalHeight)
                    {
                        var filter = new ResizeBicubic(properties.ImageWidth, properties.ImageHeight);
                        image = filter.Apply(image);
                    }
                    if (properties.Colors != null)
                    {
                        if (properties.Colors.TransparentColor != null)
                        {
                            image.MakeTransparent(ColorTranslator.FromHtml("#" + properties.Colors.TransparentColor));
                        }
                        var brightness = properties.Colors.Brightness;
                        var bfilter    = new BrightnessCorrection(brightness);
                        bfilter.ApplyInPlace(image);
                        var contrast = properties.Colors.Contrast;
                        var cfilter  = new ContrastCorrection(contrast);
                        cfilter.ApplyInPlace(image);
                        if (properties.Colors.Hue != 0)
                        {
                            var         hue    = properties.Colors.Hue;
                            HueModifier filter = new HueModifier(hue);
                            filter.ApplyInPlace(image);
                        }
                        var saturation = properties.Colors.Saturation;
                        var sfilter    = new SaturationCorrection(saturation * 0.01f);
                        sfilter.ApplyInPlace(image);
                    }
                    # region Effects
                    if (!String.IsNullOrEmpty(properties.Effects))
                    {
                        var effects = properties.Effects.Split(';');
                        foreach (var item in effects)
                        {
                            switch (item)
                            {
                            case "Grayscale":
                                var g = new Grayscale(0.2125, 0.7154, 0.0721);
                                image = g.Apply(image);
                                break;

                            case "Sepia":
                                var s = new Sepia();
                                image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
                                s.ApplyInPlace(image);
                                break;

                            case "Rotate Channels":
                                image = AForge.Imaging.Image.Clone(image, PixelFormat.Format24bppRgb);
                                var r = new RotateChannels();
                                r.ApplyInPlace(image);
                                break;

                            case "Invert":
                                var i = new Invert();
                                i.ApplyInPlace(image);
                                break;

                            case "Blur":
                                var b = new Blur();
                                b.ApplyInPlace(image);
                                break;

                            case "Gaussian Blur":
                                var gb = new GaussianBlur(4, 11);
                                gb.ApplyInPlace(image);
                                break;

                            case "Convolution":
                                int[,] kernel = { { -2, -1, 0 }, { -1, 1, 1 }, { 0, 1, 2 } };
                                var c = new Convolution(kernel);
                                c.ApplyInPlace(image);
                                break;

                            case "Edges":
                                var e = new Edges();
                                e.ApplyInPlace(image);
                                break;
                            }
                        }
                    }
                    # endregion
                }
            } catch (Exception) {