Skip to content

Instantly share code, notes, and snippets.

@jeremy-rutman
Last active December 19, 2019 12:58
Show Gist options
  • Select an option

  • Save jeremy-rutman/38f45908becf8a550222730ac7504f0f to your computer and use it in GitHub Desktop.

Select an option

Save jeremy-rutman/38f45908becf8a550222730ac7504f0f to your computer and use it in GitHub Desktop.
warp image arbitrarily (nonlinear)
import numpy as np
import cv2
def remap_test(img_arr):
H,W = img_arr.shape[0:2]
map = np.mgrid[0:H,0:W] # the map doesnt have to be same dims. as image size; it just declares what pixels in input image map to given output pixel
map_x = map[1].astype(np.float32)
map_y = map[0].astype(np.float32)
#linear warp
# for i in range(H):
# map_x[i,:]=np.arange(i,i+W,dtype=np.float32)
for i in range(H):
map_x[i,:]=map_x[i,:]+80*np.sin(2*3.14*float(i)/H)
print(map)
print(map_x,map_y)
img_warped = cv2.remap(img_arr,map_x,map_y,cv2.INTER_LINEAR)
cv2.imshow('post',img_warped)
cv2.waitKey(0)
img_arr = cv2.imread('images/cat.jpg')
img_arr = cv2.cvtColor(img_arr,cv2.COLOR_BGR2GRAY)
cv2.imshow('pre',img_arr)
cv2.waitKey(0)
remap_test(img_arr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment