Skip to content

Instantly share code, notes, and snippets.

@nvs-abhilash
Last active April 2, 2024 15:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nvs-abhilash/75a3920980fe32ffd4754bc205362125 to your computer and use it in GitHub Desktop.
Save nvs-abhilash/75a3920980fe32ffd4754bc205362125 to your computer and use it in GitHub Desktop.
Helper function to resize and rotate contours using OpenCV
import cv2
import numpy as np
def cart2pol(x, y):
theta = np.arctan2(y, x)
rho = np.hypot(x, y)
return theta, rho
def pol2cart(theta, rho):
x = rho * np.cos(theta)
y = rho * np.sin(theta)
return x, y
def rotate_contour(cnt, angle):
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cnt_norm = cnt - [cx, cy]
coordinates = cnt_norm[:, 0, :]
xs, ys = coordinates[:, 0], coordinates[:, 1]
thetas, rhos = cart2pol(xs, ys)
thetas = np.rad2deg(thetas)
thetas = (thetas + angle) % 360
thetas = np.deg2rad(thetas)
xs, ys = pol2cart(thetas, rhos)
cnt_norm[:, 0, 0] = xs
cnt_norm[:, 0, 1] = ys
cnt_rotated = cnt_norm + [cx, cy]
cnt_rotated = cnt_rotated.astype(np.int32)
return cnt_rotated
import cv2
import numpy as np
def scale_contour(cnt, scale):
M = cv2.moments(cnt)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cnt_norm = cnt - [cx, cy]
cnt_scaled = cnt_norm * scale
cnt_scaled = cnt_scaled + [cx, cy]
cnt_scaled = cnt_scaled.astype(np.int32)
return cnt_scaled
@Spawnfile
Copy link

Thank you for all good guide for rotating countours with openCV. Want to ask you about subtracting ( normalizing ) countour pixel coordinates. I did not get why exactly you did that, can you explain ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment