【问题标题】:How to fix skewed image for OCR?如何修复 OCR 的倾斜图像?
【发布时间】:2021-03-19 22:28:14
【问题描述】:

几周前我刚刚开始了 OCR 项目。我遇到了图像倾斜问题我尝试了许多不同的方法似乎没有任何效果所以请帮助:)

下面给出的倾斜图像

我想要下面给出的最终图像

我已经尝试去歪斜图像,但我无法获得最终图像。

public Image<Gray, byte> ImageDeskewOuter(Image<Gray, byte> img)
{
    img = img.Resize(img.Height, img.Width, Inter.Linear);
    Image<Gray, byte> tmp = new Image<Gray, byte>(img.Bitmap);
    tmp = tmp.ThresholdToZero(new Gray(180));

    int nZero = tmp.CountNonzero()[0] == 0 ? 1 : tmp.CountNonzero()[0];
    if (tmp.Bytes.Length / nZero < 10)
        img = tmp.Not();
    else
        img = img.ThresholdToZero(new Gray(80)).InRange(new Gray(0), new Gray(60)).Not();
    tmp = new Image<Gray, byte>(img.Bitmap).Canny(50, 150);

    List<Rectangle> rlist = new List<Rectangle>();
    Rectangle min = new Rectangle();
    Rectangle max = new Rectangle();
    VectorOfVectorOfPoint contour = new VectorOfVectorOfPoint();
    Mat hier = new Mat();
    CvInvoke.FindContours(tmp, contour, hier, RetrType.External, ChainApproxMethod.ChainApproxSimple);
    if (contour.Size > 0)
    {
        for (int i = 0; i < contour.Size; i++)
        {
            Rectangle rec = CvInvoke.BoundingRectangle(contour[i]);
            if (rec.Width > 30 && rec.Width < 120 && rec.Height > 50 && rec.Height < 120)
            {
                rlist.Add(rec);                        
            }
        }
        min = rlist.OrderBy(x => x.X).FirstOrDefault();
        max = rlist.OrderByDescending(x => x.X).FirstOrDefault();
        Rectangle roi = Rectangle.Union(min, max);
        img.ROI = roi;
    }
    if (rlist.Count > 0)
    {
        double angle = LineAngle(min.X, min.Bottom, max.X, max.Bottom, min.X, min.Bottom, max.X, min.Bottom) + 3;
        img = img.Rotate(angle, new Gray(255), false);
    }
    return img;
}

使用上述函数的最终图像

【问题讨论】:

  • #Gimby,正如我上面解释的那样,我想要纠偏图像,请注意不要跳到下一步并执行 OCR ...:)
  • 你在用什么库?
  • EmguCV.3.1.0.1 与 tesseract

标签: c# opencv emgucv


【解决方案1】:

步骤 1 > 选择所需轮廓并应用 CvInvoke.MinAreaRect() 并将图像区域复制到新图像中

步骤 2> 创建一个位图图像,其宽度是所有裁剪图像宽度的总和(如果要将每个图像并排放置)

步骤 3> 使用 Graphics 在位图上绘制图像

    public Image<Gray, byte> ImageDeskew(Image<Gray, byte> img)
    {
        img = img.Resize(img.Height, img.Width, Inter.Linear);
        Image<Gray, byte> tmp = new Image<Gray, byte>(img.Bitmap);
        tmp = new Image<Gray, byte>(AdjustContrast(tmp.Bitmap, 70));

        img = tmp.Not().ThresholdAdaptive(new Gray(255), AdaptiveThresholdType.MeanC, ThresholdType.Binary, 45, new Gray(10));
        tmp = new Image<Gray, byte>(img.Bitmap).Canny(50, 150);

         List<Image<Gray, byte>> imglist = new List<Image<Gray, byte>>();
        Rectangle min = new Rectangle();
        Rectangle max = new Rectangle();
        VectorOfVectorOfPoint contour = new VectorOfVectorOfPoint();
        Mat hier = new Mat();
        CvInvoke.FindContours(tmp, contour, hier, RetrType.External, ChainApproxMethod.ChainApproxSimple);
        if (contour.Size > 0)
        {
            for (int i = 0; i < contour.Size; i++)
            {
                RotatedRect rRect = CvInvoke.MinAreaRect(contour[i]);
                float area = rRect.Size.Width * rRect.Size.Height;
                if (area > (img.Bytes.Length / 10))
                {
                    rlist.Add(rec);
                    if (rRect.Angle > -45) imglist.Add(img.Copy(rRect));
                    else imglist.Add(img.Copy(rRect).Rotate(-90, new Gray(255), false));
                }                    
            }
        }

       
            if (imglist.Count > 0)
            {
                int xPx = imglist.Sum(x => x.Width);
                Bitmap bitmap = new Bitmap(xPx, img.Height);
                using (Graphics g = Graphics.FromImage(bitmap))
                {
                    xPx = 0;
                    imglist.Reverse();
                    foreach (Image<Gray, byte> i in imglist)
                    {
                        g.DrawImage(i.Not().Bitmap, xPx, 0);
                        xPx += i.Width;
                    }
                }
                img = new Image<Gray, byte>(bitmap).Not();
            }
        
        return img;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    相关资源
    最近更新 更多