aardio+halcon的OCR的示例程序
By
笑口常开
at 2019-03-18 • 0人收藏 • 3346人看过
指定字体的名称用于读取序列号。简单使用Industrial_0-9A-Z_NoRej pre-trained字体。
Halcon源程序位于:
C:\Program Files\MVTec\HALCON-13.0\examples\hdevelop\Applications\OCR\engraved.hdev
import win.ui; /*DSG{{*/ var winform = win.form(text="aardio+halcon ,OCR识别示例, Aardio.com.cn";right=1007;bottom=611) winform.add( button={cls="button";text="Button";left=15;top=41;right=223;bottom=108;z=1}; picturebox={cls="picturebox";left=258;top=0;right=1008;bottom=612;z=2} ) /*}}*/ import console console.open() import com var HOperatorSetX = com.CreateObject("{6ebd90e2-d219-11d2-ade5-0000c00f4ef9}") var Image = HOperatorSetX.ReadImage( "img/engraved.png" ); var Width , Height = HOperatorSetX.GetImageSize( Image ); var WindowHandle = HOperatorSetX.OpenWindow( 0 , 0 , winform.picturebox.width , winform.picturebox.height , winform.picturebox.hwnd , "transparent" , "" ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.SetDraw( WindowHandle , "margin" ); HOperatorSetX.SetLineWidth( WindowHandle , 3 ); HOperatorSetX.SetColor( WindowHandle , "red" ); var Font = HOperatorSetX.QueryFont( WindowHandle ); HOperatorSetX.SetFont( WindowHandle , Font[1]++"-Bold-30" ); winform.button.oncommand = function(id,event){ var ImageResult = HOperatorSetX.GrayRangeRect( Image , 7 , 7 ); var ImageInvert = HOperatorSetX.InvertImage( ImageResult ); var Region = HOperatorSetX.Threshold( ImageResult , 128 , 255 ); var ConnectedRegions = HOperatorSetX.Connection( Region ); var SelectedRegions = HOperatorSetX.SelectShape( ConnectedRegions , 'area', 'and', 1000, 99999 ); var SortedRegions = HOperatorSetX.SortRegion( SelectedRegions , 'first_point', 'true', 'column' ); HOperatorSetX.SetColored( WindowHandle , 6 ); HOperatorSetX.SetShape( WindowHandle , 'original' ); HOperatorSetX.SetLineWidth( WindowHandle , 2 ); HOperatorSetX.DispObj( ImageInvert , WindowHandle ); HOperatorSetX.DispObj( SortedRegions , WindowHandle ); win.delay(2000) HOperatorSetX.SetShape( WindowHandle , 'rectangle1' ); HOperatorSetX.DispObj( Image , WindowHandle ); HOperatorSetX.DispObj( SortedRegions , WindowHandle ); var RegionTrans = HOperatorSetX.ShapeTrans( SortedRegions , 'rectangle1' ); var Area , Row , Column = HOperatorSetX.AreaCenter( RegionTrans ); var MeanRow = HOperatorSetX.TupleMean( Row ); var Number = HOperatorSetX.CountObj( SortedRegions ); var OCRHandle = HOperatorSetX.ReadOcrClassMlp( 'Industrial_0-9A-Z_NoRej' ); for(i=1;Number;1){ var ObjectSelected = HOperatorSetX.SelectObj( SortedRegions , i ); var Class , Confidence = HOperatorSetX.DoOcrSingleClassMlp( ObjectSelected ,ImageInvert, OCRHandle, 1 ); HOperatorSetX.DispText( WindowHandle , Class , 'image' , MeanRow - 80 , Column[i] - 10 , 'yellow' , 'box' , 'false' ); } HOperatorSetX.ClearOcrClassMlp( OCRHandle ); } winform.show(); win.loopMessage(); return winform;
从上面代码里, 可以学到设置显示字体和大小
var Font = HOperatorSetX.QueryFont( WindowHandle ); HOperatorSetX.SetFont( WindowHandle , Font[1]++"-Bold-30" );
登录后方可回帖