I have a np.array with grayscale images and I want to apply a colormap, then save the result into a video file.
With this code (or with the commented line) I get a grayscale video anyways. Any idea why I can't have a colormap video?
color_images = np.empty([N, resolution[1], resolution[0], 3])
for i in range(0, N):
cv2.imwrite(gray_images[i])
color_images[i] = cv2.cvtColor(np.float32(gray_images[i]), cv2.COLOR_GRAY2BGR)
#color_images[i] = cv2.merge([gray_images[i], gray_images[i], gray_images[i]])
out = cv2.VideoWriter("video.mp4", cv2.VideoWriter_fourcc(*'mp4v'),
fps, (resolution[0], resolution[1]), 1)
for i in range(0, N):
out.write(np.uint8(color_images[i]))
out.release()
UPDATE: I want to have a colored image so that differences in pixel intensity can be more noticeable. (For instance use the default cmap in plt.imshow ('viridis')).
cvtColor
doesn't do that. For any grayscale pixel, it gives you the RGB/BGR pixel that looks gray with the same intensity.
If you want colormaps, you need applyColorMap
.
import numpy as np
import cv2 as cv
gray_image = ... # get it from somewhere
colormapped_image = cv.applyColorMap(gray_image, cv.COLORMAP_JET)