halcon12 字符分割(新)

来源:互联网 发布:员工技能矩阵管理办法 编辑:程序博客网 时间:2024/05/18 09:04
*2017090910254402.bmp
*2017090909081102.bmp
*read_image(Image,'H:/opencvlab/cameraProgram/CAMERA_NO_NULTI_REGION/OCR/MyCamera_daheng_bottle_ocr_1280_多线程_宛西_不改界面/MyCamera2008/images/2017090909081102.bmp')
*gen_rectangle1(Rectangle, 297, 628, 573, 1067)
*2017090719402601.bmp
*H:/opencvlab/cameraProgram/CAMERA_NO_NULTI_REGION/OCR/MyCamera_daheng_bottle_ocr_1280_多线程_宛西_不改界面/MyCamera2008/images/image2
*------------------------------- 分割图像 暗图像不带波纹 ------------------------------------//
read_image(Image,'H:/现场测试数据/ocr20170522恰恰第二次调试现场数据/ocr20170522恰恰第二次调试现场数据/images/2017052017243302.bmp')
* gen_rectangle1(Rectangle, 308, 687, 600, 922)
* reduce_domain(Image,Rectangle,ImageReduced)
* crop_domain(ImageReduced, ImageReduced)
*rgb1_to_gray(ImageReduced, GrayImage)
* gen_disc_se(SE, 'byte', 21, 21, 0)
* gray_bothat(ImageReduced,SE, ImageBotHat)
* min_max_gray(ImageBotHat,ImageBotHat, 0, Min2, Max2, Range2)
* scale_image(ImageBotHat, ImageScaled1, 256/Max2, 0)
* threshold(ImageScaled1, Region2, 90, 255)
* opening_circle (Region2, RegionOpening, 1.5)
* region_to_bin(RegionOpening, BinImage, 255, 0, 512, 512)
stop()
*------------ char_threshold算法适用于恰恰背景
*------------字符分割算法 char_threshold
* char_threshold (ImageReduced, ImageReduced, Characters, 0, 95, Threshold)
*------------闭运算去噪声-----------------------
* opening_circle (Characters, RegionOpening, 1.2)
*-------------区域转图像--------------------------
* region_to_bin(RegionOpening, BinImage1, 255, 0, 512, 512)
* write_image(BinImage1, 'bmp', 0, 'c:/segment_result.bmp')
stop()




*------------------------------ 分割图像1结束 ----------------------------------//
*------------------------------- 分割图像2 采用char_threshold ------------------//
* dev_close_window()
*注意c:/ocr4.bmp这种图像不适用于char_threshold的算法
*read_image(Image,'C:/Users/Administrator/Desktop/无标题-3.bmp')
* get_image_size(Image, Width, Height)
* dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
*------------字符分割算法 char_threshold
* char_threshold (Image, Image, Characters, 0, 87, Threshold)
*------------闭运算去噪声-----------------------
* opening_circle (Characters, RegionOpening, 1)
*-------------区域转图像--------------------------
* region_to_bin(RegionOpening, BinImage1, 255, 0, Width, Height)
* write_image(BinImage1, 'bmp', 0, 'c:/segment_result.bmp')
stop()
*------------------------------ 分割图像2结束 ---------------------------------//



*------------------------------ 先做图像匹配 ----------------------------------
* Matching 03: Obtain the model image
*read_image (Image, 'D:/opencv_images/wanxi_images/2017090909081102.bmp')
* Matching 03: build the ROI from basic regions
temp_top:=295
temp_left:=542
temp_bottom:=383
temp_right:=636
temp_width:=temp_right-temp_left
temp_height:=temp_bottom-temp_top
temp_center_row:=(temp_top+temp_bottom)/2
temp_center_col:=(temp_left+temp_right)/2
gen_rectangle1 (ModelRegion, temp_top, temp_left, temp_bottom, temp_right)
*生成检测区域矩形
chk1_top:=403
chk1_left:=687
chk1_bottom:=500
chk1_right:=937
chk1_width:=chk1_right-chk1_left
chk1_height:=chk1_bottom-chk1_top
chk1_center_row:=(chk1_top+chk1_bottom)/2
chk1_center_col:=(chk1_left+chk1_right)/2
gen_rectangle1(CheckReion1, chk1_top, chk1_left, chk1_bottom, chk1_right)
stop()
*------------------------------ 下面是ocr识别代码 --------------------------
* OCR 04: Code generated by OCR 04
* OCR 04: 
* OCR 04: Prepare text model
* OCR 04: 
* create_text_model_reader ('manual', [], TextModel)
* set_text_model_param (TextModel, 'polarity', 'light_on_dark')
* set_text_model_param (TextModel, 'is_dotprint', 'true')
* set_text_model_param (TextModel, 'char_width', 29)
* set_text_model_param (TextModel, 'char_height', 50)
* set_text_model_param (TextModel, 'stroke_width', 4.9)
* set_text_model_param (TextModel, 'return_punctuation', 'false')
* set_text_model_param (TextModel, 'return_separators', 'false')
* set_text_model_param (TextModel, 'uppercase_only', 'true')
* set_text_model_param (TextModel, 'fragment_size_min', 6)
* set_text_model_param (TextModel, 'eliminate_border_blobs', 'true')
* set_text_model_param (TextModel, 'base_line_tolerance', 0.2)
* set_text_model_param (TextModel, 'max_line_num', 3)
* OCR 04: 
* OCR 04: Load classifier
* OCR 04: 
*read_ocr_class_mlp ('DotPrint_0-9.omc', OcrHandle)
*------------------------------ OCR初始化结束 ------------------------


* Matching 03: reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* Matching 03: create the correlation model
create_ncc_model (TemplateImage, 0, rad(-10), rad(10), rad(1.5), 'use_polarity', ModelId)
*stop()
*开始匹配
*遍历文件
list_image_files('H:/现场测试数据/ocr20170522恰恰第二次调试现场数据/ocr20170522恰恰第二次调试现场数据/images','bmp', [], ImageFiles)
for Index := 0 to |ImageFiles|-1 by  1
    read_image(Image1, ImageFiles[Index])
    dev_display(Image1)
    *stop()
    find_ncc_model (Image1, ModelId, rad(-10), rad(10), 0.5, 1, 0.5, 'true', 0, MatchingRow, MatchingCol, MatchingAngle, MatchingScore)
    if (|MatchingRow| < 1)
        continue
        stop()
    endif
    Row1:=MatchingRow-temp_height/2
    Col1:=MatchingCol-temp_width/2
    Row2:=MatchingRow+temp_height/2
    Col2:=MatchingCol+temp_width/2
    *获得相对位置
    drow:=Row1-temp_top
    dcol:=Col1-temp_left
    gen_rectangle1(Rectangle, Row1, Col1, Row2, Col2) 
    *overpaint_region(Image1, Rectangle, 255, 'margin')
    Row1:=chk1_top+drow
    Col1:=chk1_left+dcol
    Row2:=chk1_bottom+drow
    Col2:=chk1_right+dcol
    gen_rectangle1(Rectangle, Row1, Col1, Row2, Col2) 
    *overpaint_region(Image1, Rectangle, 255, 'margin')
    *stop()
    *提取出检测区域图像
    reduce_domain(Image1,Rectangle,ImageReduced)
    crop_domain(ImageReduced, ImagePart)
    
     *stop()
     *二值化1 带背景波纹图像
     *read_image(ImageReduced,'C:/Users/Administrator/Desktop/无标题-3.bmp')
      get_image_size(ImagePart, Width, Height)
      dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
     *------------字符分割算法 char_threshold
      char_threshold (ImagePart, ImagePart, Characters, 0, 90, Threshold)
      stop()
     *------------闭运算去噪声-----------------------
      opening_circle (Characters, RegionOpening, 1)
      *stop()
     *-------------区域转图像--------------------------
      region_to_bin(RegionOpening, TmpObj_MonoReduced_OCR_04_0, 255, 0, Width, Height)
      write_image(TmpObj_MonoReduced_OCR_04_0, 'bmp', 0, 'H:/现场测试数据/ocr20170522恰恰第二次调试现场数据/ocr20170522恰恰第二次调试现场数据/images/image2/Result'+Index+'.bmp')
      *stop()
     *二值化2 极暗不带波纹图像
*      get_image_size(ImagePart, Width, Height)
* gen_disc_se(SE, 'byte', 21, 21, 0)
* gray_bothat(ImagePart,SE, ImageBotHat)
* min_max_gray(ImageBotHat,ImageBotHat, 0, Min2, Max2, Range2)
* scale_image(ImageBotHat, ImageScaled1, 256/Max2, 0)
* threshold(ImageScaled1, Region2, 90, 255)
* opening_circle (Region2, RegionOpening, 2)
* region_to_bin(RegionOpening, BinImage, 255, 0, Width, Height)
* write_image(BinImage, 'bmp', 0, 'D:/opencv_images/wanxi_images/image2/image3/Result'+Index+'.bmp')
*stop()
*OCR处理
*reduce_domain (TmpObj_Mono, ROI_OCR_04_0, TmpObj_MonoReduced_OCR_04_0)
   
