1,066
社区成员
发帖
与我相关
我的任务
分享
VB实现OCR文字识别
原理: 利用微软OCR控件, 只需要不到10行代码就能够实现自已的OCR文字识别软件.
1. 添加控件,需要安装office2003, 没有安装office2003的可以从别人机子上拷贝相关文件,注册regsvr32.exe mdivwctl.dll,
控件一般在这个目录下:C:\Program Files\Common Files\Microsoft Shared\MODI\11.0, 只需要相关的几个文件就可以了, 此文件夹全部文件大概在21M左右.
工程->部件->添加这个控件:Microsoft Office Document Imaging 11.0 Type Library
2.在按钮的Click事件里:
Dim strLayoutInfo As String, strLPN As String
'初始化并加载文档
Set miDoc = CreateObject("MODI.Document") '创建对象
miDoc.Create "D:\未命名.tif" '加载图片文件(必须是黑白二值图)
Screen.MousePointer = vbHourglass '设置光标忙
'识别
miDoc.Images(0).OCR miLANG_CHINESE_SIMPLIFIED, True, True '有用的就此一句,识别为中文简体
Set modiLayout = miDoc.Images(0).Layout '读出数据
strLayoutInfo = _
"Language: " & modiLayout.Language & vbCrLf & _
"Number of characters: " & modiLayout.NumChars & vbCrLf & _
"Number of fonts: " & modiLayout.NumFonts & vbCrLf & _
"Number of words: " & modiLayout.NumWords & vbCrLf & _
"Beginning of text: " & Left(modiLayout.Text, 50) & vbCrLf & _
"First word of text: " & modiLayout.Words(0).Text
MsgBox strLayoutInfo, vbInformation + vbOKOnly, "Layout Information"
Set modiLayout = Nothing
Set miDoc = Nothing
Screen.MousePointer = vbDefault
3. OK了, 是不是很简单
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
Mat img,smallImg,gray,bw;
vector<Vec4i> hierarchy;
vector<vector<Point> > contours;
int threshval=128;
Rect r;
Rect maxrect,brect;
int idx,n;
const static Scalar colors[15]={
CV_RGB( 0, 0,128),
CV_RGB( 0,128, 0),
CV_RGB( 0,128,128),
CV_RGB(128, 0, 0),
CV_RGB(128, 0,128),
CV_RGB(128,128, 0),
CV_RGB(128,128,128),
CV_RGB(160,160,160),
CV_RGB( 0, 0,255),
CV_RGB( 0,255, 0),
CV_RGB( 0,255,255),
CV_RGB(255, 0, 0),
CV_RGB(255, 0,255),
CV_RGB(255,255, 0),
CV_RGB(255,255,255),
};
Scalar color;
void gamma_correct(Mat& img, Mat& dst, double gamma) {
Mat temp;
CvMat tmp;
img.convertTo(temp, CV_32FC1, 1.0/255.0, 0.0);
tmp=temp;
cvPow(&tmp, &tmp, gamma);
temp.convertTo(dst , CV_8UC1 , 255.0 , 0.0);
}
int main() {
cvNamedWindow("display",1);
img=imread("image.jpg",1);
r.x=img.cols/10;
r.y=img.rows/3;
r.width=img.cols*8/10;
r.height=img.rows*2/3;
smallImg=img(r);
cvtColor(smallImg,gray,CV_BGR2GRAY);
// medianBlur(gray,gray,5);
equalizeHist(gray,gray);
gamma_correct(gray,gray,4.0);
imshow("display",gray);
waitKey(0);
bw=(gray>threshval);
imshow("display",bw);
waitKey(0);
Mat Structure0=getStructuringElement(MORPH_RECT,Size(3,3));
erode(bw,bw,Structure0,Point(-1,-1));
Mat Structure1=getStructuringElement(MORPH_RECT,Size(6,6));
dilate(bw,bw,Structure1, Point(-1,-1));
imshow("display",bw);
waitKey(0);
findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
if (!contours.empty()&&!hierarchy.empty()) {
idx=0;
n=0;
vector<Point> approx;
for (;idx>=0;idx=hierarchy[idx][0]) {
color=colors[idx%15];
// drawContours(smallImg,contours,idx,color,1,8,hierarchy);
approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
const Point* p = &approx[0];
int m=(int)approx.size();
polylines(smallImg, &p, &m, 1, true, color);
circle(smallImg,Point(p[0].x,p[0].y),3,color);
circle(smallImg,Point(p[1].x,p[1].y),2,color);
for (int i=2;i<m;i++) circle(smallImg,Point(p[i].x,p[i].y),1,color);
n++;
if (1==n) {
maxrect=boundingRect(Mat(contours[idx]));
} else {
brect=boundingRect(Mat(contours[idx]));
CvRect mr(maxrect),br(brect);
maxrect=cvMaxRect(&mr,&br);
}
}
circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
}
imshow("display",smallImg);
waitKey(0);
cvDestroyWindow("display");
return 0;
}