Last active
March 13, 2019 10:12
-
-
Save Ezka77/3ab2a43be715daeda2b17dd78223c7db to your computer and use it in GitHub Desktop.
OpenCV camera mire : a simple script to help telescope collimation (secondary alignment)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import cv2 | |
""" | |
OpenCV camera mire: a simple script to help telescope collimation (secondary | |
alignment) | |
Require OpenCV, binary build are included in this package: | |
- pip install opencv-python | |
Press 'q' to exit. | |
""" | |
refPt = () | |
radius_old = radius_current = 65 | |
cap = cv2.VideoCapture(0) | |
# Force Camera resolution | |
# cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1600) | |
# cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1200) | |
# print(cap.get(cv2.CAP_PROP_FRAME_WIDTH), cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) | |
# frame center | |
cap_center = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5), | |
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)) | |
def distance(x, y, a, b): | |
# Manhattan : abs(x-a) + abs(y-b) | |
# Tchebychev : max(abs(x-a), abs(y-b)) | |
# return abs(x-a) + abs(y-b) | |
xa = x - a | |
yb = y - b | |
# TODO: optimize/simplify here | |
if abs(xa) > abs(yb): | |
if x > cap_center[0]: | |
value = xa * -1 | |
else: | |
value = xa | |
else: | |
if y > cap_center[1]: | |
value = yb * -1 | |
else: | |
value = yb | |
return value | |
def update_circle(event, x, y, flags, param): | |
global refPt, radius_current, radius_old | |
if event == cv2.EVENT_LBUTTONDOWN: | |
refPt = (x, y) | |
elif event == cv2.EVENT_LBUTTONUP: | |
refPt = () | |
radius_old = radius_current | |
if refPt: | |
cursor_distance = distance(*refPt, x, y) | |
radius_current = min(cap_center[1] - 20, | |
max(20, radius_old + cursor_distance)) | |
# create window | |
w_name = 'MireCam' | |
cv2.namedWindow(w_name, cv2.WINDOW_GUI_NORMAL | cv2.WINDOW_AUTOSIZE) | |
cv2.setMouseCallback(w_name, update_circle) | |
# Draw until exit | |
while (cv2.waitKey(1) & 0xFF != ord('q')): | |
# Capture frame-by-frame | |
ret, frame = cap.read() | |
# Our operations on the frame come here | |
img = cv2.cvtColor(frame, cv2.COLOR_RGB2RGBA) | |
img = cv2.circle(img, cap_center, radius_current, (0, 0, 255), 1, | |
cv2.LINE_AA) | |
# Display the resulting frame | |
cv2.imshow(w_name, img) | |
# When everything done, release the capture | |
cap.release() | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment