-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmain.cpp
More file actions
112 lines (94 loc) · 3.48 KB
/
main.cpp
File metadata and controls
112 lines (94 loc) · 3.48 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
//
// Copyright (C) 2017 Yen-Chin, Lee
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/photo.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <tesseract/baseapi.h>
#include <regex>
#ifdef ENABLE_HIGUI
void showManyImages(char *title, cv::Mat img1, cv::Mat img2, cv::Mat img3, cv::Mat img4)
{
cv::Mat window;
cv::hconcat(img1, img2, window);
cv::hconcat(window, img3, window);
cv::hconcat(window, img4, window);
cv::imshow(title, window);
}
#endif // ENABLE_HIGUI
cv::Mat clean_captcha(const char *file)
{
// Load captcha captcha in grayscale
cv::Mat captcha = cv::imread(file, CV_LOAD_IMAGE_GRAYSCALE);
if (captcha.empty()) {
std::cerr << "Error can't open image: " << file << std::endl;
exit(-1);
}
// Convert the captcha to black and white.
cv::Mat captcha_bw;
cv::threshold(captcha, captcha_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
// Erode the image to remove dot noise and that wierd line. I use a 3x3
// rectengal as the kernal.
cv::Mat captcha_erode;
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
cv::erode(captcha_bw, captcha_erode, element);
// Some cosmetic
cv::Mat captcha_denoise;
cv::fastNlMeansDenoising(captcha_erode, captcha_denoise, 50);
#ifdef ENABLE_HIGUI
// display every step result
showManyImages("captcha", captcha, captcha_bw, captcha_erode, captcha_denoise);
#endif // ENABLE_HIGUI
return captcha_denoise;
}
std::string image_to_string(cv::Mat img)
{
tesseract::TessBaseAPI tess;
tess.Init(NULL, "eng", tesseract::OEM_DEFAULT);
tess.SetImage((uchar*) img.data, img.cols, img.rows, 1, img.cols);
// return match characters
std::string s = tess.GetUTF8Text();
std::smatch sm;
std::regex regx("[0-9A-Z]+");
std::regex_search(s, sm, regx);
return sm.str();
}
void help(char *argv[])
{
std::cout << "Usage:" << std::endl
<< " " << argv[0] << " <captcha image> " << std::endl;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
help(argv);
return 0;
}
cv::Mat img = clean_captcha(argv[1]);
#ifdef ENABLE_HIGUI
// Wait for user input to stop image display
std::cout << "Captcha: "<< image_to_string(img) << std::endl;
cv::waitKey();
#else
std::cout << image_to_string(img) << std::endl;
#endif // ENABLE_HIGUI
return 0;
}