记录一下HALCON检测螺钉是否存在

思路:打光的时候需要比较好的效果,让螺钉和周围的孔洞灰度值相差比较大,这样方便检测。

第一步就是模板匹配,用HALCON的匹配助手。下面是匹配助手生成的代码

* 
* Matching 01: ************************************************
* Matching 01: BEGIN of generated code for model initialization
* Matching 01: ************************************************
set_system ('border_shape_models', 'false')
* 
* Matching 01: Obtain the model image
read_image (Image, 'I:/学习21下/机器视觉/第4次作业/1.jpg')
* 
* Matching 01: Build the ROI from basic regions
gen_rectangle1 (ModelRegion, 1324.12, 1322.54, 2720.69, 3309.25)
* 
* Matching 01: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* 
* Matching 01: Create the shape model
create_shape_model (TemplateImage, 8, rad(0), rad(360), rad(0.0982), ['point_reduction_high','no_pregeneration'], 'use_polarity', [24,46,47], 4, ModelID)
* 
* Matching 01: Get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelID, 1)
* 
* Matching 01: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
* 
* Matching 01: Display the model contours
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()
* 
* Matching 01: END of generated code for model initialization
* Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
* Matching 01: BEGIN of generated code for model application
* Matching 01: The following operations are usually moved into
* Matching 01: that loop where the acquired images are processed
* 
* Matching 01: Find the model
find_shape_model (Image, ModelID, rad(0), rad(360), 0.5, 0, 0.5, 'least_squares', [8,1], 0.75, Row, Column, Angle, Score)
* 
* Matching 01: Transform the model contours into the detected positions
dev_display (Image)
for I := 0 to |Score| - 1 by 1
    hom_mat2d_identity (HomMat2D)
    hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
    hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
    affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
    dev_set_color ('green')
    dev_display (TransContours)
    stop ()
endfor
* 
* Matching 01: Clear model when done
stop ()
clear_shape_model (ModelID)
* Matching 01: *******************************************
* Matching 01: END of generated code for model application
* Matching 01: *******************************************
* 

在中间添加代码进行图像处理

找到感兴趣区域,转灰度,二值,计算面积

        dev_set_color ('red')
        
        *计算下来Angle几乎为0,不予考虑
        *gen_circle(LeftUp, Row[I]-cos(Angle[I])*596.0, Column[I]-cos(Angle[I])*796.5, 30)
         
        *左上角圆设置为感兴趣区域
        gen_circle(LeftUp, Row[I]-596.0, Column[I]-787.5, 30)
        *原图中截取出来这个位置的图像
        reduce_domain(Image,LeftUp,Hole1)
        *转灰度图
        rgb1_to_gray(Hole1,Hole1_gray)    
        *转二值图
        threshold(Hole1_gray,Hole1_gray_bin,150,255)   
        *求区域面积,也就是比较白的区域的面积,这样就判断是不是有白色的钉子
        area_center (Hole1_gray_bin, Area1, Row1,Column1)  

设置显示参数

        *设置一些显示的参数,画圆的颜色和显示的字体
        dev_set_color ('green')
        dev_get_window (WindowHandle)
        set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
        color := [255,0,255]

再可视化

        *判断三个区域是否有钉子,如果没有,就把区域填充为粉色并对应显示出来,如果完好无损也提示一下
        *面积阈值先设置为5,小于5就认为没有钉子
        if(Area1 <5)
            *填充为粉色
            overpaint_region (Image, LeftUp, color, 'fill')
            *提示没有钉子
            disp_message (WindowHandle, ['The Left-Up Misses!'], 'window', 12, 120, 'red', ['green','false'])
        
        
        elseif(Area2 <5)
            
            overpaint_region (Image, RightUp, color, 'fill')
            disp_message (WindowHandle, ['The Right-Up Misses!'], 'window', 12, 120, 'red', ['green','false'])
        
        
        elseif(Area3 <5)
            
            overpaint_region (Image, RightDown, color, 'fill')
            disp_message (WindowHandle, ['The Right-Down Misses!'], 'window', 12, 120, 'red', ['green','false'])
        
        else
            *都不满足就是完整的
            disp_message (WindowHandle, ['NONE Misses'], 'window', 12, 120, 'red', ['green','false'])
            
        endif

运行结果: 

 

おすすめ

転載: blog.csdn.net/weixin_51229250/article/details/120892987