Skip to content

Commit 5bdc722

Browse files
committed
correction of fisheye + crop
1 parent 546fb03 commit 5bdc722

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

conftest.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,14 @@ def pytest_collection_modifyitems(config, items):
4444
item.add_marker(pytest.mark.skip(reason="Not for selected params"))
4545

4646

47-
def bsp_image_correction(image):
47+
def bsp_image_correction(image, width, height):
4848
pts_src = np.float32([
4949
[160, 80], # top-left
5050
[1044, 87], # top-right
5151
[279, 720], # bottom-left
5252
[913, 718] # bottom-right
5353
])
5454

55-
width, height = 1000, 884
5655
pts_dst = np.float32([
5756
[0, 0],
5857
[width, 0],
@@ -65,6 +64,21 @@ def bsp_image_correction(image):
6564
return warped
6665

6766

67+
def bsp_fisheye_correction(image, width, height):
68+
K = np.array([
69+
[width, 0, width / 2], # Focal length x
70+
[0, width, height / 2], # Focal length y
71+
[0, 0, 1]]) # Principal point
72+
# [k1, k2, p1, p2, k3] - main is k1 a k2
73+
dist_coeffs = np.array([-0.3, 0.1, 0, 0, 0])
74+
75+
new_K, _ = cv2.getOptimalNewCameraMatrix(K, dist_coeffs, (width, height), 1, (width, height))
76+
map1, map2 = cv2.initUndistortRectifyMap(K, dist_coeffs, None, new_K, (width, height), 5)
77+
undistorted = cv2.remap(image, map1, map2, interpolation=cv2.INTER_LINEAR)
78+
79+
return undistorted
80+
81+
6882
def bsp_capture_image(image_path, board):
6983
# Enable auto-focus
7084
# subprocess.run(["v4l2-ctl", "-d", "/dev/video0", "--set-ctrl=focus_auto=1"])
@@ -94,7 +108,11 @@ def bsp_capture_image(image_path, board):
94108
# TODO: Crop image for {board}
95109

96110
# correction image perspective and crop
97-
frame = bsp_image_correction(frame)
111+
frame = bsp_image_correction(frame, 1000, 884)
112+
# correction of fisheye
113+
frame = bsp_fisheye_correction(frame, 1000, 884)
114+
# crop
115+
frame = frame[30:848, 38:980]
98116

99117
# Save image
100118
cv2.imwrite(image_path, frame)

0 commit comments

Comments
 (0)