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;
 }
Example #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);
        }
Example #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);
 }
Example #4
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;
        }
Example #5
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;
        }
        public void Example1()
        {
            Bitmap image = Properties.Resources.lena512;

            // Create a new Haar Wavelet transform filter
            var wavelet = new WaveletTransform(new Haar(1));

            // Apply the Wavelet transformation
            Bitmap result = wavelet.Apply(image);

            // Show on the screen
            //ImageBox.Show(result);
            Assert.IsNotNull(result);
            
            // Extract only one of the resulting images
            var crop = new Crop(new Rectangle(0, 0, 
                image.Width / 2, image.Height / 2));

            Bitmap quarter = crop.Apply(result);

            // Show on the screen
            //ImageBox.Show(quarter);
            Assert.IsNotNull(quarter);
        }
Example #7
0
        private void videoSourcePlayer1_NewFrame(object sender, ref Bitmap image)
        {
            Invert inv = new Invert();
            inv.ApplyInPlace(image);

            UnmanagedImage ui = UnmanagedImage.FromManagedImage(image);

            pictureBox1.Image = image;


            if (controller.Tracker.TrackingObject == null)
                return;

            if (controller.Tracker.TrackingObject.IsEmpty)
                return;

            var rect = controller.Tracker.TrackingObject.Rectangle;
            Crop crop = new Crop(rect);

            UnmanagedImage head = crop.Apply(ui);

            var points = new List<IntPoint>() { new IntPoint(head.Width / 2, head.Height / 2) };
            var pps = head.Collect16bppPixelValues(points);

            double mean = Accord.Statistics.Tools.Mean(pps);

            double cutoff = mean + 15;
            Threshold t = new Threshold((int)cutoff);
            var mask = t.Apply(ui);



            LevelsLinear16bpp levels = new LevelsLinear16bpp();
            levels.InGray = new IntRange((int)cutoff, 65535);
            levels.OutGray = new IntRange(0, 65535);
            levels.ApplyInPlace(ui);


            var mask8bit = AForge.Imaging.Image.Convert16bppTo8bpp(mask.ToManagedImage());



            BlobCounter bc = new BlobCounter();
            bc.ObjectsOrder = ObjectsOrder.Area;
            bc.ProcessImage(mask8bit);
            var blobs = bc.GetObjectsInformation();

            inv.ApplyInPlace(image);
            Intersect intersect = new Intersect();
            intersect.UnmanagedOverlayImage = mask;
            mask = intersect.Apply(ui);

            List<Rectangle> rects = new List<Rectangle>();

            // Extract the uppermost largest blobs.
            for (int i = 0; i < blobs.Length; i++)
            {
                double dx = (blobs[i].Rectangle.Top - controller.Tracker.TrackingObject.Center.Y);
                double d = (dx * dx) / controller.Tracker.TrackingObject.Area;
                if (d < 2 && blobs[i].Area > 1000)
                    rects.Add(blobs[i].Rectangle);
            }

            rects.Sort(compare);

            if (rects.Count > 0)
            {
                captureHand(mask, rects[0], pbLeftArm, pbLeftHand);
            }
            if (rects.Count > 1)
            {
                captureHand(mask, rects[1], pbRightArm, pbRightHand);

            }

            RectanglesMarker marker = new RectanglesMarker(rects);
            marker.MarkerColor = Color.White;
            marker.ApplyInPlace(mask8bit);

            image = mask.ToManagedImage();
        }
        private void ResizeImageMove(string file, int index, int total)
        {
            string newfile = Path.Combine(_tempFolder, "img" + _counter.ToString("000000") + ".jpg");

            Bitmap image = (Bitmap) Image.FromFile(file);
            // format image
            //AForge.Imaging.Image.FormatImage(ref image);
            //FIBITMAP dib = FreeImage.LoadEx(file);


            int dw = (int) image.Width; //FreeImage.GetWidth(dib);
            int dh = (int) image.Height; //FreeImage.GetHeight(dib);
            //int tw = ServiceProvider.Settings.DefaultSession.TimeLapse.VideoType.Width;
            //int th = ServiceProvider.Settings.DefaultSession.TimeLapse.VideoType.Height;
            int tw = 0;
            int th = 0;
            double movieaspectration = (double) th/tw;
            double mouviesize = ((double) (ServiceProvider.Settings.DefaultSession.TimeLapse.MovePercent)/100);
            int frame_h = dh >= dw ? dh : (int) (dw*movieaspectration);
            int frame_w = dw >= dh ? dw : (int) (dh*movieaspectration);
            int frame_h_dif = (int) (dh*mouviesize);
            int frame_w_dif = (int) (dw*mouviesize);
            frame_h = frame_h - frame_h_dif;
            frame_w = frame_w - frame_w_dif;
            double zw = (tw/(double) dw);
            double zh = (th/(double) dh);
            double z = ((zw <= zh) ? zw : zh);
                //!ServiceProvider.Settings.DefaultSession.TimeLapse.FillImage ? ((zw <= zh) ? zw : zh) : ((zw >= zh) ? zw : zh);

            int crop_x = frame_h_dif/total*index;
            int crop_y = 0;

            switch (ServiceProvider.Settings.DefaultSession.TimeLapse.MoveDirection)
            {
                    // left to right
                case 0:
                    {
                        crop_x = (int) ((dw - frame_w)/(double) total*index);
                        switch (ServiceProvider.Settings.DefaultSession.TimeLapse.MoveAlignment)
                        {
                            case 0:
                                crop_y = 0;
                                break;
                            case 1:
                                crop_y = (dh - frame_h)/2;
                                break;
                            case 2:
                                crop_y = (dh - frame_h);
                                break;
                        }
                    }
                    break;
                    // right to left
                case 1:
                    {
                        crop_x = (int) ((dw - frame_w)/(double) total*(total - index));
                        switch (ServiceProvider.Settings.DefaultSession.TimeLapse.MoveAlignment)
                        {
                            case 0:
                                crop_y = 0;
                                break;
                            case 1:
                                crop_y = (dh - frame_h)/2;
                                break;
                            case 2:
                                crop_y = (dh - frame_h);
                                break;
                        }
                    }
                    break;
                    // top to bottom
                case 2:
                    {
                        crop_y = (int) ((dh - frame_h)/(double) total*index);
                        switch (ServiceProvider.Settings.DefaultSession.TimeLapse.MoveAlignment)
                        {
                            case 0:
                                crop_x = 0;
                                break;
                            case 1:
                                crop_x = (dw - frame_w)/2;
                                break;
                            case 2:
                                crop_x = (dw - frame_w);
                                break;
                        }
                    }
                    break;
                    // bottom to top
                case 3:
                    {
                        crop_y = (int) ((dh - frame_h)/(double) total*(total - index));
                        switch (ServiceProvider.Settings.DefaultSession.TimeLapse.MoveAlignment)
                        {
                            case 0:
                                crop_x = 0;
                                break;
                            case 1:
                                crop_x = (dw - frame_w)/2;
                                break;
                            case 2:
                                crop_x = (dw - frame_w);
                                break;
                        }
                    }
                    break;
                case 4:
                    crop_x = (int) ((dw - frame_w)/(double) total*index);
                    crop_y = (int) ((dh - frame_h)/(double) total*index);
                    break;
                case 5:
                    crop_x = (int) ((dw - frame_w)/(double) total*(total - index));
                    crop_y = (int) ((dh - frame_h)/(double) total*(total - index));
                    break;
            }

            Crop crop = new Crop(new Rectangle(crop_x, crop_y, frame_w, frame_h));
            Bitmap newimage = crop.Apply(image);
            ResizeBilinear resizeBilinearFilter = new ResizeBilinear(tw, th);
            newimage = resizeBilinearFilter.Apply(newimage);
            newimage.Save(newfile, ImageFormat.Jpeg);
            image.Dispose();
            newimage.Dispose();
        }
