不规则ROI的提取技巧:
gen_circle (Region, Row, Column, Radius)
gen_rectangle1 (LabelRectangle, Row1, Column1, Row2, Column2)
intersection (Region, LabelRectangle, RegionIntersection)
binary_threshold(Image : Region : Method, LightDark : UsedThreshold):使用二进制阈值分割图像
hom_mat2d_translate( : : HomMat2D, Tx, Ty : HomMat2DTranslate):将平移添加到齐次二维变换矩阵
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, RowModel, ColumnModel, HomMat2DTranslate)
hom_mat2d_invert( : : HomMat2D : HomMat2DInvert):矩阵反转
sobel_amp(Image : EdgeAmplitude : FilterType, Size : ):使用sobel算子检测边缘(振幅)
1 * This example demonstrates the print inspection using 2 * a variation model. The variation model is trained 3 * using a single model image. Before applying the print inspection, 4 * the images are scaled to the same gray value range like the model. 5 * 6 *这个例子演示使用变化模型执行打印检查 7 *利用单个模型图像对变差模型进行训练 8 *【注意】在应用打印检查之前,图像被缩放到与模型相同的灰度值范围。 9 dev_update_off () 10 dev_close_window () 11 * 12 * Read model image and init visualization 13 read_image (ModelImage, ‘cap_illumination/cap_illumination_01‘) 14 get_image_size (ModelImage, Width, Height) 15 dev_open_window (0, 0, Width / 2, Height / 2, ‘black‘, WindowHandle) 16 set_display_font (WindowHandle, 14, ‘mono‘, ‘true‘, ‘false‘) 17 * 18 * Segment model region 19 dev_display (ModelImage) 20 get_model_region (ModelImage, RegionROI, ImageReduced) 21 area_center (RegionROI, Area, RowModel, ColumnModel) 22 dev_set_draw (‘margin‘) 23 dev_set_color (‘green‘) 24 dev_display (RegionROI) 25 * 26 * Create model for shape-based-matching 27 *注意这里的技巧: 28 disp_message (WindowHandle, ‘Model generation...‘, ‘window‘, 12, 12, ‘black‘, ‘true‘) 29 *1.创建模板 30 create_scaled_shape_model (ImageReduced, ‘auto‘, 0, rad(360), ‘auto‘, 0.8, 1.2, ‘auto‘, ‘auto‘, ‘use_polarity‘, ‘auto‘, ‘auto‘, ModelID) 31 *2.得到模板轮廓(位置在原点) 32 get_shape_model_contours (ModelContours, ModelID, 1) 33 * Transform model contours from the original position for visualization 34 *3.生成一个平移矩阵(平移XY为模板图像中心的位置RowModel, ColumnModel) 35 hom_mat2d_identity (HomMat2DIdentity) 36 *将平移添加到齐次二维变换矩阵 37 hom_mat2d_translate (HomMat2DIdentity, RowModel, ColumnModel, HomMat2DTranslate) 38 *4.把原点处的模板放射变换到图像上,查看效果 39 affine_trans_contour_xld (ModelContours, ModelContoursAffineTrans, HomMat2DTranslate) 40 * 41 * Create variation model for print inspection 42 *【核心算子一】创建一个印刷检测模板 43 create_variation_model (Width, Height, ‘byte‘, ‘direct‘, VariationID) 44 * Generate variation image 45 *使用sobel算子检测边缘(振幅) 46 sobel_amp (ModelImage, VarImage, ‘sum_abs‘, 5) 47 *【核心算子二】生成一个可变模板图像 48 prepare_direct_variation_model (ModelImage, VarImage, VariationID, [20,25], [1.6,1.6]) 49 * 50 * Get gray value range of the variation model 51 *得到前景色和背景色的灰度平均值 52 get_grayval_range (ModelImage, RegionROI, RegionForeground, RegionBackground, BackgroundGVModel, ForegroundGVModel) 53 * 54 * Perform print inspection 55 *循环执行印刷检测 56 for I := 1 to 9 by 1 57 read_image (rImage, ‘cap_illumination/cap_illumination_‘ + I$‘.2‘) 58 inspect_cap (rImage, RegionROI, WindowHandle, ModelID, VariationID, RowModel, ColumnModel, BackgroundGVModel, ForegroundGVModel) 59 if (I < 9) 60 disp_continue_message (WindowHandle, ‘black‘, ‘true‘) 61 stop () 62 endif 63 endfor 64 * 65 clear_shape_model (ModelID) 66 clear_variation_model (VariationID)
1 * 2 * Segment label on the cap 3 *使用二进制阈值分割图像 4 binary_threshold (rImage, BrightRegion, ‘max_separability‘, ‘light‘, UsedThreshold) 5 dilation_circle (BrightRegion, RegionDilation, 1.5) 6 connection (RegionDilation, ConnectedRegions) 7 select_shape (ConnectedRegions, SelectedRegions, ‘area‘, ‘and‘, 100, 99999) 8 union1 (SelectedRegions, LabelRegion) 9 smallest_circle (LabelRegion, Row, Column, Radius) 10 smallest_rectangle1 (LabelRegion, Row1, Column1, Row2, Column2) 11 * 12 * Define model region 13 gen_circle (Region, Row, Column, Radius) 14 gen_rectangle1 (LabelRectangle, Row1, Column1, Row2, Column2) 15 intersection (Region, LabelRectangle, RegionIntersection) 16 dilation_circle (RegionIntersection, RegionROI, 5.5) 17 reduce_domain (rImage, RegionROI, ImageReduced) 18 return ()
1 * 2 * Find shape model in the input image 3 count_seconds (StartSeconds) 4 find_scaled_shape_model (rImage, ModelID, 0, rad(360), 0.98, 1.02, 0.4, 1, 1, ‘least_squares_high‘, [5,3], 0.9, Row, Column, Angle, Scale, Score) 5 count_seconds (EndSeconds) 6 TimeObjSearch := EndSeconds - StartSeconds 7 * 8 get_shape_model_contours (ModelContours, ModelID, 1) 9 * 定位成功 10 if (|Row| > 0) 11 * 12 * Align the model region and the found label region 13 *定位模板区域,找到标签区域 14 vector_angle_to_rigid (Row[0], Column[0], Angle, RowModel[0], ColumnModel[0], 0, HomMat2DImage) 15 affine_trans_image (rImage, ImageAffineTrans, HomMat2DImage, ‘constant‘, ‘false‘) 16 get_grayval_range (ImageAffineTrans, RegionROI, RegionForegroundImage, RegionBackgroundImage, BackgroundImage, ForegroundImage) 17 * 18 * Scale image to the model‘s gray value range 19 Mult := (ForegroundGVModel - BackgroundGVModel) / (ForegroundImage - BackgroundImage) 20 Add := ForegroundGVModel - Mult * ForegroundImage 21 * Transform the contours of the models for the visualization 22 vector_angle_to_rigid (0, 0, 0, Row[0], Column[0], Angle, HomMat2DContour) 23 affine_trans_contour_xld (ModelContours, ModelContoursTrans, HomMat2DContour) 24 reduce_domain (ImageAffineTrans, RegionROI, ImageReduced) 25 scale_image (ImageReduced, ImageScaled, Mult, Add) 26 * 27 * Print Inspection 28 count_seconds (StartSeconds) 29 *【核心算子三】比较可变模板 30 compare_ext_variation_model (ImageScaled, RegionDiff, VariationID, ‘light_dark‘) 31 count_seconds (EndSeconds) 32 * Analyze bright and dark defect regions 33 dev_display (rImage) 34 count_obj (RegionDiff, NumberRegionDiff) 35 for i := 1 to NumberRegionDiff by 1 36 select_obj (RegionDiff, RegionDiffSelected, i) 37 opening_circle (RegionDiffSelected, RegionOpening, 2.5) 38 connection (RegionOpening, ConnectedRegions) 39 select_shape (ConnectedRegions, DefectRegions, ‘height‘, ‘and‘, 20, 99999) 40 * 41 * Transform regions to the original position in the input image 42 hom_mat2d_invert (HomMat2DImage, HomMat2DInvert) 43 affine_trans_region (DefectRegions, DefectRegionsTrans, HomMat2DInvert, ‘nearest_neighbor‘) 44 closing_circle (DefectRegionsTrans, DefectRegionsClosing, 1.5) 45 * 46 * Display bright and dark defects using different colors 47 dev_set_line_width (2) 48 dev_set_draw (‘fill‘) 49 if (i == 1) 50 dev_set_color (‘red‘) 51 else 52 dev_set_color (‘orange‘) 53 endif 54 dev_display (DefectRegionsClosing) 55 * 56 * Emphasize defect regions 57 dev_set_color (‘magenta‘) 58 dev_set_draw (‘margin‘) 59 union1 (DefectRegionsClosing, RegionUnion) 60 closing_circle (RegionUnion, RegionClosing, 10) 61 connection (RegionClosing, DefectRegionEnlarged) 62 *计算等效椭圆的参数。 63 elliptic_axis (DefectRegionEnlarged, Ra, Rb, Phi) 64 if (|Phi|) 65 area_center (DefectRegionEnlarged, Area, RowEllipse, ColumnEllipse) 66 gen_ellipse (Ellipse, RowEllipse, ColumnEllipse, Phi, Ra * 2, Rb * 2) 67 dev_display (Ellipse) 68 endif 69 endfor 70 dev_set_line_width (1) 71 dev_set_color (‘green‘) 72 dev_display (ModelContoursTrans) 73 * 74 TimePrintInspect := EndSeconds - StartSeconds 75 disp_message (WindowHandle, ‘Inspection Time: ‘ + ((TimeObjSearch + TimePrintInspect) * 1000.0)$‘.04‘ + ‘ ms‘, ‘window‘, 12, 12, ‘black‘, ‘true‘) 76 else 77 disp_message (WindowHandle, ‘No shape model found.‘, ‘window‘, 12, 12, ‘red‘, ‘true‘) 78 endif 79 * 80 return ()
【Halcon示例002---varaition_model_illumination】 利用单张图像创建可变模板
原文:https://www.cnblogs.com/yangmengke2018/p/11738364.html