Rotate image using nearest neighbor algorithm.

The class implements image rotation filter using nearest neighbor algorithm, which does not assume any interpolation.

Rotation is performed in counterclockwise direction.

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

Sample usage:

// create filter - rotate for 30 degrees keeping original image size RotateNearestNeighbor filter = new RotateNearestNeighbor( 30, true ); // apply the filter Bitmap newImage = filter.Apply( image );

Initial image:

Result image:

Inheritance: BaseRotateFilter
Exemple #1
0
        private void GeneratedImage()
        {
            Console.WriteLine(string.Format("Start generating\nAngle: {0}'", Grigore.Direction));
            Console.WriteLine(string.Format("Position (x, y): {0}, {1}", Grigore.Position.X, Grigore.Position.Y));

            Bitmap orig;

            try
            {
                orig = (Bitmap)Bitmap.FromFile(OriginalBoardImage);
            }
            catch
            {
                // kill any exception due to missing files
                return;
            }

            Bitmap rotatedRobot = AForge.Imaging.Image.Clone(Grigore.Image, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(orig);

            BaseRotateFilter filter = new RotateNearestNeighbor(Grigore.Direction, false);
            filter.FillColor = Color.Transparent;
            rotatedRobot = filter.Apply(rotatedRobot);

            g.DrawImage(rotatedRobot, Grigore.Position);

            g.Dispose();

            internalImageContainer = orig;

            orig.Save(dirPath + "processed-generated.jpg");
        }
Exemple #2
0
        // =========================================================
        public void RotateSnapshot(double deg)
        {
            while (overlaying)
            {
                Thread.Sleep(10);
            }
            rotating = true;
            // Convert to 24 bpp RGB Image
            Rectangle dimensions = new Rectangle(0, 0, SnapshotOriginalImage.Width, SnapshotOriginalImage.Height);
            Bitmap Snapshot24b = new Bitmap(SnapshotOriginalImage.Width, SnapshotOriginalImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            using (Graphics gr = Graphics.FromImage(Snapshot24b))
            {
                gr.DrawImage(SnapshotOriginalImage, dimensions);
            }

            RotateNearestNeighbor filter = new RotateNearestNeighbor(deg - SnapshotRotation, true);
            Snapshot24b = filter.Apply(Snapshot24b);
            // convert back to 32b, to have transparency
            Snapshot24b.MakeTransparent(Color.Black);
            SnapshotImage = Snapshot24b;
            rotating = false;
        }
Exemple #3
0
        private void GeneratedImage()
        {
            labelDegree.Text = string.Format("{0}'", Grigore.Direction);
            labelPosition.Text = string.Format("{0}, {1}", Grigore.Position.X, Grigore.Position.Y);

            Bitmap orig;

            try {
                orig = (Bitmap)Bitmap.FromFile(OriginalBoardImage);
            }
            catch
            {
                // kill any exception due to missing files
                return;
            }

            Bitmap rotatedRobot = AForge.Imaging.Image.Clone(Grigore.Image, System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            Graphics g = Graphics.FromImage(orig);

            BaseRotateFilter filter = new RotateNearestNeighbor(Grigore.Direction, false);
            filter.FillColor = Color.Transparent;
            rotatedRobot = filter.Apply(rotatedRobot);

            g.DrawImage(rotatedRobot, Grigore.Position);

            g.Dispose();

            pictureBoxGenerated.Image = orig;
        }
        //Public Methods

        public void Rotate(int angleDegClockwise)
        {
            //Validation: Check that we've been asked to rotate through a multiple of 90 deg
            if(angleDegClockwise % 90 != 0)
            {
                throw new ArgumentException("angleDeg must be a multiple of 90");
            }

            //Rotate the Bitmap
            //Note: If this is ever to be used to rotate through arbitrary angles (not 90, 180, 270 deg) then it should decide to use
            //  interpolation or nearest neighbour rotation accordingly
            RotateNearestNeighbor rotate = new RotateNearestNeighbor(-angleDegClockwise); //Rotate by minus angle, as AForge Rotate objects go anti-clockwise
            Bitmap oldBitmap = Bitmap;
            Bitmap = rotate.Apply(oldBitmap);

            //If we're working on a Segmentation object, let it handle the rotation
            if(Segmentation != null)
            {
                Segmentation.Rotate(angleDegClockwise);
            }
            else //Otherwise we're just working on the number of rows & cols
            {
                //If the angle being rotated through is not a multiple of 180 deg (e.g. 90, 270), then the rows & cols will swap
                if (angleDegClockwise % 180 != 0)
                {
                    //Swap rows & cols
                    int newRows = Cols;
                    cols = Rows;
                    rows = newRows;
                }
            }
            
            //Clean up
            oldBitmap.Dispose();
        }