【每天一个Halcon例程】20240130

halcon例程:point_line_to_hom_mat2d.hdev 

* This example program shows how to align images based on point-to-line
* correspondences. HALCON's metrology model and point_line_to_hom_mat2d are used
* to detect the position and angle of the print in the images. The found position
* and angle are used to transform the images to the reference position (the
* position of the print in the first image in this example).
* From the aligned images of correct prints, a variation model is constructed.
* This variation model is then used to check and classify images of correct and
* incorrect prints.
dev_update_off ()
read_image (Image, 'pen/pen-01')//读入图像
get_image_size (Image, Width, Height)//获得图像宽高
dev_close_window ()//关闭窗口
dev_open_window (0, Width + 12, Width, Height, 'black', WindowHandle)
dev_open_window (0, 0, Width, Height, 'black', WindowHandleAlign)//打开窗口
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
set_display_font (WindowHandleAlign, 14, 'mono', 'true', 'false')//设置显示文本的字体大小和文本类型
dev_set_color ('red')//设置窗口显示的object对象的颜色
dev_display (Image)//显示图像
* Note: the checking of the print will be restricted to the region of the clip.
* Sometimes the print is also in an incorrect position of the clip.  This will lead
* to erroneous regions at the top or bottom border of the clip and hence can
* be detected easily.
threshold (Image, Region, 100, 255)//图像阈值分割
fill_up (Region, RegionFillUp)//填充孔洞
erosion_rectangle1 (RegionFillUp, RegionROI, 1, 15)//腐蚀
* Set up the metrology model with four lines.  Four lines are used since this is
* the minimum number of point-to-line correspondences that results in a unique rigid
* transformation.
create_metrology_model (MetrologyHandle)//创建测量模型句柄
set_metrology_model_image_size (MetrologyHandle, Width, Height)//设置要测量的图像的大小,必须调用
add_metrology_object_line_measure (MetrologyHandle, [208,137,213,90], [49,327,139,307], [100,201,214,89], [82,318,56,375], 40, 5, 1, 30, ['measure_transition','min_score'], ['negative',0.5], Index)//添加直线测量对象到模型。
set_metrology_object_param (MetrologyHandle, 'all', 'num_measures', 50)//设置测量模型参数
* Apply the metrology model to the reference image and read out the results.
apply_metrology_model (Image, MetrologyHandle)//将模型应用到图像中
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_begin', RowBegin)
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_begin', ColBegin)
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_end', RowEnd)
get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_end', ColEnd)//获得模型的结果


    
    
    
    *gen_cross_contour_xld (RefPoints, RowBegin,ColEnd , 16, rad(45))//生成十字形轮廓
    *gen_cross_contour_xld (RefPoints1, RowEnd, ColBegin, 16, rad(45))

* The reference points of the model are the center points of the detected line
* segments.  They will be used to compute the transformation from the current image
* to the reference image using point_line_to_hom_mat2d below.
RowRef := 0.5 * (RowBegin + RowEnd)
ColRef := 0.5 * (ColBegin + ColEnd)
dev_set_window (WindowHandle)
dev_display (Image)
dev_set_window (WindowHandleAlign)
dev_display (Image)
get_metrology_object_measures (MeasureContours, MetrologyHandle, 'all', 'all', Row, Column)//获得测量模型的参数
dev_set_color ('green')
dev_display (MeasureContours)
dev_set_color ('green')
disp_message (WindowHandleAlign, ['Measure objects of the four','metrology line objects'], 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_set_window (WindowHandleAlign)
dev_display (Image)
get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)//获得测量模型检测结果的轮廓
gen_cross_contour_xld (RefPoints, RowRef, ColRef, 16, rad(45))//生成十字线轮廓
dev_set_line_width (2)
dev_set_color ('green')
dev_display (MeasuredLines)
dev_set_color ('blue')
dev_display (RefPoints)
dev_set_line_width (1)
disp_message (WindowHandleAlign, ['Lines measured in the reference image','and reference points on the lines'], 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Create the variation model.
create_variation_model (Width, Height, 'byte', 'standard', VariationModelID)//创建图像比较模型
for I := 1 to 15 by 1
    read_image (Image, 'pen/pen-' + I$'02d')
    * Apply the metrology model to the current image and read out the line segment
    * coordinates.
    apply_metrology_model (Image, MetrologyHandle)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_begin', RowBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_begin', ColBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_end', RowEnd)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_end', ColEnd)//获得测量模型的结果

    
    * Determine a rigid transformation based on the point-to-line correspondences
    * from the reference points to the extracted lines.  Note that this determines
    * a transformation from the reference points to the lines in the current image.
    * Therefore, we must invert this transformation to obtain the transformation
    * from the current image to the rerefence image.
    *point_line_to_hom_mat2d ('rigid', RowRef + 0.5, ColRef + 0.5, RowBegin + 0.5, ColBegin + 0.5, RowEnd + 0.5, ColEnd + 0.5, HomMat2D)
        point_line_to_hom_mat2d ('rigid', RowRef, ColRef , RowBegin, ColBegin, RowEnd, ColEnd , HomMat2D)//计算从点到线的仿射变换矩阵
    hom_mat2d_invert (HomMat2D, HomMat2DInvert)//计算矩阵的逆矩阵
    affine_trans_image (Image, ImageTrans, HomMat2DInvert, 'constant', 'false')//执行仿射变换
    * Train the variation model with the transformed, i.e., aligned image.
    train_variation_model (ImageTrans, VariationModelID)//训练图像比较模型
    dev_set_window (WindowHandleAlign)
    dev_display (Image)
    get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)//获得测量模型检测结果的轮廓
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (MeasuredLines)
    dev_set_color ('blue')
    dev_display (RefPoints)
    dev_set_line_width (1)
    disp_message (WindowHandleAlign, ['Lines measured in the current','image and reference points'], 'window', 12, 12, 'black', 'true')
    dev_set_window (WindowHandle)
    dev_clear_window ()
    dev_display (ImageTrans)
    affine_trans_contour_xld (MeasuredLines, MeasuredLinesTrans, HomMat2DInvert)//执行轮廓的仿射变换
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (MeasuredLinesTrans)
    dev_set_color ('blue')
    dev_display (RefPoints)
    dev_set_line_width (1)
    disp_message (WindowHandle, ['Measured lines transformed to the','reference image pose and reference points'], 'window', 12, 12, 'black', 'true')
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor
get_variation_model (MeanImage, VarImage, VariationModelID)//获得图像比较模型的参数
prepare_variation_model (VariationModelID, 20, 3)//准备一个图像比较模型,并且设置模型与产品图像之间的差异值
dev_set_window (WindowHandleAlign)
dev_display (MeanImage)
disp_message (WindowHandleAlign, 'Reference image', 'window', 12, 12, 'black', 'true')
dev_set_window (WindowHandle)
dev_display (VarImage)
disp_message (WindowHandle, 'Variation image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Check and classify images of correct and incorrect prints.
NumImages := 30
for I := 1 to 30 by 1
    read_image (Image, 'pen/pen-' + I$'02d')
    apply_metrology_model (Image, MetrologyHandle)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_begin', RowBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_begin', ColBegin)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'row_end', RowEnd)
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'column_end', ColEnd)
    *point_line_to_hom_mat2d ('rigid', RowRef + 0.5, ColRef + 0.5, RowBegin + 0.5, ColBegin + 0.5, RowEnd + 0.5, ColEnd + 0.5, HomMat2D)
    point_line_to_hom_mat2d ('rigid', RowRef, ColRef , RowBegin, ColBegin, RowEnd, ColEnd , HomMat2D)
    hom_mat2d_invert (HomMat2D, HomMat2DInvert)
    affine_trans_image (Image, ImageTrans, HomMat2DInvert, 'constant', 'false')//对图像进行放射变换
    reduce_domain (ImageTrans, RegionROI, ImageReduced)//抠图
    compare_variation_model (ImageReduced, RegionDiff, VariationModelID)
    connection (RegionDiff, ConnectedRegions)
    select_shape (ConnectedRegions, RegionsError, 'area', 'and', 20, 1000000)//形状面积大小筛选
    count_obj (RegionsError, NumError)
    dev_set_window (WindowHandle)
    dev_clear_window ()
    dev_display (ImageTrans)
    dev_set_color ('red')
    dev_set_draw ('margin')
    dev_display (RegionsError)
    disp_message (WindowHandle, 'Aligned image', 'window', 12, 12, 'black', 'true')
    if (NumError == 0)
        disp_message (WindowHandle, 'Print OK', 'window', 12, 300, 'green', 'false')
    else
        disp_message (WindowHandle, 'Print not OK', 'window', 12, 300, 'red', 'false')
    endif
    dev_set_window (WindowHandleAlign)
    dev_display (Image)
    get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)
    dev_set_line_width (2)
    dev_set_color ('green')
    dev_display (MeasuredLines)
    dev_set_color ('blue')
    dev_display (RefPoints)
    dev_set_line_width (1)
    disp_message (WindowHandleAlign, ['Lines measured in the current','image and reference points'], 'window', 12, 12, 'black', 'true')
    if (I < NumImages)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

这个例程主要是讲了两个知识点:①如何利用halcon中的测量模型来找边,并且通过找到的边来将图像中的检测目标对齐,使检测目标每次都在相同位置。②如何使用halcon中的差异模型来检测缺陷。

知识点一个人理解:先创建测量模型找直线,找到直线之后计算出直线中点,利用point_line_to_hom_mat2d算子计算点放射变换到直线的矩阵A。因为该例子中是想将图像对齐,所以该例采取对图像进行仿射变换,所以需要对矩阵A进行求逆,得出矩阵A的逆矩阵B。然后用放射变换算子和矩阵B来对齐图像。

知识点二可以参考这个博主的文章,说的比较详细了。

关于Halcon中variation_model模型的快速解读。 - Imageshop - 博客园 (cnblogs.com)

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值