来源:https://www.cnblogs.com/LearningC/p/15870948.html
代码流程
- 使用edges_sub_pix (Image, Edges, 'canny', 10, 20, 40),获取黑白之间的边缘线条
- 轮廓转区域
- 根据边长和面积select_shape_xld (Edges, SelectedXLD, ['rect2_len1','area'], 'and', [70,18000], [100,33000]),筛选小方格
- 再根据长宽做二次筛选,得到所有的方格
- 动态二值化各个方格,
- 图像取反,腐蚀细小的区域(存在不确定性)
- 检查是否存在区域,有则表示存在芯片
dev_close_window () dev_open_window (0, 0, 1200, 850, 'black', WindowHandle) dev_clear_window() * read_image (Image, 'D:/temp/chips/20220110112001.jpg') * read_image (Image, 'D:/temp/chips/20220110112121.jpg') * read_image (Image, 'D:/temp/chips/20220110112137.jpg') read_image (Image, 'D:/temp/chips/Image_20211227154621354.bmp') *使用canny算子提取亚像素边界 edges_sub_pix (Image, Edges, 'canny', 10, 20, 40) *根据边长和面积提取xld select_shape_xld (Edges, SelectedXLD, ['rect2_len1','area'], 'and', [70,18000], [100,33000]) *根据xld绘出区域 gen_region_contour_xld (SelectedXLD, Region, 'margin') *根据区域面积提取芯片盒子 select_shape (Region, ChipsImage, ['width','height'], \ 'and', [155,155], [170,170]) *合并区域 union1 (ChipsImage, RegionUnion) *填充区域 fill_up (RegionUnion, ROI_0) *根据联合区域裁剪感兴趣区域 reduce_domain (Image, ROI_0, ImageReduced) *联通分割区域 connection (ImageReduced, ConnectedRegions) *将每个芯片方格转换成标准矩形 shape_trans (ConnectedRegions, RegionTransRect, 'rectangle1') *排序 sort_region (RegionTransRect, SortedRegions, 'character', 'true', 'row') *总孔位数量 count_obj (SortedRegions, Number) *结果数组,0 代表没有 1代表有芯片 result:= [] *空位的数量 EmptyCount :=0 dev_display (Image) for Index := 1 to Number by 1 * 找到索引对应的区域 select_obj (SortedRegions, ALL_ROI_OF_SINGLES, Index) *裁剪区域 reduce_domain (Image, ALL_ROI_OF_SINGLES, ChipBoxRegion) *动态阈值分割 binary_threshold (ChipBoxRegion, ChipRegion, 'max_separability', 'dark', UsedThreshold) *反向选择 difference (ChipBoxRegion, ChipRegion, ChipWithLine) *联通区域 connection (ChipWithLine, ConnectedRegions1) *根据面积特征筛选区域 select_shape (ConnectedRegions1, SelectedRegions, 'area', 'and', 1000.59, 3001.78) *腐蚀 erosion_circle (SelectedRegions, ChipErosion, 2) *再次通过面积筛选 select_shape (ChipErosion, ChipInside, 'area', 'and', 1000, 3000) *腐蚀 erosion_circle (ChipInside, ChipErosion1, 2) *计算赛选个数 count_obj (ChipErosion1, chipExists) if (chipExists == 0) *判断不存在芯片的逻辑 EmptyCount := EmptyCount +1 *显示用 dev_update_on() dev_set_color ('red') dev_set_draw ('fill') dev_display (ALL_ROI_OF_SINGLES) dev_update_off() endif if(chipExists == 1) *判断有芯片的逻辑 *显示用 dev_update_on() dev_set_color ('green') dev_set_draw ('fill') shape_trans (ChipErosion1, RegionTrans, 'rectangle2') dev_display (RegionTrans) dev_update_off() endif result[Index-1] := chipExists endfor disp_message (WindowHandle, ' Holes Number:'+Number + ' , Exists Count:' + (Number - EmptyCount) , 'window', 12, 12, 'black', 'true')
需求
有如下图的一个摆盘,摆盘的方格中摆放芯片,一个格子中只放一个,我们需要知道每个方格中是否有芯片去指导我们将芯片放到空的方格中。
标签:格子,芯片,Image,dev,shape,区域,识别,select From: https://www.cnblogs.com/ken0137/p/18360496