Remap_Demo.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from __future__ import print_function
  2. import cv2 as cv
  3. import numpy as np
  4. import argparse
  5. ## [Update]
  6. def update_map(ind, map_x, map_y):
  7. if ind == 0:
  8. for i in range(map_x.shape[0]):
  9. for j in range(map_x.shape[1]):
  10. if j > map_x.shape[1]*0.25 and j < map_x.shape[1]*0.75 and i > map_x.shape[0]*0.25 and i < map_x.shape[0]*0.75:
  11. map_x[i,j] = 2 * (j-map_x.shape[1]*0.25) + 0.5
  12. map_y[i,j] = 2 * (i-map_y.shape[0]*0.25) + 0.5
  13. else:
  14. map_x[i,j] = 0
  15. map_y[i,j] = 0
  16. elif ind == 1:
  17. for i in range(map_x.shape[0]):
  18. map_x[i,:] = [x for x in range(map_x.shape[1])]
  19. for j in range(map_y.shape[1]):
  20. map_y[:,j] = [map_y.shape[0]-y for y in range(map_y.shape[0])]
  21. elif ind == 2:
  22. for i in range(map_x.shape[0]):
  23. map_x[i,:] = [map_x.shape[1]-x for x in range(map_x.shape[1])]
  24. for j in range(map_y.shape[1]):
  25. map_y[:,j] = [y for y in range(map_y.shape[0])]
  26. elif ind == 3:
  27. for i in range(map_x.shape[0]):
  28. map_x[i,:] = [map_x.shape[1]-x for x in range(map_x.shape[1])]
  29. for j in range(map_y.shape[1]):
  30. map_y[:,j] = [map_y.shape[0]-y for y in range(map_y.shape[0])]
  31. ## [Update]
  32. parser = argparse.ArgumentParser(description='Code for Remapping tutorial.')
  33. parser.add_argument('--input', help='Path to input image.', default='chicky_512.png')
  34. args = parser.parse_args()
  35. ## [Load]
  36. src = cv.imread(cv.samples.findFile(args.input), cv.IMREAD_COLOR)
  37. if src is None:
  38. print('Could not open or find the image: ', args.input)
  39. exit(0)
  40. ## [Load]
  41. ## [Create]
  42. map_x = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)
  43. map_y = np.zeros((src.shape[0], src.shape[1]), dtype=np.float32)
  44. ## [Create]
  45. ## [Window]
  46. window_name = 'Remap demo'
  47. cv.namedWindow(window_name)
  48. ## [Window]
  49. ## [Loop]
  50. ind = 0
  51. while True:
  52. update_map(ind, map_x, map_y)
  53. ind = (ind + 1) % 4
  54. dst = cv.remap(src, map_x, map_y, cv.INTER_LINEAR)
  55. cv.imshow(window_name, dst)
  56. c = cv.waitKey(1000)
  57. if c == 27:
  58. break
  59. ## [Loop]