halcon - 字符识别

dev_update_off ()
dev_get_window (WindowHandle)
* Image Acquisition 01: Code generated by Image Acquisition 01
* list_files ('C:/Users/Administrator/Desktop/Images', ['files','follow_links'], ImageFiles)
* tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
* for Index := 0 to |ImageFiles| - 1 by 1
* read_image (Image, ImageFiles[Index])

read_image (Image, 'C:/Users/1/Desktop/Cooper/Image_2023_05_16_15_12_18_907.bmp')
count_seconds (Seconds1)
rotate_image (Image, ImageRotate, 180, 'constant')
get_image_size (ImageRotate, Width, Height)
dev_set_part (0, 0, Height, Width)
*HOperatorSet.SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
* 3P ROI
gen_rectangle1 (ROI_01, 195.52, 428.8, 529.24, 982.84)
* 5P ROI
* gen_rectangle1 (ROI_01, 400, 428.8, 650, 982.84)

reduce_domain (ImageRotate, ROI_01, ImageReduced01)
threshold (ImageReduced01, Region01, 200, 255)
connection (Region01, ConnectedRegions01)
select_shape_std (ConnectedRegions01, SelectedRegions01, 'max_area', 70)
area_center (SelectedRegions01, Area01, Row01, Column01)
* D15 1 OK 2 反料NG 3 字符检测NG

* 5P 580 10000
if(Area01 == 0 or (Row01 > 580 and Area01 > 10000))
    *checkResult = false;
    *xDevice.ExecMethod("Write", new object[] { "D15", (short)2 });
    *HOperatorSet.DispObj(ho_ImageRotate, hv_WindowHandle);
    DispObj (ImageRotate)
    count_seconds (Seconds2)
    Messages := ['正反面积: ' + Area01 + ', ' + Row01 + '(不在范围内)']
    Messages := [Messages, '耗时: ' + ((Seconds2 - Seconds1)* 1000)$'.3f' + ' ms']
    set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
    disp_message (WindowHandle, Messages, 'image', 10, 10, 'black', 'true')
    set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
    disp_message (WindowHandle, 'NG', 'image', 50, 2000, 'red', 'false')
    stop ()
*     continue
    return ()
endif

* 3P ROI
gen_rectangle1 (ROI_0, 498.435, 261.654, 1200, 1251.19)
* 5P ROI
* gen_rectangle1 (ROI_0, 680, 261.654, 1100, 1251.19)

reduce_domain (ImageRotate, ROI_0, ImageReduced)
threshold (ImageReduced, Region, 100, 255)
connection (Region, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5000, 99999)
union1 (SelectedRegions, RegionUnion)
dilation_circle (RegionUnion, RegionDilation, 3.5)

paint_region (RegionDilation, ImageReduced, ImageResult, 255, 'fill')

read_ocr_class_mlp ('Industrial_0-9_NoRej.omc', OcrHandle)

create_text_model_reader ('auto', OcrHandle, TextModel)
set_text_model_param (TextModel, 'polarity', 'light_on_dark')
set_text_model_param (TextModel, 'min_char_width', 125)
set_text_model_param (TextModel, 'min_char_height', 222)
set_text_model_param (TextModel, 'min_stroke_width', 25)
set_text_model_param (TextModel, 'min_contrast', 80)

find_text (ImageResult, TextModel, TmpCtrl_ResultHandle_OCR_01_0)
* OCR 01: 
* OCR 01: Read text (classification step)
* OCR 01: Gray values for reading must be dark on bright
invert_image (ImageResult, TmpObj_MonoInverted_OCR_01_0)

get_text_result (TmpCtrl_ResultHandle_OCR_01_0, 'num_lines', TmpCtrl_NumLines)

get_text_object (Symbols_OCR_01_0, TmpCtrl_ResultHandle_OCR_01_0, 'all_lines')
TmpCtrl_Expressions := ['^[0-9][0-9][0-9][0-9][0-9]$']
* OCR 01: Do something with the results
do_ocr_word_mlp (Symbols_OCR_01_0, TmpObj_MonoInverted_OCR_01_0, OcrHandle, TmpCtrl_Expressions[0], 3, 2, SymbolNames_OCR_01_0, Confidences_OCR_01_0, Words_OCR_01_0, Scores_OCR_01_0)
clear_text_result (TmpCtrl_ResultHandle_OCR_01_0)
* OCR 01: 
* OCR 01: Cleanup (global)
clear_text_model (TextModel)
clear_ocr_class_mlp (OcrHandle)
* find_text (ImageResult, TextModel, TextResult)
* invert_image (ImageResult, TmpObj_MonoInverted_OCR_01_0)
* 如果应丢弃与图像域边界接触的区域,则为"true",否则为"false"
* set_text_model_param (TextModel, 'eliminate_border_blobs', 'true')
* 字符与周围背景的最小对比度
* set_text_model_param (TextModel, 'min_contrast', 50)
*字符的最小描边宽度(以像素为单位)。如果要在文本分割过程中自动估计最小笔画宽度,则可能会传递"auto"。
*请注意,"min_stroke_width"仅指字符。标点符号或分隔符的描边宽度不受"min_stroke_width"的限制。
* set_text_model_param (TextModel, 'min_stroke_width', 20)

* TmpCtrl_Expressions := ['^[0-9][0-9][0-9][0-9][0-9]$']

* get_text_result (TextResult, 'manual_num_lines', TmpCtrl_NumLines)
* get_text_object (Symbols_OCR_01_0, TextResult, ['manual_line', 0])

* do_ocr_word_mlp (Symbols_OCR_01_0, TmpObj_MonoInverted_OCR_01_0, OcrHandle, TmpCtrl_Expressions[0], 3, 2, SymbolNames_OCR_01_0, Confidences_OCR_01_0, Words_OCR_01_0, Scores_OCR_01_0)
* clear_text_result (TextResult)
* clear_text_model (TextModel)
* clear_ocr_class_mlp (OcrHandle)

if(Words_OCR_01_0 == '')
    OcrString := '未匹配到字符'
else
    OcrString := Words_OCR_01_0
endif

if(OcrString != '23053')
    Messages := []
    *checkResult = false;
    *xDevice.ExecMethod("Write", new object[] { "D15", (short)3 });
    *HOperatorSet.DispObj(ho_ImageRotate, hv_WindowHandle);
    DispObj (ImageRotate)
    count_seconds (Seconds2)
    
    Messages := [Messages, '匹配字符: ' + OcrString]
    Messages := [Messages, '耗时: ' + ((Seconds2 - Seconds1)* 1000)$'.3f' + ' ms']
    *DispMessage(hv_Messages);
    set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
    disp_message (WindowHandle, Messages, 'image', 10, 10, 'black', 'true')
    set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
    disp_message (WindowHandle, 'NG', 'image', 50, 2000, 'red', 'false')
    stop ()
*     continue
    return ()
endif

DispObj (ImageRotate)
SetDraw ('fill')
DispObj (Symbols_OCR_01_0)


*checkResult = true;
*xDevice.ExecMethod("Write", new object[] { "D15", (short)1 });

count_seconds (Seconds2)

Messages := ['正反面积: ' + Area01 + ', ' + Row01]
Messages := [Messages, '匹配字符: ' + OcrString]
Messages := [Messages, '耗时: ' + ((Seconds2 - Seconds1)* 1000)$'.3f' + ' ms']
set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
disp_message (WindowHandle, Messages, 'image', 10, 10, 'black', 'true')
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
disp_message (WindowHandle, 'OK', 'image', 50, 2000, 'green', 'false')
stop ()
* endfor
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值