文章目录

  • 仿射变化
  • 识别
  • 使用助手
  • 自己训练


仿射变化

halcon WriteString能显示多行吗_计算机视觉

将图片进行矫正处理

dev_close_window()
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
read_image(Image,'C:/Users/Augustine/Desktop/halcon/image.png')
*获取图片的大小
get_image_size(Image, Width, Height)
*仿射运算获取图片的角度对图片进行矫正
*选中图片的区域
gen_rectangle1 (Rectangle, 155.399, 242.882, 387.318, 763.585)
*获取文本图片的旋转角度,得到的计算的文本行旋转角度
text_line_orientation(Rectangle,Image,75,-0.4,0.523599, OrientationAngle)
*从点和角度计算刚性仿射变换
* Row1 (输入参数) ——原始点的行坐标
* Column1 (输入参数) ——原始点的列坐标
* Angle1 (输入参数) ——原始点的角度
* Row2 (输入参数) ——转换点的行坐标
* Column2 (输入参数) ——转换点的列坐标
* Angle2 (输入参数) ——转换点的角度
* HomMat2D (输出参数) ——输出转换矩阵
vector_angle_to_rigid(Height/2, Width/2, OrientationAngle, Height/2,  Width/2,0, HomMat2D)
*仿射变化作用到图片上
affine_trans_image(Image, ImageAffinTrans, HomMat2D, 'constant', 'false')

halcon WriteString能显示多行吗_计算机视觉_02

# 分割

*分割
*提取图像的点
dots_image(ImageAffinTrans, DotImage, 5, 'dark', 0)
*由于halcon中包含对点状的图像进行直接读取的功能。所以掉用点状读取算子。DotImage为输出的图像变量存放位置。5为点状的最大直径。
*如果不想选取过大的点,可以调小,可以使用默认直径。‘dark’,最重要的变量。你提取的点状区域是提取暗色还是亮色(light)还是所有(all)。
*裁剪区域
reduce_domain(DotImage, Rectangle, ImageReduced)

*二值化,前景和背景很接近的时候用快速二值化(黑白分明)
binary_threshold(ImageReduced, Region, 'max_separability', 'light', UsedThreshold)
*膨胀
dilation_rectangle1(Region, RegionDilation, 10, 10)
*形成单独的连同区域
connection(RegionDilation, ConnectedRegions)
*变换区域的形状
shape_trans(ConnectedRegions,RegionTrans,'rectangle1')
*形状选择,通过宽和高筛选掉冒号
select_shape (RegionTrans, SelectedRegions, ['width','height'], 'and', [25.27,66.039], [125.63,74.642])
*将区域划分为大小大致相等的矩形。
partition_rectangle(SelectedRegions, Partitioned, 43, 80)
*和二值化的区域求交集,找到字符
intersection(Partitioned, Region, RegionIntersection)

识别

*排序
sort_region(RegionIntersection, SortedRegions, 'character', 'true', 'row')
*读取匹配模板
read_ocr_class_mlp('DotPrint_0-9A-Z_NoRej.omc', OCRHandle)
*匹配
* 只能识别暗点所以要将图片亮暗反转
invert_image(ImageReduced, ImageInvert)
do_ocr_multi_class_mlp(SortedRegions, ImageInvert, OCRHandle, Class, Confidence)


*显示
*求连通域个数
count_obj(SortedRegions,num)
*求最小外接矩形
smallest_rectangle1(SortedRegions, Row1, Column1, Row2, Column2)
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
dev_clear_window ()
dev_display (Image)
dev_set_color ('red')
for i:=0 to 17 by 1
    set_tposition (WindowHandle,30, 120+40*i)
    write_string (WindowHandle, Class[i])
endfor

halcon WriteString能显示多行吗_2d_03

使用助手

点击打开新的OCR

halcon WriteString能显示多行吗_人工智能_04

点击打开加载图像,标记图像位置,把识别内容输入即可

halcon WriteString能显示多行吗_人工智能_05

分割

可以自动调节符号尺寸

halcon WriteString能显示多行吗_Image_06

halcon WriteString能显示多行吗_人工智能_07

自己训练

将训练的数据识别点击保存原始数据,加入训练数据,点击开始训练,保存即可

halcon WriteString能显示多行吗_人工智能_08


点击结果插入代码即可


halcon WriteString能显示多行吗_计算机视觉_09

