簡體   English   中英

通過閾值處理提取前景圖像作為掩碼

[英]Extracting foreground image as mask by thresholding

我正在尋找一種強大的方法來從背景中有一些噪音的圖像中提取前景。

所以,我想用它的圖像是:

圖像有一些背景噪音

我的嘗試是使用Otsu thresholding 我在Python中做到了如下:

from skimage.filter import threshold_otsu
import os.path as path
import matplotlib.pyplot as plt

img = io.imread(path.expanduser('~/Desktop/62.jpg'))
r_t = threshold_otsu(img[:, :, 0])
g_t = threshold_otsu(img[:, :, 1])
b_t = threshold_otsu(img[:, :, 2])

m = np.zeros((img.shape[0], img.shape[1]), dtype=np.uint8)
mask = (img[:, :, 0] < r_t) & (img[:, :, 1] < g_t) & (img[:, :, 2] < b_t)
m[~mask] = 255

plt.imshow(m)
plt.show()

這使得R,G,B閾值為(62 67 64),這有點高。 結果是:

大津的結果

此圖像也是Otsu thresholding處理效果最好的圖像之一。 如果我使用值為30的手動閾值,它可以很好地工作。 結果是:

手冊

我想知道是否還有其他方法我應該嘗試。 分割確實不是我的專業領域,我開箱即用的東西似乎有限。

你的形象看起來不是很豐富多彩 因此,您可以對灰度值執行分割,而不是分別對每種顏色執行分割,然后組合三個蒙版。

查看包scikit-image.filter還有其他幾種閾值方法。 我嘗試了所有這些並發現threshold_isodata執行得非常好,提供與您想要的圖像幾乎相同的圖像。 因此我推薦使用isodata算法。

例:

import numpy as np
import skimage.io as io
import skimage.filter as filter
import matplotlib.pyplot as plt

img = io.imread('62.jpg')
gray = np.sum(img, axis=2) # summed up over red, green, blue
#threshold = filter.threshold_otsu(gray) # delivers very high threshold
threshold = filter.threshold_isodata(gray) # works extremely well
#threshold = filter.threshold_yen(gray) # delivers even higher threshold
print(threshold)

plt.imshow(gray > threshold)
plt.show()

得到:

在此輸入圖像描述

暫無
暫無

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

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