• 计算横向和纵向的线条个数


    在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', [], [])

    以上是效果图,原图如下

    专注机器视觉,halcon c# c++联合开发
  • 相关阅读:
    机器学习决策树
    Apriori算法实战福彩双色球
    机器学习Random Forest算法简介
    机器学习CART决策树
    基本算法求最大子数组和 及其变种
    【语言处理与Python】3.6规范化文本
    【语言处理与Python】3.3使用Unicode进行文字处理
    【语言处理与Python】3.2字符串:最底层的文本处理
    【语言处理与Python】4.1回到基础
    【语言处理与Python】3.7用正则表达式为文本分词
  • 原文地址:https://www.cnblogs.com/linyugang/p/9225127.html
Copyright © 2020-2023  润新知