* OCR 03: Code generated by OCR 03
* OCR 03: 
* OCR 03: Prepare text model
* OCR 03: 
read_image(Image,'C:/Users/Augustine/Desktop/halcon/image.png')
*获取图片的大小
get_image_size(Image, Width, Height)
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
create_text_model_reader ('manual', [], TextModel)
set_text_model_param (TextModel, 'manual_is_dotprint', 'true')
set_text_model_param (TextModel, 'manual_char_width', 30)
set_text_model_param (TextModel, 'manual_char_height', 54)
set_text_model_param (TextModel, 'manual_stroke_width', 6.3)
set_text_model_param (TextModel, 'manual_return_punctuation', 'false')
set_text_model_param (TextModel, 'manual_uppercase_only', 'true')
set_text_model_param (TextModel, 'manual_fragment_size_min', 9)
set_text_model_param (TextModel, 'manual_eliminate_border_blobs', 'true')
set_text_model_param (TextModel, 'manual_base_line_tolerance', 0.2)
set_text_model_param (TextModel, 'manual_max_line_num', 2)
* OCR 03: 
* OCR 03: Load classifier
* OCR 03: 
read_ocr_class_mlp ('C:/Users/Augustine/Desktop/halcon/picture.omc', OcrHandle)
* OCR 03: ***************************************************************
* OCR 03: * The code which follows is to be executed once / image       *
* OCR 03: ***************************************************************
* OCR 03: 
* OCR 03: *************
* OCR 03: * ATTENTION *
* OCR 03: *************
* OCR 03: The image from the graphics window is not available. At this point,
* OCR 03: it is necessary to ensure an image is stored in the variable 'Image'
* OCR 03: 
* OCR 03: Perform actual processing (once per ROI)
* OCR 03: Generate regions of interest
gen_rectangle2 (ROI_OCR_03_0, 260.377, 503.855, rad(4.78136), 272.746, 86.2495)
* OCR 03: 
* OCR 03: Extract symbol regions (segmentation step)
* OCR 03: Only consider first channel for color images
access_channel (Image, TmpObj_Mono, 1)
reduce_domain (TmpObj_Mono, ROI_OCR_03_0, TmpObj_MonoReduced_OCR_03_0)
* OCR 03: 
* OCR 03: Orientation Correction (for consistent border and domain
* OCR 03: handling, this is always applied, even with no rotation)
* OCR 03: 
* OCR 03: Base orientation of ROI
TmpCtrl_Orientation := 0.0834505
* OCR 03: Build rotation matrix
hom_mat2d_identity (TmpCtrl_MatrixIdentity)
hom_mat2d_rotate (TmpCtrl_MatrixIdentity, -TmpCtrl_Orientation, 0, 0, TmpCtrl_MatrixRotation)
* OCR 03: Apply transformation to image and domain
get_domain (TmpObj_MonoReduced_OCR_03_0, TmpObj_Domain)
get_system ('clip_region', TmpCtrl_ClipRegion)
set_system ('clip_region', 'false')
dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 31)
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_03_0, TmpObj_ImageTransformed, TmpCtrl_MatrixComposite, 'constant', 'true')
dilation_circle (TmpObj_Domain, TmpObj_DomainExpanded, 31)
expand_domain_gray (TmpObj_ImageTransformed, TmpObj_ImageTransformedExpanded, 31)
reduce_domain (TmpObj_ImageTransformed, TmpObj_DomainTransformed, TmpObj_ImageTransformedReduced)
crop_part (TmpObj_ImageTransformedReduced, TmpObj_MonoReduced_OCR_03_0, 0, 0, TmpCtrl_Col2-TmpCtrl_Col1+1, TmpCtrl_Row2-TmpCtrl_Row1+1)
set_system ('clip_region', TmpCtrl_ClipRegion)
find_text (TmpObj_MonoReduced_OCR_03_0, TextModel, TmpCtrl_ResultHandle_OCR_03_0)
* OCR 03: 
* OCR 03: Read text (classification step)
get_text_object (Symbols_OCR_03_0, TmpCtrl_ResultHandle_OCR_03_0, 'manual_all_lines')
dev_display (TmpObj_MonoReduced_OCR_03_0)
dev_set_draw ('fill')
dev_set_colored (3)
dev_display (Symbols_OCR_03_0)
do_ocr_multi_class_mlp (Symbols_OCR_03_0, TmpObj_MonoReduced_OCR_03_0, OcrHandle, SymbolNames_OCR_03_0, Confidences_OCR_03_0)
* OCR 03: 
* OCR 03: Do something with the results

halcon WriteString能显示多行吗_计算机视觉_10