Skip to content

Instantly share code, notes, and snippets.

@realphongha
Last active September 14, 2023 04:50
Show Gist options
  • Save realphongha/d16665a101df74ed09b12b64c9106568 to your computer and use it in GitHub Desktop.
Save realphongha/d16665a101df74ed09b12b64c9106568 to your computer and use it in GitHub Desktop.
Images horizontal and 2d concat
import numpy as np
import cv2
def img_horizontal_concat(img1, img2, img_size=(240, 480)):
img1_resized = cv2.resize(img1, img_size)
img2_resized = cv2.resize(img2, img_size)
return np.concatenate((img1_resized, img2_resized), axis=1)
def img_2d_concat(images, input_shape):
i = 1
combined_h = None
combined_w = None
num_images = len(images)
while True:
if num_images <= i*(i-1):
combined_h = i-1
combined_w = i
break
if num_images <= i*i:
combined_h = i
combined_w = i
break
i += 1
images_draw = list()
for i in range(combined_h):
images_draw.append([np.zeros((*input_shape, 3), dtype=np.uint8)] * combined_w)
for i, image in enumerate(images):
h, w = i//combined_w, i%combined_w
images_draw[h][w] = image
combined_image = cv2.vconcat([cv2.hconcat(images_row) for images_row in images_draw])
return combined_image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment