【问题标题】:imshow a gray image and a binary image pythonimshow 灰度图像和二进制图像 python
【发布时间】:2018-03-18 20:35:14
【问题描述】:

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

这是我的代码

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

【问题讨论】:

    标签: python-2.7 numpy matplotlib computer-vision


    【解决方案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()
    

    生成的图像:

    【讨论】:

      【解决方案2】:

      问题中的代码运行良好。

      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() 
      

      【讨论】:

      • 该代码既不适合提问者,也不适合我。请解释您的代码是如何工作的
      • @RahatZaman:此代码适用于我的 Python 3.7.2 和最新的 matplotlib。 img 在 [0,1] 范围内,所以 O 为 0 和 1 是正确的。
      • @RahatZaman 我想你误解了我的回答。我正在展示一个完整的可运行代码,它可以按原样工作。也就是说,问题中的代码确实有效;但当然这取决于输入图像。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 2015-12-07
      • 2013-12-16
      • 2015-02-25
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      相关资源
      最近更新 更多