Filters and convolution¶
https://docs.opencv.org/master/d2/d96/tutorial_py_table_of_contents_imgproc.html
Simple thresholding¶
For every pixel, the same threshold is applaied. If the pixel is smaller then the threshold, it is set to 0, otherwise it is set to the maximum.
# Add a trackbar
import cv2 as cv
import numpy as np
img = np.fromfunction(lambda i, j: j, (50, 256), dtype='uint8')
modes = (cv.THRESH_BINARY,
cv.THRESH_BINARY_INV,
cv.THRESH_TRUNC,
cv.THRESH_TOZERO,
cv.THRESH_TOZERO_INV)
def trackbar(x):
"""Trackbar callback function."""
text = f'threshold={x}'
cv.displayOverlay('window', text, 1000)
ret, img1 = cv.threshold(img, x, 255, cv.THRESH_BINARY)
ret, img2 = cv.threshold(img, x, 255, cv.THRESH_BINARY_INV)
ret, img3 = cv.threshold(img, x, 255, cv.THRESH_TRUNC)
ret, img4 = cv.threshold(img, x, 255, cv.THRESH_TOZERO)
ret, img5 = cv.threshold(img, x, 255, cv.THRESH_TOZERO_INV)
cv.imshow('window', np.vstack([img, img1, img2, img3, img4, img5]))
cv.imshow('window', img)
trackbar(100)
cv.createTrackbar('threshold', 'window', 100, 255, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
Binary thresholding¶
# binary thresholding
import cv2 as cv
import numpy as np
def trackbar(x):
ret, img1 = cv.threshold(img, x, 255, cv.THRESH_BINARY)
ret, img2 = cv.threshold(img, x, 255, cv.THRESH_BINARY_INV)
cv.imshow('window', np.hstack([img, img1, img2]))
text = f'threshold={x}, mode=BINARY, BINARY_INV'
cv.displayOverlay('window', text, 1000)
img = cv.imread('eye.jpg')
trackbar(100)
cv.createTrackbar('threshold', 'window', 100, 255, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
To zero¶
# threshold to zero
import cv2 as cv
import numpy as np
def trackbar(x):
"""Trackbar callback function."""
text = f'threshold={x}, mode=TOZERO, TOZERO_INV'
cv.displayOverlay('window', text, 1000)
ret, img1 = cv.threshold(img, x, 255, cv.THRESH_TOZERO)
ret, img2 = cv.threshold(img, x, 255, cv.THRESH_TOZERO_INV)
cv.imshow('window', np.hstack([img, img1, img2]))
img = cv.imread('eye.jpg')
cv.imshow('window', img)
cv.createTrackbar('threshold', 'window', 100, 255, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
Adaptive thresholding¶
https://docs.opencv.org/master/d7/d1b/group__imgproc__misc.html#ga72b913f352e4a1b1b397736707afcde3
2D convolution¶
https://docs.opencv.org/master/d4/d13/tutorial_py_filtering.html
# convolution
import cv2 as cv
import numpy as np
kernel = np.ones((5, 5), 'float32')/25
def trackbar(x):
"""Trackbar callback function."""
d = 2*x + 1
kernel = np.ones((d, d), 'float32')/(d**2)
img1 = cv.filter2D(img, -1, kernel)
cv.imshow('window', np.hstack([img, img1]))
text = f'kernel=({d}x{d})'
cv.displayOverlay('window', text)
img = cv.imread('eye.jpg')
trackbar(2)
cv.createTrackbar('threshold', 'window', 2, 7, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
Morphological Transformations¶
Erosion¶
# morphological transformation : erode
import cv2 as cv
import numpy as np
def trackbar(x):
n = 2*x + 1
kernel = np.ones((n, n), np.uint8)
img1 = cv.erode(img, kernel, iterations=1)
cv.imshow('window', np.hstack([img, img1]))
text = f'erode, kernel={n}x{n}'
cv.displayOverlay('window', text)
img = cv.imread('j.png')
trackbar(2)
cv.createTrackbar('kernel', 'window', 2, 5, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
Dilation¶
# morphological transformation : dilation
import cv2 as cv
import numpy as np
def trackbar(x):
n = 2*x + 1
kernel = np.ones((n, n), np.uint8)
img1 = cv.dilate(img, kernel, iterations=1)
cv.imshow('window', np.hstack([img, img1]))
text = f'dilate, kernel={n}x{n}'
cv.displayOverlay('window', text)
img = cv.imread('j.png')
trackbar(2)
cv.createTrackbar('kernel', 'window', 2, 5, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
Opening¶
# morphological transformation : opening
import cv2 as cv
import numpy as np
def trackbar(x):
n = 2*x + 1
kernel = np.ones((n, n), np.uint8)
img1 = cv.morphologyEx(img, cv.MORPH_OPEN, kernel)
cv.imshow('window', np.hstack([img, img1]))
text = f'open, kernel={n}x{n}'
cv.displayOverlay('window', text)
img = cv.imread('j.png')
trackbar(2)
cv.createTrackbar('kernel', 'window', 2, 5, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
Closing¶
# morphological transformation : closing
import cv2 as cv
import numpy as np
def trackbar(x):
n = 2*x + 1
kernel = np.ones((n, n), np.uint8)
img1 = cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)
cv.imshow('window', np.hstack([img, img1]))
text = f'close, kernel={n}x{n}'
cv.displayOverlay('window', text)
img = cv.imread('j.png')
trackbar(2)
cv.createTrackbar('kernel', 'window', 2, 5, trackbar)
cv.waitKey(0)
cv.destroyAllWindows()
Image gradient - Laplacian¶
# image gradient - Laplacian
import cv2 as cv
import numpy as np
img = cv.imread('sudoku.png', cv.IMREAD_GRAYSCALE)
img1 = cv.Laplacian(img.copy(), 8)
cv.imshow('window', np.hstack([img, img1]))
cv.waitKey(0)
cv.destroyAllWindows()