Created
October 17, 2018 00:56
-
-
Save skypanther/f4d4e7c4407fd9188c47b13f805a2380 to your computer and use it in GitHub Desktop.
Trackbars (sliders) in OpenCV
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
''' | |
slider.py - Demonstrating a user of trackbars on OpenCV windows | |
Author: Tim Poulsen, github.com/skypanther | |
License: MIT | |
2018-10-15 | |
Example usage: | |
python3 slider.py -i path/to/image.jpg | |
''' | |
import argparse | |
import cv2 | |
import imutils | |
import os | |
def main(): | |
min_val = 200 | |
max_val = 300 | |
aperture_size = 3 | |
ap = argparse.ArgumentParser() | |
ap.add_argument('-i', '--image', default='', | |
help='Image to use for edge detection') | |
args = vars(ap.parse_args()) | |
file_name = args['image'] | |
if os.path.isfile(file_name) is False: | |
print('Cannot open image, quitting...') | |
exit() | |
image = cv2.imread(file_name) | |
image = imutils.resize(image, height=480) | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
cv2.namedWindow('Original') | |
cv2.createTrackbar('Min', 'Original', 0, 800, no_op) | |
cv2.createTrackbar('Max', 'Original', 100, 800, no_op) | |
cv2.imshow('Original', image) | |
while True: | |
min_val = int(cv2.getTrackbarPos('Min', 'Original')) | |
max_val = int(cv2.getTrackbarPos('Max', 'Original')) | |
print('Min: {}'.format(min_val)) | |
print('Max: {}'.format(max_val)) | |
edges = cv2.Canny(gray, min_val, max_val, aperture_size) | |
cv2.imshow('Edges', imutils.resize(edges, height=480)) | |
if cv2.waitKey(1) & 0xFF == ord("q"): | |
cv2.destroyAllWindows() | |
exit() | |
def no_op(new_val): | |
pass | |
if __name__ == '__main__': | |
main() |
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
''' | |
slider2.py - Demonstrating a user of trackbars on OpenCV windows | |
Author: Tim Poulsen, github.com/skypanther | |
License: MIT | |
2018-10-15 | |
Example usage: | |
python3 slider2.py -i path/to/image.jpg | |
''' | |
import argparse | |
import cv2 | |
import imutils | |
import os | |
edge_params = { | |
'min_val': 200, | |
'max_val': 300, | |
'aperture_size': 3 | |
} | |
gray = None | |
def main(): | |
global edge_params, gray | |
ap = argparse.ArgumentParser() | |
ap.add_argument('-i', '--image', default='', | |
help='Image to use for edge detection') | |
args = vars(ap.parse_args()) | |
file_name = args['image'] | |
if os.path.isfile(file_name) is False: | |
print('Cannot open image, quitting...') | |
exit() | |
image = cv2.imread(file_name) | |
image = imutils.resize(image, height=480) | |
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) | |
cv2.namedWindow('Original') | |
cv2.createTrackbar('Min', 'Original', 0, 800, min_change) | |
cv2.createTrackbar('Max', 'Original', 100, 800, max_change) | |
cv2.imshow('Original', image) | |
redraw_edges() | |
while True: | |
if cv2.waitKey(1) & 0xFF == ord("q"): | |
cv2.destroyAllWindows() | |
exit() | |
def min_change(new_val): | |
change_params('min_val', new_val) | |
def max_change(new_val): | |
change_params('max_val', new_val) | |
def change_params(name, value): | |
global edge_params | |
edge_params[name] = value | |
print(edge_params) | |
redraw_edges() | |
def redraw_edges(): | |
edges = cv2.Canny(gray, | |
edge_params['min_val'], | |
edge_params['max_val'], | |
edge_params['aperture_size']) | |
cv2.imshow('Edges', imutils.resize(edges, height=480)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment