检测并修复文本偏斜 [英] Detect and fix text skew

查看:132
本文介绍了检测并修复文本偏斜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法(使用像OpenCV这样的东西)来检测文本偏斜并通过旋转图像来纠正它?非常喜欢这个?







如果你知道角度,旋转图像似乎很容易,但对于我正在处理的图像,我不会......它需要以某种方式被检测到。

解决方案

根据您的上述评论,这里是基于教程的代码,可能会有帮助。


Is there a way (using something like OpenCV) to detect text skew and correct it by rotating the image? Pretty much like this?

Rotating an image seems easy enough if you know the angle, but for the images I'm processing, I wont...it will need to be detected somehow.

解决方案

Based on your above comment, here is the code based on the tutorial here, working fine for the above image,

Source

Rotated

 Mat src=imread("text.png",0);
 Mat thr,dst;
 threshold(src,thr,200,255,THRESH_BINARY_INV);
 imshow("thr",thr);

  std::vector<cv::Point> points;
  cv::Mat_<uchar>::iterator it = thr.begin<uchar>();
  cv::Mat_<uchar>::iterator end = thr.end<uchar>();
  for (; it != end; ++it)
    if (*it)
      points.push_back(it.pos());

  cv::RotatedRect box = cv::minAreaRect(cv::Mat(points));
  cv::Mat rot_mat = cv::getRotationMatrix2D(box.center, box.angle, 1);

  //cv::Mat rotated(src.size(),src.type(),Scalar(255,255,255));
  Mat rotated;
  cv::warpAffine(src, rotated, rot_mat, src.size(), cv::INTER_CUBIC);
 imshow("rotated",rotated);

Edit:

Also see the answer here , might be helpful.

这篇关于检测并修复文本偏斜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