TW_OCR32调用

130 篇文章 1 订阅

TW_OCR32.dll下载


//回调函数

long FAR PASCAL ProgressStart()

{
return 1;
}


long FAR PASCAL ProgressEnd()
{
return 1;
}


long FAR PASCAL ProgressSetStage(int wStageNumber, byte& Name)
{
//StageName = Array("读取图象", 版面分析, "倾斜校正", "识别", "后处理")
//' Select Case wStageNumber
//'    Case 0
//ProgressForm.Label1 = "正在" & StageName(wStageNumber)
return 1;
}


long FAR PASCAL ProgressSetPercent(int percent)
{
//ProgressForm.Label2 = percent & "%"
//ProgressForm.ProgressBar1.Value = percent
return 1;

}


别人调用和声明的例子:

/*
procedure TTFOcr.GetOcrInformation(nLine, nPos: Integer; var FRect: TRect);
var
 FGetInofrmation:TTW_GetNodeInformation;
 FFont:Word;
 trust:array[0..10] of Byte;
 simword:String[11];
 begin
 if FLibHandle= 0 then
 FLibHandle :=  LoadLibrary(PChar(FLibName));
 @FGetInofrmation:= GetProcAddress(FLibHandle, 'TW_GetNodeInformation');
 FGetInofrmation(nLine,nPos,FFont,FRect,@simword[0],@trust[0]);
 end;
 
procedure TTFOcr.InitalizeOCRInstance;
begin
inherited;
FLibName:= ExtractFilePath(Application.ExeName) + DEF_LIBNAME;
end;

 procedure TTFOcr.OCR(ALanguageID: Integer; KeepLayer: Boolean);
 var
 AStrings:TStrings;
 sTxtF,sImgF:string;
 i:integer;
 FStart:TTW_Start;
 FEnd:TTW_End;
 FRecgF:TTW_RecogFile;
 FCallBack: TTW_CallBackAddress;
 FCloseImg:TTW_CloseImage;
 FLoadTrace:TTW_LoadTrace;
 FUnLoadTrace:TTW_UnloadTrace;
 err:integer;
 
begin
inherited;
AStrings:= TStringList.Create;
if FLibHandle= 0 then
FLibHandle :=  LoadLibrary(PChar(FLibName));

 @FStart:= GetProcAddress(FLibHandle, 'TW_Start');
 @FEnd:= GetProcAddress(FLibHandle, 'TW_End');
 @FRecgF:= GetProcAddress(FLibHandle, 'TW_RecogFile');
 @FCallBack:= GetProcAddress(FLibHandle, 'TW_CallBackAddress');
 @FCloseImg:= GetProcAddress(FLibHandle, 'TW_CloseImage');
 @FUnLoadTrace:= GetProcAddress(FLibHandle, 'TW_UnloadTrace');
 @FLoadTrace:= GetProcAddress(FLibHandle, 'TW_LoadTrace');
 try
 FUnLoadTrace;
 err:= FStart(0,ALanguageID,0, 0);
 if err<>0 then
 Raise Exception.Create('ê?±e′í?ó£?′í?óo?£o'+IntToStr(err));
 err:= FCallBack(Longint(@ProgressStart),Longint(@ProgressFinish), Longint(@ProgressSetStage),Longint(@ProgressSetPerc));
 if err<>0 then
 Raise Exception.Create('ê?±e′í?ó£?′í?óo?£o'+IntToStr(err));
 STxtF:= ChangeFileExt(FFileName,'.TXT');
 err:= FRecgF(PChar(FFileName), PChar(STxtF), 0);
 if err<>0 then begin
 Raise Exception.Create('ê?±e′í?ó£?′í?óo?£o'+IntToStr(err));
 end;
 FLoadTrace(PChar(FFileName));
 FCloseImg;
 FEnd;
 except
 on E: Exception do
 begin
 FLogs.Add(E.Message);
 DeleteTem(FFileName,False);
 end;
 end;
 
sTxtF:= ChangeFileExt(FFileName,'.txt');
if FileExists(sTxtF)  then
begin
//if Append then

 AStrings.LoadFromFile(sTxtF);
 if not KeepLayer then
 for i:= 0 to  AStrings.Count -1 do
 FText:= FText + #13 + AStrings.Strings[i]
 else
 FText:= AStrings.Text;
 end;
 DeleteTem(FFileName,False);
 AStrings.Free;
 end;
 

 
procedure TTFOcr.SetLibName(const Value: string);
begin
FLibName := Value;
if FLibHandle= 0 then
FLibHandle :=  LoadLibrary(PChar(FLibName));
end;

 procedure TTFOcr.SetLogs(const Value: TStrings);
 begin
 FLogs := Value;
 end;
 
end.
*/
/*Public Declare Function TW_CallBackAddress Lib "tw_ocr32.dll" (ByVal start As Long, ByVal finish As Long, ByVal stage As Long, ByVal percent As Long) As Long
---------
Function ProgressStart() As Long
ProgressForm.Show
ProgressForm.Label1 = "正在调识别库"
DoEvents
ProgressStart = 1
End Function


Function ProgressEnd() As Long
Unload ProgressForm
ProgressEnd = 1
End Function


Function ProgressSetStage(ByVal wStageNumber As Integer, ByRef Name As Byte) As Long
Dim StageName As Variant
StageName = Array("读取图象", 版面分析, "倾斜校正", "识别", "后处理")
' Select Case wStageNumber
'    Case 0
ProgressForm.Label1 = "正在" & StageName(wStageNumber)
ProgressSetStage = 1
End Function


Function ProgressSetPerc(ByVal percent As Integer) As Long
ProgressForm.Label2 = percent & "%"
ProgressForm.ProgressBar1.Value = percent
ProgressSetPerc = 1
End Function
---------------------
TW_CallBackAddress(AddressOf Progress.ProgressStart, AddressOf Progress.ProgressEnd, AddressOf Progress.ProgressSetStage, AddressOf Progress.ProgressSetPerc)*/

//TW_CloseImage, TW_End, TW_ExportFile, TW_GetNodeInformation, TW_Layout, TW_LoadImage, TW_LoadTrace, TW_RecogFile, 
//TW_RecogRegions, TW_Start, TW_UnloadTrace


//int TW_Start(int nCode, int nFont, BOOL bOutFull, HANDLE hApplicationInstance);
//err:= FStart(0,ALanguageID,0, 0);
//int TW_CallBackAddress(LPFNTW_ProgressStart, LPFNTW_ProgressEnd, LPFNTW_ProgressSetStage, LPFNTW_ProgressSetPercent);
//err:= FCallBack(Longint(@ProgressStart),Longint(@ProgressFinish), Longint(@ProgressSetStage),Longint(@ProgressSetPerc)); 
//int TW_RecogFile(LPSTR lpstrImageFileName,LPSTR lpstrTextFileName,int nOption);
//err:= FRecgF(PChar(FFileName), PChar(STxtF), 0);
//int TW_LoadTrace(LPCSTR lpszFileName);
//FLoadTrace(PChar(FFileName));
//void TW_CloseImage();
//FCloseImg;
//int TW_ExportFile(LPCSTR *lpTifList, DWORD *dwErrCode, int nTifCount, LPCSTR lpPdfPathName, DWORD option);
//void TW_End();
/*int ProcessTiff2PDFFromTHOCR(
LPCSTR *lpTifList, // Tif文件列表
DWORD *dwErrCode, // 相应的错误码
int nTifCount, // Tif文件个数
LPCSTR lpPdfPathName, // 生成的PDF文件名
DWORD option
);*/
/*TW_Start(0,5,0,0);   
TW_CallBackAddress(ProgressStart,ProgressEnd,   0,   ProgressStart);     //   
TW_RecogFile(PChar('E:\TemOCR.bmp'),   PChar('E:\TemOCR.TXT'),   0);   
    TW_End;*/  


整理后的API声明和调用:

typedef int (FAR PASCAL *LPFNTW_Start)(int ,int ,BOOL ,HANDLE);
typedef void (FAR PASCAL *LPFNTW_End)();
typedef long (FAR PASCAL *LPFNTW_ProgressStart)();
typedef long (FAR PASCAL *LPFNTW_ProgressEnd)();
//typedef long (FAR PASCAL *LPFNTW_ProgressSetStage)(int wStageNumber, byte& Name);
typedef long (FAR PASCAL *LPFNTW_ProgressSetStage)(int, byte&);
//typedef long (FAR PASCAL *LPFNTW_ProgressSetPerc)(int percent);
typedef long (FAR PASCAL *LPFNTW_ProgressSetPercent)(int);
typedef int (FAR PASCAL *LPFNTW_CallBackAddress)(
LPFNTW_ProgressStart, LPFNTW_ProgressEnd, LPFNTW_ProgressSetStage, LPFNTW_ProgressSetPercent);
typedef int (FAR PASCAL *LPFNTW_RecogFile)(LPSTR, LPSTR, int);


typedef int (FAR PASCAL *LPFNTW_LoadTrace)(LPCSTR);
    typedef int (FAR PASCAL *LPFNTW_GetNodeInformation)(int, int, WORD&, RECT&, LPWORD ,LPBYTE);
typedef void (FAR PASCAL *LPFNTW_UnloadTrace)();


typedef void (FAR PASCAL *LPFNTW_TW_CloseImage)();

LPFNTW_Start lpfnTW_Start = NULL;
LPFNTW_CallBackAddress lpfnTW_CallBackAddress = NULL;
LPFNTW_RecogFile lpfnTW_RecogFile = NULL;
LPFNTW_End lpfnTW_End = NULL;

LPFNTW_LoadTrace lpfnTW_LoadTrace = NULL;
LPFNTW_GetNodeInformation lpfnTW_GetNodeInformation = NULL;
LPFNTW_UnloadTrace lpfnTW_UnloadTrace = NULL;


LPFNTW_TW_CloseImage lpfnTW_CloseImage = NULL;

HMODULE hModule = LoadLibrary(_T("Tw_ocr32.dll"));
if(hModule) {


lpfnTW_Start = (LPFNTW_Start)GetProcAddress(hModule,"TW_Start");
lpfnTW_CallBackAddress = (LPFNTW_CallBackAddress)GetProcAddress(hModule,"TW_CallBackAddress");
lpfnTW_RecogFile = (LPFNTW_RecogFile)GetProcAddress(hModule,"TW_RecogFile");
lpfnTW_End = (LPFNTW_End)GetProcAddress(hModule,"TW_End");
lpfnTW_LoadTrace = (LPFNTW_LoadTrace)GetProcAddress(hModule,"TW_LoadTrace");
lpfnTW_GetNodeInformation = (LPFNTW_GetNodeInformation)GetProcAddress(hModule,"TW_GetNodeInformation");
lpfnTW_UnloadTrace = (LPFNTW_UnloadTrace)GetProcAddress(hModule,"TW_UnloadTrace");
lpfnTW_CloseImage = (LPFNTW_TW_CloseImage)GetProcAddress(hModule,"TW_CloseImage");

if(lpfnTW_Start && lpfnTW_RecogFile && lpfnTW_End) {
lpfnTW_UnloadTrace();
lpfnTW_Start(0, 5, 0, 0);
lpfnTW_CallBackAddress(ProgressStart, ProgressEnd, ProgressSetStage, ProgressSetPercent);
lpfnTW_RecogFile("D:\\tw_ocr32\\orderNo.tif", "D:\\tw_ocr32\\resultFile.txt", 0);
lpfnTW_End();
lpfnTW_LoadTrace("D:\\tw_ocr32\\orderNo.tif");
lpfnTW_CloseImage();
}


FreeLibrary(hModule);
hModule = NULL;
}

项目细节: 首先载入源图像,并进行尺寸预处理。 载入源图像image并作拷贝为org,将image按原始h,w的比例大小设置为高度为500的图像。 进行边缘检测和轮廓检测 在灰度化->边缘检测->轮廓检测后,将轮廓按轮廓的面积进行排序(注意这里默认是顺序的即从小到大,我们需要从大到小排序,所以reverse = True),取面积最大的前5个轮廓,并用多边形逼近(cv.approxPolyDP)的方法将轮廓近似出来,因为检测的轮廓有圆形有长矩形,我们需要的检测的目标轮廓是四边形(类似于矩形)。所以我们经过筛选得到我们需要的四边形的坐标。 坐标的透视变换 由多边形逼近轮廓的方法得到的坐标 是每个轮廓逆时钟方向的各个顶点的坐标,而我们想要顺时针方向的各个顶点的坐标,所以需要先对轮廓坐标重新排序。接着需要求出四边形轮廓的高和宽,来创建一个dst数组:该数组为[[0,0],[width-1,0],[width-1,height-1],[0,height-1] 。将四边形轮廓坐标和dst输入到cv.getPerspectiveTransform 函数里,得到透视变换的M矩阵。接着将用M矩阵对原图像做透视变化,其中得出的warped的大小为(width,height),这样透视变换就做完了。 简单点说:首先读取两个坐标数组,计算变换矩阵;然后根据变换矩阵对原图进行透视变换,并输出到目标画布, OCR识别 在OCR识别之前要对待识别的图像进行预处理,即灰度二值化,接着利用ocr指令来识别。 源码: import cv2 as cv import numpy as np import pytesseract def order_point(pts): rect = np.zeros((4, 2), dtype = "float32") s = pts.sum(axis = 1) rect[0] = pts[np.argmin(s)] rect[2] = pts[np.argmax(s)] diff = np.diff(pts,axis=1) rect[1] = pts[np.argmin(diff)] rect[3] = pts[np.argmax(diff)] return rect def four_point_transfer(image,pts): rect = order_point(pts) (tl,tr,br,bl) = rect width1 = np.sqrt((tr[0]-tl[0])*(tr[0]-tl[0])+(tr[1]-tl[1])*(tr[1]-tl[1])) width2 = np.sqrt((br[0]-bl[0])*(br[0]-bl[0])+(br[1]-bl[1])*(br[1]-bl[1])) width = max(width1,width2) #python中有max函数和np.max函数,前者是比较两个数值的大小取最大值,后者是取出数组的最大值 height1 = np.sqrt((tr[0]-br[0])*(tr[0]-br[0])+(tr[1]-br[1])*(tr[1]-br[1])) height2 = np.sqrt((tl[0]-bl[0])*(tl[0]-bl[0])+(tl[1]-bl[1])*(tl[1]-bl[1])) height = max(height1,height2) dst = np.array([[0,0],[width-1,0],[width-1,height-1],[0,height-1]],dtype="float32") M = cv.getPerspectiveTransform(rect,dst) warped =cv.warpPerspective(image,M,(width,height)) return warped def resize(image,height=None): if height is None: return image else : h,w= image.shape[:2] #shape:h,w,channel image[h(row),w(col),channel] r = height/h width = int(w*r) #关于size函数参数的一般是(宽,高) image = cv.resize(image,(width,height),interpolation=cv.INTER_AREA) #还有resize(img,(宽,高)),即先列后行 return image #利用cv.bounding()得到x,y,width,height #其它情况一般都是先行后列(高,宽) #如shape得到参数,或者roi区域内部参数,建立新的Mat 都是先行后列 image = cv.imread("E:\opencv\picture\page.jpg") orig = image.copy() image = resize(image,height=500) ratio = orig.shape[0]/500 #边缘检测 image_gray = cv.cvtColor(image,cv.COLOR_BGR2GRAY) image_gray = cv.GaussianBlur(image_gray,(5,5),0) image_edge = cv.Canny(image_gray,75,200) #轮廓检测 image_contours = cv.findContours(image_edge.copy(),cv.RETR_LIST,cv.CHAIN_APPROX_SIMPLE)[1] countours = sorted(image_contours,key=cv.contourArea,reverse=True)[:5] for c in countours: arc = cv.arcLength(c,closed=True) approx = cv.approxPolyDP(c,arc*0.02,True) if len(approx) == 4: screen_shot = approx break cv.drawContours(image,[screen_shot],-1,(0,0,255),2) warped =four_point_transfer(orig,screen_shot.reshape(4,2)*ratio) cv.imshow('warped_window',resize(warped,height=650)) warped =cv.cvtColor(warped,cv.COLOR_BGR2GRAY) scan = cv.threshold(warped,0,255,cv.THRESH_BINARY|cv.THRESH_OTSU)[1] cv.imwrite("E:/opencv/picture/scan.png",scan) cv.imshow("scan ",scan) scanstring = pytesseract.image_to_string(scan) print(scanstring) cv.waitKey(0) cv.destroyAllWindows()
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值