验证码识别技术
??ÓÃ???????ÌÐò??
view plaincopy to clipboardprint?
01.#include
02.#include
03.#include
04.#include
05.using namespace std;
08.int main( int argc, char** argv )
09.{
10. cvNamedWindow("1");
11. cvNamedWindow("2");
12. IplConvKernel* se = cvCreateStructuringElementEx(2, 2, 1, 1, CV_SHAPE_CROSS);
13.
14. CvANN_MLP mlp;
15. mlp.load( "mpl.xml" );
16.
17. for(int i=1; i<=100; i++)
18. {
19. // ?úÉúÎÄ?þÃû
20. char fn[1024];
21. sprintf(fn, ".//sample//%d.jpg", i); 22.
23. // ?ÁÈ?Í?Æ?
24. IplImage* gray = cvLoadImage(fn, CV_LOAD_IMAGE_GRAYSCALE);
25.
26. // ?þÖµ??
27. cvThreshold(gray, gray, 180, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
28. // È??ß?ò
29. cvRectangle(gray, cvPoint(0, 0), cvPoint(gray->width-1, gray->height-1),
CV_RGB(255, 255, 255));
30. // È?Ôë
31. cvDilate(gray, gray, se);
32.
33. /*
34. // µ?Õû?Ç?È
35. cvShowImage("1", gray);
36. IplImage* rote = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 1 );
37. double t = tan(10.0 / 180.0 * CV_PI); 38. int w = gray->width;
39. int h = gray->height;
40. for(int i = 0; iimageData +
gray->widthStep * i;
43. unsigned char* lineRote = (unsigned char*)rote->imageData + rote->widthStep * i;
44. for(int j = 0; j=10 )// ÎÄ×ÖÐèÒªÓÐ10ÏñËØ?ß?È 69. {
70. // ?æÖÆ?ÃÁ?Í?ÇøÓòµ?character 71. cvZero(gray);
72. IplImage* character = cvCreateImage(cvSize(rect.width, rect.height), IPL_DEPTH_8U, 1);
73. cvZero(character);
74. cvDrawContours(character, p, CV_RGB(255, 255, 255), CV_RGB(0, 0,
0), -1, -1, 8, cvPoint(-rect.x, -rect.y));
75.
76. // ?éÒ???
77. IplImage* normal = cvCreateImage(cvSize(16, 16), IPL_DEPTH_8U, 1);
78. cvResize(character, normal, CV_INTER_AREA); 79. cvThreshold(normal, normal, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);// ÐÞÕý
80.
81. // ?ÆËãÊäÈëÏòÁ?
82. float input[256];
83. for(int i=0; i<256; i++) 84. input[i] = (normal->imageData[i]==-1); 85.
86.#if 0
87. // ÓÃ??ÊäÈë?á?û
88. cvShowImage("2", normal); 89. char c = cvWaitKey(0);
90. if(c==27)
91. return 0;
92.
93. // ?àÂë0-9:0-9 a-z:10-35 94. unsigned char cc = 255; 95. if(c>='A'&&c<='Z')
96. cc=c-'A'+10;
97. else if(c>='a'&&c<='z') 98. cc=c-'a'+10;
99. else if(c>='0'&&c<='9') 100. cc=c-'0';
101.
102. if(cc!=255)
103. {
104. // ת???ÉÊä?öÏòÁ? 105. float output[36];
106. for(int i=0; i<36; i++) 107. output[i] = 0.0f; 108. output[cc] = 1.0f; 109.
110. // ?æ??µ?Åú??ÀíÎÄ?þ 111. static FILE* batch = fopen("batch", "wb");
112. fwrite(output, 4*36, 1, batch); 113. fwrite(input, 4*256, 1, batch); 114.
115. static int count = 0; 116. cout<h_next;
136. }
137. cout<
#include
#include
#include
using namespace std;
int main( int argc, char** argv )
{
cvNamedWindow("1");
cvNamedWindow("2");
IplConvKernel* se = cvCreateStructuringElementEx(2, 2, 1, 1, CV_SHAPE_CROSS);
CvANN_MLP mlp;
mlp.load( "mpl.xml" );
for(int i=1; i<=100; i++)
{
// ?úÉúÎÄ?þÃû
char fn[1024];
sprintf(fn, ".//sample//%d.jpg", i);
// ?ÁÈ?Í?Æ?
IplImage* gray = cvLoadImage(fn, CV_LOAD_IMAGE_GRAYSCALE);
// ?þÖµ??
cvThreshold(gray, gray, 180, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
// È??ß?ò
cvRectangle(gray, cvPoint(0, 0), cvPoint(gray->width-1, gray->height-1),
CV_RGB(255, 255, 255));
// È?Ôë
cvDilate(gray, gray, se);
/*
// µ?Õû?Ç?È
cvShowImage("1", gray);
IplImage* rote = cvCreateImage( cvGetSize(gray), IPL_DEPTH_8U, 1 );
double t = tan(10.0 / 180.0 * CV_PI);
int w = gray->width;
int h = gray->height;
for(int i = 0; iimageData + gray->widthStep
* i;
unsigned char* lineRote = (unsigned char*)rote->imageData + rote->widthStep
* i;
for(int j = 0; j=10 )// ÎÄ×ÖÐèÒªÓÐ10ÏñËØ?ß?È
{
// ?æÖÆ?ÃÁ?Í?ÇøÓòµ?character
cvZero(gray);
IplImage* character = cvCreateImage(cvSize(rect.width, rect.height),
IPL_DEPTH_8U, 1);
cvZero(character);
cvDrawContours(character, p, CV_RGB(255, 255, 255), CV_RGB(0, 0, 0),
-1, -1, 8, cvPoint(-rect.x, -rect.y));
// ?éÒ???
IplImage* normal = cvCreateImage(cvSize(16, 16), IPL_DEPTH_8U, 1);
cvResize(character, normal, CV_INTER_AREA);
cvThreshold(normal, normal, 0, 255, CV_THRESH_BINARY |
CV_THRESH_OTSU);// ÐÞÕý
// ?ÆËãÊäÈëÏòÁ?
float input[256];
for(int i=0; i<256; i++)
input[i] = (normal->imageData[i]==-1);
#if 0
// ÓÃ??ÊäÈë?á?û
cvShowImage("2", normal);
char c = cvWaitKey(0);
if(c==27)
return 0;
// ?àÂë0-9:0-9 a-z:10-35
unsigned char cc = 255;
if(c>='A'&&c<='Z')
cc=c-'A'+10;
else if(c>='a'&&c<='z')
cc=c-'a'+10;
else if(c>='0'&&c<='9')
cc=c-'0';
if(cc!=255)
{
// ת???ÉÊä?öÏòÁ?
float output[36];
for(int i=0; i<36; i++)
output[i] = 0.0f;
output[cc] = 1.0f;
// ?æ??µ?Åú??ÀíÎÄ?þ
static FILE* batch = fopen("batch", "wb");
fwrite(output, 4*36, 1, batch);
fwrite(input, 4*256, 1, batch);
static int count = 0;
cout<h_next;
}
cout<
02.#include
03.using namespace std;
04.
05.void print_mat(CvMat& mat)
06.{
07. int count = 0;
08. for(int i=0; i<4/*mat.rows*/; i++) 09. {
10. for(int j=0; jdata.fl, size-1, 1, f); 28. CvMat outputs, inputs;
29. cvGetCols(batch, &outputs, 0, 36); 30. cvGetCols(batch, &inputs, 36, 36+256); 31.
32. // ÐÂ??MPL
33. CvANN_MLP mlp;
34. int layer_sz[] = { 256, 20, 36 }; 35. CvMat layer_sizes = cvMat( 1, 3, CV_32S, layer_sz );
36. mlp.create( &layer_sizes );
37.
38. // ѵÁ?
39. system( "time" );
40. mlp.train( &inputs, &outputs, NULL, NULL, 41. CvANN_MLP_TrainParams(cvTermCriteria(CV_TERMCRIT_ITER,300,0.01),
CvANN_MLP_TrainParams::RPROP, 0.01) 42. );
43. system( "time" );
44.
45. // ?æ??MPL
46. mlp.save( "mpl.xml" );
47.
48. // ?âÊÔ
49. int right = 0;
50. CvMat* output = cvCreateMat( 1, 36, CV_32F ); 51. for(int i=0; i
#include
using namespace std;
void print_mat(CvMat& mat)
{
int count = 0;
for(int i=0; i<4/*mat.rows*/; i++)
{
for(int j=0; jdata.fl, size-1, 1, f);
CvMat outputs, inputs;
cvGetCols(batch, &outputs, 0, 36);
cvGetCols(batch, &inputs, 36, 36+256);
// ÐÂ??MPL
CvANN_MLP mlp;
int layer_sz[] = { 256, 20, 36 };
CvMat layer_sizes = cvMat( 1, 3, CV_32S, layer_sz );
mlp.create( &layer_sizes );
// ѵÁ?
system( "time" );
mlp.train( &inputs, &outputs, NULL, NULL,
CvANN_MLP_TrainParams(cvTermCriteria(CV_TERMCRIT_ITER,300,0.01),
CvANN_MLP_TrainParams::RPROP, 0.01)
);
system( "time" );
// ?æ??MPL
mlp.save( "mpl.xml" );
// ?âÊÔ
int right = 0;
CvMat* output = cvCreateMat( 1, 36, CV_32F );
for(int i=0; i