首页 > 解决方案 > 从特定点而不是中心旋转图像c#

问题描述

我有几张如下图所示的图像,我想将它们旋转几次以进行 OCR。每次10度。

我现在遇到的问题是我可以从中心旋转图像,但是黑色字母始终位于图像中的随机点。因此,虽然我可以再将它旋转 10 度,但它通常会脱离图像。

如何从字母当前所在的中心旋转下面的图像?

请记住,它的位置是随机的。

这是我目前用于旋转的代码

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;

这是我创建的用于查找字母第一个黑色像素位置的代码。

        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;
                }
            }
        }

但是,当我在旋转代码中使用该位置时,我只会得到一个空白的白色图像。

包含字母的图片

在此处输入图像描述

标签: c#imageimage-processing

解决方案


推荐阅读