我想要一个彩色图像并将其转换为二进制图像,其中接近黑色或白色返回False,并且所有中间值返回True。Python - 使用中间值转换为二进制彩色图像
以下两个条件同时施加的正确语法是什么?
binary = color.rgb2gray(img) > 0.05
binary = color.rgb2gray(img) < 0.95
如果我用这个:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from skimage import color
import requests
from PIL import Image
from StringIO import StringIO
url = 'https://mycarta.files.wordpress.com/2014/03/spectrogram_jet.png'
r = requests.get(url)
img = np.asarray(Image.open(StringIO(r.content)).convert('RGB'))
然后:
binary = color.rgb2gray(img) < 0.95
我会得到,我可以绘制一个合适的二进制图像:
fig = plt.figure(figsize=(10,10))
ax = fig.add_subplot(111)
plt.imshow(binary, cmap='gray')
ax.xaxis.set_ticks([])
ax.yaxis.set_ticks([])
plt.show()
同样与此:
color.rgb2gray(img) < 0.95
但是,如果我想他们这样在一起:
binary = color.rgb2gray(img) > 0.05 and color.rgb2gray(img) < 0.95
我得到这个消息:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
你跑什么代码“一起尝试”? –
与@ caenyon的回答建议一样:binary = color.rgb2gray(img)> 0.05 and color.rgb2gray(img)<0.95 – MyCarta
什么是img变量?一个完整的例子会更容易(导入等) – Felix