Tuesday, 16 March 2021

opencv 15 smoothing | blurring



adaptive threshold

blur (2d convolution)

2d convolution kernel

gaussian blur removes high frequency noise

gaussian convolution kernel

median blur removes salt and pepper noise

bilateral blur preserves edges
#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)

    kernel = np.ones((5, 5), np.float32)/25
    #conv = cv2.filter2D(th1, -1, kernel)
    blur = cv2.blur(th1, (5,5))

    #gaussianBlur (frame, kernel, sigma)
    #designed to remove high frequency noise
    gblur = cv2.GaussianBlur(th1, (5, 5), 0)

    #median method designed to remove salt and pepper noise
    median = cv2.medianBlur(th1, 5)

    #bilateralFilter preserve edges
    bilateralFilter = cv2.bilateralFilter(th1, 9, 75, 75)

    cv2.imshow('frame', frame)
    cv2.imshow('threshold', th1)
    cv2.imshow('convolution', blur)
    cv2.imshow('gaussian', gblur)
    cv2.imshow('median', median)
    cv2.imshow('bilater', bilateralFilter)

    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