import numpy as np import cv2 img = cv2.imread('./images/trex.png') cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows() cv2.imwrite('trex.jpg', img)
import cv2 image = cv2.imread('./images/trex.png') cv2.imshow("Original", image) (b, g, r) = image[0, 0] print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b)) image[0, 0] = (0, 0, 255) (b, g, r) = image[0, 0] print("Pixel at (0, 0) - Red: {}, Green: {}, Blue: {}".format(r, g, b)) corner = image[0:100, 0:100] cv2.imshow("Corner", corner) image[0:100, 0:100] = (0, 255, 0) cv2.imshow("Updated", image) cv2.waitKey(0)
import numpy as np import cv2 canvas = np.zeros((300, 300, 3), dtype="uint8") green = (0, 255, 0) cv2.line(canvas, (0, 0), (300, 300), green) cv2.imshow("Canvas", canvas) cv2.waitKey(0) red = (0, 0, 255) cv2.line(canvas, (300, 0), (0, 300), red, 3) cv2.imshow("Canvas", canvas) cv2.waitKey(0) cv2.rectangle(canvas, (10, 10), (60, 60), green) cv2.imshow("Canvas", canvas) cv2.waitKey(0) cv2.rectangle(canvas, (50, 200), (200, 225), red, 5) cv2.imshow("Canvas", canvas) cv2.waitKey(0) blue = (255, 0, 0) cv2.rectangle(canvas, (200, 50), (225, 125), blue, -1) cv2.imshow("Canvas", canvas) cv2.waitKey(0) canvas = np.zeros((300, 300, 3), dtype="uint8") (centerX, centerY) = (canvas.shape[1] // 2, canvas.shape[0] // 2) white = (255, 255, 255) for r in range(0, 175, 25): cv2.circle(canvas, (centerX, centerY), r, white) cv2.imshow("Canvas", canvas) cv2.waitKey(0) for i in range(0, 25): radius = np.random.randint(5, high=200) color = np.random.randint(0, high=256, size=(3,)).tolist() pt = np.random.randint(0, high=300, size=(2,)) cv2.circle(canvas, tuple(pt), radius, color, -1) cv2.imshow("Canvas", canvas) cv2.waitKey(0)
import imutils import cv2 image = cv2.imread('./images/trex.png') cv2.imshow("Original", image) (h, w) = image.shape[:2] center = (w // 2, h // 2) M = cv2.getRotationMatrix2D(center, 45, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) cv2.imshow("Rotated by 45 Degrees", rotated) M = cv2.getRotationMatrix2D(center, -90, 1.0) rotated = cv2.warpAffine(image, M, (w, h)) cv2.imshow("Rotated by -90 Degrees", rotated) rotated = imutils.rotate(image, 180) cv2.imshow("Rotated by 180 Degrees", rotated) cv2.waitKey(0)
import imutils import cv2 image = cv2.imread('./images/trex.png') cv2.imshow("Original", image) r = 150.0 / image.shape[1] dim = (150, int(image.shape[0] * r)) resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) cv2.imshow("Resized (Width)", resized) r = 50.0 / image.shape[0] dim = (int(image.shape[1] * r), 50) resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA) cv2.imshow("Resized (Height)", resized) cv2.waitKey(0) resized = imutils.resize(image, width = 100) cv2.imshow("Resized via Function", resized) cv2.waitKey(0) resized = imutils.resize(image, height = 50) cv2.imshow("Resized via Function h=50", resized) cv2.waitKey(0)
import cv2 image = cv2.imread('./images/trex.png') cv2.imshow("Original", image) flipped = cv2.flip(image, 1) cv2.imshow("Flipped Horizontally", flipped) flipped = cv2.flip(image, 0) cv2.imshow("Flipped Vertically", flipped) flipped = cv2.flip(image, -1) cv2.imshow("Flipped Horizontally & Vertically", flipped) cv2.waitKey(0)
import cv2 image = cv2.imread('./images/trex.png') cv2.imshow("Original", image) cropped = image[30:120, 240:335] cv2.imshow("T-Rex Face", cropped) cv2.waitKey(0)
import numpy as np import cv2 image = cv2.imread('./images/trex.png') cv2.imshow("Original", image) print("max of 255: {}".format(cv2.add(np.uint8([200]), np.uint8([100])))) print("min of 0: {}".format(cv2.subtract(np.uint8([50]), np.uint8([100])))) print("wrap around: {}".format(np.uint8([200]) + np.uint8([100]))) print("wrap around: {}".format(np.uint8([50]) - np.uint8([100]))) M = np.ones(image.shape, dtype = "uint8") * 100 added = cv2.add(image, M) cv2.imshow("Added", added) M = np.ones(image.shape, dtype = "uint8") * 50 subtracted = cv2.subtract(image, M) cv2.imshow("Subtracted", subtracted) cv2.waitKey(0)
import numpy as np import cv2 rectangle = np.zeros((300, 300), dtype = "uint8") cv2.rectangle(rectangle, (25, 25), (275, 275), 255, -1) cv2.imshow("Rectangle", rectangle) circle = np.zeros((300, 300), dtype = "uint8") cv2.circle(circle, (150, 150), 150, 255, -1) cv2.imshow("Circle", circle) bitwiseAnd = cv2.bitwise_and(rectangle, circle) cv2.imshow("AND", bitwiseAnd) cv2.waitKey(0) bitwiseOr = cv2.bitwise_or(rectangle, circle) cv2.imshow("OR", bitwiseOr) cv2.waitKey(0) bitwiseXor = cv2.bitwise_xor(rectangle, circle) cv2.imshow("XOR", bitwiseXor) cv2.waitKey(0) bitwiseNot = cv2.bitwise_not(circle) cv2.imshow("NOT", bitwiseNot) cv2.waitKey(0)
import numpy as np import cv2 image = cv2.imread('./images/beach.png') cv2.imshow("Original", image) mask = np.zeros(image.shape[:2], dtype="uint8") (cX, cY) = (image.shape[1] // 2, image.shape[0] // 2) cv2.rectangle(mask, (cX - 75, cY - 75), (cX + 75, cY + 75), 255, -1) cv2.imshow("Mask", mask) masked = cv2.bitwise_and(image, image, mask=mask) cv2.imshow("Mask Applied to Image", masked) cv2.waitKey(0) mask = np.zeros(image.shape[:2], dtype="uint8") cv2.circle(mask, (cX, cY), 100, 255, -1) masked = cv2.bitwise_and(image, image, mask=mask) cv2.imshow("Mask", mask) cv2.imshow("Mask Applied to Image", masked) cv2.waitKey(0)
import numpy as np import cv2 image = cv2.imread('./images/beach.png') (B, G, R) = cv2.split(image) cv2.imshow("Red", R) cv2.imshow("Green", G) cv2.imshow("Blue", B) cv2.waitKey(0) merged = cv2.merge([B, G, R]) cv2.imshow("Merged", merged) cv2.waitKey(0) cv2.destroyAllWindows() zeros = np.zeros(image.shape[:2], dtype = "uint8") cv2.imshow("Red", cv2.merge([zeros, zeros, R])) cv2.imshow("Green", cv2.merge([zeros, G, zeros])) cv2.imshow("Blue", cv2.merge([B, zeros, zeros])) cv2.waitKey(0)
import cv2 image = cv2.imread('./images/beach.png') cv2.imshow("Original", image) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Gray", gray) hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) cv2.imshow("HSV", hsv) lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB) cv2.imshow("L*a*b*", lab) cv2.waitKey(0)
from matplotlib import pyplot as plt import cv2 image = cv2.imread('./images/beach.png') image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Original", image) hist = cv2.calcHist([image], [0], None, [256], [0, 256]) plt.figure() plt.title("Grayscale Histogram") plt.xlabel("Bins") plt.ylabel("# of Pixels") plt.plot(hist) plt.xlim([0, 256]) plt.show() cv2.waitKey(0)
from matplotlib import pyplot as plt import cv2 image = cv2.imread('./images/beach.png') cv2.imshow("Original", image) chans = cv2.split(image) colors = ("b", "g", "r") plt.figure() plt.title("’Flattened’ Color Histogram") plt.xlabel("Bins") plt.ylabel("# of Pixels") for (chan, color) in zip(chans, colors): hist = cv2.calcHist([chan], [0], None, [256], [0, 256]) plt.plot(hist, color=color) plt.xlim([0, 256]) fig = plt.figure() ax = fig.add_subplot(131) hist = cv2.calcHist([chans[1], chans[0]], [0, 1], None, [32, 32], [0, 256, 0, 256]) p = ax.imshow(hist, interpolation="nearest") ax.set_title("2D Color Histogram for G and B") plt.colorbar(p) ax = fig.add_subplot(132) hist = cv2.calcHist([chans[1], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256]) p = ax.imshow(hist, interpolation="nearest") ax.set_title("2D Color Histogram for G and R") plt.colorbar(p) ax = fig.add_subplot(133) hist = cv2.calcHist([chans[0], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256]) p = ax.imshow(hist, interpolation="nearest") ax.set_title("2D Color Histogram for B and R") plt.colorbar(p) print("2D histogram shape: {}, with {} values".format(hist.shape, hist.flatten().shape[0])) hist = cv2.calcHist([image], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256]) print("3D histogram shape: {}, with {} values".format(hist.shape, hist.flatten().shape[0])) plt.show()
import numpy as np import cv2 image = cv2.imread('./images/beach.png') image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) eq = cv2.equalizeHist(image) cv2.imshow("Histogram Equalization", np.hstack([image, eq])) cv2.waitKey(0)
from matplotlib import pyplot as plt import numpy as np import cv2 def plot_histogram(image, title, mask=None): chans = cv2.split(image) colors = ("b", "g", "r") plt.figure() plt.title(title) plt.xlabel("Bins") plt.ylabel("# of Pixels") for (chan, color) in zip(chans, colors): hist = cv2.calcHist([chan], [0], mask, [256], [0, 256]) plt.plot(hist, color=color) plt.xlim([0, 256]) image = cv2.imread('./images/beach.png') cv2.imshow("Original", image) plot_histogram(image, "Histogram for Original Image") mask = np.zeros(image.shape[:2], dtype="uint8") cv2.rectangle(mask, (15, 15), (130, 100), 255, -1) cv2.imshow("Mask", mask) masked = cv2.bitwise_and(image, image, mask=mask) cv2.imshow("Applying the Mask", masked) plot_histogram(image, "Histogram for Masked Image", mask=mask) plt.show()
import numpy as np import cv2 image = cv2.imread('./images/trex.png') cv2.imshow("Original", image) blurred = np.hstack([cv2.blur(image, (3, 3)), cv2.blur(image, (5, 5)), cv2.blur(image, (7, 7))]) cv2.imshow("Averaged", blurred) cv2.waitKey(0) blurred = np.hstack([cv2.GaussianBlur(image, (3, 3), 0), cv2.GaussianBlur(image, (5, 5), 0), cv2.GaussianBlur(image, (7, 7), 0)]) cv2.imshow("Gaussian", blurred) cv2.waitKey(0) blurred = np.hstack([cv2.medianBlur(image, 3), cv2.medianBlur(image, 5), cv2.medianBlur(image, 7)]) cv2.imshow("Median", blurred) cv2.waitKey(0) blurred = np.hstack([cv2.bilateralFilter(image, 5, 21, 21), cv2.bilateralFilter(image, 7, 31, 31), cv2.bilateralFilter(image, 9, 41, 41)]) cv2.imshow("Bilateral", blurred) cv2.waitKey(0)
import cv2 image = cv2.imread('./images/coins.png') image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(image, (5, 5), 0) cv2.imshow("Image", image) (T, thresh) = cv2.threshold(blurred, 155, 255, cv2.THRESH_BINARY) cv2.imshow("Threshold Binary", thresh) (T, threshInv) = cv2.threshold(blurred, 155, 255, cv2. THRESH_BINARY_INV) cv2.imshow("Threshold Binary Inverse", threshInv) cv2.imshow("Coins", cv2.bitwise_and(image, image, mask = threshInv)) cv2.waitKey(0)
import mahotas import cv2 image = cv2.imread('./images/coins.png') image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(image, (5, 5), 0) cv2.imshow("Image", image) T = mahotas.thresholding.otsu(blurred) print("Otsu’s threshold: {}".format(T)) thresh = image.copy() thresh[thresh > T] = 255 thresh[thresh < 255] = 0 thresh = cv2.bitwise_not(thresh) cv2.imshow("Otsu", thresh) T = mahotas.thresholding.rc(blurred) print("Riddler-Calvard: {}".format(T)) thresh = image.copy() thresh[thresh > T] = 255 thresh[thresh < 255] = 0 thresh = cv2.bitwise_not(thresh) cv2.imshow("Riddler-Calvard", thresh) cv2.waitKey(0)
import numpy as np import cv2 image = cv2.imread('./images/coins.png') image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2.imshow("Original", image) lap = cv2.Laplacian(image, cv2.CV_64F) lap = np.uint8(np.absolute(lap)) cv2.imshow("Laplacian", lap) cv2.waitKey(0) sobelX = cv2.Sobel(image, cv2.CV_64F, 1, 0) sobelY = cv2.Sobel(image, cv2.CV_64F, 0, 1) sobelX = np.uint8(np.absolute(sobelX)) sobelY = np.uint8(np.absolute(sobelY)) sobelCombined = cv2.bitwise_or(sobelX, sobelY) cv2.imshow("Sobel X", sobelX) cv2.imshow("Sobel Y", sobelY) cv2.imshow("Sobel Combined", sobelCombined) cv2.waitKey(0)
import cv2 image = cv2.imread('./images/coins.png') image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) image = cv2.GaussianBlur(image, (5, 5), 0) cv2.imshow("Blurred", image) canny = cv2.Canny(image, 30, 150) cv2.imshow("Canny", canny) cv2.waitKey(0)
import numpy as np import cv2 image = cv2.imread('./images/coins.png') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) blurred = cv2.GaussianBlur(gray, (11, 11), 0) cv2.imshow("Image", image) edged = cv2.Canny(blurred, 30, 150) cv2.imshow("Edges", edged) (_, cnts, _) = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print("I count {} coins in this image".format(len(cnts))) coins = image.copy() cv2.drawContours(coins, cnts, -1, (0, 255, 0), 2) cv2.imshow("Coins", coins) cv2.waitKey(0) cv2.drawContours(coins, cnts, 0, (0, 255, 0), 2) cv2.drawContours(coins, cnts, 1, (0, 255, 0), 2) cv2.drawContours(coins, cnts, 2, (0, 255, 0), 2) for (i, c) in enumerate(cnts): (x, y, w, h) = cv2.boundingRect(c) print("Coin #{}".format(i + 1)) coin = image[y:y + h, x:x + w] cv2.imshow("Coin", coin) mask = np.zeros(image.shape[:2], dtype="uint8") ((centerX, centerY), radius) = cv2.minEnclosingCircle(c) cv2.circle(mask, (int(centerX), int(centerY)), int(radius), 255, -1) mask = mask[y:y + h, x:x + w] cv2.imshow("Masked Coin", cv2.bitwise_and(coin, coin, mask=mask)) cv2.waitKey(0)