此代码作用于摄像头模组实拍图片检测坏点。用到的算子是动态阈值分割,实现逻辑是,实拍图转灰度图,然后进行动态阈值分割获得坏点。最后在原图上框出坏点。

 1 *此文件夹包含待测实拍图
 2 list_files ('E:/1(1)/1(1)', 'files', Files)
 3 l:=|Files|
 4 *设置画框规格
 5 dev_set_draw ('margin')
 6 dev_set_line_width (1)
 7 *循环每张图进行local_threshold获取黑点即坏点
 8 for i := 1 to l-1 by 1
 9     read_image (Image, Files[i])
10     rgb1_to_gray (Image, GrayImage)
11             dev_display (GrayImage)
12         dev_display (Rectangle)
13     local_threshold (GrayImage, Region, 'adapted_std_deviation', 'dark', [], [])
14     *获取坏点并计算数量
15     connection (Region, ConnectedRegions)
16     count_obj (ConnectedRegions, Number)
17     if (Number!=0)
18         *循环画框
19     for j := 1 to Number by 1
20         select_obj (ConnectedRegions, ObjectSelected, j)
21         area_center (ObjectSelected, Area, Row, Column)
22         gen_rectangle2 (Rectangle, Row, Column, 0, 20, 20)
23 
24     endfor
25     endif
26 
27 endfor

效果图如下:

Halcon学习:坏点检测-冯金伟博客园