• python图片处理(三)


    ji那天用到了python图片处理中的二值图像的骨架提取,在matlab中通过输入图像的二值,来处理得到图像的骨架,

    skelimage = bwmorph(im, 'skel', inf);
    在matlab中bwmorph的函数功能: 对二值图像进行数学形态学(Mathematical Morphology)运算。
    语法格式:
    BW2 = bwmorph(BW,operation)
    对二值图像进行指定的形态学处理。
    BW2 = bwmorph(BW,operation,n)
    对二值图像进行n次指定的形态学处理。 n可以是Inf(无穷大), 这意味着将一直对该图像做同样的形态学处理直到图像不再发生变化。
    其中operation有多种,可以是:
    'bothat':进行“bottom hat”形态学运算,即返回源图像减去闭运算的图像;
    'branchpoints':找到骨架中的分支点;
    'bridge':进行像素连接操作;
    'clean':去除图像中孤立的亮点,比如, 一个像素点, 像素值为1, 其周围像素的像素值全为0, 则这个孤立的亮点将被去除;
    'close':进行形态学闭运算(即先膨胀后腐蚀);
    'diag': 采用对角线填充, 去除八邻域的背景;
    'dilate': 使用结构元素ones(3)对图像进行膨胀运算;
    'endpoints':找到骨架中的结束点;
    'erode':使用结构元素ones(3)对图像进行腐蚀运算;
    'fill':填充孤立的黑点, 比如3*3的矩阵, 除了中间元素为0外, 其余元素全部为1, 则这个0将被填充为1;
    'hbreak':断开图像中的H型连接;
    'majority':如果一个像素的8邻域中有等于或超过5个像素点的像素值为1, 则将该点像素值置1;
    'open':进行形态学开运算(即先腐蚀后膨胀);
    'remove':如果一个像素点的4邻域都为1, 则该像素点将被置0;该选项将导致边界像素上的1被保留下来;
    'skel':在这里n = Inf,骨架提取但保持图像中物体不发生断裂;不改变图像欧拉数;
    'spur':去除小的分支, 或引用电学术语“毛刺”;
    'thicken':在这里n = Inf, 通过在边界上添加像素达到加粗物体轮廓的目的;
    'thin':在这里n = Inf,进行细化操作;
    'tophat':进行“top hat”形态学运算, 返回源图像减去开运算的图像;
    然后在python中,skdimage模块提供了两种方法可以获得图像的骨架,分别是Skeletonize()函数和medial_axis()函数。
    二值图的骨架提取,在博客中有详细的介绍,关于图片的连通性博客中有详细的介绍。
    用Skeletonize
    #-*-coding:utf-8-*-
    import os
    from skimage import morphology,draw
    import numpy as np
    import matplotlib.pyplot as plt
    from skimage import io,data,color
    from skimage import measure
    
    path = 'timg.jpg'
    img = io.imread(path)
    print(img.shape)
    row,col = img.shape[:2] 
    mmap = np.zeros([row,col])
    #因为图像是三维的所以在这块取第一维
    for i in range(row):
        for j in range(col):
            mmap[i,j] = img[i,j,0]
    mmap = (mmap < 0.5) * 1  #图像二值化
    img2 = morphology.skeletonize(mmap) #图像的二值化骨架提取
    fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2, figsize=(8, 4))
    ax1.imshow(img, cmap=plt.cm.gray)
    ax1.axis('off')
    ax1.set_title('img', fontsize=20)
    ax2.imshow(img2, cmap=plt.cm.gray)
    ax2.axis('off')
    ax2.set_title('img2', fontsize=20)
    fig.tight_layout()
    plt.show()

    图片结果:

    在matlab中有求骨架顶点的

    outEndPoints = BOHitOrMiss(skelimage, 'end')

    在python中没有找到相应的求骨架顶点的函数。
    BOHitOrMiss这个函数是编写的,并不是库函数。
    BOHitOrMiss.m
    function out = BOHitOrMiss ( im, method )
    %%  BOHitOrMiss - end and triple points detection
    %   
    %   REFERENCE:
    %       Cecilia Di Ruberto, 
    %       Recognition of shapes by attributed skeletal graphs, 
    %       Pattern Recognition 37, 21-31, 2004
    %   
    %   HELP:                                       
    %       Matlab's interpretation of the algorithm
    %       1 ->  1; 0 -> -1; * ->  0;
    %
    %   INPUT:
    %       im      - binary image.
    %       method  - 'end' or 'triple'.
    %
    %   OUTPUT:
    %       out     - image with detected points
    %
    %   USAGE:
    %        % Skeleton
    %        sk = bwmorph(im, 'thin', inf);
    %        % Hit or Miss
    %        out1 = BOHitOrMiss(sk, 'end');
    %        out2 = BOHitOrMiss(sk, 'triple');
    % 
    %   AUTHOR:
    %       Boguslaw Obara, http://boguslawobara.net/
    %
    %   VERSION:
    %       0.1 - 16/07/2008 First implementation
    
    %% SE - endpoints 
    if strcmp(method, 'end')
    se(:,:,1) = [   -1  1 -1   ;...
                    -1  1 -1   ;...
                    -1 -1 -1   ];
                
    se(:,:,2) = [   -1 -1  1   ;...
                    -1  1 -1   ;...
                    -1 -1 -1   ];
                
    se(:,:,3) = [   -1 -1 -1   ;...
                    -1  1  1   ;...
                    -1 -1 -1   ];
                
    se(:,:,4) = [   -1 -1 -1   ;...
                    -1  1 -1   ;...
                    -1 -1  1   ];
                
    se(:,:,5) = [   -1 -1 -1   ;...
                    -1  1 -1   ;...
                    -1  1 -1   ];
                
    se(:,:,6) = [   -1 -1 -1   ;...
                    -1  1 -1   ;...
                     1 -1 -1   ];
                
    se(:,:,7) = [   -1 -1 -1   ;...
                     1  1 -1   ;...
                    -1 -1 -1   ];
                
    se(:,:,8) = [    1 -1 -1   ;...
                    -1  1 -1   ;...
                    -1 -1 -1   ];
    %% SE - triple points (junctions)
    elseif strcmp(method, 'triple')
    se(:,:,1) = [   -1  1 -1   ;...
                     1  1  1   ;...
                    -1 -1 -1   ];
    
    se(:,:,2) = [    1 -1  1   ;...
                    -1  1 -1   ;...
                    -1 -1  1   ];
    
    se(:,:,3) = [   -1  1 -1   ;...
                    -1  1  1   ;...
                    -1  1 -1   ];
    
    se(:,:,4) = [   -1 -1  1   ;...
                    -1  1 -1   ;...
                     1 -1  1   ];
    
    se(:,:,5) = [   -1 -1 -1   ;...
                     1  1  1   ;...
                    -1  1 -1   ];
    
    se(:,:,6) = [    1 -1 -1   ;...
                    -1  1 -1   ;...
                     1 -1  1   ];
    
    se(:,:,7) = [   -1  1 -1   ;...
                     1  1 -1   ;...
                    -1  1 -1   ];
    
    se(:,:,8) = [    1 -1  1   ;...
                    -1  1 -1   ;...
                     1 -1 -1   ];
    
    se(:,:,9) = [   -1  1 -1   ;...
                    -1  1  1   ;...
                     1 -1 -1   ];
    
    se(:,:,10)= [   -1 -1  1   ;...
                     1  1 -1   ;...
                    -1 -1  1   ];
    
    se(:,:,11)= [    1 -1 -1   ;...
                    -1  1  1   ;...
                    -1  1 -1   ];
    
    se(:,:,12)= [   -1  1 -1   ;...
                    -1  1 -1   ;...
                     1 -1  1   ];
    
    se(:,:,13)= [   -1 -1  1   ;...
                     1  1 -1   ;...
                    -1  1 -1   ];
    
    se(:,:,14)= [    1 -1 -1   ;...
                    -1  1  1   ;...
                     1 -1 -1   ];
    
    se(:,:,15)= [   -1  1 -1   ;...
                     1  1 -1   ;...
                    -1 -1  1   ];
    
    se(:,:,16)= [    1 -1  1   ;...
                    -1  1 -1   ;...
                    -1  1 -1   ];
    end
    %%  Hit or Miss
    out = zeros(size(im));
    if strcmp(method, 'end') || strcmp(method, 'triple')
        for i=1:size(se,3)
            hom = bwhitmiss(im, se(:,:,i));
            out = max(out, hom);
        end
    end
    %% 
    end
  • 相关阅读:
    ADODB.Stream
    今天发现一个好玩的翻译接口
    有趣的对角线公式
    jboss 虚拟路径
    省市区拆分字符串
    jboss-eap-6.2修改端口号
    把excel、txt当数据库来查询
    TS流PAT/PMT详解
    iOS/iPhone 程序文件目录结构以及启动流程
    onvif规范的实现:onvif开发常用调试方法 和常见的segmentation fault错误
  • 原文地址:https://www.cnblogs.com/chenyang920/p/8254065.html
Copyright © 2020-2023  润新知