感謝您閱讀我的問題。將圖像(np.array)轉換爲二進制圖像
我是python新手,對scipy感興趣。我試圖弄清楚如何將Racoon的圖像(在scipy misc中)製作成二進制圖像(黑色,白色)。這不是在scipy-lecture教程中教的。
這是到目前爲止我的代碼:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from scipy import misc #here is how you get the racoon image
face = misc.face()
image = misc.face(gray=True)
plt.imshow(image, cmap=plt.cm.gray)
print image.shape
def binary_racoon(image, lowerthreshold, upperthreshold):
img = image.copy()
shape = np.shape(img)
for i in range(shape[1]):
for j in range(shape[0]):
if img[i,j] < lowerthreshold and img[i,j] > upperthreshold:
#then assign black to the pixel
else:
#then assign white to the pixel
return img
convertedpicture = binary_racoon(image, 80, 100)
plt.imshow(convertedpicture, cmap=plt.cm.gist_gray)
我已經看到了OpenCV的使用使圖片二元其他人,但我想知道我是如何通過遍歷像素做到這樣?我不知道什麼值給上限和下限,所以我猜測80和100。還有一種方法可以確定這一點嗎?
爲什麼你會想到'lowerthreshold> IMG [I,J]和IMG [I,J]> upperthreshold'永遠是TRUE;?這意味着'80> 100'! – Eric