* OCR 04: 
* OCR 04: Orientation Correction (for consistent border and domain
* OCR 04: handling, this is always applied, even with no rotation)
* OCR 04: 
* OCR 04: Base orientation of ROI
* TmpCtrl_Orientation := 0
* OCR 04: Corrected for text line orientation
* TmpCtrl_RangeMin := -0.174533
* TmpCtrl_RangeMax := 0.174533
* text_line_orientation (TmpObj_MonoReduced_OCR_04_0, TmpObj_MonoReduced_OCR_04_0, 50, TmpCtrl_Orientation+TmpCtrl_RangeMin, TmpCtrl_Orientation+TmpCtrl_RangeMax, TmpCtrl_Orientation)
* OCR 04: Build rotation matrix
* hom_mat2d_identity (TmpCtrl_MatrixIdentity)
* hom_mat2d_rotate (TmpCtrl_MatrixIdentity, -TmpCtrl_Orientation, 0, 0, TmpCtrl_MatrixRotation)
* OCR 04: Apply transformation to image and domain
* get_domain (TmpObj_MonoReduced_OCR_04_0, TmpObj_Domain)
* get_system ('clip_region', TmpCtrl_ClipRegion)
* set_system ('clip_region', 'false')
* dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 24)
* affine_trans_region (TmpObj_DomainExpanded, TmpObj_DomainTransformedRaw, TmpCtrl_MatrixRotation, 'true')
* smallest_rectangle1 (TmpObj_DomainTransformedRaw, TmpCtrl_Row1, TmpCtrl_Col1, TmpCtrl_Row2, TmpCtrl_Col2)
* hom_mat2d_translate (TmpCtrl_MatrixIdentity, -TmpCtrl_Row1, -TmpCtrl_Col1, TmpCtrl_MatrixTranslation)
* hom_mat2d_compose (TmpCtrl_MatrixTranslation, TmpCtrl_MatrixRotation, TmpCtrl_MatrixComposite)
* affine_trans_region (TmpObj_Domain, TmpObj_DomainTransformed, TmpCtrl_MatrixComposite, 'true')
* affine_trans_image (TmpObj_MonoReduced_OCR_04_0, TmpObj_ImageTransformed, TmpCtrl_MatrixComposite, 'constant', 'true')
* dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 24)
* expand_domain_gray (TmpObj_ImageTransformed, TmpObj_ImageTransformedExpanded, 24)
* reduce_domain (TmpObj_ImageTransformed, TmpObj_DomainTransformed, TmpObj_ImageTransformedReduced)
* crop_part (TmpObj_ImageTransformedReduced, TmpObj_MonoReduced_OCR_04_0, 0, 0, TmpCtrl_Col2-TmpCtrl_Col1+1, TmpCtrl_Row2-TmpCtrl_Row1+1)
* set_system ('clip_region', TmpCtrl_ClipRegion)
* OCR 04: 
* OCR 04: Slant Correction
* OCR 04: 
* text_line_slant (TmpObj_MonoReduced_OCR_04_0, TmpObj_MonoReduced_OCR_04_0, 50, -0.244346, 0.174533, TmpCtrl_Slant)
* OCR 04: Build slant matrix
* hom_mat2d_identity (TmpCtrl_MatrixIdentity)
* hom_mat2d_slant (TmpCtrl_MatrixIdentity, -TmpCtrl_Slant, 'x', 0, 0, TmpCtrl_MatrixSlant)
* OCR 04: Apply transformation to image and domain
* get_domain (TmpObj_MonoReduced_OCR_04_0, TmpObj_Domain)
* get_system ('clip_region', TmpCtrl_ClipRegion)
* set_system ('clip_region', 'false')
* dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 24)
* affine_trans_region (TmpObj_DomainExpanded, TmpObj_DomainTransformedRaw, TmpCtrl_MatrixSlant, 'true')
* smallest_rectangle1 (TmpObj_DomainTransformedRaw, TmpCtrl_Row1, TmpCtrl_Col1, TmpCtrl_Row2, TmpCtrl_Col2)
* hom_mat2d_translate (TmpCtrl_MatrixIdentity, -TmpCtrl_Row1, -TmpCtrl_Col1, TmpCtrl_MatrixTranslation)
* hom_mat2d_compose (TmpCtrl_MatrixTranslation, TmpCtrl_MatrixSlant, TmpCtrl_MatrixComposite)
* affine_trans_region (TmpObj_Domain, TmpObj_DomainTransformed, TmpCtrl_MatrixComposite, 'true')
* affine_trans_image (TmpObj_MonoReduced_OCR_04_0, TmpObj_ImageTransformed, TmpCtrl_MatrixComposite, 'constant', 'true')
* dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 24)
* expand_domain_gray (TmpObj_ImageTransformed, TmpObj_ImageTransformedExpanded, 24)
* reduce_domain (TmpObj_ImageTransformed, TmpObj_DomainTransformed, TmpObj_MonoReduced_OCR_04_0)
* set_system ('clip_region', TmpCtrl_ClipRegion)
* find_text (TmpObj_MonoReduced_OCR_04_0, TextModel, TmpCtrl_ResultHandle_OCR_04_0)
* OCR 04: 
* OCR 04: Read text (classification step)
* OCR 04: Gray values for reading must be dark on bright
* invert_image (TmpObj_MonoReduced_OCR_04_0, TmpObj_MonoInverted_OCR_04_0)
* TmpCtrl_Expressions := ['^[0-9][0-9][0-9][0-9][0-9][0-9]$','^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$','^[0-9][0-9][0-9][0-9][0-9][0-9]$']
* get_text_result (TmpCtrl_ResultHandle_OCR_04_0, 'num_lines', TmpCtrl_NumLines)
* string_result:=''
* for TmpCtrl_LineIndex := 0 to TmpCtrl_NumLines - 1 by 1
*     get_text_object (Symbols_OCR_04_0, TmpCtrl_ResultHandle_OCR_04_0, ['line', TmpCtrl_LineIndex])
*     dev_display (TmpObj_MonoInverted_OCR_04_0)
*     dev_set_draw ('fill')
*     dev_set_colored (3)
*     dev_display (Symbols_OCR_04_0)
*     do_ocr_word_mlp (Symbols_OCR_04_0, TmpObj_MonoInverted_OCR_04_0, OcrHandle, TmpCtrl_Expressions[TmpCtrl_LineIndex % 3], 3, 2, SymbolNames_OCR_04_0, Confidences_OCR_04_0, Words_OCR_04_0, Scores_OCR_04_0)
    * OCR 04: Do something with the results
