Monday, 15 March 2021
opencv 14 Morphological Transformations
original image
applied adaptive mean thresholding
after threshold, applied dilation
after threshold, applied corrosion
after threshold, applied corrosion then dilation (opening)
after threshold, applied dilation then corrosion (closing)
morphological gradient is the difference between dilation and corrosion
top hat is the difference between opening and original image
#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)
kernal = np.ones((5, 5), np.uint8)
dilation = cv2.dilate(th1, kernal, iterations=1)
erosion = cv2.erode(th1, kernal, iterations=1)
#erosion then dilation
opening = cv2.morphologyEx(th1, cv2.MORPH_OPEN, kernal)
#dilation then erosion
closing = cv2.morphologyEx(th1, cv2.MORPH_CLOSE, kernal)
#morphological gredient, difference between dilation and erosion
mg = cv2.morphologyEx(th1, cv2.MORPH_GRADIENT, kernal)
#top hat, difference between image and opening of image
top_hat = cv2.morphologyEx(th1, cv2.MORPH_TOPHAT, kernal)
cv2.imshow('frame', frame)
cv2.imshow('mask', th1)
cv2.imshow('dilation', dilation)
cv2.imshow('erosion', erosion)
cv2.imshow('opening', opening)
cv2.imshow('closing', closing)
cv2.imshow('morphological', mg)
cv2.imshow('top hat', top_hat)
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:
adaptive threshold
Sunday, 14 March 2021
opencv 13 adaptive thresholding
original image
adaptive mean thresholding
adaptive gaussian thresholding
#main.pyimport 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)
cv2.imshow('frame', frame)
cv2.imshow('threshold mean', th1)
cv2.imshow('threshold gaussian', th2)
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:
Saturday, 13 March 2021
opencv 12 adjust contrast
original image, contrast = 0
increased contrast to 5
#main.py
import numpy as np
import cv2
cap = cv2.VideoCapture("assets/Santa Barbara.mp4")
brightness = 50
contrast = 0
trackbars_img = np.uint8(np.full((50, 500, 3), 255))
cv2.imshow('trackbars', trackbars_img)
def brightness_change(x):
global brightness
brightness = x
def contrast_change(x):
global contrast
contrast = x
cv2.createTrackbar('Brightness', 'trackbars', 50, 100, brightness_change)
cv2.createTrackbar('Contrast', 'trackbars', 0, 10, contrast_change)
while True:
ret, frame = cap.read()
width = int(cap.get(3))
height = int(cap.get(4))
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
brightness_adjuster = int((brightness - 50) / 50 * 255)
#print(brightness_adjuster)
v_int16 = np.int16(v) + brightness_adjuster
v_int16[v_int16 > 255] = 255
v_int16[v_int16 < 0] = 0
#print(v_int16)
v = np.uint8(v_int16)
#print(v)
final_hsv = cv2.merge((h, s, v))
image = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
#cv2.imshow('frame', image)
lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
#cv2.imshow('lab', lab)
l, a, b = cv2.split(lab)
#cv2.imshow('l_channel', l)
#cv2.imshow('a_channel', a)
#cv2.imshow('b_channel', b)
clahe = cv2.createCLAHE(clipLimit=0.01 + contrast, tileGridSize=(8, 8))
cl = clahe.apply(l)
#cv2.imshow('CLAHE output', cl)
limg = cv2.merge((cl, a, b))
#cv2.imshow('limg', limg)
final = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
cv2.imshow('frame', final)
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:
differences between saturation and contrast
Contrast is the difference between light and dark.
Saturation is the intensity of color.
adjust contrast
CIELAB color
CLAHE and Thresholding
Friday, 12 March 2021
opencv 11 trackbar adjust brightness
original image
increase brightness, trackbar value 75
decrease brightness, trackbar value 25
#main.py
import numpy as np
import cv2
cap = cv2.VideoCapture("assets/Santa Barbara.mp4")
brightness = 50
#create trackbar with white background
trackbars_img = np.uint8(np.full((50, 500, 3), 255))
cv2.imshow('trackbars', trackbars_img)
def brightness_change(x):
global brightness
brightness = x
#create trackbar (trackbar name, window name, default value, max value, onChange)
cv2.createTrackbar('Brightness', 'trackbars', 50, 100, brightness_change)
while True:
ret, frame = cap.read()
width = int(cap.get(3))
height = int(cap.get(4))
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
h, s, v = cv2.split(hsv)
brightness_adjuster = int((brightness - 50) / 50 * 255)
#print(brightness_adjuster)
v_int16 = np.int16(v) + brightness_adjuster
v_int16[v_int16 > 255] = 255
v_int16[v_int16 < 0] = 0
#print(v_int16)
v = np.uint8(v_int16)
#print(v)
final_hsv = cv2.merge((h, s, v))
image = cv2.cvtColor(final_hsv, cv2.COLOR_HSV2BGR)
cv2.imshow('frame', image)
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:
adjust brightness
np.full
Subscribe to:
Posts (Atom)


