Example #9
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();
        }
Example #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;
 }
Example #11
0
        private void captureHand(UnmanagedImage mask, Rectangle rect, PictureBox pbArm, PictureBox pbHand)
        {
            Crop c = new Crop(rect);
            var handImage = c.Apply(mask);

            var ps = handImage.Collect16bppPixelValues(handImage.CollectActivePixels());

            if (ps.Length > 0)
            {
                ushort max = Matrix.Max(ps);

                LevelsLinear16bpp levels = new LevelsLinear16bpp();
                levels.InGray = new IntRange(0, max);
                levels.OutGray = new IntRange(0, 65535);
                levels.ApplyInPlace(handImage);


               // pbArm.Image = handImage.ToManagedImage();


                double cutoff = 30000;
                Threshold th = new Threshold((int)cutoff);
                var handMask = th.Apply(handImage);

                var handMask8bit = AForge.Imaging.Image.Convert16bppTo8bpp(handMask.ToManagedImage());

                BlobCounter bch = new BlobCounter();
                bch.ObjectsOrder = ObjectsOrder.Area;
                bch.ProcessImage(handMask8bit);
                var blob = bch.GetObjectsInformation();

                if (blob.Length > 0)
                {
                    Intersect inters = new Intersect();
                    inters.UnmanagedOverlayImage = handMask;
                    inters.ApplyInPlace(handImage);

                    Crop ch = new Crop(blob[0].Rectangle);
                    handImage = ch.Apply(handImage);

                    ResizeNearestNeighbor res = new ResizeNearestNeighbor(25, 25);
                    handImage = res.Apply(handImage);

                    var leftHand = AForge.Imaging.Image.Convert16bppTo8bpp(handImage.ToManagedImage());

                    pbHand.Image = leftHand;
                }
            }
        }
        /// <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);
        }
        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;
        }
