Resize image using bilinear interpolation algorithm.

The class implements image resizing filter using bilinear interpolation algorithm.

The filter accepts 8 grayscale images and 24/32 bpp color images for processing.

Sample usage:

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

Initial image:

Result image:

Наследование: AForge.Imaging.Filters.BaseResizeFilter
Пример #1
1
 private Bitmap ResizeBitmap(Bitmap bmp)
 {
     ResizeBilinear resizer = new ResizeBilinear(pb_loaded.Width, pb_loaded.Height);
     bmp = resizer.Apply(bmp);
     //bmp.Save(fileNameCounter+"resized.png");
     return bmp;
 }
Пример #2
0
		public static System.Drawing.Image ResizeImage(System.Drawing.Image SourceImage)
		{
			try {
//				// binarization filtering sequence
//	            FiltersSequence filter = new FiltersSequence(
//	                new Crop(rect),
//	                new Median(),
//	                new ContrastCorrection(),
//	                //new Mean(),
//	                new AForge.Imaging.Filters.Blur(),
//	                new GrayscaleBT709(),
//	                //new Threshold(),
//	                new Threshold(),
//	                new Invert()
//	                
//	            );
	
	
	            // load image
	            Bitmap image = (Bitmap)SourceImage;
	
	            // format image
	            AForge.Imaging.Image.Clone(image,image.PixelFormat);
	//            AForge.Imaging.Image.FormatImage(ref image);
	
	            // lock the source image
	            BitmapData sourceData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
	
	            // create filter
	            ResizeBilinear filter = new ResizeBilinear( image.Width/2, image.Height/2 );
	            // apply the filter
//	            Bitmap newImage = filter.Apply( image );
	            
	            UnmanagedImage binarySource = filter.Apply(new UnmanagedImage(sourceData));
	
	
	            Bitmap binarizedImage= binarySource.ToManagedImage();
	            
	
	            // unlock source image
	            image.UnlockBits(sourceData);
	
	            // dispose temporary binary source image
	            binarySource.Dispose();
	
	            System.Drawing.Image img= ( System.Drawing.Image)binarizedImage;
	            
	            return img;

			} catch (Exception ex) {
				throw ex;
			}
			
		}
        public ColorPart()
        {
            //previousHSV = null;

            resizeFilter = new ResizeBilinear(128, 128);

            pixelateFilter = new Pixellate();
            pixelateFilter.PixelWidth = 32;
            pixelateFilter.PixelHeight = 32;

            redChannelFilter = new ChannelFiltering();
            greenChannelFilter = new ChannelFiltering();
            blueChannelFilter = new ChannelFiltering();
            intRange = new IntRange(0, 0);
            redChannelFilter.Green = intRange; redChannelFilter.Blue = intRange;
            greenChannelFilter.Red = intRange; greenChannelFilter.Blue = intRange;
            blueChannelFilter.Red = intRange; blueChannelFilter.Green = intRange;

            frameMatrices = new List<int[, ,]>();
        }
Пример #4
0
 public static Bitmap Resize(this Bitmap source, int height)
 {
     var proportionalWidht = (double)(source.Width*height) / (double)source.Height;
     ResizeBilinear filter = new ResizeBilinear((int)Math.Round(proportionalWidht, 0), height);
     return filter.Apply(source);
 }
Пример #5
0
        /// <summary>
        /// Hàm thay đổi kích thước ảnh sử dụng thuật toán Bilinear của thư viện AForge.Imaging.Filters và save ảnh đó làm ảnh thumb
        /// Khi chỉ cần thay đổi một chiều và chiều còn lại được aouto thì chỉ cần chuyền vào một chiều cần thay đổi kích thước
        /// </summary>
        /// <param name="width">Chiều rộng của ảnh mới</param>
        /// <param name="height">Chiều cao của ảnh mới</param>
        /// <returns>Bitmap mới</returns>s
        public bool ResizeAndSave(int? width, int? height, string savePath)
        {
            if (width == null && height == null)
                return false;

            int newWidth = width ?? (sourceWidth * height.Value) / sourceHeight;
            var newHeigth = height ?? (sourceHeight * width.Value) / sourceWidth;

            ResizeBilinear resizeBilinear = new ResizeBilinear(newWidth, newHeigth);
            var newImage = resizeBilinear.Apply(sourceBitmap);
            newImage.Save(savePath, GetImageFormat(savePath));
            return true;
        }
Пример #6
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);
        }
Пример #7
0
        public static float TestTemplateOnScreen(string path, float percentage)
        {
            var template = ChangePixelFormat(new Bitmap(path), PixelFormat.Format24bppRgb);
            var sourceImage = ChangePixelFormat(GrabScreenGDI(gameScreen), PixelFormat.Format24bppRgb);
            var rb = new ResizeBilinear(sourceImage.Width/4, sourceImage.Height/4);
            sourceImage = rb.Apply(sourceImage);
            var tm = new ExhaustiveTemplateMatching(percentage);
            var matchings = tm.ProcessImage(sourceImage, template);
            var data = sourceImage.LockBits(
                new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                ImageLockMode.ReadWrite, sourceImage.PixelFormat);

            MainWindow.Log("%" + percentage + " got " + matchings.Length + " matches");
            if (matchings.Length != 1)
            {
                if (matchings.Length == 0)
                {
                    percentage -= 0.005f;
                }
                if (matchings.Length > 1)
                {
                    percentage += 0.0025f;
                }
                percentage = TestTemplateOnScreen(path, percentage);
            }
            foreach (var m in matchings)
            {
                Drawing.Rectangle(data, m.Rectangle, Color.LimeGreen);
            }
            sourceImage.UnlockBits(data);
            //sourceImage.Save("./images/restest.png");
            return percentage;
        }
Пример #8
0
        /// <summary>
        /// Detects and recognizes cards from source image
        /// </summary>
        /// <param name="source">Source image to be scanned</param>
        /// <returns>Recognized Cards</returns>
        public List<Card> Recognize(Bitmap source)
        {
            List<Card> collection = new List<Card>();
            
            Bitmap temp = source.Clone(source.PixelFormat) as Bitmap; //Clone image to keep original image

            FiltersSequence seq = new FiltersSequence();
            seq.Add(Grayscale.CommonAlgorithms.BT709);  //First add  grayScaling filter
            seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
            temp = seq.Apply(source); // Apply filters on source image

            //Extract blobs from image whose size width and height larger than 150
            BlobCounter extractor = new BlobCounter();
            extractor.FilterBlobs = true;
            extractor.MinWidth = extractor.MinHeight = 150;
            extractor.MaxWidth = extractor.MaxHeight = 350;
            extractor.ProcessImage(temp);

            //Will be used transform(extract) cards on source image 
            QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation();

            //Will be used resize(scaling) cards 
            ResizeBilinear resizer = new ResizeBilinear(CardWidth, CardHeight);

            foreach (Blob blob in extractor.GetObjectsInformation())
            {
                //Get Edge points of card
                List<IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob);
                //Calculate/Find corners of card on source image from edge points
                List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);

                quadTransformer.SourceQuadrilateral = corners; //Set corners for transforming card 
                quadTransformer.AutomaticSizeCalculaton = true;

                Bitmap cardImg = quadTransformer.Apply(source); //Extract(transform) card image

                if (cardImg.Width > cardImg.Height) //If card is positioned horizontally
                {
                    WriteableBitmap wbmp=(WriteableBitmap)cardImg;
                    wbmp = wbmp.Rotate(90);
                    cardImg = (Bitmap)wbmp; //Rotate
                }
                cardImg = resizer.Apply(cardImg); //Normalize card size

                Card card = new Card(cardImg, corners.ToArray()); //Create Card Object
                char color = ScanColor(card.GetTopLeftPart()); //Scan color
                bool faceCard = IsFaceCard(cardImg); //Determine type of card(face or not)

                if (!faceCard)
                {
                    card.Suit = ScanSuit(cardImg, color); //Scan Suit of non-face card
                    card.Rank = ScanRank(cardImg); //Scan Rank of non-face card
                }
                else
                {
                    Bitmap topLeft = card.GetTopLeftPart();

                    seq = null;
                    seq = new FiltersSequence();

                    seq.Add(Grayscale.CommonAlgorithms.BT709);
                    seq.Add(new BradleyLocalThresholding());
                    topLeft = seq.Apply(topLeft);
                    BlobsFiltering bFilter = new BlobsFiltering(5, 5, 150, 150);
                    bFilter.ApplyInPlace(topLeft); //Filter blobs that can not be a suit

                    //topLeft.Save("topleft.bmp", ImageFormat.Bmp);

                    card.Suit = ScanFaceSuit(topLeft, color); //Scan suit of face card
                    card.Rank = ScanFaceRank(topLeft); //Scan rank of face card
                }
                collection.Add(card); //Add card to collection
            }
            return collection;
        }
