在ihalcon中遇到计算横向和纵向的线条个数的案例,http://www.ihalcon.com/read-9785.html
论坛当前网友使用的是图像增强的方式,下面使用另一种方式处理,代码较简单,就不加注释了,
使用原理为统计横向和纵向的灰度值分布并转化为region来处理
1 read_image (Image, '1.jpg') 2 rgb1_to_gray (Image, GrayImage) 3 gray_projections (GrayImage, GrayImage, 'simple', HorProjection, VertProjection) 4 gen_image_const(Image2, 'byte', |VertProjection|,512) 5 6 gen_region_points(Region, VertProjection+200, [0:|VertProjection|-1]) 7 gen_region_line(RegionLines, 0,0,0,|VertProjection|-1) 8 union2(Region,RegionLines, RegionUnion) 9 closing_rectangle1(RegionUnion, RegionClosing, 1,500) 10 opening_rectangle1 (RegionClosing, RegionOpening, 10, 10) 11 dilation_circle (RegionOpening, RegionDilation, 5.5) 12 13 difference (RegionClosing, RegionDilation, RegionDifference) 14 connection (RegionDifference, ConnectedRegions) 15 16 select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 10, 99999) 17 count_obj (SelectedRegions, Number) 18 19 20 gen_image_const (Image3, 'byte', 512, |HorProjection|) 21 22 gen_region_points (Region1, [0:|HorProjection|-1], HorProjection+200) 23 gen_region_line (RegionLines1, 0, 0, |HorProjection|-1, 0) 24 union2 (Region1, RegionLines1, RegionUnion1) 25 closing_rectangle1 (RegionUnion1, RegionClosing1, 500, 1) 26 opening_rectangle1 (RegionClosing1, RegionOpening1, 10, 10) 27 dilation_circle (RegionOpening1, RegionDilation1, 2.5) 28 29 difference (RegionClosing1, RegionDilation1, RegionDifference1) 30 31 connection (RegionDifference1, ConnectedRegions1) 32 select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 10, 99999) 33 34 count_obj (SelectedRegions1, Number1) 35 dev_set_draw ('margin') 36 dev_display (GrayImage) 37 dev_display (SelectedRegions) 38 dev_display (SelectedRegions1) 39 dev_disp_text ('竖向条纹:'+Number+' 横向条纹:'+Number1, 'window', 12, 12, 'black', [], [])
以上是效果图,原图如下