imshow灰色图像和二进制图像python

时间:2017-10-07 01:35:48

标签: python-2.7 numpy matplotlib computer-vision

我有灰度图像和二进制图像,我想用hstack并排绘制它们。看起来有一种调整可以让二进制变暗。有人遇到过这个问题吗?

enter image description here

这是我的代码

O = (self.img >= t) * 1
I = img
both = np.hstack((I, O))
imshow(both, cmap='gray')
show() 

2 个答案:

答案 0 :(得分:1)

这是为了证明与我不了解其数据的情况有所不同。我怀疑数组'O'中的所有值都是零,因此,该图以黑色窗格显示。

import numpy as np
import matplotlib.pyplot as plt

fig=plt.figure(figsize=(8, 4))

# make up some data for demo purposes
raw = np.random.randint(10, size=(6,6))
# apply some logic operatioin to the data
O = (raw >= 5) * 1   # get either 0 or 1 in the array
I = np.random.randint(10, size=(6,6))  # get 0-9 in the array

# plot each image ...
# ... side by side
fig.add_subplot(1, 2, 1)   # subplot one
plt.imshow(I, cmap=plt.cm.gray)

fig.add_subplot(1, 2, 2)   # subplot two
# my data is OK to use gray colormap (0:black, 1:white)
plt.imshow(O, cmap=plt.cm.gray)  # use appropriate colormap here
plt.show()

结果图片:

enter image description here

答案 1 :(得分:0)

问题的代码工作正常。

import matplotlib.pyplot as plt
import numpy as np

img = plt.imread("https://i.stack.imgur.com/oos05.png")[88:456,82:326]
t = 0.5

O = (img >= t) * 1
I = img
both = np.hstack((I, O))
plt.imshow(both, cmap='gray')
plt.show() 

enter image description here