您的位置:首页 > 编程语言 > Python开发

Python + OpenCV实现基于傅里叶变换(FFT)的旋转文本校正(文字方向检测)

2016-11-03 09:57 2036 查看

OpenCV实现基于傅里叶变换的旋转文本校正

from: http://johnhany.net/2013/11/dft-based-text-rotation-correction/
发布于
2013年11月25日 作者: John Hany11,222次阅读

代码

        先给出代码,再详细解释一下过程:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

#define GRAY_THRESH 150
#define HOUGH_VOTE 100

//#define DEGREE 27

int main(int argc, char **argv)
{
//Read a single-channel image
const char* filename = "imageText.jpg";
Mat srcImg = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
if(srcImg.empty())
return -1;
imshow("source", srcImg);

Point center(srcImg.cols/2, srcImg.rows/2);

#ifdef DEGREE
//Rotate source image
Mat rotMatS = getRotationMatrix2D(center, DEGREE, 1.0);
warpAffine(srcImg, srcImg, rotMatS, srcImg.size(), 1, 0, Scalar(255,255,255));
imshow("RotatedSrc", srcImg);
//imwrite("imageText_R.jpg",srcImg);
#endif

//Expand image to an optimal size, for faster processing speed
//Set widths of borders in four directions
//If borderType==BORDER_CONSTANT, fill the borders with (0,0,0)
Mat padded;
int opWidth = getOptimalDFTSize(srcImg.rows);
int opHeight = getOptimalDFTSize(srcImg.cols);
copyMakeBorder(srcImg, padded, 0, opWidth-srcImg.rows, 0, opHeight-srcImg.cols, BORDER_CONSTANT, Scalar::all(0));

Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat comImg;
//Merge into a double-channel image
merge(planes,2,comImg);

//Use the same image as input and output,
//so that the results can fit in Mat well
dft(comImg, comImg);

//Compute the magnitude
//planes[0]=Re(DFT(I)), planes[1]=Im(DFT(I))
//magnitude=sqrt(Re^2+Im^2)
split(comImg, planes);
magnitude(planes[0], planes[1], planes[0]);

//Switch to logarithmic scale, for better visual results
//M2=log(1+M1)
Mat magMat = planes[0];
magMat += Scalar::all(1);
log(magMat, magMat);

//Crop the spectrum
//Width and height of magMat should be even, so that they can be divided by 2
//-2 is 11111110 in binary system, operator & make sure width and height are always even
magMat = magMat(Rect(0, 0, magMat.cols & -2, magMat.rows & -2));

//Rearrange the quadrants of Fourier image,
//so that the origin is at the center of image,
//and move the high frequency to the corners
int cx = magMat.cols/2;
int cy = magMat.rows/2;

Mat q0(magMat, Rect(0, 0, cx, cy));
Mat q1(magMat, Rect(0, cy, cx, cy));
Mat q2(magMat, Rect(cx, cy, cx, cy));
Mat q3(magMat, Rect(cx, 0, cx, cy));

Mat tmp;
q0.copyTo(tmp);
q2.copyTo(q0);
tmp.copyTo(q2);

q1.copyTo(tmp);
q3.copyTo(q1);
tmp.copyTo(q3);

//Normalize the magnitude to [0,1], then to[0,255]
normalize(magMat, magMat, 0, 1, CV_MINMAX);
Mat magImg(magMat.size(), CV_8UC1);
magMat.convertTo(magImg,CV_8UC1,255,0);
imshow("magnitude", magImg);
//imwrite("imageText_mag.jpg",magImg);

//Turn into binary image
threshold(magImg,magImg,GRAY_THRESH,255,CV_THRESH_BINARY);
imshow("mag_binary", magImg);
//imwrite("imageText_bin.jpg",magImg);

//Find lines with Hough Transformation
vector<Vec2f> lines;
float pi180 = (float)CV_PI/180;
Mat linImg(magImg.size(),CV_8UC3);
HoughLines(magImg,lines,1,pi180,HOUGH_VOTE,0,0);
int numLines = lines.size();
for(int l=0; l<numLines; l++)
{
float rho = lines[l][0], theta = lines[l][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line(linImg,pt1,pt2,Scalar(255,0,0),3,8,0);
}
imshow("lines",linImg);
//imwrite("imageText_line.jpg",linImg);
if(lines.size() == 3){
cout << "found three angels:" << endl;
cout << lines[0][1]*180/CV_PI << endl << lines[1][1]*180/CV_PI << endl << lines[2][1]*180/CV_PI << endl << endl;
}

//Find the proper angel from the three found angels
float angel=0;
float piThresh = (float)CV_PI/90;
float pi2 = CV_PI/2;
for(int l=0; l<numLines; l++)
{
float theta = lines[l][1];
if(abs(theta) < piThresh || abs(theta-pi2) < piThresh)
continue;
else{
angel = theta;
break;
}
}

//Calculate the rotation angel
//The image has to be square,
//so that the rotation angel can be calculate right
angel = angel<pi2 ? angel : angel-CV_PI;
if(angel != pi2){
float angelT = srcImg.rows*tan(angel)/srcImg.cols;
angel = atan(angelT);
}
float angelD = angel*180/(float)CV_PI;
cout << "the rotation angel to be applied:" << endl << angelD << endl << endl;

//Rotate the image to recover
Mat rotMat = getRotationMatrix2D(center,angelD,1.0);
Mat dstImg = Mat::ones(srcImg.size(),CV_8UC3);
warpAffine(srcImg,dstImg,rotMat,srcImg.size(),1,0,Scalar(255,255,255));
imshow("result",dstImg);
//imwrite("imageText_D.jpg",dstImg);

waitKey(0);

return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

#include <opencv2/core/core.hpp>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <iostream>
 

using namespacecv;
usingnamespacestd;

 
#define GRAY_THRESH 150

#define HOUGH_VOTE 100
 

//#define DEGREE 27
 

int main(intargc,char**argv)
{

//Read a single-channel image
constchar*filename="imageText.jpg";

Mat srcImg=imread(filename,CV_LOAD_IMAGE_GRAYSCALE);
if(srcImg.empty())

return-1;
imshow("source",srcImg);

 
Pointcenter(srcImg.cols/2,srcImg.rows/2);

 
#ifdef DEGREE

//Rotate source image
MatrotMatS=getRotationMatrix2D(center,DEGREE,1.0);

warpAffine(srcImg,srcImg,rotMatS,srcImg.size(),1,0,Scalar(255,255,255));
imshow("RotatedSrc",srcImg);

//imwrite("imageText_R.jpg",srcImg);
#endif

 
//Expand image to an optimal size, for faster processing speed

//Set widths of borders in four directions
//If borderType==BORDER_CONSTANT, fill the borders with (0,0,0)

Mat padded;
intopWidth=getOptimalDFTSize(srcImg.rows);

intopHeight=getOptimalDFTSize(srcImg.cols);
copyMakeBorder(srcImg,padded,0,opWidth-srcImg.rows,0,opHeight-srcImg.cols,BORDER_CONSTANT,Scalar::all(0));

 
Matplanes[]={Mat_<float>(padded),Mat::zeros(padded.size(),CV_32F)};

Mat comImg;
//Merge into a double-channel image

merge(planes,2,comImg);
 

//Use the same image as input and output,
//so that the results can fit in Mat well

dft(comImg,comImg);
 

//Compute the magnitude
//planes[0]=Re(DFT(I)), planes[1]=Im(DFT(I))

//magnitude=sqrt(Re^2+Im^2)
split(comImg,planes);

magnitude(planes[0],planes[1],planes[0]);
 

//Switch to logarithmic scale, for better visual results
//M2=log(1+M1)

Mat magMat=planes[0];
magMat+=Scalar::all(1);

log(magMat,magMat);
 

//Crop the spectrum
//Width and height of magMat should be even, so that they can be divided by 2

//-2 is 11111110 in binary system, operator & make sure width and height are always even
magMat=magMat(Rect(0,0,magMat.cols&-2,magMat.rows&-2));

 
//Rearrange the quadrants of Fourier image,

//so that the origin is at the center of image,
//and move the high frequency to the corners

intcx=magMat.cols/2;
intcy=magMat.rows/2;

 
Matq0(magMat,Rect(0,0,cx,cy));

Mat q1(magMat,Rect(0,cy,cx,cy));
Matq2(magMat,Rect(cx,cy,cx,cy));

Mat q3(magMat,Rect(cx,0,cx,cy));
 

Mat tmp;
q0.copyTo(tmp);

q2.copyTo(q0);
tmp.copyTo(q2);

 
q1.copyTo(tmp);

q3.copyTo(q1);
tmp.copyTo(q3);

 
//Normalize the magnitude to [0,1], then to[0,255]

normalize(magMat,magMat,0,1,CV_MINMAX);
MatmagImg(magMat.size(),CV_8UC1);

magMat.convertTo(magImg,CV_8UC1,255,0);
imshow("magnitude",magImg);

//imwrite("imageText_mag.jpg",magImg);
 

//Turn into binary image
threshold(magImg,magImg,GRAY_THRESH,255,CV_THRESH_BINARY);

imshow("mag_binary",magImg);
//imwrite("imageText_bin.jpg",magImg);

 
//Find lines with Hough Transformation

vector<Vec2f>lines;
floatpi180=(float)CV_PI/180;

Mat linImg(magImg.size(),CV_8UC3);
HoughLines(magImg,lines,1,pi180,HOUGH_VOTE,0,0);

intnumLines=lines.size();
for(intl=0;l<numLines;l++)

{
floatrho=lines[l][0],theta=lines[l][1];

Point pt1,pt2;
doublea=cos(theta),b=sin(theta);

doublex0=a*rho,y0=b*rho;
pt1.x=cvRound(x0+1000*(-b));

pt1.y=cvRound(y0+1000*(a));
pt2.x=cvRound(x0-1000*(-b));

pt2.y=cvRound(y0-1000*(a));
line(linImg,pt1,pt2,Scalar(255,0,0),3,8,0);

}
imshow("lines",linImg);

//imwrite("imageText_line.jpg",linImg);
if(lines.size()==3){

cout<<"found three angels:"<<endl;
cout<<lines[0][1]*180/CV_PI<<endl<<lines[1][1]*180/CV_PI<<endl<<lines[2][1]*180/CV_PI<<endl<<endl;

}
 

//Find the proper angel from the three found angels
floatangel=0;

floatpiThresh=(float)CV_PI/90;
floatpi2=CV_PI/2;

for(intl=0;l<numLines;l++)
{

floattheta=lines[l][1];
if(abs(theta)<piThresh||abs(theta-pi2)<piThresh)

continue;
else{

angel=theta;
break;

}
}

 
//Calculate the rotation angel

//The image has to be square,
//so that the rotation angel can be calculate right

angel=angel<pi2?angel:angel-CV_PI;
if(angel!=pi2){

floatangelT=srcImg.rows*tan(angel)/srcImg.cols;
angel=atan(angelT);

}
floatangelD=angel*180/(float)CV_PI;

cout<<"the rotation angel to be applied:"<<endl<<angelD<<endl<<endl;
 

//Rotate the image to recover
MatrotMat=getRotationMatrix2D(center,angelD,1.0);

Mat dstImg=Mat::ones(srcImg.size(),CV_8UC3);
warpAffine(srcImg,dstImg,rotMat,srcImg.size(),1,0,Scalar(255,255,255));

imshow("result",dstImg);
//imwrite("imageText_D.jpg",dstImg);

waitKey(0);

 
return0;

}

过程
读取图片


Mat srcImg = imread(filename, CV_LOAD_IMAGE_GRAYSCALE);
if(srcImg.empty())
return -1;

1
2
3

MatsrcImg=imread(filename,CV_LOAD_IMAGE_GRAYSCALE);

if(srcImg.empty())
return-1;

        srcImg.empty()用来判断是否成功读进图像,如果srcImg中没有数据,在后面的步骤会产生内存错误。

        由于处理的是文本,彩色信息不会提供额外帮助,所以要用CV_LOAD_IMAGE_GRAYSCALE表明以灰度形式读进图像。

        假定读取的图像如下:





旋转原图像(可选)

Point center(srcImg.cols/2, srcImg.rows/2);

#ifdef DEGREE
//Rotate source image
Mat rotMatS = getRotationMatrix2D(center, DEGREE, 1.0);
warpAffine(srcImg, srcImg, rotMatS, srcImg.size(), 1, 0, Scalar(255,255,255));
imshow("RotatedSrc", srcImg);
//imwrite("H:\\imageText_02_R.jpg",srcImg);
#endif

1
2
3
4
5
6
7
8
9

Pointcenter(srcImg.cols/2,srcImg.rows/2);

 
#ifdef DEGREE

//Rotate source image
MatrotMatS=getRotationMatrix2D(center,DEGREE,1.0);

warpAffine(srcImg,srcImg,rotMatS,srcImg.size(),1,0,Scalar(255,255,255));
imshow("RotatedSrc",srcImg);

//imwrite("H:\\imageText_02_R.jpg",srcImg);
#endif

        如果手头没有这样的倾斜图像,可以选择一张正放的文本图像,再把第12行#define DEGREE那行前的注释符号去掉。然后这部分代码就会把所给的图像旋转你规定的角度,再交给后面处理。

图像延扩

Mat padded;
int opWidth = getOptimalDFTSize(srcImg.rows);
int opHeight = getOptimalDFTSize(srcImg.cols);
copyMakeBorder(srcImg, padded, 0, opWidth-srcImg.rows, 0, opHeight-srcImg.cols, BORDER_CONSTANT, Scalar::all(0));

1
2
3
4

Matpadded;

int opWidth=getOptimalDFTSize(srcImg.rows);
intopHeight=getOptimalDFTSize(srcImg.cols);

copyMakeBorder(srcImg,padded,0,opWidth-srcImg.rows,0,opHeight-srcImg.cols,BORDER_CONSTANT,Scalar::all(0));

        OpenCV中的DFT采用的是快速算法,这种算法要求图像的尺寸是2、3和5的倍数时处理速度最快。所以需要用getOptimalDFTSize()找到最适合的尺寸,然后用copyMakeBorder()填充多余的部分。这里是让原图像和扩大的图像左上角对齐。填充的颜色如果是纯色对变换结果的影响不会很大,后面寻找倾斜线的过程又会完全忽略这一点影响。

DFT

Mat planes[] = {Mat_<float>(padded), Mat::zeros(padded.size(), CV_32F)};
Mat comImg;

merge(planes,2,comImg);

dft(comImg, comImg);

1
2
3
4
5
6

Matplanes[]={Mat_<float>(padded),Mat::zeros(padded.size(),CV_32F)};

Mat comImg;
 

merge(planes,2,comImg);
 

dft(comImg,comImg);

        DFT要分别计算实部和虚部,把要处理的图像作为输入的实部、一个全零的图像作为输入的虚部。dft()输入和输出应该分别为单张图像,所以要先用merge()把实虚部图像合并,分别处于图像comImg的两个通道内。计算得到的实虚部仍然保存在comImg的两个通道内。

获得DFT图像

split(comImg, planes);
magnitude(planes[0], planes[1], planes[0]);

Mat magMat = planes[0];
magMat += Scalar::all(1);
log(magMat, magMat);

1
2
3
4
5
6

split(comImg,planes);

magnitude(planes[0],planes[1],planes[0]);
 

Mat magMat=planes[0];
magMat+=Scalar::all(1);

log(magMat,magMat);

        一般都会用幅度图像来表示图像傅里叶的变换结果(傅里叶谱)。

        幅度的计算公式:magnitude = sqrt(Re(DFT)^2 + Im(DFT)^2)。

        由于幅度的变化范围很大,而一般图像亮度范围只有[0,255],容易造成一大片漆黑,只有几个点很亮。所以要用log函数把数值的范围缩小。

 

magMat = magMat(Rect(0, 0, magMat.cols & -2, magMat.rows & -2));

int cx = magMat.cols/2;
int cy = magMat.rows/2;

Mat q0(magMat, Rect(0, 0, cx, cy));
Mat q1(magMat, Rect(0, cy, cx, cy));
Mat q2(magMat, Rect(cx, cy, cx, cy));
Mat q3(magMat, Rect(cx, 0, cx, cy));

Mat tmp;
q0.copyTo(tmp);
q2.copyTo(q0);
tmp.copyTo(q2);

q1.copyTo(tmp);
q3.copyTo(q1);
tmp.copyTo(q3);

normalize(magMat, magMat, 0, 1, CV_MINMAX);
Mat magImg(magMat.size(), CV_8UC1);
magMat.convertTo(magImg,CV_8UC1,255,0);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

magMat=magMat(Rect(0,0,magMat.cols&-2,magMat.rows&-2));

 
intcx=magMat.cols/2;

int cy=magMat.rows/2;
 

Mat q0(magMat,Rect(0,0,cx,cy));
Matq1(magMat,Rect(0,cy,cx,cy));

Mat q2(magMat,Rect(cx,cy,cx,cy));
Matq3(magMat,Rect(cx,0,cx,cy));

 
Mattmp;

q0.copyTo(tmp);
q2.copyTo(q0);

tmp.copyTo(q2);
 

q1.copyTo(tmp);
q3.copyTo(q1);

tmp.copyTo(q3);
 

normalize(magMat,magMat,0,1,CV_MINMAX);
MatmagImg(magMat.size(),CV_8UC1);

magMat.convertTo(magImg,CV_8UC1,255,0);

        dft()直接获得的结果中,低频部分位于四角,高频部分位于中间。习惯上会把图像做四等份,互相对调,使低频部分位于图像中心,也就是让频域原点位于中心。

 





        虽然用log()缩小了数据范围,但仍然不能保证数值都落在[0,255]之内,所以要先用normalize()规范化到[0,1]内,再用convertTo()把小数映射到[0,255]内的整数。结果保存在一幅单通道图像内:

 





Hough直线检测

        从傅里叶谱可以明显地看到一条过中心点的倾斜直线。要想求出这个倾斜角,首先要在图像上找出这条直线。

        一个很方便的方法是采用霍夫(Hough)变换检测直线。

threshold(magImg,magImg,GRAY_THRESH,255,CV_THRESH_BINARY);

1

threshold(magImg,magImg,GRAY_THRESH,255,CV_THRESH_BINARY);

        Hough变换要求输入图像是二值的,所以要用threshold()把图像二值化。

        二值化的一种结果:





vector<Vec2f> lines;
float pi180 = (float)CV_PI/180;
Mat linImg(magImg.size(),CV_8UC3);
HoughLines(magImg,lines,1,pi180,HOUGH_VOTE,0,0);
int numLines = lines.size();
for(int l=0; l<numLines; l++)
{
float rho = lines[l][0], theta = lines[l][1];
Point pt1, pt2;
double a = cos(theta), b = sin(theta);
double x0 = a*rho, y0 = b*rho;
pt1.x = cvRound(x0 + 1000*(-b));
pt1.y = cvRound(y0 + 1000*(a));
pt2.x = cvRound(x0 - 1000*(-b));
pt2.y = cvRound(y0 - 1000*(a));
line(linImg,pt1,pt2,Scalar(255,0,0),3,8,0);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

vector<Vec2f>lines;

float pi180=(float)CV_PI/180;
MatlinImg(magImg.size(),CV_8UC3);

HoughLines(magImg,lines,1,pi180,HOUGH_VOTE,0,0);
intnumLines=lines.size();

for(intl=0;l<numLines;l++)
{

floatrho=lines[l][0],theta=lines[l][1];
Pointpt1,pt2;

doublea=cos(theta),b=sin(theta);
doublex0=a*rho,y0=b*rho;

pt1.x=cvRound(x0+1000*(-b));
pt1.y=cvRound(y0+1000*(a));

pt2.x=cvRound(x0-1000*(-b));
pt2.y=cvRound(y0-1000*(a));

line(linImg,pt1,pt2,Scalar(255,0,0),3,8,0);
}

        这一部分用HoughLines()检测图像中可能存在的直线,并把直线参数保存在向量组lines中,然后绘制出找到的直线。

        两个参数GRAY_THRESH和HOUGH_VOTE需要手动指定,不同的图像需要设置不同的参数,同一段文本旋转不同的角度也需要不同的参数。GRAY_THRESH越大,二值化的阈值就越高;HOUGH_VOTE越大,霍夫检测的投票数就越高(需要更多的共线点来确定一条直线)。说白了,如果发现二值化图像中直线附近有很多散点,就要适当提高GRAY_THRESH;如果发现从二值图像的一条直线上检测到了几条角度相差很小的直线,就需要适当提高HOUGH_VOTE。我们希望得到的结果时刚好检测到三条直线(有时只能检测到一条直线,后面会给出一个例子)。

        检测到的直线:





计算倾斜角

        上面得到了三个角度,一个是0度,一个是90度,另一个就是我们所需要的倾斜角。要把这个角找出来,而且要考虑误差。

float angel=0;
float piThresh = (float)CV_PI/90;
float pi2 = CV_PI/2;
for(int l=0; l<numLines; l++)
{
float theta = lines[l][1];
if(abs(theta) < piThresh || abs(theta-pi2) < piThresh)
continue;
else{
angel = theta;
break;
}
}

angel = angel<pi2 ? angel : angel-CV_PI;
if(angel != pi2){
float angelT = srcImg.rows*tan(angel)/srcImg.cols;
angel = atan(angelT);
}
float angelD = angel*180/(float)CV_PI;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

floatangel=0;

float piThresh=(float)CV_PI/90;
floatpi2=CV_PI/2;

for(intl=0;l<numLines;l++)
{

floattheta=lines[l][1];
if(abs(theta)<piThresh||abs(theta-pi2)<piThresh)

continue;
else{

angel=theta;
break;

}
}

 
angel=angel<pi2?angel:angel-CV_PI;

if(angel!=pi2){
floatangelT=srcImg.rows*tan(angel)/srcImg.cols;

angel=atan(angelT);
}

float angelD=angel*180/(float)CV_PI;

        由于DFT的特点,只有输入图像是正方形时,检测到的角才是文本真正旋转的角度。但我们的输入图像不一定是正方形的,所以要根据图像的长宽比改变这个角度。

        还有一个需要注意的细节,虽然HoughLines()输出的倾斜角在[0,180)之间,但在[0,90]和(90,180)之间这个角的含义是不同的。请看图示:

 





        当倾斜角大于90度时,(180-倾斜角)才是直线相对竖直方向的偏离角度。在OpenCV中,逆时针旋转,角度为正。要把图像转回去,这个角度就变成了(倾斜角-180)。
校正图像

        最后一步,当然是把图像转回去~

Mat rotMat = getRotationMatrix2D(center,angelD,1.0);
Mat dstImg = Mat::ones(srcImg.size(),CV_8UC3);
warpAffine(srcImg,dstImg,rotMat,srcImg.size(),1,0,Scalar(255,255,255));

1
2
3

MatrotMat=getRotationMatrix2D(center,angelD,1.0);

Mat dstImg=Mat::ones(srcImg.size(),CV_8UC3);
warpAffine(srcImg,dstImg,rotMat,srcImg.size(),1,0,Scalar(255,255,255));

 

        先用getRotationMatrix2D()获得一个2*3的仿射变换矩阵,再把这个矩阵输入warpAffine(),做一个单纯旋转的仿射变换。warpAffine()的最后一个参数Scalar(255,255,255)是把由于旋转产生的空白用白色填充。

        校正的结果:





一个检测单条直线的例子
原始图像:





傅里叶谱:





        只有一条明显的直线。还好仅有的这条直线正是我们所需要的。

检测直线:





校正结果:





对中文的效果

        我们来试试看这段程序对中文的校正效果。

输入图像:





傅里叶谱:





        可以发现有许多条平行的亮线,其中过频域原点的那条长度最长,最容易检测出来。

检测直线:





校正结果:





        虽然中文和英文在文字上有很大的不同,但字母(或者文字)的高度比较一致,使得行与行之间的分隔很明显。所以它们的频域特征是相似的。

对其他语言文字的效果

        我从IMDB.com摘取影片《教父》的英文介绍,然后用谷歌翻译成其他文字进行测试。

阿拉伯语













一枚反例

老挝语:





傅里叶谱:





一种二值化的结果:





直线检测:





        这种文字的很多字母的上下方多了很多“笔画”(我不知道该怎么称呼那些小曲线),让行与行之间的分离变得不明显,使得频域特征变得不明显。

        虽然用肉眼可以看出傅里叶谱中存在一条倾斜的直线,但它的亮度太低,二值化过程很难排除噪声,导致直线检测会首先检出噪声产生的直线。这也是我的程序目前受限之处。需要增加一个过滤散点噪声的步骤以增加程序的适用范围。

参考:Discrete Fourier Transform — OpenCV 2.4.7.0 documentation

代码还可以在这里下载:https://github.com/johnhany/textRotCorrect

2014.1.3更新:

        由于文章内的图片右下角存在水印,若直接使用文章内的图片进行处理会使频域原点附近增加一团亮点,妨碍直线的检出。而且为了节省空间,图片是经过缩小的,使得字母的边缘变得模糊,频域特征也减弱。为此我提供了十幅没有水印的图片,供想要亲手实验的朋友使用。下载链接

类别:图像处理标签:opencv,文本处理

文章导航

N条线段求交的扫描线算法
Java+MySQL实现网络爬虫程序

共有21条评论


ma说道:
2016年5月31日 上午11:43

您好,请问一下您使用的这个方法是有没有发过相应的文章,因为我的实验里用到了您的方法,论文的时候需要写这个方法的来源,所以想问一下您这个方法来自您自己发过的文章里的还是其他书本或期刊上的。谢谢您。

回复


John
Hany
说道:
2016年6月2日 上午11:14

我的方法并不是完全原创,参考的资料也只有文章末尾的那个官方文档页面。那个页面的最后一节只是提到水平与倾斜的文本频谱图像是有差异的,也没有提及相关的文献,我也是自己用代码实验了一下,所以也没有发过相应的文章。某些杂志是允许把网址作为参考文献的,可以按照格式要求直接把这篇文章的链接写上。

回复


ma说道:
2016年7月13日 上午11:05

谢谢。

回复


ma说道:
2016年5月28日 下午6:45

您好,看了您的方法后,我用C语言试用了一下,但是不知道为什么在HoughLines后,呈现出来的图象是全黑的。为了方便,我贴出这部分的代码,不知道哪里出了问题,想向您请教一下。谢谢。

 

CvMemStorage *storage  = cvCreateMemStorage(0);

 CvSeq* lines = NULL;

 float pi180 = (float)CV_PI/180;

 lines = cvHoughLines2(Image,storage,CV_HOUGH_STANDARD,1,pi180,HOUGH_VOTE,0,0); //Image是之前的二值化图象

IplImage *lineImg;

 lineImg = cvCreateImage(cvGetSize(src),IPL_DEPTH_8U,1);

 int numLines = lines->total;

 int i=0;

 for(i=0;i<numLines;i++)

   {

     float *line = (float*) cvGetSeqElem(lines,i);

     float rho = line[0];

     float theta = line[1];

     CvPoint pt1,pt2;

     double a = cos(theta), b = sin(theta);

     double x0 = a*rho, y0 = b*rho;

     pt1.x = cvRound(x0+1000*(-b));

     pt1.y = cvRound(y0+1000*(a));

     pt2.x = cvRound(x0-1000*(-b));

     pt2.y = cvRound(y0-1000*(a));

     cvLine(lineImg,pt1,pt2,CV_RGB(255,0,0),3,8,0);

}

    cvNamedWindow("line",0);

    cvShowImage("line",lineImg);

    cvWaitKey(10000);

回复


John
Hany
说道:
2016年6月2日 上午11:15

抱歉,最近事情比较多,现在问题解决了吗?

回复


ma说道:
2016年7月13日 上午11:03

已经解决了,抱歉,最近都没有看信息。

回复


席大军说道:
2016年4月20日 上午11:50

很好用的代码,十分感谢!

回复


John
Hany
说道:
2016年4月20日 下午12:35

多谢支持!

回复


元冲说道:
2016年1月7日 下午4:05

作者您好,我想做的是一幅图像经过傅里叶变换之后,分别得到实部和虚部的数据,看了您的文章,深受启发,但是遇到个问题,下面三步算是对原有图像填充的尺寸,但是填充值后傅里叶变换得到的虚部和实部尺寸也已改变,怎么让实部虚部数据都还原到填充前的尺寸呢?非常感谢!

int opWidth = getOptimalDFTSize(srcImg.rows);

int opHeight = getOptimalDFTSize(srcImg.cols);

copyMakeBorder(srcImg, padded, 0, opWidth-srcImg.rows, 0, opHeight-srcImg.cols, BORDER_CONST

回复


John
Hany
说道:
2016年1月8日 上午12:43

原有图像的纯色填充对频域的影响应该是可以忽略的。况且这三个函数改变的是图像的空间域,经过FFT之后得到的频域图像,其尺寸也失去了与原来空间域尺寸比较的意义。虽然OpenCV库函数默认产生的频谱图像本身的大小与输入图像相同,但实际是对频域横纵坐标缩放的结果,其本质上仍然是沿坐标轴对称的正方形区域。如果直接对频谱图像切割,会造成原始图像频率上信息的丢失(高频或是低频,取决于是否将四象限对调)。

关于频域对应空间域的关系,可以参考这个问题的第一个答案,个人觉得解释得很精湛:http://dsp.stackexchange.com/questions/1637/what-does-frequency-domain-denote-in-case-of-images

回复


元冲说道:
2016年1月10日 下午5:51

感谢您的详细解答,可能我太笨啦,有个问题还是想不太明白,希望您解惑。问题描述:其实我是在把MATLAB代码变成c++时遇到的提取图像经过fft变换后的实部和虚部数据这个问题,下面是MATLAB代码:

//tilted_array2,array1是灰度图像

//PCE是一个计算相关度的函数

TA = fft2(tilted_array2);           clear tilted_array2

FA = fft2(array1);                  clear array1

FF = FA .* TA;                      clear FA TA

ret = real(ifft2(FF));

detection = PCE(ret)

可以看到ret得到的是FF傅里叶逆变换之后的实部数据,然后在PCE函数中得到相关度的数值。

我把上面的MATLAB代码变成如下c++代码(调用了opencv库)(菜鸟代码可能写的乱七八糟,见谅~):

Mat paddedTA;

  int mTA = getOptimalDFTSize(tilted_array2.rows);  

  int nTA = getOptimalDFTSize(tilted_array2.cols);

  copyMakeBorder(tilted_array2, paddedTA,0,mTA-tilted_array2.rows,0,nTA-tilted_array2.cols, BORDER_CONSTANT, Scalar::all(0));

Mat planesTA[] ={Mat_<float(paddedTA),Mat::zeros(paddedTA.size(), CV_32F) };

    Mat TA;

    merge(planesTA, 2, TA);

    dft(TA, TA);

    split(TA,planesTA); 

Mat paddedFA;

    int mFA = getOptimalDFTSize(array1.rows); 

    int nFA = getOptimalDFTSize(array1.cols);

    copyMakeBorder(array1, paddedFA, 0, mFA-array1.rows, 0, nFA-array1.cols, BORDER_CONSTANT, Scalar::all(0));

    Mat planesFA[] = {Mat_<float>(paddedFA), Mat::zeros(paddedFA.size(), CV_32F) };

    Mat FA;

    merge(planesFA, 2, FA);

    dft(FA, FA);

    split(FA, planesFA);

 //下面实现MATLAB中的FF = FA .* TA;   

Mat FF1,FF2,FF3,FF4;

    FF1=planesFA[0].mul(planesTA[0]);

    FF2=planesFA[1].mul(planesTA[1]);

    FF3=planesFA[0].mul(planesTA[1]);

    FF4=planesFA[1].mul(planesTA[0]);

    Mat planesFF[] = {Mat::zeros(paddedTA.size(), CV_32F), Mat::zeros(paddedTA.size(), CV_32F) };

    subtract(FF1,FF2,planesFF[0]);

    add(FF3,FF4,planesFF[1]);

    Mat FF;

    merge(planesFF, 2, FF);

    

    idft(FF,FF,cv::DFT_SCALE | cv::DFT_REAL_OUTPUT );

    split(FF,planesFF);

    Mat rel=planesFF[0];

    PCE(rel,detection);

因为getOptimalDFTSize()函数是把输入图像的尺寸调节成2^n或2,3,5乘积这种形式,当我的输入图像像素满足是2^n或2,3,5乘积这种形式时,程序不会对原图像填充补0,这时opencv得到的planesTA、planesFA的实部和虚部数据都与MATLAB中TA、FA值一样(行列数也都一样),PCE函数得到的最后结果detection也是一样的。但是,如果输入图像不满足像素是2^n或2,3,5乘积这种形式时,程序会对输入图像进行填充,这样得到的

planesTA、planesFA的行列都是填充之后的行列,与输入图像行列不同,这时

planesTA、planesFA的实部和虚部数据都与MATLAB中TA、FA对应数据肯定不同,但我想着可能对最后的PCE函数结果没有影响,结果却是这种情况下opencv的PCE结果与MATLAB也是不同的。

所以我想请教您的是,输入图像像素不满足条件时最后PCE结果不同的这个问题该怎么解决?不胜感谢!

回复


元冲说道:
2016年1月10日 下午6:01

为方便您的理解我贴出函数PCE的MATLAB代码:

shift_range = [0,0];

Cinrange = rel(end-shift_range(1):end,end-shift_range(2):end);

[max_cc, imax] = max(Cinrange(:));

[ypeak, xpeak] = ind2sub(size(Cinrange),imax(1));

peakheight = Cinrange(ypeak,xpeak);

C_without_peak = RemoveNeighborhood(C,[ypeak, xpeak],squaresize);//RemoveNeighborhood是一个函数,下面有代码

correl = rel(end,end);        clear rel

PCE_energy = mean(C_without_peak.*C_without_peak);

detection = peakheight.^2/PCE_energy * sign(peakheight);

 

//RemoveNeighborhood函数

function Y = RemoveNeighborhood(X,x,ssize)

% Remove a 2-D neighborhood around x=[x1,x2] from matrix X and output a 1-D vector Y

% ssize     square neighborhood has size (ssize x ssize) square

[M,N] = size(X);

radius = (ssize-1)/2;

X = circshift(X,[radius-x(1)+1,radius-x(2)+1]);

Y = X(ssize+1:end,1:ssize);  

Y = Y(:);

Y = [Y;X(M*ssize+1:end)'];

非常感谢!

 

 

 

回复


xtkwfn说道:
2016年4月13日 下午2:53

在fft前不进行速度优化,直接对图像进行操作即可


Jason说道:
2015年12月11日 上午10:55

是把高频部分移到中心位置吧?

回复


John
Hany
说道:
2015年12月11日 下午4:30

象限换位的目的就是把频域原点移到图像中心,所以移位后低频是位于中心区域的。频谱图像中心的小亮点F(0,0)表示图像的平均灰度,也能说明这一点。

感兴趣的话,可以试着把一幅图像作高斯模糊,再比较一下前后的频谱图像,坐标轴附近的点亮度是会增加的。

回复


wahaha893611361说道:
2015年11月9日 下午3:58

试了一下 英文很好用,中文就不行了,葡萄牙语也不行,西班牙语还能凑合着用。总之,非常感谢博主分享。另有什么改善的方法么?

回复


John
Hany
说道:
2015年11月10日 下午12:37

多谢支持!就方法上的改进可以用调整参数,过滤散点噪声等手段,但从理论上我目前还没有更好的改进思路。

回复


ET民工说道:
2015年9月12日 下午4:26

非常感谢如此详细的文章!

回复


John
Hany
说道:
2015年9月13日 上午1:25

多谢支持!

回复


马东兴说道:
2014年9月30日 下午7:01

能不能判断出一个图片中的三角形倾斜的角度?

回复


John
Hany
说道:
2015年1月27日 下午5:38

抱歉回复的很晚。本文的方法是不能用来计算单个三角形倾斜的角度的,只能考虑其他方法

回复
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: