版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第OpenCV實(shí)現(xiàn)傾斜文字校正基于OpenCV的傾斜文字校正,供大家參考,具體內(nèi)容如下
使用OpenCV里example中的的傾斜文本作為輸入,本文的目的即將該傾斜的文本校正成水平方向的文本。
主要思路為:
讀取圖像-Canny邊緣檢測(cè)-形態(tài)學(xué)操作-提取最小外接矩形-計(jì)算旋轉(zhuǎn)矩陣-仿射變換校正文本圖像
原始圖像:
提取最小外接矩形區(qū)域
校正后的圖像
主要涉及的API
創(chuàng)建滑動(dòng)條
這個(gè)API可以創(chuàng)建一個(gè)滑動(dòng)條,可以在不改變程序的情況下更改變量的值來顯示圖像變化的效果。在這個(gè)場(chǎng)景中使用創(chuàng)建滑動(dòng)條來調(diào)節(jié)Canny邊緣檢測(cè)的閾值,提取合適的邊緣。
```cpp
Clickingthelabelofeachtrackbarenableseditingthetrackbarvaluesmanually.
@paramtrackbarnameNameofthecreatedtrackbar.\\第一個(gè)參數(shù)為滑動(dòng)條的名稱
@paramwinnameNameofthewindowthatwillbeusedasaparentofthecreatedtrackbar.\\滑動(dòng)條所依附的窗口名稱
@paramvalueOptionalpointertoanintegervariablewhosevaluereflectsthepositionoftheslider.Uponcreation,thesliderpositionisdefinedbythisvariable.
\\引用值,即拖動(dòng)滑動(dòng)條所改變的值,需要提前定義,定義好的值即為滑動(dòng)條的初始值。
@paramcountMaximalpositionoftheslider.Theminimalpositionisalways0.\\滑動(dòng)條的最大位置
@paramonChangePointertothefunctiontobecalledeverytimethesliderchangesposition.ThisfunctionshouldbeprototypedasvoidFoo(int,void\*);,wherethefirstparameteristhetrackbarpositionandthesecondparameteristheuserdata(seethenextparameter).IfthecallbackistheNULLpointer,nocallbacksarecalled,butonlyvalueisupdated.
\\定義回調(diào)函數(shù),每次滑動(dòng)滑動(dòng)條都會(huì)調(diào)用這個(gè)回調(diào)函數(shù)?;卣{(diào)函數(shù)的格式為voidFoo(int,void*)其中第一個(gè)參數(shù)為軌跡條的位置,第二個(gè)參數(shù)是用戶數(shù)據(jù)userdata。
@paramuserdataUserdatathatispassedasistothecallback.Itcanbeusedtohandletrackbareventswithoutusingglobalvariables.
\\用戶傳遞給回調(diào)函數(shù)的數(shù)據(jù)userdata,這里使用的是全局變量,因此這一項(xiàng)可以忽略。
CV_EXPORTSintcreateTrackbar(constStringtrackbarname,constStringwinname,
int*value,intcount,
TrackbarCallbackonChange=0,
void*userdata=0);
###Canny邊緣檢測(cè)
使用Canny邊緣檢測(cè)算法提取文本圖像的邊緣
```cpp
/**@briefFindsedgesinanimageusingtheCannyalgorithm@citeCanny86.
ThefunctionfindsedgesintheinputimageandmarksthemintheoutputmapedgesusingtheCannyalgorithm.Thesmallestvaluebetweenthreshold1andthreshold2isusedforedgelinking.Thelargestvalueisusedtofindinitialsegmentsofstrongedges.See
/wiki/Canny_edge_detector
@paramimage8-bitinputimage.
//輸入圖像
@paramedgesoutputedgemap;singlechannels8-bitimage,whichhasthesamesizeasimage.
//輸出單通道的八位圖像
@paramthreshold1firstthresholdforthehysteresisprocedure.
//閾值(低于此閾值的直接被剔除)
@paramthreshold2secondthresholdforthehysteresisprocedure.
//閾值2(高于此閾值的被認(rèn)為為邊緣)
@paramapertureSizeaperturesizefortheSobeloperator.
//表示Sobel算子的模板大小
@paramL2gradientaflag,indicatingwhetheramoreaccurate\f$L_2\f$norm
\f$=\sqrt{(dI/dx)^2+(dI/dy)^2}\f$shouldbeusedtocalculatetheimagegradientmagnitude(L2gradient=true),orwhetherthedefault\f$L_1\f$norm\f$=|dI/dx|+|dI/dy|\f$isenough(L2gradient=false).
//使用更精確的L2范數(shù)(True)還是默認(rèn)的L1范數(shù)(默認(rèn))
CV_EXPORTS_WvoidCanny(InputArrayimage,OutputArrayedges,
doublethreshold1,doublethreshold2,
intapertureSize=3,boolL2gradient=false);
邊緣檢測(cè)的結(jié)果,由于測(cè)試的樣本圖像比較理想,調(diào)節(jié)滑動(dòng)條的位置影響不是很大。如果圖像質(zhì)量較差,也可以添加圖像預(yù)處理步驟優(yōu)化源圖像。
形態(tài)學(xué)處理
使用形態(tài)學(xué)處理操作連接經(jīng)過Canny邊緣檢測(cè)處理后字符縫隙,使其變成一個(gè)連通域。
步驟:創(chuàng)建結(jié)構(gòu)元素(getStructuringElement)-膨脹處理
Matsrc_dilate;
Matkernel=getStructuringElement(MORPH_RECT,Size(11,11),Point());
//創(chuàng)建結(jié)構(gòu)元素的大小,第一個(gè)參數(shù)為結(jié)構(gòu)元素的形狀(矩形MORPH_RECT,十字形結(jié)構(gòu)MORPH_CROSS,橢圓形結(jié)構(gòu)MORPH_ELLIPSE),第二個(gè)參數(shù)為結(jié)構(gòu)元素的大小,第三個(gè)參數(shù)為錨點(diǎn)的位置。
dilate(Canny_edge,src_dilate,kernel,Point(-1,-1),1,BORDER_DEFAULT);
//膨脹圖像,第一個(gè)參數(shù)為輸入圖像,第二個(gè)為輸出圖像,第三個(gè)參數(shù)為結(jié)構(gòu)元素,第四個(gè)參數(shù)為錨點(diǎn)的位置,第五個(gè)參數(shù)為迭代的次數(shù),最后一個(gè)參數(shù)為邊界填充的類型
膨脹后的圖像顯示為:
查找輪廓
vectorvectorPointContours;
//定義邊緣的點(diǎn)集
vectorVec4ihierarchy;
//定義邊緣的層次關(guān)系
findContours(src_dilate,Contours,hierarchy,RETR_TREE,CHAIN_APPROX_NONE,Point());
/**@briefFindscontoursinabinaryimage.
Thefunctionretrievescontoursfromthebinaryimageusingthealgorithm@citeSuzuki85.Thecontoursareausefultoolforshapeanalysisandobjectdetectionandrecognition.Seesquares.cppintheOpenCVsampledirectory.
@noteSinceopencv3.2sourceimageisnotmodifiedbythisfunction.
@paramimageSource,an8-bitsingle-channelimage.Non-zeropixelsaretreatedas1's.Zeropixelsremain0's,sotheimageistreatedasbinary.Youcanuse#compare,#inRange,#threshold,#adaptiveThreshold,#Canny,andotherstocreateabinaryimageoutofagrayscaleorcolorone.Ifmodeequalsto#RETR_CCOMPor#RETR_FLOODFILL,theinputcanalsobea32-bitintegerimageoflabels(CV_32SC1).
//輸入圖像
@paramcontoursDetectedcontours.Eachcontourisstoredasavectorofpoints(e.g.
std::vectorstd::vectorcv::Point).
//輸出邊緣的點(diǎn)集
@paramhierarchyOptionaloutputvector(e.g.std::vectorcv::Vec4i),containinginformationabouttheimagetopology(拓?fù)浣Y(jié)構(gòu)).Ithasasmanyelementsasthenumberofcontours.Foreachi-thcontourcontours[i],theelementshierarchy[i][0],hierarchy[i][1],hierarchy[i][2],andhierarchy[i][3]aresetto0-basedindicesincontoursofthenextandpreviouscontoursatthesamehierarchicallevel,thefirstchildcontourandtheparentcontour,respectively.Ifforthecontouritherearenonext,previous,parent,ornestedcontours,thecorrespondingelementsofhierarchy[i]willbenegative.
//輪廓之間的層次結(jié)構(gòu)關(guān)系(下一個(gè)輪廓、前一個(gè)輪廓、第一個(gè)子輪廓、父輪廓)
@parammodeContourretrievalmode,see#RetrievalModes
//檢索模式:RETR_EXTERNAL僅檢索外部輪廓
RETR_LIST在不建立層次結(jié)構(gòu)的情況下檢索所有輪廓
RETR_CCOMP檢索具有兩級(jí)層次結(jié)構(gòu)(外部和孔)的所有輪廓。
RETR_TREE檢索所有輪廓,在輪廓之間創(chuàng)建完整的層次結(jié)構(gòu)
@parammethodContourapproximationmethod,see#ContourApproximationModes
//輪廓的形狀近似方法
//CV_CHAIN_APPROX_NONE不對(duì)輪廓應(yīng)用任何近似方法并存儲(chǔ)輪廓點(diǎn)
//CV_CHAIN_APPROX_SIMPLE壓縮所有水平、垂直和對(duì)角線段,僅存儲(chǔ)起點(diǎn)和終點(diǎn)
//等其他近似算法
@paramoffsetOptionaloffsetbywhicheverycontourpointisshifted.ThisisusefulifthecontoursareextractedfromtheimageROIandthentheyshouldbeanalyzedinthewholeimagecontext.
//定義偏移量
CV_EXPORTS_WvoidfindContours(InputArrayimage,OutputArrayOfArrayscontours,
OutputArrayhierarchy,intmode,
intmethod,Pointoffset=Point());
最小外接矩形
旋轉(zhuǎn)矩形對(duì)象類型RotatedRect
輪廓的最小外接矩形minAreaRect
/**@briefFindsarotatedrectangleoftheminimumareaenclosingtheinput2Dpointset.
Thefunctioncalculatesandreturnstheminimum-areaboundingrectangle(possiblyrotated)foraspecifiedpointset.DevelopershouldkeepinmindthatthereturnedRotatedRectcancontainnegativeindiceswhendataisclosetothecontainingMatelementboundary.
//該函數(shù)輸入邊緣的點(diǎn)集,并返回一個(gè)最小外接矩形,當(dāng)數(shù)據(jù)靠近邊界的時(shí)候,返回的RotatedRect可能包含有負(fù)所引。
@parampointsInputvectorof2Dpoints,storedinstd::vector\orMat
CV_EXPORTS_WRotatedRectminAreaRect(InputArraypoints);
計(jì)算旋轉(zhuǎn)矩陣
MatRotation=getRotationMatrix2D(center,RRect_degree,1.0);
Matoutput;
warpAffine(src,output,Rotation,src.size(),INTER_CUBIC,BORDER_CONSTANT,Scalar(255,255,255));
//輸入要變換圖像,輸出圖像,定義旋轉(zhuǎn)矩陣,定義插值方式,邊界類型(邊界類型需要注意)
#includeiostream
#includeopencv2/opencv.hpp
#includeopencv2/highgui.hpp
#includeopencv2/calib3d.hpp
usingnamespacestd;
usingnamespacecv;
//DefineMat
Matsrc,gray_src,dst;
constchar*IWindow="InputWindow";
constchar*OWindow="OutputWindow";
intcanny_threshold=100;
intthreshold_level=255;
voidCanny_function(int,void*);
intmain(intargc,char**argv){
src=imread("D:/Delete/imageTextR.png");
if(src.empty()){
cout"Couldnotloadimage"endl;
return-1;
}
//CoverttoGrayScale
cvtColor(src,gray_src,COLOR_BGR2GRAY);
namedWindow(IWindow,WINDOW_AUTOSIZE);
imshow(IWindow,src);
namedWindow(OWindow,WINDOW_AUTOSIZE);
createTrackbar("Canny_Threshold",IWindow,canny_threshold,threshold_level,Canny_function);
waitKey();
return0;
voidCanny_function(int,void*){
MatCanny_edge;
Canny(gray_src,Canny_edge,canny_threshold,canny_threshold*2,3,false);
//DisplayCanny_edge
//imshow(OWindow,Canny_edge);
Matsrc_dilate;
Matkernel=getStructuringElement(MORPH_RECT,Size(11,11),Point());
dilate(Canny_edge,src_dilate,kernel,Point(-1,-1),1,BORDER_DEFAULT);
//DisplayDilateimage
imshow(OWindow,src_dilate);
//FindContour
vectorvectorPointContours;
vectorVec4ihierarchy;
findContours(src_dilate,Contours,hierarchy,RETR_TREE,CHAIN_APPROX_NONE,Point());
//SelecttheMaxareaContour
doubleMaxAreaRRect=0;
intSizeContour=0;
for(size_tt=0;tContours.size();t++){
RotatedRectRRect=minAreaRect(Contours[t]);
doubleAreaRRect=0;
AreaRRect=RRect.size.area();
MaxAreaRRect=max(MaxAreaRRect,AreaRRect);
}
doubleRRect_degree=
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年農(nóng)業(yè)文化遺產(chǎn)活化利用指南
- 煙草制品經(jīng)營(yíng)風(fēng)險(xiǎn)防控管理手冊(cè)
- 2026青龍湖(河北)產(chǎn)業(yè)發(fā)展集團(tuán)有限公司招聘15人備考題庫(kù)及一套參考答案詳解
- 2026年原型設(shè)計(jì)工具高階應(yīng)用培訓(xùn)
- 計(jì)算機(jī)行業(yè)年度策略:AI應(yīng)用加快全球格局重塑中
- 職業(yè)健康風(fēng)險(xiǎn)評(píng)估與員工職業(yè)發(fā)展動(dòng)態(tài)調(diào)整機(jī)制
- 職業(yè)健康促進(jìn)與職業(yè)健康效益優(yōu)化
- 職業(yè)健康與心理健康的整合干預(yù)策略-2
- 陽(yáng)江2025年廣東陽(yáng)江陽(yáng)西縣新墟鎮(zhèn)招聘合同制禁毒工作人員筆試歷年參考題庫(kù)附帶答案詳解
- 邢臺(tái)2025年河北邢臺(tái)市襄都區(qū)招聘中小學(xué)幼兒園教師75人筆試歷年參考題庫(kù)附帶答案詳解
- 云南省玉溪市2025-2026學(xué)年八年級(jí)上學(xué)期1月期末物理試題(原卷版+解析版)
- 2026年哈爾濱通河縣第一批公益性崗位招聘62人考試參考試題及答案解析
- 六年級(jí)寒假家長(zhǎng)會(huì)課件
- 就業(yè)協(xié)議書解約函模板
- 物流鐵路專用線工程節(jié)能評(píng)估報(bào)告
- 2026天津市南開區(qū)衛(wèi)生健康系統(tǒng)招聘事業(yè)單位60人(含高層次人才)備考核心試題附答案解析
- 重瞼手術(shù)知情同意書
- 研發(fā)部門員工加班管理細(xì)則
- 46566-2025溫室氣體管理體系管理手冊(cè)及全套程序文件
- 九師聯(lián)盟2026屆高三上學(xué)期12月聯(lián)考英語(yǔ)(第4次質(zhì)量檢測(cè))(含答案)
- 第21章 反比例函數(shù)(單元測(cè)試·綜合卷)(含答案)-滬科版(2024)九上
評(píng)論
0/150
提交評(píng)論