2017-07-16 54 views
1

我一直在學習Canny邊緣檢測函數,Hough變換和蒙版以選擇圖像中特定的一組邊緣。應用Canny功能時,您是否可以首先應用遮罩?

我只是想知道 - 我見過人們首先應用這兩個功能,然後使用面膜。首先應用面膜會提高性能嗎?

當然,如果您只在遮罩區域應用Canny函數和Hough變換,將會比將其應用於整個區域然後拾取遮罩區域更快。但也許我是誤解。

我不確定它是否相關,但我正在使用Python和OpenCV庫。我知道它的功能不能在圖像的一個子集上運行。但我想明白爲什麼會這樣。

+0

hiw你會先應用面膜嗎?如果你在Canny輸入中使所有不被蒙版的像素變成黑色,你最有可能在被蒙版的邊框處出現許多邊緣。可能或可能不是你想要的... – Micka

回答

3

是的,你可以先應用面具,但這會給予嚴重的低於標準的結果。

例如,請考慮下面的代碼:

import numpy as np 
import matplotlib.pyplot as plt 
import scipy 
from skimage import feature 

# Create image 
image = scipy.misc.face(gray=True) 
plt.figure() 
plt.imshow(image, cmap='gray') 
plt.title('image') 

# Create a simple mask 
x, y = np.mgrid[:image.shape[0], :image.shape[1]] 
mask = (x > 200) & (x < 500) & (y > 300) & (y < 700) 
plt.figure() 
plt.imshow(image * mask, cmap='gray') 
plt.title('masked image') 

# Find edges with both methods 
edges1 = feature.canny(image, sigma=3) 
edges1 *= mask 

plt.figure() 
plt.imshow(edges1, cmap='gray') 
plt.title('Mask then find edges') 

masked_image = image * mask 
edges2 = feature.canny(masked_image, sigma=3) 

plt.figure() 
plt.imshow(edges2, cmap='gray') 
plt.title('Find edges then mask') 

其中給出以下結果:

enter image description here enter image description here enter image description here enter image description here

注意怎麼樣,如果你申請的邊緣,然後面膜探測器,你會得到這個奇怪的框架。這是因爲遮罩產生了一些從一開始就不存在的新邊緣。

+0

我明白了 - 所以如果你想嘗試填充它,你會得到一個紅色的盒子,基本上是由於這個'框架'? –

+1

是的,那就是你會得到的。這就是說,如果你的面具是矩形的,你可以提取圖像的子集並單獨應用邊緣檢測器。 –

+1

巨大的答案,非常感謝! –

相關問題