halcon瑕疵检测-光度立体法检测药片包装背面的缺陷

在工业领域,表面检测是一个非常广泛的应用领域。在halcon中,使用增强的光度立体视觉方法,三维表面检测被加强。利用阴影可方便快速的检测物体表面的缺口或凹痕。使用光度立体视觉方法可在复杂图像中轻松找到表面缺陷。药片包装的背面,不平整并且还有很多文字。此示例便是使用光度立体法,检测药片包装背面的缺陷。
示例代码如下:

    * 该示例通过使用光度立体技术检测药片包装背面的缺陷    * 输入是4张不同的药片包装背面图片,光从不同的角度照射         * Initialization    dev_close_window ()    dev_update_off ()    dev_open_window (0, 0, 512, 512, 'black', WindowHandle)    set_display_font (WindowHandle, 14, 'mono', 'true', 'false')    Message := 'Inspect the backside of a blister'    Message[1] := 'using photometric stereo. In this case four'    Message[2] := 'different light orientations were used.'    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')    disp_continue_message (WindowHandle, 'black', 'true')    stop ()    *    * Show input images with different illumination    * 1 读图像,依次读取多张图像    * 下面的for循环,依次显示4张从不同角度拍摄的药片包装的背面图像    read_image (Images, 'photometric_stereo/blister_back_0' + [1:4])    for I := 1 to 4 by 1        Message := 'Acquire image ' + I + ' of 4'        select_obj (Images, ObjectSelected, I)        dev_display (ObjectSelected)        disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')        wait_seconds (0.5)    endfor    *    * Apply photometric stereo to determine the albedo    * and the surface gradient.    * 2. 应用光度立体得到反照率图像和表面梯度图像         * 描述了从图像中心指向右侧的方向与投射到平面中的光的方向之间的角度。    * 也就是说,当观察图像(或相应的场景)时,倾斜角度为0表示光线来自右侧,    * 倾斜角度为90表示光线来自顶部,倾斜角度为180表示 光是从左边来的    Tilts := [6.1,95.0,-176.1,-86.8]    * 物平面与照明方向之间的角度    Slants := [41.4,42.6,41.7,40.9]    ResultType := ['gradient','albedo']    * 该算子得到反照率图像和表面梯度图像    photometric_stereo (Images, HeightField, Gradient, Albedo, Slants, Tilts, ResultType, 'poisson', [], [])    *    * Display the albedo image    * 显示反照率图像    dev_display (Albedo)    disp_message (WindowHandle, 'Albedo image', 'window', 12, 12, 'black', 'true')    disp_continue_message (WindowHandle, 'black', 'true')    stop ()    *    * Calculate the gaussian curvature of the surface    * using the gradient field as input for the operator    * 3. 使用之前得到的表面梯度,计算表面的高斯曲率,得到高斯曲率图像    * derivate_vector_field.    * Defects are usually easy to detect in the curvature image.    * 在曲率图像上能更容易的进行检测    * 该算子通过之前得到的表面梯度得到高斯曲率图像    derivate_vector_field (Gradient, GaussCurvature, 1, 'gauss_curvature')    *    * Detect defects    * 检测缺陷    *    * Segment the tablet areas in the curvature image    * 4. 对高斯曲率图像进行预处理和Blob分析,从而得到缺陷区域    * 在曲率图像中,我们先分开各个药片区域    regiongrowing (GaussCurvature, Regions, 1, 1, 0.001, 250)    * 通过宽和高的特征,进行特征选择    select_shape (Regions, TabletRegions, ['width','height'], 'and', [150,150], [200,200])    * 凸性形状转换,针对区域    shape_trans (TabletRegions, TabletRegions, 'convex')    * 区域联合    union1 (TabletRegions, TabletRegions)    * 腐蚀    erosion_circle (TabletRegions, TabletRegions, 3.5)    * Search for defects inside the tablet areas    * 在药片区域搜寻缺陷    * 抠图    reduce_domain (GaussCurvature, TabletRegions, ImageReduced)    * 计算图像各个像素的绝对值,存在此次处理的原因是:高斯曲率图像存在负值    * 缺陷处,高斯曲率会比较大    abs_image (ImageReduced, ImageAbs)    * 二值化 ,灰度直方图    threshold (ImageAbs, Region, 0.03, 255)    * 闭运算    closing_circle (Region, RegionClosing, 10.5)    * 断开得到连通域    connection (RegionClosing, ConnectedRegions)    * 通过面积特征,进行特征选择    select_shape (ConnectedRegions, Defects, 'area', 'and', 10, 99999)    * 获得缺陷区域的中心点行列坐标    area_center (Defects, Area, Row, Column)    * 生成圆形区域    gen_circle (Circle, Row, Column, gen_tuple_const(|Row|,20.5))    * Display the defects in curvature image    * 5. 接下来在高斯曲率图像中标记缺陷区域    dev_set_draw ('margin')    dev_set_color ('red')    dev_set_line_width (2)    dev_display (GaussCurvature)    dev_display (Circle)    Message := 'The defect can easily be detected'    Message[1] := 'in the surface curvature image'    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')    stop ()    * Display the defects in the albedo image    * 6. 在反照率图像中标记出缺陷    dev_set_draw ('margin')    dev_set_color ('red')    dev_display (Albedo)    dev_display (Circle)    disp_message (WindowHandle, 'Defect in albedo image', 'window', 12, 12, 'black', 'true')



重点说明:
1. 光度立体法便是在得到高斯曲率图像之后,在高斯曲率图像上进行预处理和Blob分析,检测出缺陷。
2. 高斯曲率图像的获得,是通过derivate_vector_field算子获得的,该算子中,算子中需要输入表面梯度图像,因此,在使用该算子之前,需要先获得表面梯度图像。
3. 表面梯度图像的获得,是通过photometric_stereo算子获得的,该算子可以同时得到表面梯度图像和反照率图像。该算子需要多张从不同角度拍照所得到的图像作为输入。

执行流程:
待检测的四张图像之一:

反照率图像:

高斯曲率图像:

二值化处理得到药片区域后:

预处理之后抠图得到药片图像:

通过Blob分析和预处理,得到缺陷区域:

在高斯曲率图像中标记出缺陷:

在反照率图像中标记处缺陷:

来源:https://blog.csdn.net/ymj7150697/article/details/103296968

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值