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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM