Reputation: 125
After resizing, the channel of the binary image is not showing. For this reason I had to use np.expand_dims method. Any solution to how I can overcome this situation, Thanks.
img = cv2.resize(img,(256, 256))
img = model.predict(np.expand_dims(img , axis=0))[0]
If I print the shape here it shows (256,256,1)
But I need to resize it for further process. If I only resize by
img = cv2.resize(img ,(720,1280))
Here the shape becomes (720,1280) # (channel number -> 1 is missing here)
If I do as below it works fine with shape (720,1280,1). But it's slowing down the frame rate as a result my processed video becoming laggy!!
img = np.expand_dims(cv2.resize(img ,(720,1280)), axis=2)
Upvotes: 3
Views: 3326
Reputation: 5000
So what cv2.resize()
does is when it receives an image with at least 2 channels, it keeps the extra band, but when it is only one channel, it drops it.
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)
I would suggest if you are always having a single band/ channel, keep using that and upon prediction on your model, pass it like this:
img = cv2.resize(img,(256, 256))
# So img.shape = (256, 256)
img = model.predict(img[None, ...])[0]
And same after that:
img = cv2.resize(img ,(720,1280))[None, ...]
Upvotes: 4