Morphological transformation – OpenCV 3.4 with python 3 Tutorial 17

Source code Images transformation: [python] import cv2 import numpy as np img = cv2.imread("balls.jpg", cv2.IMREAD_GRAYSCALE) _, mask = cv2.threshold(img, 250, 255, cv2.THRESH_BINARY_INV) kernel = np.ones((5, 5), np.uint8) dilation = cv2.dilate(mask, kernel) erosion = cv2.erode(mask, kernel, iterations=6) cv2.imshow("Image", img) cv2.imshow("Mask", mask) cv2.imshow("Dilation", dilation) cv2.imshow("Erosion", erosion) cv2.waitKey(0) cv2.destroyAllWindows() [/python]   Real Time Video Morphological transformation: [python] import […]

Smoothing images – OpenCV 3.4 with python 3 Tutorial 16

[python] import cv2 import numpy as np img = cv2.imread("early_1800.jpg") averaging = cv2.blur(img, (21, 21)) gaussian = cv2.GaussianBlur(img, (21, 21), 0) median = cv2.medianBlur(img, 5) bilateral = cv2.bilateralFilter(img, 9, 350, 350) cv2.imshow("Original image", img) cv2.imshow("Averaging", averaging) cv2.imshow("Gaussian", gaussian) cv2.imshow("Median", median) cv2.imshow("Bilateral", bilateral) cv2.waitKey(0) cv2.destroyAllWindows() [/python]   Files: early_1800.jpg balloons_noisy.png carpet.jpg lake.jpg

Adaptive thresholding – OpenCV 3.4 with python 3 Tutorial 15

[python] import cv2 import numpy as np img = cv2.imread("book_page.jpg") _, threshold = cv2.threshold(img, 155, 255, cv2.THRESH_BINARY) img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) mean_c = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 15, 12) gaus = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 91, 12) cv2.imshow("Img", img) cv2.imshow("Binary threshold", threshold) cv2.imshow("Mean C", mean_c) cv2.imshow("Gaussian", gaus) cv2.waitKey(0) cv2.destroyAllWindows() [/python]   Files: book_page.jpg

Affine transformation – OpenCV 3.4 with python 3 Tutorial 14

[python] import cv2 import numpy as np img = cv2.imread("grid.jpg") rows, cols, ch = img.shape cv2.circle(img, (83, 90), 5, (0, 0, 255), -1) cv2.circle(img, (447, 90), 5, (0, 0, 255), -1) cv2.circle(img, (83, 472), 5, (0, 0, 255), -1) pts1 = np.float32([[83, 90], [447, 90], [83, 472]]) pts2 = np.float32([[0, 0], [447, 90], [150, 472]]) […]

Basic geometric transformations – OpenCV 3.4 with python 3 Tutorial 12

[python] import cv2 import numpy as np img = cv2.imread("red_panda.jpg") rows, cols, ch = img.shape print("Height: ", rows) print("Width: ", cols) scaled_img = cv2.resize(img, None, fx=1/2, fy=1/2) matrix_t = np.float32([[1, 0, -100], [0, 1, -30]]) translated_img = cv2.warpAffine(img, matrix_t, (cols, rows)) matrix_r = cv2.getRotationMatrix2D((cols/2, rows/2), 90, 0.5) rotated_img = cv2.warpAffine(img, matrix_r, (cols, rows)) cv2.imshow("Original image", […]

Histograms – OpenCV 3.4 with python 3 Tutorial 11

Source code: [python] import cv2 import numpy as np from matplotlib import pyplot as plt img = cv2.imread("sea_beach.jpg") b, g, r = cv2.split(img) cv2.imshow("img", img) cv2.imshow("b", b) cv2.imshow("g", g) cv2.imshow("r", r) plt.hist(b.ravel(), 256, [0, 256]) plt.hist(g.ravel(), 256, [0, 256]) plt.hist(r.ravel(), 256, [0, 256]) plt.show() [/python] Files: sea.jpg sea_beach.jpg

Basic Thresholding – OpenCV 3.4 with python 3 Tutorial 10

Threshold: [python] import cv2 import numpy as np img = cv2.imread("black_to_white.jpeg", cv2.IMREAD_GRAYSCALE) _, threshold_binary = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY) _, threshold_binary_inv = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY_INV) _, threshold_trunc = cv2.threshold(img, 128, 255, cv2.THRESH_TRUNC) _, threshold_to_zero = cv2.threshold(img, 12, 255, cv2.THRESH_TOZERO) cv2.imshow("Image", img) cv2.imshow("th binary", threshold_binary) cv2.imshow("th binary inv", threshold_binary_inv) cv2.imshow("th trunc", threshold_trunc) cv2.imshow("th to zero", […]

Trackbars – OpenCV 3.4 with python 3 Tutorial 8

Source code: [python] import cv2 import numpy as np def nothing(x): pass cap = cv2.VideoCapture(0) cv2.namedWindow("frame") cv2.createTrackbar("test", "frame", 50, 500, nothing) cv2.createTrackbar("color/gray", "frame", 0, 1, nothing) while True: _, frame = cap.read() test = cv2.getTrackbarPos("test", "frame") font = cv2.FONT_HERSHEY_COMPLEX cv2.putText(frame, str(test), (50, 150), font, 4, (0, 0, 255)) s = cv2.getTrackbarPos("color/gray", "frame") if s == […]