*     string_result := string_result + Words_OCR_04_0
*     stop()
* endfor
* stop()
* clear_text_result (TmpCtrl_ResultHandle_OCR_04_0)    
     
     
     
endfor










* Matching 03: END of generated code for model initialization
* Matching 03: BEGIN of generated code for model application
* Matching 03: the following operations are usually moved into
* Matching 03: that loop where the aquired images are processed
* Matching 03: Find the model
find_ncc_model (Image, ModelId, rad(-10), rad(10), 0.5, 1, 0.5, 'true', 0, MatchingRow, MatchingCol, MatchingAngle, MatchingScore)
* Matching 03: display the centers of the matches in the detected positions
for MatchingObjIdx := 0 to |MatchingScore| - 1 by 1
    * Matching 03: display the center of the match
    gen_cross_contour_xld (TransContours, MatchingRow, MatchingCol, 20, MatchingAngle)
    dev_display (TransContours)
endfor
* Matching 03: Clear model when done
clear_ncc_model (ModelId)
* Matching 03: END of generated code for model application








 
 




* OCR 04: ***************************************************************
* OCR 04: * The code which follows is to be executed once / image       *
* OCR 04: ***************************************************************
* OCR 04: 
* OCR 04: Load image
read_image (Image, 'C:/segment_result.bmp')
* OCR 04: 
* OCR 04: Perform actual processing (once per ROI)
* OCR 04: Generate regions of interest
gen_rectangle1 (ROI_OCR_04_0, 1.14706, 2.95904, 243.265, 372.5)
* OCR 04: 
* OCR 04: Extract symbol regions (segmentation step)
* OCR 04: Only consider first channel for color images
access_channel (Image, TmpObj_Mono, 1)
reduce_domain (TmpObj_Mono, ROI_OCR_04_0, TmpObj_MonoReduced_OCR_04_0)


* OCR 04: 
* OCR 04: Cleanup (global)
clear_text_model (TextModel)
clear_ocr_class_mlp (OcrHandle)











原创粉丝点击