Advertisement
dan-masek

Simple zoom via warpAffine.

Aug 16th, 2024
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3.  
  4. img = cv2.imread('lena3.png', cv2.IMREAD_COLOR)
  5. h, w = img.shape[:2]
  6.  
  7. scale = 1.0
  8.  
  9. while(True):
  10.     scale *= 1.001
  11.    
  12.     # Prepare transformation matrices
  13.     # Translate to have origin at the center of the image
  14.     M_translate_0 = np.array([[1,0,-w/2],[0,1,-h/2],[0,0,1]], dtype=np.float32)
  15.     # Scale around the origin
  16.     M_scale = np.array([[scale,0,0],[0,scale,0],[0,0,1]], dtype=np.float32)
  17.     # Translate back to have origin at the top-left corner again
  18.     M_translate_1 = np.array([[1,0,w/2],[0,1,h/2],[0,0,1]], dtype=np.float32)
  19.  
  20.     # Calculate the composite transformation matrix
  21.     M = np.matmul(M_translate_1, np.matmul(M_scale, M_translate_0))
  22.    
  23.     # Apply transformation (dropping the last row of the matrix, since it's not needed here)
  24.     result = cv2.warpAffine(img, M[:2,], (w, h))
  25.  
  26.     cv2.imshow('', result)
  27.     cv2.waitKey(20)
  28.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement