简体   繁体   中英

How to draw rectangle on original image after rotating image for tesseract?

I have an image with some text in 90 deg, which I'm reading using tesseract and c#. Since the accuracy of reading rotated text is low in tesseract I'm creating an ROI around the text and rotating the roi to make the ROI part of image straight and then reading it with tesseract.

To summarize - There is a main image, within the main image I'm drawing and ROI around the text >> then I'm rotating the ROI 90 degree to make it straight >> then I'm reading the text >> then I draw the bounding rect around each character.

But the bounding box that I get is drawn like it was a straight image and not the original 90 deg ROI. I need the bounding boxes to be drawn on the original ROI. How do I do that?

Here is how it looks: 在此处输入图像描述

Here is how I want it to look: 在此处输入图像描述

This is the code I use to draw rectangle around each character:

rotatedimg = mainimg.Clone();

for (int i = 0; i <roiincrement; i++)
{
    rotatedimg.ROI = ROIRect[i];
    rotatedimg.Rotate(90.0, new McvScalar(255,255,255); // rotating the image 0 deg to make it straight for tesseract to read

    ///....reading part of tesseract.
    var page = tesseract.Process(rotatedimg, PageSegMode.Auto)
    using (var iter = page.GetIterator()) //this parts draw the rect for each character
    {
        iter.Begin();
        Rect symbolBounds;
        do
        {
            if (iter.TryGetBoundingBox(PageIteratorLevel.Symbol, out symbolBounds))
            {                                       
                CvInvoke.cvRectangle(resultimg, new System.Drawing.Point(ROIRect[i].X + symbolBounds.X1, ROIRect[i].Y + symbolBounds.Y1), new System.Drawing.Point(ROIRect[i].X + symbolBounds.X2, ROIRect[i].Y + symbolBounds.Y2), new MCvScalar(0, 255, 0), 1, LINE_TYPE.FOUR_CONNECTED, 0);
            }
        } while (iter.Next(PageIteratorLevel.Symbol));
    }
}

Well I couldn't find any reasonable answers to this question so I took a shortcut. I simply took the ROI of the image before it is rotated for tesseract to read, then found the contours and draw rectangles around those contours. By doing this I get the required bounding boxes. It does add a couple of ms to the processing time, but not much.

This is the result I get now: 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM