加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

模板匹配(基于形状分析)

(2015-08-19 17:10:51)
标签:

机器视觉

图像处理

艾特视觉

艾特自动化

分类: Halcon例子

例程:variation_model_illumination.hdev

 

 

  1. This example demonstrates the print inspection using  
  2. variation model. The variation model is trained  
  3. using single model image. Before applying the print inspection,  
  4. the images are scaled to the same gray value range like the model.  
  5.   
  6. dev_update_off ()  
  7. dev_close_window ()  
  8.   
  9. Read model image and init visualization  
  10. read_image (ModelImage, 'cap_illumination/cap_illumination_01' 
  11. get_image_size (ModelImage, Width, Height)  
  12. dev_open_window (0, 0, Width/2, Height/2, 'black'WindowHandle)  
  13. set_display_font (WindowHandle, 12, 'courier''true''false' 
  14.   
  15. Segment model region  
  16. dev_display (ModelImage)  
  17. 取模型区域(区域,裁减的区域图片)  
  18. get_model_region (ModelImage, RegionROI, ImageReduced)   
  19. 取面积中心  
  20. area_center (RegionROI, Area, RowModel, ColumnModel)  
  21. dev_set_draw ('margin' 
  22. dev_set_color ('green' 
  23. dev_display (RegionROI)  
  24.   
  25. Create model for shape-based-matching  
  26. disp_message (WindowHandle, 'Model generation...''window'5, 10, 'green''false' 
  27. 创建形状模型  
  28. create_scaled_shape_model (ImageReduced, 'auto'0, rad(360), 'auto'0.8, 1.2, 'auto''auto''use_polarity''auto''auto'ModelID)  
  29. 取形状模型的轮廓  
  30. get_shape_model_contours (ModelContours, ModelID, 1)  
  31. Transform model contours from the original position for visualization  
  32. 构建二维变换同样的同质变换矩阵  
  33. hom_mat2d_identity (HomMat2DIdentity)  
  34. 为一个同质二维变换矩阵添加一个转化  
  35. hom_mat2d_translate (HomMat2DIdentity, RowModel, ColumnModel, HomMat2DTranslate)  
  36. 对XLD轮廓(contour)进行一个任意二维仿射变换。  
  37. affine_trans_contour_xld (ModelContours, ModelContoursAffinTrans, HomMat2DTranslate)  
  38.   
  39. Create variation model for print inspection  
  40. 创建一个可变模型为了检查打印  
  41. create_variation_model (Width, Height, 'byte''direct'VariationID)  
  42. Generate variation image  
  43. 使用Sobel算子检测边缘(幅值)。  
  44. sobel_amp (ModelImage, VarImage, 'sum_abs'5)  
  45. 为图像对比准备一个变化模型。  
  46. prepare_direct_variation_model (ModelImage, VarImage, VariationID, [20,25], [1.6,1.6])  
  47.   
  48. Get gray value range of the variation model  
  49. 得到的变化模型的灰度值范围  
  50. get_grayval_range (ModelImage, RegionROI, RegionForeground, RegionBackground, BackgroundGVModel, ForegroundGVModel)  
  51.   
  52. Display model for shape-based matching  
  53. dev_display (ModelImage)  
  54. dev_set_color ('green' 
  55. dev_display (ModelContoursAffinTrans)  
  56. disp_continue_message (WindowHandle, 'black''true' 
  57. stop ()  
  58.   
  59. Perform print inspection  
  60. for := to by  
  61.     read_image (rImage, 'cap_illumination/cap_illumination_'+i$'.2' 
  62.       
  63.     inspect_cap (rImage, RegionROI, WindowHandle, ModelID, VariationID, RowModel, ColumnModel, BackgroundGVModel, ForegroundGVModel)  
  64.     if (i 9)  
  65.         disp_continue_message (WindowHandle, 'black''true' 
  66.         stop ()  
  67.     endif  
  68. endfor  
  69.   
  70. clear_shape_model (ModelID)  
  71. clear_variation_model (VariationID)  


 



 

 

  1. Find shape model in the input image  
  2. 记录结束时间  
  3. count_seconds (StartSeconds)  
  4. 寻找形状模型相匹配的区域的参数  
  5. 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)  
  6. 记录结束时间  
  7. count_seconds (EndSeconds)  
  8. 计算匹配时间  
  9. TimeObjSearch := EndSeconds StartSeconds  
  10. 取轮廓  
  11. get_shape_model_contours (ModelContours, ModelID, 1)  
  12.   
  13. if (|Row| 0)  
  14.       
  15.     Align the model region and the found label region  
  16.     计算模型与图象的偏移角度矩阵(以模型为基准)  
  17.     vector_angle_to_rigid (Row[0], Column[0], Angle, RowModel[0], ColumnModel[0], 0, HomMat2DImage)  
  18.     应用矩阵  
  19.     affine_trans_image (rImage, ImageAffinTrans, HomMat2DImage, 'constant''false' 
  20.     *取灰度值  
  21.     get_grayval_range (ImageAffinTrans, RegionROI, RegionForegroundImage, RegionBackgroundImage, BackgroundImage, ForegroundImage)  
  22.       
  23.     Scale image to the model's gray value range  
  24.       
  25.     Mult := (ForegroundGVModel BackgroundGVModel)/ (ForegroundImage BackgroundImage)  
  26.       
  27.     Add := ForegroundGVModel Mult*ForegroundImage  
  28.     Transform the contours of the models for the visualization  
  29.     计算图象与模型的偏移角度矩阵(以图象为基准)  
  30.     vector_angle_to_rigid (0, 0, 0, Row[0], Column[0], Angle, HomMat2DContour)  
  31.     应用  
  32.     affine_trans_contour_xld (ModelContours, ModelContoursTrans, HomMat2DContour)  
  33.     裁减  
  34.     reduce_domain (ImageAffinTrans, RegionROI, ImageReduced)  
  35.     为一个图像的灰度值分级  
  36.     scale_image (ImageReduced, ImageScaled, Mult, Add)  
  37.       
  38.     Print Inspection 打印检查  
  39.     count_seconds (StartSeconds)  
  40.     比较  
  41.     compare_ext_variation_model (ImageScaled, RegionDiff, VariationID, 'light_dark' 
  42.     count_seconds (EndSeconds)  
  43.     Analyze bright and dark defect regions  
  44.     dev_display (rImage)  
  45.     计算objects的个数  
  46.     count_obj (RegionDiff, NumberRegionDiff)  
  47.     for := to NumberRegionDiff by  
  48.         select_obj (RegionDiff, RegionDiffSelected, i)  
  49.         打开具有圆形结构的区域  
  50.         opening_circle (RegionDiffSelected, RegionOpening, 2.5)  
  51.         连通区域  
  52.         connection (RegionOpening, ConnectedRegions)  
  53.         选择带有某些特征的区域,Operation是运算,如“与”“或”  
  54.         select_shape (ConnectedRegions, DefectRegions, 'height''and'20, 99999)  
  55.           
  56.         Transform regions to the original position in the input image  
  57.         插入一个同质二维变换矩阵  
  58.         hom_mat2d_invert (HomMat2DImage, HomMat2DInvert)  
  59.         对范围進行任意的最簡二維變換  
  60.         affine_trans_region (DefectRegions, DefectRegionsTrans, HomMat2DInvert, 'false' 
  61.         接近圆形  
  62.         closing_circle (DefectRegionsTrans, DefectRegionsClosing, 1.5)  
  63.           
  64.         Display bright and dark defects using different colors  
  65.         dev_set_line_width (2)  
  66.         dev_set_draw ('fill' 
  67.         if (i 1)  
  68.             dev_set_color ('red' 
  69.         else  
  70.             dev_set_color ('orange' 
  71.         endif  
  72.         dev_display (DefectRegionsClosing)  
  73.           
  74.         Emphasize defect regions  
  75.         dev_set_color ('magenta' 
  76.         dev_set_draw ('margin' 
  77.         并集图像  
  78.         union1 (DefectRegionsClosing, RegionUnion)  
  79.         靠近的闭环图形  
  80.         closing_circle (RegionUnion, RegionClosing, 10)  
  81.         连接  
  82.         connection (RegionClosing, DefectRegionEnlarged)  
  83.         计算Region区域中的椭圆参数  
  84.         elliptic_axis (DefectRegionEnlarged, Ra, Rb, Phi)  
  85.         if (|Phi|)  
  86.             area_center (DefectRegionEnlarged, Area, RowEllipse, ColumnEllipse)  
  87.             转换椭圆  
  88.             gen_ellipse (Ellipse, RowEllipse, ColumnEllipse, Phi, Ra*2, Rb*2)  
  89.             dev_display (Ellipse)  
  90.         endif  
  91.     endfor  
  92.     dev_set_line_width (1)  
  93.     dev_set_color ('green' 
  94.     dev_display (ModelContoursTrans)  
  95.       
  96.     TimePrintInspect := EndSeconds-StartSeconds  
  97.     p_print_text (WindowHandle, 5, 10, 'Inspection Time: ' ((TimeObjSearch TimePrintInspect)*1000.0)$'.04' ms''green' 
  98. else  
  99.     p_print_text (WindowHandle, 5, 10, 'No shape model found!''red' 
  100. endif  
  101.   
  102. return ()  


 

  1. get_grayval_range (ModelImage, RegionROI, RegionForeground, RegionBackground, BackgroundGVModel, ForegroundGVModel)  
  2. 原图区域裁剪  
  3. reduce_domain (Image, RegionROI, ImageReduced)  
  4. 采用自动阀值分离背景  
  5. bin_threshold (ImageReduced, RegionBackground)  
  6. 计算两个区域的不同得出前景  
  7. difference (RegionROI, RegionBackground, RegionForeground)  
  8. 计算前景主次灰度值(计算灰度值的平均值和偏差)  
  9. intensity (RegionForeground, Image, ForegroundGVal, DeviationFG)  
  10. 计算背景主次灰度值  
  11. intensity (RegionBackground, Image, BackgroundGVal, DeviationBG)  
  12. return ()  


 


 

  1.   
  2. Segment label on the cap  
  3. 使用一个自动确定的阈值分割图像  
  4. bin_threshold (rImage, BrightRegion)  
  5. 计算两个范围的不同的范围(特定区域反选)  
  6. difference (rImage, BrightRegion, RegionDifference)  
  7. 膨胀边圈  
  8. dilation_circle (RegionDifference, RegionDilation, 1.5)  
  9. 连接  
  10. connection (RegionDilation, ConnectedRegions)  
  11. 选择形状  
  12. select_shape (ConnectedRegions, SelectedRegions, 'area''and'100, 99999)  
  13. 并集  
  14. union1 (SelectedRegions, LabelRegion)  
  15. 获取圆形区域的参数  
  16. smallest_circle (LabelRegion, Row, Column, Radius)  
  17. 获取矩形区域的参数  
  18. smallest_rectangle1 (LabelRegion, Row1, Column1, Row2, Column2)  
  19.   
  20. Define model region  
  21. 变换圆  
  22. gen_circle (Region, Row, Column, Radius)  
  23. 变换矩形  
  24. gen_rectangle1 (LabelRectangle, Row1, Column1, Row2, Column2)  
  25. 取两个范围相交范围  
  26. intersection (Region, LabelRectangle, RegionIntersection)  
  27. 膨胀一个圆的结构的  
  28. dilation_circle (RegionIntersection, RegionROI, 5.5)  
  29. reduce_domain (rImage, RegionROI, ImageReduced)  
  30. return ()  


 

  1.   

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有