This repository was archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
265 lines (239 loc) · 9.13 KB
/
main.cpp
File metadata and controls
265 lines (239 loc) · 9.13 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#define _CRT_SECURE_NO_WARNINGS
#include <opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <iostream>
#include <string.h>
#include <ctype.h>
#include <vector>
using namespace std;
using namespace cv;
#define GRAY_THRESH 150
#define HOUGH_VOTE 100
class Receipt
{
public:
Receipt(string path)
{
m_path=path;
flag=0;
}
void TiltCorrection()
{
Mat srcImg = imread(m_path,0); //读取图片
int t=1000;
resize(srcImg, srcImg, Size(t, srcImg.rows*1.0 / srcImg.cols * t), 0, 0, CV_INTER_LINEAR);
Image=srcImg;
//return srcImg;
Point center(srcImg.cols / 2, srcImg.rows / 2);
//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);
//Turn into binary image
threshold(magImg, magImg, GRAY_THRESH, 255, CV_THRESH_BINARY);
//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);
}
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));
if (abs(angelD)<45.0)Image=dstImg;
}
void ProcessStr()
{
FILE* fp=fopen("temp","r");
FILE* fp2=fopen("result","at");
char s[1024],t1[100],t2[100];
int flag1=12;
while (!feof(fp)) {
int cnt=0,cnt2=0;
fgets(s,1024,fp); //读取一行
int len=strlen(s);
if(len<8)continue;
for(int i=len-1;i>=0&&cnt<flag1;i--)
if(s[i]>='0'&&s[i]<='9')t1[cnt++]=s[i];
for(int i=cnt-1;i>=0;i--)t2[cnt2++]=t1[i];
t2[cnt2]=0;
if(cnt2==12)flag=(t2[0]=='1')?1:2,flag1=8;
if(cnt2)fprintf(fp2,"%s\n", t2); //输出
}
fclose(fp);
remove("temp");
fclose(fp2);
}
void ocr(Mat data){
char *outText;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
// Initialize tesseract-ocr with English, without specifying tessdata path
if (api->Init(NULL, "chi_sim")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
//api->SetVariable("tessedit_char_whitelist", "0123456789");
//api->SetVariable("tessedit_char_blacklist", "!o");
api->SetPageSegMode(tesseract::PSM_SPARSE_TEXT_OSD);
api->SetImage((uchar*)data.data, data.size().width, data.size().height, data.channels(), data.step1());
// Get OCR result
outText = api->GetUTF8Text();
//printf("%s ",outText);
api->Clear();
api->End();
FILE* fp=fopen("temp","w");
fprintf(fp,"%s",outText);
fclose(fp);
ProcessStr();
}
void Img2b() //二值化
{
//threshold(Image, Image, 170, 255, THRESH_BINARY_INV);
int blockSize =41 ;
int constValue = 5;
cv::adaptiveThreshold(Image, Image, 255, CV_ADAPTIVE_THRESH_MEAN_C, CV_THRESH_BINARY, blockSize, constValue);
//imshow("二值化",Image);
}
void ProcessImage()
{
TiltCorrection();
temp=Image.clone();
/// 使用中值平滑
// imshow("dd1",Image);
medianBlur ( Image, Image, 1);
//imshow("dd",temp);
Img2b();
ocr(Image);
//imshow("发票",Image);
//imwrite("temp.jpg", Image);
}
string type()
{
string s[3]={"无效","国税","地税"};
return s[flag];
}
private:
string m_path;
Mat Image;
Mat temp;
int flag;
vector<vector<Point> > contours;
};
void Output(string result)
{
FILE * fp = fopen("result", "r");
cout<<endl;
cout<<result<<endl;
if(result=="无效");
else{
char str[][20] = { "发票代码:", "发票号码:", "验证码:" },s[1024];
int cnt=(result=="国税")?2:3,cnt1=0;
while (!feof(fp)&&cnt1<cnt) {
fgets(s,1024,fp); //读取一行
if(strlen(s)>0)printf("%s%s",str[cnt1++],s);
}
fclose(fp);
}
}
//分两种情况,卷票使用eng,blocksize小一点,constsize大一点,普通发票使用chi_sim
int main(int argc, char* argv[])
{
remove("result");
string path = argv[1];
//string path ="test/6.jpg";
Receipt receipt(path);
receipt.ProcessImage();
string result=receipt.type();
Output(result);
waitKey(0);
destroyAllWindows();
return 0;
}