Performs quadrilateral transformation of an area in the source image.

The class implements simple algorithm described by Olivier Thill for transforming quadrilateral area from a source image into rectangular image. The idea of the algorithm is based on finding for each line of destination rectangular image a corresponding line connecting "left" and "right" sides of quadrilateral in a source image. Then the line is linearly transformed into the line in destination image.

Due to simplicity of the algorithm it does not do any correction for perspective.

To make sure the algorithm works correctly, it is preferred if the "left-top" corner of the quadrilateral (screen coordinates system) is specified first in the list of quadrilateral's corners. At least user need to make sure that the "left" side (side connecting first and the last corner) and the "right" side (side connecting second and third corners) are not horizontal.

Use QuadrilateralTransformation to avoid the above mentioned limitations, which is a more advanced quadrilateral transformation algorithms (although a bit more computationally expensive).

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

Sample usage:

// define quadrilateral's corners List<IntPoint> corners = new List<IntPoint>( ); corners.Add( new IntPoint( 99, 99 ) ); corners.Add( new IntPoint( 156, 79 ) ); corners.Add( new IntPoint( 184, 126 ) ); corners.Add( new IntPoint( 122, 150 ) ); // create filter SimpleQuadrilateralTransformation filter = new SimpleQuadrilateralTransformation( corners, 200, 200 ); // apply the filter Bitmap newImage = filter.Apply( image );

Initial image:

Result image:

Inheritance: BaseTransformationFilter
        private Bitmap processImage(Bitmap img)
        {
            //Generate closing structural element
            short[,] structEl = new short[13, 13];

            for (int i = 0; i < 13; i++)
                for (int j = 0; j < 13; j++)
                    if ((i - 6) * (i - 6) + (j - 6) * (j - 6) < 64)
                        structEl[i, j] = 1;
                    else
                        structEl[i, j] = -1;

            //Initialize filters
            HSLFiltering borderFind = new HSLFiltering();
            Closing borderClose = new Closing(structEl);
            Invert invert = new Invert();
            Grayscale grayFilter = new Grayscale(0, 0, 1.0);
            Threshold bwFilter = new Threshold(1);

            PointedColorFloodFill blackout = new PointedColorFloodFill();
            blackout.Tolerance = Color.FromArgb(0, 0, 0);
            blackout.FillColor = Color.FromArgb(0, 0, 0);

            ExtractBiggestBlob getgame = new ExtractBiggestBlob();
            getgame.OriginalImage = new Bitmap(img);

            GrayscaleToRGB colorFilter = new GrayscaleToRGB();

            //Color determined with ColorProbe.
            borderFind.Hue        = new IntRange(190, 200);
            borderFind.Saturation = new Range(0.6f, 0.8f);
            borderFind.Luminance  = new Range(0.6f, 1.0f);

            borderFind.ApplyInPlace(img);
            borderClose.ApplyInPlace(img);
            img = grayFilter.Apply(img);
            bwFilter.ApplyInPlace(img);
            invert.ApplyInPlace(img);
            img = colorFilter.Apply(img);

            blackout.StartingPoint = new AForge.IntPoint(0, 0);
            blackout.ApplyInPlace(img);

            img = getgame.Apply(img);

            int tilesx = img.Width / 56;
            int tilesy = img.Height / 56;
            int offsetx = 56 * (int)(tilesx - img.Width / 56.0);
            int offsety = 56 * (int)(tilesy - img.Height / 56.0);

            if ((Math.Abs(offsetx) > 11) || (Math.Abs(offsety) > 11))
                    throw new GameNotFoundException();

            List<IntPoint> corners = new List<IntPoint>();
            Dictionary<IntPoint, Bitmap> tiles = new Dictionary<IntPoint, Bitmap>();
            SimpleQuadrilateralTransformation tileXtract = new SimpleQuadrilateralTransformation();

            for (int j = 0; j < tilesy; j++)
                for (int i = 0; i < tilesx; i++)
                {
                    corners.Add(new IntPoint(offsetx + i * 56,           offsety + j * 56          ));
                    corners.Add(new IntPoint(offsetx + i * 56,           offsety + (j + 1) * 56 - 1));
                    corners.Add(new IntPoint(offsetx + (i + 1) * 56 - 1, offsety + (j + 1) * 56 - 1));
                    corners.Add(new IntPoint(offsetx + (i + 1) * 56 - 1, offsety + j * 56          ));

                    tileXtract.SourceQuadrilateral = corners;

                    tiles.Add(new IntPoint(i, j), tileXtract.Apply(img));

                    corners.Clear();
                }

            img = (Bitmap)Properties.Resources.ResourceManager.GetObject("cb");

            /*Graphics g = Graphics.FromImage(img);
            Pen bluePen = new Pen(Color.Blue, 2);

            for (int i = 0, n = blobs.Length; i < n; i++)
            {
                List<IntPoint> edgePoints = blobCounter.GetBlobsEdgePoints(blobs[i]);
                if (edgePoints.Count > 1)
                {
                    List<IntPoint> corners = PointsCloud.FindQuadrilateralCorners(edgePoints);
                    g.DrawPolygon(bluePen, ToPointsArray(corners));
                }
            }

            bluePen.Dispose();
            g.Dispose();
            */

            return img;
        }
Exemple #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="QuadrilateralTransformationNearestNeighbor"/> class.
 /// </summary>
 ///
 /// <param name="sourceCorners">Corners of the source quadrilateral area.</param>
 ///
 /// <remarks><para>This constructor sets <see cref="AutomaticSizeCalculaton"/> to
 /// <see langword="true"/>, which means that destination image will have width and
 /// height automatically calculated based on <see cref="SourceCorners"/> property.</para></remarks>
 ///
 public QuadrilateralTransformationNearestNeighbor(List <IntPoint> sourceCorners)
 {
     baseFilter = new SimpleQuadrilateralTransformation(sourceCorners)
     {
         UseInterpolation = false
     };
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QuadrilateralTransformationBilinear"/> class.
 /// </summary>
 ///
 /// <param name="sourceCorners">Corners of the source quadrilateral area.</param>
 ///
 /// <remarks><para>This constructor sets <see cref="AutomaticSizeCalculaton"/> to
 /// <see langword="true"/>, which means that destination image will have width and
 /// height automatically calculated based on <see cref="SourceCorners"/> property.</para></remarks>
 ///
 public QuadrilateralTransformationBilinear(List <IntPoint> sourceCorners)
 {
     baseFilter = new SimpleQuadrilateralTransformation(sourceCorners)
     {
         UseInterpolation = true
     };
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QuadrilateralTransformationBilinear"/> class.
 /// </summary>
 ///
 /// <param name="sourceCorners">Corners of the source quadrilateral area.</param>
 /// <param name="newWidth">Width of the new transformed image.</param>
 /// <param name="newHeight">Height of the new transformed image.</param>
 ///
 /// <remarks><para>This constructor sets <see cref="AutomaticSizeCalculaton"/> to
 /// <see langword="false"/>, which means that destination image will have width and
 /// height as specified by user.</para></remarks>
 ///
 public QuadrilateralTransformationBilinear(List <IntPoint> sourceCorners, int newWidth, int newHeight)
 {
     baseFilter = new SimpleQuadrilateralTransformation(sourceCorners, newWidth, newHeight);
     baseFilter.UseInterpolation = true;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QuadrilateralTransformationNearestNeighbor"/> class.
 /// </summary>
 ///
 /// <param name="sourceCorners">Corners of the source quadrilateral area.</param>
 /// <param name="newWidth">Width of the new transformed image.</param>
 /// <param name="newHeight">Height of the new transformed image.</param>
 ///
 /// <remarks><para>This constructor sets <see cref="AutomaticSizeCalculaton"/> to
 /// <see langword="false"/>, which means that destination image will have width and
 /// height as specified by user.</para></remarks>
 ///
 public QuadrilateralTransformationNearestNeighbor(List <IntPoint> sourceCorners, int newWidth, int newHeight)
 {
     this.baseFilter = new SimpleQuadrilateralTransformation(sourceCorners, newWidth, newHeight);
     this.baseFilter.UseInterpolation = false;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QuadrilateralTransformationNearestNeighbor"/> class.
 /// </summary>
 /// 
 /// <param name="sourceCorners">Corners of the source quadrilateral area.</param>
 /// 
 /// <remarks><para>This constructor sets <see cref="AutomaticSizeCalculaton"/> to
 /// <see langword="true"/>, which means that destination image will have width and
 /// height automatically calculated based on <see cref="SourceCorners"/> property.</para></remarks>
 ///
 public QuadrilateralTransformationNearestNeighbor( List<IntPoint> sourceCorners ) 
 {
     baseFilter = new SimpleQuadrilateralTransformation( sourceCorners );
     baseFilter.UseInterpolation = false;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QuadrilateralTransformationBilinear"/> class.
 /// </summary>
 /// 
 /// <param name="sourceCorners">Corners of the source quadrilateral area.</param>
 /// 
 /// <remarks><para>This constructor sets <see cref="AutomaticSizeCalculaton"/> to
 /// <see langword="true"/>, which means that destination image will have width and
 /// height automatically calculated based on <see cref="SourceCorners"/> property.</para></remarks>
 ///
 public QuadrilateralTransformationBilinear( List<IntPoint> sourceCorners )
 {
     baseFilter = new SimpleQuadrilateralTransformation( sourceCorners );
     baseFilter.UseInterpolation = true;
 }