I am trying to resize the input image to 736 x 736 as output size preserving the aspect ratio of the original image and add zero paddings while doing so.
The function image_resize_add_padding() works fine and is doing what I am trying to do. The resized image looks good while displaying using cv2.imshow() function
but while saving using cv2.imwrite() function it seems to be a fully black image.
How do I save the correct image as it was displayed?
import cv2
import numpy as np
def image_resize_add_padding(image, target_size):
ih, iw = target_size
h, w, _ = image.shape
scale = min(iw/w, ih/h)
nw, nh = int(scale * w), int(scale * h)
image_resized = cv2.resize(image, (nw, nh))
image_paded = np.full(shape=[ih, iw, 3], fill_value=128.0)
dw, dh = (iw - nw) // 2, (ih-nh) // 2
image_paded[dh:nh+dh, dw:nw+dw, :] = image_resized
image_paded = image_paded / 255.
return image_paded
input_size = 736
image_path = "test_image.jpg"
original_image = cv2.imread(image_path)
output_image = image_resize_add_padding(
np.copy(original_image), [input_size, input_size])
cv2.imshow('image', output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.imwrite('test_output.jpg', output_image)