Thursday, 25 March 2021

improve image quality in video with opencv videowriter python

I am trying to create video from the images which are read from the binary log file. However, the output video is low but the output file properties looks good with file size and same settings for the image size, fps and duration. Here is my code.

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
writer = cv2.VideoWriter(output_file_video, fourcc, 10.0, (912, 496), isColor=False)
for frame_index in range(0, 99)):
   # processing of file and extract image for each frame index
   img = np.reshape(ii, (frame_info.height, frame_info.width))
   img = (img / 16).astype('uint8') # convert to uint8

   if frame_info.width != video_resolution_width or frame_info.height != video_resolution_height:
       img = cv2.resize(img, (video_resolution_width, video_resolution_height))
   writer.write(img)

cv2.destroyAllWindows()
writer.release()

Currently the img size before resize is

img.shape

Out[3]: (992, 1824)

I hope I am not doing anything wrong with the image size settings.

Here is the data(buffer) https://www.filehosting.org/file/details/931364/data1.out

I created the file using the below command:

data.astype('int16').tofile(r"data1.out")

Once I have the data I perform the following steps to get img since the data represents as 12 bits per pixel.

fst_uint8, mid_uint8, lst_uint8 = np.reshape(data, (data.shape[0] // 3, 3)).astype(np.uint16).T
fst_uint12 = (fst_uint8 << 4) + (mid_uint8 >> 4)
snd_uint12 = ((mid_uint8 % 16) << 8) + lst_uint8
ii = np.reshape(np.concatenate((fst_uint12[:, None], snd_uint12[:, None]), axis=1), 2 * fst_uint12.shape[0])
img = np.reshape(ii, (frame_info.height, frame_info.width))


from improve image quality in video with opencv videowriter python

No comments:

Post a Comment