簡體   English   中英

使用python / PIL查找圖像組件

[英]Find Image components using python/PIL

在PIL / Pillow中是否有一個功能,對於灰度圖像,將圖像分成包含構成原始圖像的組件的子圖像? 例如,png灰度圖像,其中包含一組塊。 這里,圖像類型始終與背景形成高對比度。

我不想使用openCV,我只需要一些普通的blob檢測,並希望Pillow / PIL可能已經有了這樣做。

對的,這是可能的。 您可以在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:

在此輸入圖像描述

output.png:

在此輸入圖像描述

不使用PIL,但值得一看我想:我從一個圖像文件列表開始,我已經導入為numpy數組列表,我創建了一個布爾版本列表,其中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 = []

然后我使用label來識別不同的區域,使用8方向連接,而regionprops給了我一堆指標,例如大小和位置。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM