OpenCV - Detect skew angle
来源:互联网 发布:淘宝千人千面在哪设置 编辑:程序博客网 时间:2024/05/17 02:33
OpenCV
Lately I have been interested in the library OpenCV. This library, initially developed by Intel, is dedicated to computer vision applications and is known to be one of the fastest (and maybe the fastest ?) library available for real-time computer vision.
And yet, if you think the library is not fast enough you can install Intel Integrated Performance Primitives (IPP) on your system and benefit from heavily optimized routines. If you have several cores available on your computer, you can also install Intel Threading Building Blocks (TBB). And if it's still not fast enough, you can run some operations on your GPU using CUDA !
All these features looked really exciting, so I decided to experiment a little bit with OpenCV in order to test its interface and expressive power. In this article I present a simple and very short project that detects the skew angle of a digitized document. My goal is not to present an error-proof program but rather to show the ease of use of OpenCV.
Skew angle
The detection of the skew angle of a document is a common preprocessing step in document analysis.
The document might have been slightly rotated during its digitization. It is therefore necessary to compute the skew angle and to rotate the text before going further in the processing pipeline (i.e. words/letters segmentation and later OCR).
We will work on 5 different images of the same short (but famous) text.
The naming convention for those images is rather simple, the first letter stands for the sign of the angle (p for plus, m for minus) and the following number is the value of the angle.
m8.jpg has therefore been rotated by an angle of -8 degrees.
We assume here that the noise introduced by digitization has been removed by a previous preprocessing stage. We also assume that the text has been isolated: no images, horizontal or vertical separators, etc.
Implementation with OpenCV
First, let's declare a function compute_skew, it takes a path to an image as input and outputs the detected angle to standard output.
First we load the image and stores its size in a variable, very simple.
In image processing, objects are white and the background is black, we have the opposite, we need to invert the colors of our image:
And here is the result:
In order to compute the skew we must find straight lines in the text. In a text line, we have several letters side by side, lines should therefore be formed by finding long lines of white pixels in the image. Here is an example below:
Of course as characters have an height, we find several lines for each actual line from the text. By fine tuning the parameters used later or by using preprocessing we can decrease the number of lines.
So, how do we find lines in the image ? We use a powerful mathematical tool called the Hough transform. I won't dig into mathematical details, but the main idea of the Hough transform is to use a 2D accumulator in order to count how many times a given line has been found in the image, the whole image is scanned and by a voting system the "best" lines are identified.
We used a more efficient variant of the Standard Hough Transform (SHT) called the Probabilistic Hough Transform (PHT). In OpenCV the PHT is implemented under the name HoughLinesP.
In addition to the standard parameters of the Hough Transform, we have two additional parameters:
- minLineLength – The minimum line length. Line segments shorter than that will be rejected. This is a great tool in order to prune out small residual lines.
- maxLineGap – The maximum allowed gap between points on the same line to link them.
This could be interesting for a multi-columns text, for example we could choose to not link lines from different text columns.
Back to C++ now, in OpenCV the PHT stores the end points of the line whereas the SHT stores the line in polar coordinates (relative to the origin). We need a vector in order to store all the end points:
We are ready for the Hough transform now:
We use a step size of 1 for
minLineLength is width/2, this is not an unreasonable assumption if the text is well isolated.
maxLineGap is 20, it seems a sound value for a gap.
In the remaining of the code we simply calculate the angle between each line and the horizontal line using the atan2 math function and we compute the mean angle of all the lines.
For debugging purposes we also draw all the lines in a new image called disp_lines and we display this image in a new window.
We just need a main function in order to call compute_skew on several images:
That's all, here are the skew angles we obtain for each image, we have a pretty good accuracy:
- OpenCV - Detect skew angle
- OpenCV - Detect skew angle
- OpenCV - Bounding Box & Skew Angle
- OpenCV RotatedRect angle is wrong
- Face Detect base OpenCV
- angle
- Angle
- wide angle lens stereo calibration with opencv
- Skew
- First time play with JavaCV/OpenCV to detect faces
- OpenCV 的人脸detect及PCA匹配
- OpenCV 的人脸detect及PCA匹配
- opencv学习笔记( 3 )——face detect
- How to Detect and Track Object With OpenCV
- 角度 Angle
- angle 函数
- Vector3.Angle
- **View Angle
- VMWare Workstation 11.0安装Mac OS虚拟机(二)
- Adaboost 算法的原理与推导
- 使用内存映射文件读写大文件
- hive UDF 分享 通过IP地址获取IDC//省份/城市的信息http://my.oschina.net/wangjiankui/blog/263994
- Java4Android-对象的转型
- OpenCV - Detect skew angle
- 逆向调试初探
- IOS开发UI篇--IOS动画(Core Animation)总结
- Android基础入门教程——2.3.4 ImageView(图像视图)
- 关于idea运行项目时突然java文件报错
- 各种排序算法性能之间的比较
- Storm-源码分析-LocalState (backtype.storm.utils)
- Java 枚举变量解析
- 【JavaWeb】(10)微信公众号开发进阶