Example #14
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;
                }
            }
        }
        // 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 );
            }
        }
        /// <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;
        }
Example #17
0
        /// <summary>
        ///   Camshift algorithm
        /// </summary>
        /// 
        private void camshift(UnmanagedImage frame)
        {
            int width = frame.Width;
            int height = frame.Height;

            Rectangle area = new Rectangle(0, 0, width, height);

            // Compute tracking object center
            float objX = Math.Max(0, Math.Min(searchWindow.X + searchWindow.Width * 0.5f, width));
            float objY = Math.Max(0, Math.Min(searchWindow.Y + searchWindow.Height * 0.5f, height));
            float objAngle;


            // Compute mean shift
            CentralMoments moments = meanShift(frame);

            SizeF objSize = moments.GetSizeAndOrientation(out objAngle);


            if (Single.IsNaN(objSize.Width) || Single.IsNaN(objSize.Height) ||
                Single.IsNaN(objAngle) || objSize.Width < 1 || objSize.Height < 1)
            {
                Reset();
                return;
            }

            // Truncate to integer coordinates
            IntPoint center = new IntPoint((int)objX, (int)objY);

            Rectangle rec = new Rectangle((int)(objX - objSize.Width * 0.5f),
                                          (int)(objY - objSize.Height * 0.5f),
                                          (int)objSize.Width, (int)objSize.Height);

            angleHistory.Push(objAngle);

            // Create tracking object
            IsSteady = checkSteadiness();
            trackingObject.Rectangle = rec;
            trackingObject.Center = center;
            trackingObject.Angle = smooth ? (float)angleHistory.Mean : objAngle;

            if (extract)
            {
                Rectangle inner = rec;

                xHistory.Push(rec.X);
                yHistory.Push(rec.Y);
                widthHistory.Push(rec.Width);
                heightHistory.Push(rec.Height);

                inner.X = (int)xHistory.Mean;
                inner.Y = (int)yHistory.Mean;
                inner.Width = (int)widthHistory.Mean;
                inner.Height = (int)heightHistory.Mean;

                inner.Intersect(area);

                Crop crop = new Crop(inner);

                // TODO: Perform rotation of the extracted object
                //RotateNearestNeighbor rotate = new RotateNearestNeighbor((objAngle - Math.PI / 2) * 180f / Math.PI, true);
                //trackingObject.Image = rotate.Apply(crop.Apply(frame));

                trackingObject.Image = crop.Apply(frame);
            }

            // Compute a new search window size
            searchWindow.Width = (int)(1.1f * objSize.Width);
            searchWindow.Height = (int)((aspectRatio != 0) ?
                (aspectRatio * objSize.Width) : (1.1f * objSize.Height));
        }
Example #18
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);
        }
Example #19
0
        private Bitmap CropDiff(Bitmap pixelDiff)
        {
            if (selection != null)
            {
                var crop = new Crop(selection.Value);

                return crop.Apply(pixelDiff);
            }

            return pixelDiff;
        }
Example #20
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;
 }
Example #21
0
File: Camera.cs Project: 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());
                }
            }
        }
Example #22
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;
 }
 public Bitmap GetSelectedPicture()
 {
     var crop = new Crop(Rect);
     var newBmp = crop.Apply(pictureBox1.Image as Bitmap);
     return newBmp;
 }
        /// <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;
        }
Example #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();

        }
Example #26
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);
        }
Example #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;
        }
Example #28
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;
        }
        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;
        }
        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";

                        }

               }
        }
Example #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);
            }
        }
Example #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) {