例程:variation_model_illumination.hdev
* This example demonstrates the print inspection using * a variation model. The variation model is trained * using a single model image. Before applying the print inspection, * the images are scaled to the same gray value range like the model. * dev_update_off () dev_close_window () * * Read model image and init visualization read_image (ModelImage, 'cap_illumination/cap_illumination_01') get_image_size (ModelImage, Width, Height) dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle) set_display_font (WindowHandle, 12, 'courier', 'true', 'false') * * Segment model region dev_display (ModelImage) * 取模型区域(区域,裁减的区域图片) get_model_region (ModelImage, RegionROI, ImageReduced) * 取面积中心 area_center (RegionROI, Area, RowModel, ColumnModel) dev_set_draw ('margin') dev_set_color ('green') dev_display (RegionROI) * * Create model for shape-based-matching disp_message (WindowHandle, 'Model generation...', 'window', 5, 10, 'green', 'false') * 创建形状模型 create_scaled_shape_model (ImageReduced, 'auto', 0, rad(360), 'auto', 0.8, 1.2, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID) * 取形状模型的轮廓 get_shape_model_contours (ModelContours, ModelID, 1) * Transform model contours from the original position for visualization * 构建二维变换同样的同质变换矩阵 hom_mat2d_identity (HomMat2DIdentity) * 为一个同质二维变换矩阵添加一个转化 hom_mat2d_translate (HomMat2DIdentity, RowModel, ColumnModel, HomMat2DTranslate) * 对XLD轮廓(contour)进行一个任意二维仿射变换。 affine_trans_contour_xld (ModelContours, ModelContoursAffinTrans, HomMat2DTranslate) * * Create variation model for print inspection * 创建一个可变模型为了检查打印 create_variation_model (Width, Height, 'byte', 'direct', VariationID) * Generate variation image * 使用Sobel算子检测边缘(幅值)。 sobel_amp (ModelImage, VarImage, 'sum_abs', 5) * 为图像对比准备一个变化模型。 prepare_direct_variation_model (ModelImage, VarImage, VariationID, [20,25], [1.6,1.6]) * * Get gray value range of the variation model * 得到的变化模型的灰度值范围 get_grayval_range (ModelImage, RegionROI, RegionForeground, RegionBackground, BackgroundGVModel, ForegroundGVModel) * * Display model for shape-based matching dev_display (ModelImage) dev_set_color ('green') dev_display (ModelContoursAffinTrans) disp_continue_message (WindowHandle, 'black', 'true') stop () * * Perform print inspection for i := 1 to 9 by 1 read_image (rImage, 'cap_illumination/cap_illumination_'+i$'.2') inspect_cap (rImage, RegionROI, WindowHandle, ModelID, VariationID, RowModel, ColumnModel, BackgroundGVModel, ForegroundGVModel) if (i < 9) disp_continue_message (WindowHandle, 'black', 'true') stop () endif endfor * clear_shape_model (ModelID) clear_variation_model (VariationID)
* Find shape model in the input image * 记录结束时间 count_seconds (StartSeconds) * 寻找形状模型相匹配的区域的参数 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) * 记录结束时间 count_seconds (EndSeconds) * 计算匹配时间 TimeObjSearch := EndSeconds - StartSeconds * 取轮廓 get_shape_model_contours (ModelContours, ModelID, 1) * if (|Row| > 0) * * Align the model region and the found label region * 计算模型与图象的偏移角度矩阵(以模型为基准) vector_angle_to_rigid (Row[0], Column[0], Angle, RowModel[0], ColumnModel[0], 0, HomMat2DImage) * 应用矩阵 affine_trans_image (rImage, ImageAffinTrans, HomMat2DImage, 'constant', 'false') *取灰度值 get_grayval_range (ImageAffinTrans, RegionROI, RegionForegroundImage, RegionBackgroundImage, BackgroundImage, ForegroundImage) * * Scale image to the model's gray value range Mult := (ForegroundGVModel - BackgroundGVModel)/ (ForegroundImage - BackgroundImage) Add := ForegroundGVModel - Mult*ForegroundImage * Transform the contours of the models for the visualization * 计算图象与模型的偏移角度矩阵(以图象为基准) vector_angle_to_rigid (0, 0, 0, Row[0], Column[0], Angle, HomMat2DContour) * 应用 affine_trans_contour_xld (ModelContours, ModelContoursTrans, HomMat2DContour) * 裁减 reduce_domain (ImageAffinTrans, RegionROI, ImageReduced) * 为一个图像的灰度值分级 scale_image (ImageReduced, ImageScaled, Mult, Add) * * Print Inspection 打印检查 count_seconds (StartSeconds) * 比较 compare_ext_variation_model (ImageScaled, RegionDiff, VariationID, 'light_dark') count_seconds (EndSeconds) * Analyze bright and dark defect regions dev_display (rImage) * 计算objects的个数 count_obj (RegionDiff, NumberRegionDiff) for i := 1 to NumberRegionDiff by 1 select_obj (RegionDiff, RegionDiffSelected, i) * 打开具有圆形结构的区域 opening_circle (RegionDiffSelected, RegionOpening, 2.5) * 连通区域 connection (RegionOpening, ConnectedRegions) * 选择带有某些特征的区域,Operation是运算,如“与”“或” select_shape (ConnectedRegions, DefectRegions, 'height', 'and', 20, 99999) * * Transform regions to the original position in the input image * 插入一个同质二维变换矩阵 hom_mat2d_invert (HomMat2DImage, HomMat2DInvert) * 对范围進行任意的最簡二維變換 affine_trans_region (DefectRegions, DefectRegionsTrans, HomMat2DInvert, 'false') * 接近圆形 closing_circle (DefectRegionsTrans, DefectRegionsClosing, 1.5) * * Display bright and dark defects using different colors dev_set_line_width (2) dev_set_draw ('fill') if (i = 1) dev_set_color ('red') else dev_set_color ('orange') endif dev_display (DefectRegionsClosing) * * Emphasize defect regions dev_set_color ('magenta') dev_set_draw ('margin') * 并集图像 union1 (DefectRegionsClosing, RegionUnion) * 靠近的闭环图形 closing_circle (RegionUnion, RegionClosing, 10) * 连接 connection (RegionClosing, DefectRegionEnlarged) * 计算Region区域中的椭圆参数 elliptic_axis (DefectRegionEnlarged, Ra, Rb, Phi) if (|Phi|) area_center (DefectRegionEnlarged, Area, RowEllipse, ColumnEllipse) * 转换椭圆 gen_ellipse (Ellipse, RowEllipse, ColumnEllipse, Phi, Ra*2, Rb*2) dev_display (Ellipse) endif endfor dev_set_line_width (1) dev_set_color ('green') dev_display (ModelContoursTrans) * TimePrintInspect := EndSeconds-StartSeconds p_print_text (WindowHandle, 5, 10, 'Inspection Time: ' + ((TimeObjSearch + TimePrintInspect)*1000.0)$'.04' + ' ms', 'green') else p_print_text (WindowHandle, 5, 10, 'No shape model found!', 'red') endif * return ()
get_grayval_range (ModelImage, RegionROI, RegionForeground, RegionBackground, BackgroundGVModel, ForegroundGVModel) * 原图区域裁剪 reduce_domain (Image, RegionROI, ImageReduced) * 采用自动阀值分离背景 bin_threshold (ImageReduced, RegionBackground) * 计算两个区域的不同得出前景 difference (RegionROI, RegionBackground, RegionForeground) * 计算前景主次灰度值(计算灰度值的平均值和偏差) intensity (RegionForeground, Image, ForegroundGVal, DeviationFG) * 计算背景主次灰度值 intensity (RegionBackground, Image, BackgroundGVal, DeviationBG) return ()
* * Segment label on the cap * 使用一个自动确定的阈值分割图像 bin_threshold (rImage, BrightRegion) * 计算两个范围的不同的范围(特定区域反选) difference (rImage, BrightRegion, RegionDifference) * 膨胀边圈 dilation_circle (RegionDifference, RegionDilation, 1.5) * 连接 connection (RegionDilation, ConnectedRegions) * 选择形状 select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 100, 99999) * 并集 union1 (SelectedRegions, LabelRegion) * 获取圆形区域的参数 smallest_circle (LabelRegion, Row, Column, Radius) * 获取矩形区域的参数 smallest_rectangle1 (LabelRegion, Row1, Column1, Row2, Column2) * * Define model region * 变换圆 gen_circle (Region, Row, Column, Radius) * 变换矩形 gen_rectangle1 (LabelRectangle, Row1, Column1, Row2, Column2) * 取两个范围相交范围 intersection (Region, LabelRectangle, RegionIntersection) * 膨胀一个圆的结构的 dilation_circle (RegionIntersection, RegionROI, 5.5) reduce_domain (rImage, RegionROI, ImageReduced) return ()