2016-09-07 97 views
1

在PIL/Pillow中是否有一個函數用於灰度圖像,將圖像分成包含組成原始圖像的組件的子圖像?例如,一個png灰度圖像,其中有一組塊。這裏,圖像類型總是與背景具有高對比度。使用python/PIL查找圖像組件

我不想使用openCV,我只需要一些常規的斑點檢測,並希望Pillow/PIL可能有這樣做的事情。

+0

那你到底想幹什麼?你的圖片是什麼樣的?你需要提供一些額外的信息,理想情況下,你已經嘗試了一些代碼示例 – meetaig

+0

我特別問PIL/Pillow中是否有函數可以執行此操作。我已經明確表示我沒有工作代碼。 – user1658296

+1

那麼沒有神奇的'do_what_i_want()'函數。你需要詳細說明。一般來說,如果你說你有很好的對比度,那麼從閾值開始。像floodfill這樣的標籤算法可以識別不同的斑點,然後你可以計算邊界框。 – meetaig

回答

-1

是的,這是可能的。您可以在PIL中使用edge檢測算法。 示例代碼:

from PIL import Image, ImageFilter 
image = Image.open('/tmp/sample.png').convert('RGB') 
image = image.filter(ImageFilter.FIND_EDGES) 
image.save('/tmp/output.png') 

sample.png:

enter image description here

output.png:

enter image description here

+1

何時可以提取矩形,當有更多?你如何識別找到矩形的區域? – meetaig

0

不使用PIL,但值得一看,我認爲: 我從我導入的圖像文件列表開始,列表爲numpy arr AYS,我創建布爾版本的列表,其中threshold> 0

from skimage.measure import label, regionprops 
import numpy as np 

bool_array_list= [] 

for image in image_files: 
    bool_array = np.copy(image) 
    bool_array[np.where(bool_array > 0)] = 1 
    bool_array_list.append(bool_array) 

img_region_list = [] 

然後我使用標籤來標識不同的區域,採用8方向的連接,並regionprops給了我一堆指標,如大小和位置。

for item in bool_array_list: 
    tmp_region_list = regionprops(label(item, 
             connectivity=2 
             ) 
           ) 
    img_region_list.append(tmp_region_list)