Reference processing for rapid detection of missing PCB components based on visual halcon

The general steps for missing element detection of conventional pcb.
First: Before sales, it is necessary to quickly determine whether the component is a patch, lead type, or conventional exposed type, and use ROI to directly perform rapid blob analysis, and use the feature area selection to quickly complete the early evaluation work.
Second: According to the requirements, whether template matching is required for one-to-many or many-to-many processing.
Third: the actual code writing, for the output file of template matching, it should be written into a new file.
This request:
halcon program to detect whether there is a resistance or the resistance falls off the resistance next to c6

read_image (Image, 'D:/shijue/11/pcb缺陷/Image__2022-05-04__17-01-48.bmp')
*读图
gen_rectangle2 (ROI_0, 58.5102, 543.681, rad(-101.309), 70.7985, 80.6545)
*对c6进行ROI选取
reduce_domain (Image, ROI_0, ImageReduced2)
*裁图
rgb1_to_gray (ImageReduced2, GrayImage)
*转为单通道灰度值
threshold (GrayImage, Regions2, 168, 220)
*二值化扣出c6
closing_rectangle1 (Regions2, RegionClosing2, 5,5)
*膨胀,防止字符没有扣好
reduce_domain (Image, RegionClosing2, ImageReduced3)
*裁图
inspect_shape_model (ImageReduced3, ModelImages, ModelRegions,5,25)
select_obj (ModelImages, ObjectSelected, 1)
dev_display (ObjectSelected)
create_shape_model (ImageReduced3,  3, 0, rad(360), 'auto', 'none', 'use_polarity', 30, 10, ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
*以上创建模板
find_shape_model (Image, ModelID, 0, rad(360), 0.7, 1, 0.5, 'least_squares', 0, 0.5, RowCheck, ColumnCheck, AngleCheck, Score)
*根据模板寻找c6
if (|Score| > 0)
    *判断是否找到c6
        vector_angle_to_rigid (0, 0, 0, RowCheck, ColumnCheck, AngleCheck, HomMat2D)
        affine_trans_region (ObjectSelected, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
        affine_trans_image (ModelImages, ImageAffineTrans, HomMat2D, 'constant', 'false')
        dev_display (ImageAffineTrans)
        dev_display (RegionAffineTrans)
        *对找到的c6进行仿射变化
        orientation_region (RegionAffineTrans, Phi)
        area_center (RegionAffineTrans, Area, Row, Column)
        *获取c6的坐标和角度
        vector_angle_to_rigid (Row, Column, 0, Row, Column, rad(0), HomMat2D)
        affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
        affine_trans_region (RegionAffineTrans, RegionAffineTrans2, HomMat2D, 'nearest_neighbor')
        *将c6运用到图中
        reduce_domain (ImageAffineTrans, RegionAffineTrans2, ImageReduced4)
        *裁图
        rgb1_to_gray (ImageReduced4, GrayImage1)
        *转为单通道灰度
        shape_trans (GrayImage1, RegionTrans, 'convex')
        *练成一个区域
        area_center (RegionTrans, Area1, Row1, Column1)
        orientation_region (RegionTrans, Phi1)
        *获取中心坐标和角度
        gen_rectangle2_contour_xld (Rectangle1, Row, Column1-300, Phi1, 150,80)
        *根据实际绘制一个在c6旁的元件的矩形轮廓
        dev_display (ImageAffineTrans)
        dev_display (Rectangle1)
        *显示检测是否正确
        gen_rectangle2 (Rectangle1, Row, Column1-300, Phi1, 150,80)
        *根据轮廓,绘制矩形区域
        reduce_domain (ImageAffineTrans, Rectangle1, ImageReduced5)
        *裁图,对元件进行处理
        decompose3 (ImageReduced5, Red, Green, Blue)
        trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')
        *转化为hsv
        threshold (Saturation, Regions, 0, 89)
        *对s进行二值化
        opening_rectangle1 (Regions, RegionOpening, 3,3)
        closing_rectangle1 (RegionOpening, RegionClosing1,  3,3)
        *以上为blob分析
        connection (RegionClosing1, ConnectedRegions1)
        select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 11907.4, 14351.9)
        *判断SelectedRegions1数组是否为空即可
endif
*以下为快速实验处理,可在售前阶段,快速评估产品是否有能力完成
*没有元件处理,常规blob分析
gen_rectangle2 (ROI_0, 157, 780.727, rad(-12.3391), 151.872, 74.1818)
reduce_domain (Image, ROI_0, ImageReduced)
decompose3 (ImageReduced, Red, Green, Blue)
trans_from_rgb (Red, Green, Blue, Hue, Saturation, Intensity, 'hsv')
threshold (Saturation, Regions, 0, 89)
opening_rectangle1 (Regions, RegionOpening, 3,3)
closing_rectangle1 (RegionOpening, RegionClosing1,  3,3)
connection (RegionClosing1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 11907.4, 14351.9)

*有元件处理
read_image (Image1, 'D:/shijue/11/pcb缺陷/Image__2022-04-03__17-25-28.bmp')
gen_rectangle2 (ROI_0, 866.364, 915.182, rad(17.1027), 126.123, 79.6594)
reduce_domain (Image1, ROI_0, ImageReduced1)
decompose3 (ImageReduced1, Red1, Green1, Blue1)
trans_from_rgb (Red1, Green1, Blue1, Hue1, Saturation1, Intensity1, 'hsv')
threshold (Saturation1, Regions1, 0, 81)
opening_rectangle1 (Regions1, RegionOpening1, 3,3)
closing_rectangle1 (RegionOpening1, RegionClosing,3,3)
connection (RegionClosing, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 11907.4, 14351.9)
*判断SelectedRegions数组不为为空即可

The actual completion effect
insert image description here
No original output area selection is empty
insert image description here

Test picture reference
Since csdn has the required picture upload size, so the jpg compressed image here is not the original picture, if you need the original picture, you can private message me.
Please add a picture description
Please add a picture description

Guess you like

Origin blog.csdn.net/m0_51559565/article/details/124637960