Home > Net >  Stitching sub images to reconstruct original image
Stitching sub images to reconstruct original image

Time:11-23

Assume that we have an image which splitted into 4 sub images vertically. After split, we shuffle the sub images. How can I get original image from sub images? OR, how we can correctly sort the sub images to get original one even we do not know the order? enter image description here

CodePudding user response:

Depending how you split your image, one solution is saving the original index/location with the image in a tuple. Once they get shuffled, you can then sort the list of images based off this index getting back the original order. Then use hconcat to stitch them together.

import cv2
img = cv2.imread('img.png')
img_list = []
# split image in half
img_B = img_list.append((1, img[:, img.shape[1]//2:]))
img_A = img_list.append((0, img[:, :img.shape[1]//2]))
# img_list is in order 1, 0
sorted_list = sorted(img_list, key=lambda x: x[0])
# sorted to correct order of 0, 1

result = cv2.hconcat([sorted_list[0][1], sorted_list[1][1]])  

CodePudding user response:

Assuming that you know the order of the image splits, you could join them back using hconcat()

# horizontally concatenates images
# of same height 
im_h = cv2.hconcat([imgA, imgB])    

# show the output image
cv2.imshow('man_image.jpeg', im_h)
  • Related