2017-03-22 137 views
0

我一直在使用https://gist.github.com/chsasank/4d8f68caf01f041a6453e67fb30f8f5a進行圖像處理。這需要一個只有一個通道的圖像(請參閱assert len(image.shape) == 2)如何調整代碼使其適用於彩色圖像 - 因此具有三個通道的圖像?如何適應彩色圖像灰度圖像的算法?

+0

這個問題有什麼不好? – Make42

+0

它可能取決於所選的算法。您可以將算法分別應用於每個顏色通道然後以某種方式合併,之前您可以將圖像灰度化,也可以在之前將顏色轉換爲不同的顏色空間或完全不同的顏色空間。 – Trilarion

回答

1

最簡單的方法是將圖像分成多個通道,然後在重新映射後合併回來。

# make sure it's not gray 
assert len(image.shape) == 3 

# grab the image resolution 
shape = image.shape[:2] 

# random_state, gaussian and meshgrid 
. 
. 
. 

#calculate indices just as before 
indices = np.reshape(x+dx, (-1, 1)), np.reshape(y+dy, (-1, 1)) 

# split the image into 3 channels 
b, g, r = cv2.split(image) 

# do the mapping on all of the channels separately 
b = map_coordinates(b, indices, order=1).reshape(shape) 
g = map_coordinates(g, indices, order=1).reshape(shape) 
r = map_coordinates(r, indices, order=1).reshape(shape) 

# return merged result 
return cv2.merge((b,g,r)) 
+0

你確定'shape = image.shape [:1]'是正確的嗎? – Make42

+0

@ Make42謝謝你,當然我的意思是[:2] – m3h0w

相關問題