Saturday 20 March 2021

opencv 19 contours



adaptive threshold

contour
#main.py
import numpy as np
import cv2

cap = cv2.VideoCapture("assets/Santa Barbara.mp4")

while True:
    ret, frame = cap.read()
    width = int(cap.get(3))
    height = int(cap.get(4))

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    #adaptive threshold (frame, max pixel value, adaptive method, threshold type, neighbour block size, c constant)
    #th1 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)
    th2 = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)

    contours, hierarchy = cv2.findContours(th2, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

    contour_img = np.uint8(np.full((height, width, 3), 255))

    cv2.drawContours(contour_img, contours, -1, (0, 0, 0), 1)

    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)
    cv2.imshow('threshold', th2)
    cv2.imshow('contourss', contour_img)

    if cv2.waitKey(1) == ord('q'):
        break

    if cv2.waitKey(1) == ord('p'):
        # wait until any key is pressed
        cv2.waitKey(-1)

cap.release()
cv2.destroyAllWindows()

reference:

No comments:

Post a Comment