感谢您阅读我的问题。
我是 Python 的新手,对 Scipy 很感兴趣。我正在尝试找出如何将 Scipy misc 中的浣熊图像转换为二进制图像(黑白)。这在 Scipy-lecture 教程中没有教授。
以下是目前我的代码:
我是 Python 的新手,对 Scipy 很感兴趣。我正在尝试找出如何将 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] and img[i,j] > upperthreshold
会是True
呢?这意味着80 > 100
! - Eric