谢谢你阅读我的问题。
我是python新手,对scipy很感兴趣。我正试图弄清楚如何将浣熊的图像(在scipy misc中)转换成二进制图像(黑白)。这不是在scipy讲座教程中教的。
到目前为止这是我的代码:
%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。还有办法确定吗?
不需要迭代图像数组的x和y位置。使用numpy数组检查数组是否高于感兴趣的阈值。下面是一些生成布尔(真/假)数组作为黑白图像的代码。
你想得太多了:
在
numpy
中,比较运算符应用于整个数组元素。注意,必须使用&
而不是and
来组合布尔值,因为python不允许numpy
重载and
如果其他人正在寻找一个简单的例子来进行实验,下面是我用来对图像进行二值化的方法:
输入:
输出:
相关问题 更多 >
编程相关推荐