Пример #9
0
 public Bitmap ResizeShoot(Bitmap shoot)
 {
     ResizeBilinear rb = new ResizeBilinear(pb_Cinema.Width, pb_Cinema.Height);
     return rb.Apply(shoot);
 }
        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();
        }
Пример #11
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();
        }
        /// <summary>
        /// Function that resizes image
        /// </summary>
        /// <param name="bmp">Image to be resized</param>
        /// <returns>Resized image</returns>
        private Bitmap ResizeBitmap(Bitmap bmp)
        {
            ResizeBilinear resizer = new ResizeBilinear(resizeW, resizeH);

            return resizer.Apply(bmp);
        }
        private Bitmap ResizeBitmap(Bitmap bmp)
        {
            ResizeBilinear resizer = new ResizeBilinear(pictureBox1.Width, pictureBox1.Height);

            return resizer.Apply(bmp);
        }
Пример #14
0
        /// <summary>
        /// Crop the blob from the image
        /// </summary>
        private Bitmap CropBlob(BlobPanel blob, System.Drawing.Image source, int rotationAngel = 0)
        {
            // Create the target image, this is a squared image.
            int size = Math.Max(blob.Height, blob.Width);
            Bitmap newImage = new Bitmap(size, size, PixelFormat.Format24bppRgb);

            // Get the graphics object of the image.
            Graphics g = Graphics.FromImage(newImage);

            // Create the background color to use (the image we create is larger than the blob (as we squared it)
            // so this would be the color of the excess areas.
            Color bColor = Color.FromArgb((int)txtExtractedBackColor.Value, (int)txtExtractedBackColor.Value, (int)txtExtractedBackColor.Value);

            // Fill back color.
            g.FillRectangle(new SolidBrush(bColor), 0, 0, size, size);

            // Now we clip the blob from the PictureBox image.
            g.DrawImage(source, new Rectangle(0, 0, blob.Width, blob.Height), blob.Left, blob.Top, blob.Width, blob.Height, GraphicsUnit.Pixel);
            g.Dispose();

            if (rotationAngel != 0)
            {
                RotateBilinear filter = new RotateBilinear(rotationAngel, true);
                filter.FillColor = bColor;
                // apply the filter
                newImage = filter.Apply(newImage);
            }

            // Resize the image.
            ResizeBilinear resizefilter = new ResizeBilinear((int)txtExportSize.Value, (int)txtExportSize.Value);
            newImage = resizefilter.Apply(newImage);

            return newImage;
        }
        private Bitmap ResizeBitmap(Bitmap bmp)
        {
            ResizeBilinear resizer = new ResizeBilinear(pb_loaded.Width, pb_loaded.Height);

            return resizer.Apply(bmp);
        }
        public ActionResult Process(HttpPostedFileBase file)
        {
            var model = new List<TrainingDataItem>();

            var bitmap = new Bitmap(file.InputStream);

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

            // step 1 - turn background to black
            var filter = new Invert();
            filter.ApplyInPlace(bitmapData);

            ColorFiltering colorFilter = new ColorFiltering();

            colorFilter.Red = new IntRange(0, 64);
            colorFilter.Green = new IntRange(0, 64);
            colorFilter.Blue = new IntRange(0, 64);
            colorFilter.FillOutsideRange = false;

            colorFilter.ApplyInPlace(bitmapData);

            // step 2 - locating objects
            BlobCounter blobCounter = new BlobCounter();

            blobCounter.FilterBlobs = true;
            blobCounter.MinHeight = 5;
            blobCounter.MinWidth = 5;

            blobCounter.ProcessImage(bitmapData);
            var blobs = blobCounter.GetObjectsInformation();
            bitmap.UnlockBits(bitmapData);

            // get information about detected objects
            var shapeChecker = new SimpleShapeChecker();

            int circleCount = 0;
            foreach (
                var blob in
                    blobs.ToArray()
                        .OrderBy(b => b.Rectangle.Top)
                        .ThenBy(b => b.Rectangle.Left)
                        .ThenByDescending(b => b.Area))
            {

                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);

                AForge.Point center;
                float radius;

                if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                {

                    //Todo: filter on the largest radius * 90% to deal with resolutions
                    if (radius < 40)
                        continue;

                    blobCounter.ExtractBlobsImage(bitmap, blob, false);
                    var letter = blob.Image.ToManagedImage(true);
                    var resizeFilter = new ResizeBilinear(150, 150);
                    letter = resizeFilter.Apply(letter);
                    var base64 = (letter.ToBase64());
                    var sb = new StringBuilder();

                    var bwBitmap = new Bitmap(75, 75, PixelFormat.Format32bppArgb);

                    for (int y = 40; y < 115; y++)
                    {
                        for (int x = 40; x < 115; x++)
                        {
                            var color = letter.GetPixel(x, y);
                            if (color.Name == "ff000000")
                            {
                                sb.Append("0");
                                bwBitmap.SetPixel(x - 40, y - 40, Color.Black);
                            }
                            else
                            {
                                sb.Append("1");
                                bwBitmap.SetPixel(x - 40, y - 40, Color.White);
                            }
                        }

                        sb.AppendLine();
                    }

                    model.Add(new TrainingDataItem
                    {
                        Base64Image = (bwBitmap.ToBase64()),
                        Letter = TrainingData.GetBestGuess(bwBitmap)
                    });
                }
            }

            return View(model.ToArray());
        }
Пример #17
0
 public static Bitmap ResizeBilinear(Bitmap bmp, int width, int height)
 {
     // create filter
     ResizeBilinear filter = new ResizeBilinear(width, height);
     // apply the filter
     Bitmap newImage = filter.Apply(bmp);
     return newImage;
 }
 public static Bitmap ResizeImage(Bitmap pBitmap, int pWidth, int pHeight)
 {
     ResizeBilinear filter = new ResizeBilinear(pWidth, pHeight);
     return filter.Apply(pBitmap);
 }
Пример #19
0
        //used to actually resize the bitmap to match the reference image.
        private static Bitmap resizeBitmap(Bitmap bitmap, int width, int height)
        {
            ResizeBilinear  resizer = new ResizeBilinear(width, height);

            Bitmap result = resizer.Apply(bitmap);
            return result;
        }
Пример #20
0
        public ActionResult Index(string Data, int Smallest = 3, int Largest = 6, HttpPostedFileBase file = null)
        {
            string base64Image = null;
            if (file != null)
            {
                //try to determine data from posted file
                var bitmap = new Bitmap(file.InputStream);

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

                // step 1 - turn background to black
                var filter = new Invert();
                filter.ApplyInPlace(bitmapData);

                ColorFiltering colorFilter = new ColorFiltering();

                colorFilter.Red = new IntRange(0, 64);
                colorFilter.Green = new IntRange(0, 64);
                colorFilter.Blue = new IntRange(0, 64);
                colorFilter.FillOutsideRange = false;

                colorFilter.ApplyInPlace(bitmapData);

                // step 2 - locating objects
                BlobCounter blobCounter = new BlobCounter();

                blobCounter.FilterBlobs = true;
                blobCounter.MinHeight = 5;
                blobCounter.MinWidth = 5;

                blobCounter.ProcessImage(bitmapData);
                var blobs = blobCounter.GetObjectsInformation();
                bitmap.UnlockBits(bitmapData);
                base64Image = bitmap.ToBase64();
                // get information about detected objects
                var shapeChecker = new SimpleShapeChecker();

                var letters = new List<Letter>();

                int circleCount = 0;
                foreach (
                    var blob in
                        blobs.ToArray()
                            .OrderBy(b => b.Rectangle.Top)
                            .ThenBy(b => b.Rectangle.Left)
                            .ThenByDescending(b => b.Area))
                {

                    List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blob);

                    AForge.Point center;
                    float radius;

                    if (shapeChecker.IsCircle(edgePoints, out center, out radius))
                    {

                        //Todo: filter on the largest radius * 90% to deal with resolutions
                        if (radius < 40)
                            continue;

                        blobCounter.ExtractBlobsImage(bitmap, blob, false);
                        var letter = blob.Image.ToManagedImage(true);
                        var resizeFilter = new ResizeBilinear(150, 150);
                        letter = resizeFilter.Apply(letter);

                        var bwBitmap = new Bitmap(75, 75, PixelFormat.Format32bppArgb);

                        for (int y = 40; y < 115; y++)
                        {
                            for (int x = 40; x < 115; x++)
                            {
                                var color = letter.GetPixel(x, y);
                                if (color.Name == "ff000000")
                                {
                                    bwBitmap.SetPixel(x - 40, y - 40, Color.Black);
                                }
                                else
                                {
                                    bwBitmap.SetPixel(x - 40, y - 40, Color.White);
                                }
                            }
                        }

                       letters.Add(new Letter
                       {
                           L = TrainingData.GetBestGuess(bwBitmap),
                           X = center.X,
                           Y = center.Y,
                           Radius = radius
                       });
                    }
                }

                var minX = letters.Min(c => c.X);
                var maxX = letters.Max(c => c.X);

                var minY = letters.Min(c => c.Y);
                var maxY = letters.Max(c => c.Y);

                var smallestRadius = letters.Min(c => c.Radius);

                var numberOfItemsPerRow = (int)((maxX - minX)/ smallestRadius / 2);
                var numberOfItemsPerCol = (int)((maxY - minY) / smallestRadius / 2);

                var spaceBetweenPointsX = (maxX - minX)/numberOfItemsPerRow;
                var spaceBetweenPointsY = (maxY - minY) / numberOfItemsPerCol;

                var varianceDelta = smallestRadius*.05f; //allow 5% pixel float

                var puzzle = new StringBuilder();
                for (var y = minY; y <= maxY; y += spaceBetweenPointsY)
                {

                    for (var x = minX; x <= maxX; x += spaceBetweenPointsX)
                    {
                        var item = letters.FirstOrDefault(c => c.X > x - varianceDelta && c.X < x + varianceDelta
                                                               && c.Y > y - varianceDelta && c.Y < y + varianceDelta);

                        if (item != null)
                            puzzle.Append(item.L);
                        else
                            puzzle.Append("_");
                    }

                    puzzle.AppendLine();
                }

                Data = puzzle.ToString();

            }

            var solutions = SolvePuzzle(Data, Smallest, Largest);
            return View(new PuzzleModel
            {
                Data = Data,
                Largest = Largest,
                Smallest = Smallest,
                Solutions = solutions,
                Base64Image = base64Image
            });
        }
