-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistortion.py
51 lines (41 loc) · 1.46 KB
/
distortion.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import cv2
import numpy as np
import matplotlib.pyplot as plt
def conrad_brown_distortion(img, k1, k2, k3):
"""
Apply Conrad Brown distortion model to an image.
Args:
- img: Input image (numpy array).
- k1: Distortion coefficient k1.
- k2: Distortion coefficient k2.
- k3: Distortion coefficient k3.
Returns:
- distortion_profile: Distortion profile at different radial distances.
"""
h, w = img.shape[:2]
cx, cy = w // 2, h // 2
x, y = np.meshgrid(np.arange(w), np.arange(h))
# Calculate radial distance from center
radial_distance = np.sqrt((x - cx)**2 + (y - cy)**2)
# Normalize radial distance
normalized_distance = radial_distance / np.sqrt(cx**2 + cy**2)
# Apply distortion model - 3 parameter distortion model
distortion = k1 * normalized_distance**2 + k2 * normalized_distance**4 + k3 * normalized_distance**6
return distortion
# Load image
filename = "checkerboard_dots_squares"
image_path = r"C:\Users\rowan\Documents\masters\sw_comparison\lens_distortion_model\source_images\{:s}.png".format(filename)
original_img = cv2.imread(image_path)
# Distortion coefficients
k1 = 0.001
k2 = 0.01
k3 = 0.001
# Apply distortion
distortion_profile = conrad_brown_distortion(original_img, k1, k2, k3)
# Plot the distortion profile
plt.plot(distortion_profile)
plt.xlabel('Radial Distance')
plt.ylabel('Distortion Amount')
plt.title('Distortion Profile')
plt.grid(True)
plt.show()