在旋转图像上绘制矩形

问题描述 投票:0回答:1

我正在尝试对旋转 90 度的文本进行 ocr。因此,为了正确阅读文本,我在其周围绘制一个 ROI 并旋转它以使其笔直,如下所示:

foundimg = foundimg.Rotate(90), new Gray(255), false);

然后进行ocr,并绘制边界矩形。 但我无法在原始图像上的正确位置绘制矩形。

当我使用此代码绘制它时:

foreach (Rectangle drawrect in charrect)
{
    CvInvoke.Rectangle(resultimg, drawrect, new MCvScalar(0, 252, 0), 1, LineType.EightConnected, 0);
}

我得到这个输出:

enter image description here

当我尝试使用此代码旋转矩形时:

foreach (Rectangle drawrect in charrect)
{

    int rotatedX = drawrect.Y + aroi.X;  // Swap X and Y
    int rotatedY = foundimg.Width - drawrect.X - drawrect.Width;  

    int rotatedWidth = drawrect.Height; 
    int rotatedHeight = drawrect.Width;  

    Rectangle rotatedRect = new Rectangle(rotatedX, -rotatedY, rotatedWidth, rotatedHeight);

    CvInvoke.Rectangle(resultimg, rotatedRect, new MCvScalar(0, 252, 0), 1, LineType.EightConnected, 0);
}

我得到这个输出

enter image description here

我似乎无法正确绘制矩形。 请帮忙。

c# .net opencv emgucv
1个回答
0
投票

交换旋转后必须将裁剪 roi Y 坐标添加到 Y:

      foreach (Rectangle drawrect in temprect)
                {
                    // Adjust the coordinates based on the rotation
                    int rotatedX = drawrect.Y + aroi.X;  // Swap X and Y
                    int rotatedY = drawrect.X + aroi.Y;  // Swap X and Y, no need to adjust for the rotation

                    int rotatedWidth = drawrect.Height;  // Swap Width and Height
                    int rotatedHeight = drawrect.Width;  // Swap Width and Height

                    Rectangle rotatedRect = new Rectangle(rotatedX, rotatedY, rotatedWidth, rotatedHeight);

                    CvInvoke.Rectangle(rimg, rotatedRect, new MCvScalar(0, 252, 0), 1, LineType.EightConnected, 0);
                }   
© www.soinside.com 2019 - 2024. All rights reserved.