Пример #21
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();

        }
Пример #22
0
        public void Tick()
        {
            if (NeedSizeUpdate)
            {
                UpdatePosition();
            }

            if (NeedsRefresh)
            {
                bool alert = false;
                lock (this)
                {
                    if (RefreshImage || (_imgplan == null && !String.IsNullOrEmpty(Fpobject.image)))
                    {
                        if (_imgplan!=null)
                        {
                            try
                            {
                                _imgplan.Dispose();
                            } catch
                            {
                            }
                            _imgplan = null;
                        }
                        if (_imgview != null)
                        {
                            try
                            {
                                _imgview.Dispose();
                            }
                            catch
                            {
                            }
                            _imgview = null;
                        }
                        var img = (Bitmap)Image.FromFile(Fpobject.image);
                        if (!Fpobject.originalsize)
                        {
                            var rf = new ResizeBilinear(533, 400);
                            _imgplan = rf.Apply(img);
                            _imgview = (Bitmap)_imgplan.Clone();
                        }
                        else
                        {
                            _imgplan = img;
                            _imgview = (Bitmap)_imgplan.Clone();
                        }
                        RefreshImage = false;
                    }
                    if (_imgplan == null)
                        return;

                    Graphics gLf = Graphics.FromImage(_imgview);
                    gLf.DrawImage(_imgplan, 0, 0,_imgplan.Width,_imgplan.Height);

                    bool itemRemoved = false;
                    double wrat = Convert.ToDouble(ImageWidth) / 533d;
                    double hrat = Convert.ToDouble(ImageHeight) / 400d;

                    foreach (objectsFloorplanObjectsEntry fpoe in Fpobject.objects.@object)
                    {
                        var p = new Point(fpoe.x, fpoe.y);
                        if (Fpobject.originalsize)
                        {
                            p.X = Convert.ToInt32(p.X*wrat);
                            p.Y = Convert.ToInt32(p.Y*hrat);
                        }
                        if (fpoe.fov == 0)
                            fpoe.fov = 135;
                        if (fpoe.radius == 0)
                            fpoe.radius = 80;
                        switch (fpoe.type)
                        {
                            case "camera":
                                {
                                    var cw = MainClass.GetCameraWindow(fpoe.id);
                                    if (cw != null)
                                    {
                                        double drad = (fpoe.angle - 180) * Math.PI / 180;
                                        var points = new[]
                                            {
                                                new Point(p.X + 11+Convert.ToInt32(20*Math.Cos(drad)), p.Y + 11 + Convert.ToInt32((20* Math.Sin(drad)))),
                                                new Point(p.X + 11+Convert.ToInt32(20*Math.Cos(drad+(135*Math.PI/180))), p.Y + 11 + Convert.ToInt32((20* Math.Sin(drad+(135*Math.PI/180))))),
                                                new Point(p.X + 11+Convert.ToInt32(10*Math.Cos(drad+(180*Math.PI/180))), p.Y + 11 + Convert.ToInt32((10* Math.Sin(drad+(180*Math.PI/180))))),
                                                new Point(p.X + 11+Convert.ToInt32(20*Math.Cos(drad-(135*Math.PI/180))), p.Y + 11 + Convert.ToInt32((20* Math.Sin(drad-(135*Math.PI/180)))))
                                            };
                                        if (cw.Camobject.settings.active && !cw.VideoSourceErrorState)
                                        {
                                            int offset = (fpoe.radius / 2) - 11;
                                            if (cw.Alerted)
                                            {
                                                gLf.FillPolygon(_alertBrush, points);

                                                gLf.FillPie(_alertBrushScanner, p.X - offset, p.Y - offset, fpoe.radius, fpoe.radius,
                                                            (float)(fpoe.angle - 180 - (fpoe.fov / 2)), fpoe.fov);
                                                alert = true;
                                            }
                                            else
                                            {
                                                gLf.FillPolygon(_noalertBrush, points);
                                                gLf.FillPie(_noalertBrushScanner, p.X - offset, p.Y - offset, fpoe.radius, fpoe.radius,
                                                            (float)(fpoe.angle - 180 - (fpoe.fov / 2)), fpoe.fov);
                                            }
                                        }
                                        else
                                        {
                                            gLf.FillPolygon(_offlineBrush, points);
                                        }

                                    }
                                    else
                                    {
                                        fpoe.id = -2;
                                        itemRemoved = true;
                                    }
                                }
                                break;
                            case "microphone":
                                {
                                    var vw = MainClass.GetVolumeLevel(fpoe.id);
                                    if (vw != null)
                                    {
                                        if (vw.Micobject.settings.active && !vw.AudioSourceErrorState)
                                        {
                                            if (vw.Alerted)
                                            {
                                                gLf.FillEllipse(_alertBrush, p.X - 20, p.Y - 20, 40, 40);
                                                alert = true;
                                            }
                                            else
                                            {
                                                gLf.FillEllipse(_noalertBrush, p.X - 15, p.Y - 15, 30, 30);
                                            }
                                        }
                                        else
                                        {
                                            gLf.FillEllipse(_offlineBrush, p.X - 15, p.Y - 15, 30, 30);
                                        }
                                    }
                                    else
                                    {
                                        fpoe.id = -2;
                                        itemRemoved = true;
                                    }
                                }
                                break;
                        }
                    }

                    if (itemRemoved)
                        Fpobject.objects.@object = [email protected](fpoe => fpoe.id > -2).ToArray();

                    gLf.Dispose();
                }
                Invalidate();
                LastRefreshTimestamp = Helper.Now.UnixTicks();
                NeedsRefresh = false;
                IsAlert = alert;
            }
        }
        /// <summary>
        /// Detects and recognizes cards from source image
        /// </summary>
        /// <param name="source">Source image to be scanned</param>
        /// <returns>Recognized Cards</returns>
        public CardCollection Recognize(Bitmap source)
        {
            CardCollection collection = new CardCollection();  //Collection that will hold cards
            Bitmap temp = source.Clone() as Bitmap; //Clone image to keep original image

            FiltersSequence seq = new FiltersSequence();
            seq.Add(Grayscale.CommonAlgorithms.BT709);  //First add  grayScaling filter
            seq.Add(new OtsuThreshold()); //Then add binarization(thresholding) filter
            temp = seq.Apply(source); // Apply filters on source image

            //Extract blobs from image whose size width and height larger than 150
            BlobCounter extractor = new BlobCounter();
            extractor.FilterBlobs = true;
            extractor.MinWidth = extractor.MinHeight = 150;
            extractor.MaxWidth = extractor.MaxHeight = 350;
            extractor.ProcessImage(temp);

            //Will be used transform(extract) cards on source image
            QuadrilateralTransformation quadTransformer = new QuadrilateralTransformation();

            //Will be used resize(scaling) cards
            ResizeBilinear resizer = new ResizeBilinear(CardWidth, CardHeight);

            foreach (Blob blob in extractor.GetObjectsInformation())
            {
                //Get Edge points of card
                List<IntPoint> edgePoints = extractor.GetBlobsEdgePoints(blob);
                //Calculate/Find corners of card on source image from edge points
                List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);

                quadTransformer.SourceQuadrilateral = corners; //Set corners for transforming card
                quadTransformer.AutomaticSizeCalculaton = true;

                Bitmap cardImg = quadTransformer.Apply(source); //Extract(transform) card image

                if (cardImg.Width > cardImg.Height) //If card is positioned horizontally
                    cardImg.RotateFlip(RotateFlipType.Rotate90FlipNone); //Rotate
                cardImg = resizer.Apply(cardImg); //Normalize card size

                Card card = new Card(cardImg, corners.ToArray()); //Create Card Object
                bool faceCard = IsFaceCard(cardImg); //Determine type of card(face or not)

                ResizeBicubic res;

                seq.Clear();
                seq.Add(Grayscale.CommonAlgorithms.BT709);
                seq.Add(new OtsuThreshold());

                Bitmap topLeftSuit = card.GetTopLeftSuitPart();
                Bitmap bmp = seq.Apply(topLeftSuit);

                bmp = CutWhiteSpaces(bmp);
                res = new ResizeBicubic(32, 40);
                bmp = res.Apply(bmp);

                Bitmap topLeftRank = card.GetTopLeftRankPart();
                Bitmap bmp2 = seq.Apply(topLeftRank);

                bmp2 = CutWhiteSpaces(bmp2);

                seq.Clear();
                seq.Add(new OtsuThreshold());
                bmp = seq.Apply(bmp);
                card.Suit = ScanSuit(bmp);

                if (!faceCard)
                {
                    res = new ResizeBicubic(26, 40);
                    bmp2 = res.Apply(bmp2);
                    seq.Clear();
                    seq.Add(new OtsuThreshold());
                    bmp2 = seq.Apply(bmp2);
                    card.Rank = ScanRank(bmp2);
                }
                else
                {
                    res = new ResizeBicubic(32, 40);
                    bmp2 = res.Apply(bmp2);
                    seq.Clear();
                    seq.Add(new OtsuThreshold());
                    bmp2 = seq.Apply(bmp2);
                    card.Rank = ScanFaceRank(bmp2);
                }
                collection.Add(card); //Add card to collection
            }
            return collection;
        }
Пример #24
0
        /// <summary>
        /// Hàm thay đổi kích thước ảnh sử dụng thuật toán Bilinear của thư viện AForge.Imaging.Filters
        /// Khi chỉ cần thay đổi một chiều và chiều còn lại được aouto thì chỉ cần chuyền vào một chiều cần thay đổi kích thước
        /// </summary>
        /// <param name="width">Chiều rộng của ảnh mới</param>
        /// <param name="height">Chiều cao của ảnh mới</param>
        /// <returns>Bitmap mới</returns>
        public Bitmap Resize(int? width, int? height)
        {
            if (width == null && height == null)
                return sourceBitmap;

            int newWidth = width ?? (sourceWidth * height.Value) / sourceHeight;
            var newHeigth = height ?? (sourceHeight * width.Value) / sourceWidth;

            ResizeBilinear resizeBilinear = new ResizeBilinear(newWidth, newHeigth);
            return resizeBilinear.Apply(sourceBitmap);
        }
Пример #25
0
 private void BtnChooseFileClick(object sender, EventArgs e)
 {
     var ofd = new OpenFileDialog
                   {
                       InitialDirectory = Program.AppPath + @"backgrounds\",
                       Filter = "Image Files|*.jpg;*.gif;*.bmp;*.png;*.jpeg",
                       FilterIndex = 1
                   };
     if (ofd.ShowDialog(this) == DialogResult.OK)
     {
         string fileName = ofd.FileName;
         Fpc.Fpobject.image = fileName;
         Image img = Image.FromFile(fileName);
         try
         {
             Fpc.ImgPlan = (Bitmap) img.Clone();//
             var rf = new ResizeBilinear(533, 400);
             _floorPlanImage = rf.Apply((Bitmap)img);
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
         Fpc.NeedSizeUpdate = true;
         Fpc.NeedsRefresh = true;
         if (txtName.Text.Trim() == "")
             txtName.Text = "Plan " + MainForm.NextFloorPlanId;
         _pnlPlan.Invalidate();
     }
     ofd.Dispose();
 }
Пример #26
0
        //scaling of a bitmap with preserving proportions
        public Bitmap scaleImage(int PbWidth, int PbHeigth, Bitmap bitmap)
        {
            float ratio;
            bool horizontal = false;
            if (bitmap.Width > bitmap.Height)
            {
                ratio = (float)PbWidth / (float)bitmap.Width;
                horizontal = true;
            }
            else ratio = (float)bitmap.Height / (float)PbHeigth;
            int heigth, width;

            float w = bitmap.Width * ratio;
            width = (int)w;

            float h = bitmap.Height * ratio;
            heigth = (int)h;

            ResizeBilinear filter = new ResizeBilinear(width, heigth);
            bitmap = filter.Apply(bitmap);

            return bitmap;
        }
Пример #27
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:System.Object"/> class.
 /// </summary>
 /// <remarks></remarks>
 public TileDetector()
 {
     median = new Median();
     blobCounter = new BlobCounter();
     blobCounter.FilterBlobs = true;
     blobCounter.MinWidth = (int)Math.Ceiling(15f / scale);
     blobCounter.MinHeight = (int)Math.Ceiling(15f / scale);
     blobCounter.ObjectsOrder = ObjectsOrder.XY;
     resize = new ResizeBilinear(640, 480);
     this.filteredBoard = new Bitmap(640, 480);
     this.tileBlobs = new List<Blob>();
 }
Пример #28
0
        public static TemplateMatch FindOnScreen(string path, float percentage, bool shouldResize, Rectangle cropRect)
        {
            Console.Beep();
            if (ShouldBringToFront) ControlInput.BringHeroesToFront();
            var template = ChangePixelFormat(new Bitmap(path), PixelFormat.Format24bppRgb);
            var src = GrabScreenGDI(gameScreen);
            var sourceImage = ChangePixelFormat(src, PixelFormat.Format24bppRgb);
            src.Dispose();
            if (shouldResize)
            {
                var rb = new ResizeBilinear(sourceImage.Width/4, sourceImage.Height/4);
                sourceImage = rb.Apply(sourceImage);
            }
            //sourceImage.Save("./images/resized.png");
            var ed = new HomogenityEdgeDetector();

            if (!cropRect.Size.IsEmpty)
            {
                var cb = new Crop(cropRect);
                sourceImage = cb.Apply(sourceImage);
                //sourceImage.Save("./images/resized_cropped.png");
            }

            //ed.Apply(Grayscale.CommonAlgorithms.BT709.Apply(sourceImage)).Save("./images/EdgeDetection.png");

            var tm = new ExhaustiveTemplateMatching(percentage);
            var matchings = tm.ProcessImage(sourceImage, template);
            var data = sourceImage.LockBits(
                new Rectangle(0, 0, sourceImage.Width, sourceImage.Height),
                ImageLockMode.ReadWrite, sourceImage.PixelFormat);

            TemplateMatch topMatch = null;
            foreach (var m in matchings)
            {
                if (topMatch == null) topMatch = m;
                if (m.Similarity > topMatch.Similarity) topMatch = m;
                var col = Color.Red;
                col = Color.FromArgb((byte) ((int) (m.Similarity*255)), col);
                Drawing.Rectangle(data, m.Rectangle, col);
            }
            sourceImage.UnlockBits(data);
            //sourceImage.Save("./images/res.png");
            sourceImage.Dispose();
            template.Dispose();
            GC.Collect();
            return topMatch;
        }