【问题标题】:Image channel missing after resizing image with OpenCV使用 OpenCV 调整图像大小后图像通道丢失
【发布时间】:2021-09-30 18:46:11
【问题描述】:

调整大小后,二值图像的通道未显示。出于这个原因,我不得不使用 np.expand_dims 方法。任何解决我如何克服这种情况的方法,谢谢。

img = cv2.resize(img,(256, 256))
img = model.predict(np.expand_dims(img , axis=0))[0]

如果我在这里打印它显示的形状 (256,256,1)

但我需要调整它的大小以进行进一步处理。如果我只调整大小

img = cv2.resize(img ,(720,1280))

这里的形状变成(720,1280)#(通道号->这里缺少1)

如果我按照以下方式操作,它可以很好地处理形状 (720,1280,1)。但它降低了帧速率,导致我处理的视频变得迟钝!!

img = np.expand_dims(cv2.resize(img ,(720,1280)), axis=2)

【问题讨论】:

    标签: python numpy opencv image-processing


    【解决方案1】:

    所以cv2.resize() 所做的是,当它接收到至少有 2 个通道的图像时,它会保留额外的频带,但当它只有一个通道时,它会丢弃它。

    import numpy as np
    import cv2
    
    img1 = np.random.random((64, 64, 1)) * 255
    img2 = np.random.random((64, 64, 3)) * 255
    
    cv2.resize(img1.astype(np.uint8), (256, 256)).shape
    # Output (256, 256)
    cv2.resize(img2.astype(np.uint8), (256, 256)).shape
    # Output (256, 256, 3)
    

    我建议如果您始终只有一个波段/频道,请继续使用它,并根据您的模型进行预测,像这样传递它:

    img = cv2.resize(img,(256, 256))
    # So img.shape = (256, 256)
    img = model.predict(img[None, ...])[0]
    

    之后也是这样:

    img = cv2.resize(img ,(720,1280))[None, ...]
    

    【讨论】:

    • 应用 this , img = cv2.resize(img ,(720,1280))[None, ...] 在我的情况下显示形状 (1,720,1280) 但我认为它可以工作但任何技术像 (720,1280,1) 一样洗牌。
    • @MSI 因此,您可以使用img[:, :, None] 将其显示在最后一维
    猜你喜欢
    • 2020-08-20
    • 2012-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多