文章目录
- get_domain 返回所有输入图像的定义域作为一个区域
- add_channels 给区域增加灰度值
- find_shape_model 发现匹配模板
- find_shape_models 发现最佳模板
- 示例
get_domain 返回所有输入图像的定义域作为一个区域
Halcon 中的区域
get_domain(Image : Domain : : )
Image : 图像 (input_object): 类型可以是 (多通道-) 图像数组,对象类型为字节、方向、循环、int1、int2、uint2、int4、int8、实数、复数或矢量场。这是输入图像。Domain : 域 (output_object): 类型是区域数组,对象类型。这是输入图像的定义域。
add_channels 给区域增加灰度值
add_channels(Regions, Image : GrayRegions : : )
region(-array) → object: 这个参数是指输入的区域,通常是一个数组,描述了图像中的某些区域,但不包含像素值。
Image (input_object) (multichannel-)image → object: 这个参数是指包含像素值的输入图像,可能是多通道图像。
GrayRegions (output_object) image(-array) → object: 这个参数表示输出的图像或图像数组,其中包含了具有像素值的区域,每个输入区域对应一个输出图像。
find_shape_model 发现匹配模板
find_shape_model(Image : : //搜索图像ModelID, //模板句柄AngleStart, // 搜索时的起始角度AngleExtent, //搜索时的角度范围,必须与创建模板时的有交集MinScore, //最小匹配值,输出的匹配的得分Score 大于该值NumMatches, //定义要输出的匹配的最大个数MaxOverlap, //当找到的目标存在重叠时,且重叠大于该值时选择一个好的输出
//如果MaxOverlap=0, 找到的目标区域不能存在重叠, 如果MaxOverla p=1,所有找到的目标区域都要返回。SubPixel, //计算精度的设置,五种模式,多选2,3NumLevels, //搜索时金字塔的层数Greediness : //贪婪度,搜索启发式,一般都设为0.9,越高速度快,容易出现找不到的情况//0安全慢;1块不稳定;其他就是介于中间值 Row,Column, Angle, Score) //输出匹配位置的行和列坐标、角度、得分。
find_shape_models 发现最佳模板
find_shape_models(Image : : ModelIDs, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels, Greediness : Row, Column, Angle, Score, Model)Image (input_object):这个参数表示输入图像数据,这可能是多通道图像数组,用于在其中找到模型。ModelIDs (input_control):这是模板或模型的ID数组,用于指定哪一个或多个模型将在图像中寻找。AngleStart (input_control):指定模型可能的最小旋转角度,单位是弧度。例如,-0.39弧度表示模型可能有一些初始旋转。AngleExtent (input_control):模型的旋转角度范围,单位为弧度。例如,0.79弧度表示可以允许模型在指定角度范围内旋转。MinScore (input_control):找到模型实例的最小评分标准。0到1之间的值,表示匹配得分的下限。NumMatches (input_control):要找到的模型实例的数量。如果为0,表示找出所有匹配的实例。MaxOverlap (input_control):指定模型实例之间的最大重叠程度。值在0到1之间,表示允许多大程度的重叠。SubPixel (input_control):选择是否使用子像素精度进行计算。如果不等于 'none',表示使用特定的子像素方法。NumLevels (input_control):用于匹配时的金字塔层级数量。更多的层级可能提供更快的匹配,但可能牺牲准确性。Greediness (input_control):搜索算法的“贪心度”。0表示安全但慢,1表示快速但可能错过匹配。值在0到1之间。Row (output_control)、Column (output_control):找到的模型实例的行坐标和列坐标。Angle (output_control):找到的模型实例的旋转角度。Score (output_control):找到的模型实例的匹配得分。Model (output_control):找到的模型实例的索引。
示例
* This example program checks the right position of a label on
* a shampoo bottle.
* First, two shape models for the bottle and the label are created.
* Then, the models are searched in a sequence of images,
* and it is checked, if their relative position is within the
* specified tolerances.
*
* init visualization
dev_update_off ()
dev_set_draw ('margin')
dev_set_line_width (2)
*
* load reference image for model preparation
* 0.读取图片
set_system ('clip_region', 'false')
read_image (Image, 'packaging/shampoo_01')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_display (Image)
*
* prepare the shape model for matching
*
* generate first model region and search ROI
* 1.产生第一个感兴趣的区域
BottleModelRow := 131
BottleModelColumn := 370
BottleModelLength1 := 350
BottleModelLength2 := 35
BottleModelPhi := -0.1093
* 绘制矩形
gen_rectangle2 (Rectangle1, BottleModelRow, BottleModelColumn, BottleModelPhi, BottleModelLength1, BottleModelLength2)
gen_rectangle2 (Rectangle2, BottleModelRow + 220, BottleModelColumn, -BottleModelPhi, BottleModelLength1, BottleModelLength2)
* 将矩形联合
union2 (Rectangle1, Rectangle2, TemplateBottleRegion)
* 获取整个图像的区域
get_domain (Image, Domain)
* 计算区域的交集
intersection (TemplateBottleRegion, Domain, TemplateBottleRegion)
* 获取相交的中心点区域和坐标
area_center (TemplateBottleRegion, Area, RowBottleRef, ColumnBottleRef)
* 产生一个圆
gen_circle (SearchROIBottle, RowBottleRef, ColumnBottleRef, 40)
* 将感兴趣的区域裁剪
reduce_domain (Image, TemplateBottleRegion, ImageReduced)
*
* create shape model
* 2.创建形状匹配模型 ModelIDBottle
create_shape_model (ImageReduced, 5, -rad(3), rad(6), 0, 'auto', 'use_polarity', 25, 3, ModelIDBottle)
*
* display shape model
dev_display (Image)
dev_set_color ('forest green')
dev_display (TemplateBottleRegion)
dev_set_color ('slate blue')
disp_message (WindowHandle, 'bottle shape model', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* generate second model region and search ROI
* 3.产生第二个感兴趣的区域
LabelRow1 := 180
LabelRow2 := 310
LabelColumn1 := 50
LabelColumn2 := 470
* 产生一个矩形
gen_rectangle1 (TemplateLabelRegion, LabelRow1, LabelColumn1, LabelRow2, LabelColumn2)
* 获取区域的中心点以及坐标
area_center (TemplateLabelRegion, Area1, RowLabelRef, ColumnLabelRef)
* 产生一个圆
gen_circle (SearchROILabel, RowLabelRef, ColumnLabelRef, 60)
* 将感兴趣的区域裁剪
reduce_domain (Image, TemplateLabelRegion, ImageReduced)
* 4.检查已创建的形状模型并获得其属性信息
inspect_shape_model (ImageReduced, ModelImages, ModelRegions, 1, 25)
*
* create shape model
* 创建模板 ModelIDLabel1
create_shape_model (ImageReduced, 5, rad(-3), rad(6), 0, 'auto', 'use_polarity', 25, 5, ModelIDLabel1)
* 将匹配模板选择180度创建模板 ModelIDLabel2
create_shape_model (ImageReduced, 5, rad(180 - 3), rad(6), 0, 'auto', 'use_polarity', 25, 5, ModelIDLabel2)
ModelIDsLabel := [ModelIDLabel1,ModelIDLabel2]
*
* display label model
dev_display (Image)
dev_set_color ('forest green')
dev_display (TemplateLabelRegion)
dev_set_color ('slate blue')
dev_display (ModelRegions)
disp_message (WindowHandle, 'create label shape model', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* MainLoop
*
* check relative positions of label and bottle in all images
* 5.检测位置
for Index := 1 to 13 by 1read_image (Image, 'packaging/shampoo_' + Index$'.2')dev_display (Image)disp_message (WindowHandle, 'check label position', 'window', 12, 12, 'black', 'true')count_seconds (s1)* 裁剪第一次的搜索圆reduce_domain (Image, SearchROIBottle, ImageReduced)* 通过ModelIDBottle寻找搜索圆,获取ScoreBottlefind_shape_model (ImageReduced, ModelIDBottle, -rad(3), rad(6), 0.7, 1, 0.5, 'least_squares', 0, 0.9, RowBottle, ColumnBottle, AngleBottle, ScoreBottle)* prepare search ROIs in search image* 将两个搜索标记合并concat_obj (SearchROILabel, SearchROILabel, SearchROIs)add_channels (SearchROIs, Image, GrayRegions)* * search shape models* 通过ModelIDsLabel 查找匹配模板获取Score得分,注意角度范围,并且获取FoundModelfind_shape_models (GrayRegions, ModelIDsLabel, [rad(-3),rad(180 - 3)], [rad(6),rad(6)], 0.6, 1, 1, 'interpolation', 0, 0.9, Row, Column, Angle, Score, FoundModel)count_seconds (s2)if (|Score| != 1 or |ScoreBottle| != 1)disp_message (WindowHandle, 'Model not found', 'window', 40, 12, 'red', 'true')else* 如果模板if (ModelIDsLabel[FoundModel] == ModelIDLabel2)* 显示图标选择 180°disp_message (WindowHandle, 'Label rotated by 180°', 'window', 40, 12, 'red', 'true')else* calculate y deviation* 计算y的偏移量Diffy := (RowBottle - Row) - (RowBottleRef - RowLabelRef)* calculate x deviation* 计算x的偏移量Diffx := (ColumnBottle - Column) - (ColumnBottleRef - ColumnLabelRef)* * check the rotation angle of the bottle label* 计算旋转角度Diffa := deg(AngleBottle - Angle)* * calculate the time elapsedTime := s2 - s1* * display results* 显示结果Color := 'black'ModelColor := 'forest green'if (abs(Diffx) > 3)Color := [Color,'red']ModelColor := 'red'elseColor := [Color,'forest green']endifif (abs(Diffy) > 1)Color := [Color,'red']ModelColor := 'red'elseColor := [Color,'forest green']endifif (abs(Diffa) > 1)Color := [Color,'red']ModelColor := 'red'elseColor := [Color,'forest green']endif* 显示匹配结果dev_display_shape_matching_results (ModelIDsLabel, ['slate blue',ModelColor], Row, Column, Angle, 1, 1, FoundModel)* 提示消息disp_message (WindowHandle, ['Time = ' + (Time * 1000)$' .2' + ' ms','Diff x = ' + Diffx$' .2','Diff y = ' + Diffy$' .2','Diff angle = ' + Diffa$' .2'], 'window', 35, 12, Color, 'true')endif* endifif (Index < 13)disp_continue_message (WindowHandle, 'black', 'true')stop ()elsedisp_message (WindowHandle, 'Program finished', 'window', 450, 430, Color, 'true')endif
endfor
stop ()
clear_shape_model (ModelIDLabel1)
clear_shape_model (ModelIDLabel2)
clear_shape_model (ModelIDBottle)
set_system ('clip_region', 'true')