机器视觉模板匹配,halcon示例讲解,入门篇

更新时间:2023-07-30 14:40:48 阅读: 评论:0

机器视觉模板匹配,halcon⽰例讲解,⼊门篇
机器视觉模板匹配,halcon⽰例讲解,⼊门篇
基于灰度值模板
基于形状(最常⽤的。受光照有⼀定影响)
//使⽤ROI区域绘制模板。使⽤blob分析创建模板。使⽤CAD导⼊轮廓创建模板
//⼀对多。多对多。
基于相关性(不受光照的影响,不受变焦影响)
基于灰度值(⼏乎不⽤,受光照影响很⼤)
基于局部变形
基于组件
基于描述符号
例程⼀:⽅法-》模板匹配 ->find_ncc_model_exposure.hdev
详细描述:Find an object despite linear illumination change using correlation-baked matching
代码:
//读图
read_image (Image, ‘cap_exposure/cap_exposure_03’)
dev_clo_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
t_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘fal’)
dev_update_off ()
//预处理
gen_circle (Circle, 246, 336, 150)
/
/选择瓶盖所在的圆形区域
area_center (Circle, Area, RowRef, ColumnRef)
//求取中⼼⾯积
reduce_domain (Image, Circle, ImageReduced)
//裁剪
create_ncc_model (ImageReduced, ‘auto’, 0, 0, ‘auto’, ‘u_polarity’, ModelID)
//创建模型。ImageReduced:输⼊图像。‘auto’:⾦字塔图像层数(⾃动)。0:最⼩旋转⾓度。0:最⼤旋转范围。‘auto’:旋转⾓度的步长(⾃动)。‘u_polarity’:匹配标准(极性匹配)。ModelID:模型句柄
dev_t_draw (‘margin’)
dev_display (Image)
dev_t_color (‘yellow’)
dev_display (Circle)
disp_message (WindowHandle, ‘Trained NCC model’, ‘window’, 12, 12, ‘black’, ‘true’)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
stop ()
Rows := []
Cols := []
//查找
for J := 1 to 10 by 1
read_image (Image, ‘cap_exposure/cap_exposure_’ + J$‘02’)
//读取图像路径。
find_ncc_model (Image, ModelID, 0, 0, 0.5, 1, 0.5, ‘true’, 0, Row, Column, Angle, Score)
/
/相关性匹配。光照不均匀,亮暗变化,但是背景不能复杂
// Image:输⼊图像。ModelID:模板句柄。0, 0:最⼩旋转⾓度,旋转⾓度范围。0.5:匹配性分数。1:寻找模板最⼤个数(0为全都找)。0.5:重叠度。‘true’:是否采⽤亚像素精度。0:使⽤的⾦字塔层数(越多搜索越快,准确度下降)。Row,Column,Angle:物体的⾏列坐标旋转⾓度。Score:精度分数。
//显⽰
Rows := [Rows,Row]缓解压力的方法
Cols := [Cols,Column]
dev_display (Image)
dev_display_ncc_matching_results (ModelID, ‘green’, Row, Column, Angle, 0)
disp_message (WindowHandle, ‘Found NCC model’, ‘window’, 12, 12, ‘black’, ‘true’)儿童学画500例
if (J < 10)
disp_continue_message (WindowHandle, ‘black’, ‘true’)
endif
stop ()
endfor
StdDevRows := deviation(Rows)
StdDevCols := deviation(Cols)
(⼀对多)
例程⼆:⽅法-》模板匹配(基于形状) ->find_scaled _shape_model.hdev
详细描述:find object using shape-ba matching(with scaling and rotation). 使⽤形状基础匹配(缩放和旋转)查找对象。
代码:
This example program shows how to find scaled and rotated shape models.
dev_update_pc (‘off’)
dev_update_window (‘off’)
dev_update_var (‘off’)
//读图
read_image (Image, ‘green-dot’)
get_image_size (Image, Width, Height)
dev_clo_window ()
dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)
dev_t_color (‘red’)
dev_display (Image)
//blob分析抠图
threshold (Image, Region, 0, 128)四字成语及意思
//⼆值化
connection (Region, ConnectedRegions)电解水原理
lect_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 10000, 20000)
男人玩你最明显的表现
fill_up (SelectedRegions, RegionFillUp)
dilation_circle (RegionFillUp, RegionDilation, 5.5)
reduce_domain (Image, RegionDilation, ImageReduced)
//抠图结束
create_scaled_shape_model (ImageReduced, 5, rad(-45), rad(90), ‘auto’, 0.8, 1.0, ‘auto’, ‘none’,
‘ignore_global_polarity’, 40, 10, ModelID)
//创建模板。
/
/scaled:带缩放泡菜煎饼
// ImageReduced:输⼊图像。5,:⾦字塔层数。rad(-45):模板旋转的最⼩⾓度。rad(90):模板旋转的最⼤⾓度。‘auto’:步长。0.8,:最⼩放⼤倍率。1.0,:最⼤放⼤倍率。‘auto’:放⼤倍率步长。‘none’:⽣成模板⽅法。
‘ignore_global_polarity’:匹配度量。40,:对⽐度。10,:最⼩对⽐度。ModelID:模板句柄。
//模板输出的轮廓为具有多个对象拼接的图像轮廓
get_shape_model_contours (Model, ModelID, 1)
//得到模板形状,将多个对象轮廓整成⼀体状态
area_center (RegionFillUp, Area, RowRef, ColumnRef)
vector_angle_to_rigid (0, 0, 0, RowRef, ColumnRef, 0, HomMat2D)
affine_trans_contour_xld (Model, ModelTrans, HomMat2D)
//仿射变换显⽰。
dev_display (Image)
dev_display (ModelTrans)
read_image (ImageSearch, ‘green-dots’)
dev_display (ImageSearch)
//显⽰图像
find_scaled_shape_model (ImageSearch, ModelID, rad(-45), rad(90), 0.8, 1.0, 0.5, 0, 0.5, ‘least_squares’, 5, 0.8, Row, Column, Angle, Scale, Score)
//对模板进⾏匹配
//scaled:⽀持缩放
// ImageSearch:输⼊图像。ModelID:模板句柄。rad(-45):最⼩旋转⾓度。rad(90):最⼤旋转⾓度。0.8, 1.0,:最⼩和最⼤缩放⽐例。0.5,:最⼩匹配分数。0,:数量。0.5,:重叠度。‘least_squares’:是否基于亚像素精度。5,:匹配⾦字塔层数。0.8,:贪婪度。Row, Column, Angle:输出横纵坐标和⾓度。Scale,:缩放⽐例。Score:匹配精度分数
for I := 0 to |Score| - 1 by 1
//显⽰仿射变换
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row[I], Column[I], HomMat2DTranslate)
hom_mat2d_rotate (HomMat2DTranslate, Angle[I], Row[I], Column[I], HomMat2DRotate)
hom_mat2d_scale (HomMat2DRotate, Scale[I], Scale[I], Row[I], Column[I], HomMat2DScale)
affine_trans_contour_xld (Model, ModelTrans, HomMat2DScale)
dev_display (ModelTrans)
endfor
Halcon例程
(⼀对多)
例程三:⽅法-》模板匹配(基于形状) ->create_roi_viavvision.hdev
详细描述:create a shape models from XLD contours (从XLD等⾼线创建形状模型)
dev_update_window (‘off’)
image acquisition and window size
open_framegrabber (‘File’, 1, 1, 0, 0, 0, 0, ‘default’, -1, ‘default’, -1, ‘default’, ‘pendulum/pendulum.q’,‘default’, -1, 1, FGHandle)
grab_image (ModelImage, FGHandle)
get_image_pointer1 (ModelImage, Pointer, Type, Width, Height)
dev_clo_window ()
dev_open_window (0, 0, Width, Height, ‘black’, WindowHandle)
dev_t_part (0, 0, Height - 1, Width - 1)
dev_display (ModelImage)
colors and other ttings for the visualization
dev_t_color (‘cyan’)
dev_t_draw (‘margin’)
dev_t_line_width (2)
stop ()
------------------- start of the application ----------------
step 1: extract the bright regions
//blob分析抠图
threshold (ModelImage, BrightRegions, 200, 255)
connection (BrightRegions, ConnectedRegions)
fill_up (ConnectedRegions, FilledRegions)
dev_display (ModelImage)
dev_display (FilledRegions)
stop ()
step 2: lect the region of the card
lect_shape (FilledRegions, Card, ‘area’, ‘and’, 1800, 1900)
dev_t_part (round(0.2 * Height), round(0.1 * Width) - 1, round(0.7 * Height) - 1, round(0.6 * Width) - 1)
//放⼤图像。round(0.2 * Height), round(0.1 * Width) - 1,:放⼤区域的左上横纵坐标。round(0.7 * Height) - 1, round(0.6 * Width) – 1:放⼤区域的右下横纵坐标。
dev_display (ModelImage)
dev_display (Card)
stop ()
step 3: u the card as an ROI for the next steps
reduce_domain (ModelImage, Card, ImageCard)信息能力
//裁剪区域
stop ()
step 4: extract the logo
dev_t_color (‘blue’)
threshold (ImageCard, DarkRegions, 0, 230)
connection (DarkRegions, ConnectedRegions)
lect_shape (ConnectedRegions, Characters, ‘area’, ‘and’, 150, 450)
lect_shape (ConnectedRegions, Characters, ‘area’, ‘and’, 150, 450)
union1 (Characters, CharacterRegion)
dev_display (ModelImage)
dev_display (CharacterRegion)
stop ()
step 5: enlarge the region slightly
dilation_circle (CharacterRegion, ROI, 1.5)
dev_display (ModelImage)
dev_display (ROI)
stop ()
step 6: creating the model
dev_t_part (0, 0, Height - 1, Width - 1)
dev_display (ModelImage)
reduce_domain (ModelImage, ROI, ImageROI)
inspect_shape_model (ImageROI, ShapeModelImages, ShapeModelRegions, 5, 25)
//多个可以使⽤的匹配模板,⽤于检查那个模板好⽤。5,:模板图像个数。25:对⽐度
lect_obj (ShapeModelRegions, ShapeModelRegion, 1)
//对合适的模板进⾏特征选择
dev_display (ShapeModelRegion)
create_shape_model (ImageROI, 3, 0, rad(360), ‘auto’, ‘none’, ‘u_polarity’, 30, 10, ModelID)
get_shape_model_contours (ShapeModel, ModelID, 1)
//创建模板
张先的词
stop ()
step 7: arching the logo
for i := 1 to 30 by 1
grab_image (SearchImage, FGHandle)
find_shape_model (SearchImage, ModelID, 0, rad(360), 0.7, 1, 0.5, ‘least_squares’, 0, 0.5, RowCheck, ColumnCheck, AngleCheck, Score)
//匹配模板
if (|Score| > 0)
vector_angle_to_rigid (0, 0, 0, RowCheck, ColumnCheck, AngleCheck, MovementOfObject)
affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfObject)
//仿射变换显⽰
dev_display (SearchImage)
dev_display (ModelAtNewPosition)
endif
endfor
stop ()
------------------- end of the application -----------------
clean up
dev_update_window (‘on’)
clear_shape_model (ModelID)
clo_framegrabber (FGHandle)
相关例⼦:(⼀对多)
find_aniso_shape_model.hdev(寻找⼤⼩不等相似的图像)
Find_cocoa_packages_max(对整幅图创建模板)
Inspect_shampoo_label.hdev(创建多个模板【⼀对多】)
Reu_model.hdev(写⼊模板⽂件,读取模板⽂件)
Synthetic_circle.hdev(形成轮廓并以轮廓为模板)
Pm_measure_board.hdev(使⽤模板匹配后进⾏测量)

本文发布于:2023-07-30 14:40:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1102067.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:模板   匹配   轮廓   旋转   图像   形状   区域   对象
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图