Cv2.createTrackbar using python

I am new to python and opencv, I want to create a track bar to manage the hierarchy of the cv2.findContours function, but I don’t know how to add it to the source code this is the code:

import cv2
import cv2.cv as cv

cv2.namedWindow("test")
vc = cv2.VideoCapture(2);
retVal, frame = vc.read();
while True:
    if frame is not None:   
        imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
        ret,thresh = cv2.threshold(imgray,127,255,0)
        contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)

        cv2.drawContours(frame, contours, -1, (0,255,0), 2)

        cv2.imshow("test", frame)

     rval, frame = vc.read()

     if cv2.waitKey(1) & 0xFF == 27:
          break

cv.DestroyAllWindows()

early

+4
source share
1 answer

I hope you decide to solve your problems, but I will try to explain it if you did not. You can create a window using the named window function, and then associate the track bar with this window.

cv2.namedWindow('test')
cv2.createTrackbar('thrs1', 'test', 300, 800, callback)
# Do whatever you want with contours
cv2.imshow('test', frame)

You will find the createTrackbar function here: cv2.createTrackbar

callback is a pointer to a function that will be called every time the slides change its position.

+5
source

Source: https://habr.com/ru/post/1536343/


All Articles