【发布时间】: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