使用 OpenCV 调整图像大小后图像通道丢失
Image channel missing after resizing image with OpenCV
调整大小后,二值图像的通道不显示。出于这个原因,我不得不使用 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)
所以 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, ...]
调整大小后,二值图像的通道不显示。出于这个原因,我不得不使用 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)
所以 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, ...]