Last active
March 31, 2021 16:58
-
-
Save rytse/ce811e790fa491e9c8c13c0f65e1fb2a to your computer and use it in GitHub Desktop.
Python OpenCV script for HSV filtering, finding contours, and displaying the contours
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
import cv2 | |
import numpy as np | |
def nothing(n): | |
pass | |
# Contours w/ greatest number of points | |
# TODO max by area | |
def biggestContourI(contours): | |
maxVal = 0 | |
maxI = None | |
for i in range(0, len(contours) - 1): | |
if len(contours[i]) > maxVal: | |
cs = contours[i] | |
maxVal = len(contours[i]) | |
maxI = i | |
return maxI | |
iLowH = 155; | |
iHighH = 225; | |
iLowS = 47; | |
iHighS = 126; | |
iLowV = 82; | |
iHighV = 132; | |
cv2.namedWindow('Control') | |
cv2.createTrackbar("LowH", "Control", iLowH, 255, nothing); | |
cv2.createTrackbar("HighH", "Control", iHighH, 255, nothing); | |
cv2.createTrackbar("LowS", "Control", iLowS, 255, nothing); | |
cv2.createTrackbar("HighS", "Control", iHighS, 255, nothing); | |
cv2.createTrackbar("LowV", "Control", iLowV, 255, nothing); | |
cv2.createTrackbar("HighV", "Control", iHighV, 255, nothing); | |
cam = cv2.VideoCapture(0) | |
while True: | |
ret_val, img = cam.read() | |
lh = cv2.getTrackbarPos('LowH', 'Control') | |
ls = cv2.getTrackbarPos('LowS', 'Control') | |
lv = cv2.getTrackbarPos('LowV', 'Control') | |
hh = cv2.getTrackbarPos('HighH', 'Control') | |
hs = cv2.getTrackbarPos('HighS', 'Control') | |
hv = cv2.getTrackbarPos('HighV', 'Control') | |
lower = np.array([lh, ls, lv], dtype = "uint8") | |
higher = np.array([hh, hs, hv], dtype = "uint8") | |
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
h, w = img.shape[:2] | |
flt = cv2.inRange(hsv, lower, higher); | |
contours0, hierarchy = cv2.findContours(flt, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) | |
# Only draw the biggest one | |
bc = biggestContourI(contours0) | |
cv2.drawContours(img,contours0, bc, (0,255,0), 3) | |
cv2.imshow('my webcam', img) | |
cv2.imshow('hsv', hsv) | |
cv2.imshow('flt', flt) | |
if cv2.waitKey(1) == 27: | |
break # esc to quit | |
cv2.destroyAllWindows() |
@Nawaytes looks like they changed the API at some point for findContours
to no longer return im2
. I fixed it, it should work now with Python 3.9.2
with cv2
version 4.5.1
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i was try your code, but how to fix it. thx