Last active
May 11, 2022 19:21
-
-
Save geofis/61aef66ac9f19bf6118860303dadaf00 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Generate tiles from image, then create a layered/stacked 3D perspective. | |
| # Modified from: https://stackoverflow.com/questions/56279466/how-do-i-create-a-figure-displaying-a-stack-of-images-at-an-angle | |
| # Modules | |
| import cv2 | |
| import numpy as np | |
| # Load source image | |
| im = cv2.imread('yearly_forest_loss_patches_1_ha_per_100_sq_km.jpg') | |
| im | |
| M = im.shape[0]//3 | |
| N = im.shape[1]//6 | |
| images = [im[x:x+M,y:y+N] for x in range(0,im.shape[0],M) for y in range(0,im.shape[1],N)] | |
| w, h = 500,1800 | |
| # Corners of images to be pasted onto background | |
| pts2 = np.float32([[0,0],[w,0],[0,w],[w,w]]) | |
| for i in range(18): | |
| img = np.zeros((w,w,3), np.uint8) | |
| img[:,:,:] = i*42, 255, 255 | |
| img = cv2.cvtColor(img, cv2.COLOR_HSV2BGR) | |
| images.append(img) | |
| # Create a background | |
| bg = np.zeros((w,h,3), np.uint8) | |
| # Define where to paste the images | |
| top, bottom, dx, width, middle, left_buffer = 100, 450, 50, 150, 300, 200 | |
| # top, bottom, dx, width, middle, left_buffer = 100, 500, 200, 200, 300, 50 | |
| # Create a mask | |
| mask = np.zeros((w,w), np.uint8) | |
| mask[:] = 255 | |
| bg_zeros = np.zeros_like(bg) | |
| for i in range(18): | |
| # Get the image | |
| img = images[i] | |
| # Compute where to paste the image | |
| left = left_buffer+dx*i | |
| right = left_buffer+dx*i + width | |
| mid = int((left + right)/2) | |
| pts1 = np.float32([[mid, top], [right, middle], | |
| [left, middle], [mid, bottom]]) | |
| # Warp the image | |
| M = cv2.getPerspectiveTransform(pts2,pts1) | |
| dst = cv2.warpPerspective(img,M,(h,w)) | |
| # Warp the mask and mask out the background | |
| cmask = cv2.warpPerspective(mask, M, (h,w)) | |
| bg = cv2.bitwise_and(bg,bg,mask = 255-cmask) | |
| # Add the image to the background | |
| bg += dst | |
| cv2.imshow('bg', bg) | |
| cv2.waitKey() | |
| cv2.destroyAllWindows() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
From this:
To this: