簡體   English   中英

在python中使用相應的蒙版進行圖像分割

[英]Image segmentation using corresponding masks in python

我要分割的圖像具有相應的蒙版。

我將圖像放在一個文件夾中,並將它們對應的遮罩放在另一個文件夾中。 我正在嘗試應用這些蒙版或使用python中的兩個for循環將它們乘以圖像,以獲取分段圖像。

我正在使用以下代碼:

def ImageSegmentation():

SegmentedImages = []

for img_path in os.listdir('C:/Users/mab/Desktop/images/'):
    img=io.imread('C:/Users/mab/Desktop/data/'+img_path)
    for img_path2 in os.listdir('C:/Users/mab/Desktop/masks/'):
        Mask = io.imread('C:/Users/mab/Desktop/masks/'+img_path2)

        [indx, indy] = np.where(Mask==0)
        Color_Masked = img.copy()
        Color_Masked[indx,indy] = 0

        matplotlib.image.imsave('C:/Users/mab/Desktop/SegmentedImages/'+img_path2,Color_Masked)
        segs.append(Color_Masked)
 return np.vstack(Color_Masked)

當我為單個圖像和單個蒙版(沒有文件夾和循環)嘗試此代碼時,此代碼有效。

但是,當我嘗試遍歷兩個文件夾中的圖像和蒙版時,我得到的輸出圖像被錯誤的蒙版(而不是其對應的蒙版)分割。

我無法單獨分割每個圖像而不會循環播放,因為我有500多個圖像及其遮罩。

我不知道我在此代碼中缺少什么或放置了什么錯誤,我該如何解決? 另外,有沒有更簡單的方法來獲取分割的圖像?

除非我被嚴重誤解,否則您只需要以下內容:

import glob

filelist = glob.glob('C:/Users/mab/Desktop/images/*.png') 
for i in filelist:
    mask = i.replace("images","masks")
    print(i,mask)

在我的iMac上,這種事情會產生:

/Users/mark/StackOverflow/images/b.png /Users/mark/StackOverflow/masks/b.png
/Users/mark/StackOverflow/images/a.png /Users/mark/StackOverflow/masks/a.png

暫無
暫無

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

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