Created
February 16, 2023 08:04
-
-
Save unot/43a1c1460b19679c4371852f10f65622 to your computer and use it in GitHub Desktop.
a test script for OpenCV trackbar
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
from matplotlib import pyplot as plt | |
import numpy as np | |
import cv2 | |
import sys | |
def printing(position): | |
#print(position) | |
return | |
def make_hist(src: np.ndarray): | |
gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) | |
fig, ax = plt.subplots() | |
ax.hist(gray.flatten(), bins=32, ec='k') | |
fig.canvas.draw() | |
data = fig.canvas.tostring_rgb() | |
w, h = fig.canvas.get_width_height() | |
c = len(data) // (w * h) | |
dst = np.frombuffer(data, dtype=np.uint8).reshape(h, w, c) | |
dst = cv2.cvtColor(dst, cv2.COLOR_RGB2BGR) | |
plt.close() | |
return dst | |
img = cv2.imread(sys.argv[1]) | |
cv2.namedWindow("window") | |
cv2.namedWindow("control") | |
hist = make_hist(img) | |
cv2.imshow('control', hist) | |
threshold = 150 | |
offsetx = 100 | |
offsety = 60 | |
r = 1.75 | |
cv2.createTrackbar("track", "control", threshold, 255, printing) | |
while True: | |
ret, th = cv2.threshold(img, threshold, 255, cv2.THRESH_BINARY) | |
threshold = cv2.getTrackbarPos("track", "control") | |
cv2.imshow('window', th) | |
histol = hist.copy() | |
cv2.line(histol, (int(r*threshold+offsetx), offsety), (int(r*threshold+offsetx), offsety+370), (0,0,255), thickness=1, lineType=cv2.LINE_AA) | |
cv2.imshow('control', histol) | |
if cv2.waitKey(10) == 27: | |
break | |
cv2.destroyAllWindows() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment