0

I have several images like the one below where I want to rotate them several times for OCR. 10 degrees each time.

The problem I have now is that I can rotate the images from the center however the black letter is always at a random point in the image. So while I may be able to rotate it by 10 degrees once any more and it usually goes off the image.

How can I rotate the image below from the center of where the letter currently is?

Keep in mind its location is random.

This is the code I'm currently using for rotation

Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
using (Graphics g = Graphics.FromImage(rotatedImage))
{
    // Set the rotation point to the center in the matrix
    g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
    // Rotate
    g.RotateTransform(angle);
    // Restore rotation point in the matrix
    g.TranslateTransform(-bmp.Width / 2, -bmp.Height / 2);
    // Draw the image on the bitmap
    g.DrawImage(bmp, new System.Drawing.Point(0, 0));
}

return rotatedImage;

And this is the code I've created to find the location of the first black pixel of the letter.

        float limit = 0.1f;

        int x = 0;
        int y = 0;

        bool done = false;

        for (int i = 0; i < bmp.Width; i++)
        {
            if (done) break;

            for (int j = 0; j < bmp.Height; j++)
            {
                if (done) break;

                System.Drawing.Color c = bmp.GetPixel(i, j);

                if (c.GetBrightness() < limit)
                {
                    x = i; y = j; done = true;
                }
            }
        }

However when I use that location in the rotation code I just get a blank white image.

Image containing letter

enter image description here

Mr J
  • 2,655
  • 4
  • 37
  • 58
  • You would probably need an additional algorithm to find the center of the letter. One possible way is to find the bounding rectangle by scanning the pixels. Then the center of the rectangle would be your rotation point. – Metheny Apr 24 '19 at 15:48
  • What OCR Software are you using? I am not 100% sure but somewhat confident that nowadays you don't need to rotate (yourself) for getting decent OCR results. – Fildor Apr 24 '19 at 15:49
  • Does [this answer](https://stackoverflow.com/a/26527737/4416750) help? – Lews Therin Apr 24 '19 at 15:49
  • I've tried tesseract and Iron and they both struggle unless the letter is rotated to the expected angle unfortunately – Mr J Apr 24 '19 at 15:49
  • `bmp.Width / 2, bmp.Height / 2` That is the center. Use a different point then. Finding the 1st pixel will not do. You also need the last (right-bottom) pixel of that letter.. – TaW Apr 24 '19 at 16:34

0 Answers0