I have an image with shape (120, 470, 4) and I want to resize it to another size eg (100, 400, 4). I try to achieve it by using cv2.resize
function as bellow
reshaped_image = cv2.resize(image, (100, 400))
and paste the reshaped_image inside a bigger transparent image, but the problem is reshaped_image has the shape (100, 400, 3) instead of (100, 400, 4).
Is there something wrong I'm doing or the cv2.resize
function will lose transparency information? And at last I'll be pleased to know about your solution. (with simple example if possible)
regards
Here is an example which loads a 3 channel image, takes its blue channel and copies it as alpha channel (which makes no sense at all, but I had no 4 channel image at hand) and merges it again as a 4 channel image. Then this image is resized.
If you load a 4 channel image, the flag -1 indicates that the image is loaded unchanged, so you can load and split all 4 channels directly.
Shapes of all images are printed, and resizing works. I am working opencv version 3.1.0.
Hope that helps.
import cv2
import numpy as np
img = cv2.imread('image.jpg', -1)
print img.shape
b_channel, g_channel, r_channel = cv2.split(img)
alpha_channel = b_channel
print b_channel.shape
print g_channel.shape
print r_channel.shape
print alpha_channel.shape
img_RGBA = cv2.merge((b_channel, g_channel, r_channel, alpha_channel))
print img_RGBA.shape
res = cv2.resize(img_RGBA,(100, 400), interpolation = cv2.INTER_CUBIC)
print res.shape
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.