OpenCV 小ネタ集1
はじめに
- image 描写
- gray color convert
- RGB or GBR or HSV ...
import cv2 import matplotlib.pyplot as plt def cv2_imshow(title, image): plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) plt.title(title) # as opencv loads in BGR format by default, we want to show it in RGB. plt.show() image = cv2.imread('./images/input.jpg') gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) cv2_imshow("Hello world", image) cv2_imshow("Gray", gray_image)
histgram
import cv2 import numpy as np # We need to import matplotlib to create our histogram plots from matplotlib import pyplot as plt image = cv2.imread('../images/input.jpg') histogram = cv2.calcHist([image], [0], None, [256], [0, 256]) # We plot a histogram, ravel() flatens our image array plt.hist(image.ravel(), 256, [0, 256]); plt.show() # Viewing Separate Color Channels color = ('b', 'g', 'r') # We now separate the colors and plot each in the Histogram for i, col in enumerate(color): histogram2 = cv2.calcHist([image], [i], None, [256], [0, 256]) plt.plot(histogram2, color = col) plt.xlim([0,256]) plt.show()
Affine translation
- translation
height, weight = image.shape[:2] quarter_height = height // 4 quarter_weight = weight // 4 T = np.float32([[1, 0, quarter_weight], [0, 1, quarter_height]]) img_translation = cv2.warpAffine(image, T, (weight, height)) cv2_imshow('image translation', img_translation)
- rotation
height, weight = image.shape[:2] # cv2.getRotationMatrix2D((center_weight, center_weight), angle, scale) rotation_matrix = cv2.getRotationMatrix2D((weight // 2, height // 2), 90, 1) img_rotation = cv2.warpAffine(image, rotation_matrix, (weight, height)) # no black space # img_rotation = cv2.transpose(image) cv2_imshow('image rotation', img_rotation)
- resize : scaling and iterpolation
image_scaled = cv2.resize(image, None, fx=0.75, fy=0.75) cv2_imshow('Scaling - Linear Interpolation', image_scaled) image_scaled = cv2.resize(image, None, fx=2, fy=2, interpolation=cv2.INTER_CUBIC) cv2_imshow('Scaling - Cubic Interpolation', image_scaled) image_scaled = cv2.resize(image, (900, 1400), interpolation=cv2.INTER_AREA) cv2_imshow('Scaling - Skewed Size', image_scaled)
Pyramid
smaller = cv2.pyrDown(image) larger = cv2.pyrUp(smaller) cv2_imshow('Smaller', smaller) cv2_imshow('Larger', larger)
Cropped
height, weight = image.shape[:2] start_row, start_col = int(height * 0.25), int(weight * 0.25) end_row, end_col = int(height * 0.75), int(weight * 0.75) cropped_image = image[start_row:end_row, start_col:end_col] cv2_imshow('Smaller', cropped_image)
Arithmetic Operations
# added M = np.ones(image.shape, dtype='uint8') * 75 added = cv2.add(image, M) cv2_imshow('Added', added)
Bitwise Operation
# 長方形 square = np.zeros((300, 300), np.uint8) cv2.rectangle(square, (50, 50), (250, 250), 255, -2) cv2_imshow('Square', square) # 楕円 http://opencv.jp/opencv-2svn/cpp/drawing_functions.html ellipse = np.zeros((300, 300), np.uint8) cv2.ellipse(ellipse, (150, 150), (150, 150), 30, 0, 180, 255, -1) cv2_imshow('Square', ellipse) # Bitwise ## And And = cv2.bitwise_and(square, ellipse) cv2_imshow("And", And) ## Or Or = cv2.bitwise_or(square, ellipse) cv2_imshow("Or", Or)
Blurred (ぼやける)
elephant_image = cv2.imread('./images/elephant.jpg') # Create 3x3 kernel kerner_3x3 = np.ones((3, 3), np.float32) / 9 blurred = cv2.filter2D(elephant_image, -1, kerner_3x3) cv2_imshow('Original', elephant_image) cv2_imshow('Blurred', blurred) # Blurring method blur = cv2.blur(elephant_image, (3, 3)) cv2_imshow('Averaging Blur method', blur) # Gaussian Blur method Gaussian = cv2.GaussianBlur(elephant_image, (7, 7), 0) cv2_imshow('Gaussian Blur method', Gaussian) # Median Blur method Median = cv2.medianBlur(elephant_image, 5) cv2_imshow('Median Blur method', Median) # Bilateral Blur method (very efficient. noise cancel), エッジ保存 bilateral = cv2.bilateralFilter(elephant_image, 9, 75, 75) cv2_imshow('Bilateral Blur method', bilateral) # Image De-noising Non-Local Means Denoising dst = cv2.fastNlMeansDenoisingColored(elephant_image, None, 6, 6, 7, 21) cv2_imshow('Fast Means Denoising', dst)
sharpening
# filter2D は Convと逆のフィルタリング kernel_sharpening = np.array([ [-1, -1, -1], [-1, 9, -1], [-1, -1, -1] ]) shapened = cv2.filter2D(image, -1, kernel_sharpening) cv2_imshow('Image Sharpening', shapened)
thresholding, Binarization & Adaptive Thresholding
import cv2 import numpy as np # Load our image as greyscale image = cv2.imread('images/gradient.jpg',0) cv2_imshow('Original', image) # Values below 127 goes to 0 (black, everything above goes to 255 (white) ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) cv2_imshow('1 Threshold Binary', thresh1) # Values below 127 go to 255 and values above 127 go to 0 (reverse of above) ret,thresh2 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY_INV) cv2_imshow('2 Threshold Binary Inverse', thresh2) # Values above 127 are truncated (held) at 127 (the 255 argument is unused) ret,thresh3 = cv2.threshold(image, 127, 255, cv2.THRESH_TRUNC) cv2_imshow('3 THRESH TRUNC', thresh3) # Values below 127 go to 0, above 127 are unchanged ret,thresh4 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO) cv2_imshow('4 THRESH TOZERO', thresh4) # Resever of above, below 127 is unchanged, above 127 goes to 0 ret,thresh5 = cv2.threshold(image, 127, 255, cv2.THRESH_TOZERO_INV) cv2_imshow('5 THRESH TOZERO INV', thresh5)
# Load our new image image = cv2.imread('images/Origin_of_Species.jpg', 0) cv2_imshow('Original', image) # Values below 127 goes to 0 (black, everything above goes to 255 (white) ret,thresh1 = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY) cv2_imshow('Threshold Binary', thresh1) # It's good practice to blur images as it removes noise image = cv2.GaussianBlur(image, (3, 3), 0) # Using adaptiveThreshold thresh = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 5) cv2_imshow("Adaptive Mean Thresholding", thresh) _, th2 = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) cv2_imshow("Otsu's Thresholding", thresh) # Otsu's thresholding after Gaussian filtering blur = cv2.GaussianBlur(image, (5,5), 0) _, th3 = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) cv2_imshow("Guassian Otsu's Thresholding", thresh)
dilation(文字幅を厚く) and Erosion(文字幅を薄く)
- この場合、白いほうが分厚くなるので感覚値とは異なる。
- open: erosion folowed by dilation
- close: dilation followed by erosion
image = cv2.imread('images/opencv_inv.png', 0) cv2_imshow('Original', image) # Let's define our kernel size kernel = np.ones((5,5), np.uint8) # Now we erode erosion = cv2.erode(image, kernel, iterations = 1) cv2_imshow('Erosion', erosion) # dilation = cv2.dilate(image, kernel, iterations = 1) cv2_imshow('Dilation', dilation) # Opening - Good for removing noise opening = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel) cv2_imshow('Opening', opening) # Closing - Good for removing noise closing = cv2.morphologyEx(image, cv2.MORPH_CLOSE, kernel) cv2_imshow('Closing', closing)
edge detecting: sobel, Laplacian, canny
image = cv2.imread('images/input.jpg',0) height, width = image.shape # Extract Sobel Edges sobel_x = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=5) sobel_y = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=5) cv2_imshow('Original', image) cv2_imshow('Sobel X', sobel_x.astype('float32')) cv2_imshow('Sobel Y', sobel_y.astype('float32')) sobel_OR = cv2.bitwise_or(sobel_x, sobel_y) cv2_imshow('sobel_OR', sobel_OR.astype('float32')) laplacian = cv2.Laplacian(image, cv2.CV_64F) cv2_imshow('Laplacian', laplacian.astype('float32')) ## Then, we need to provide two values: threshold1 and threshold2. Any gradient value larger than threshold2 # is considered to be an edge. Any value below threshold1 is considered not to be an edge. #Values in between threshold1 and threshold2 are either classified as edges or non-edges based on how their #intensities are “connected”. In this case, any gradient values below 60 are considered non-edges #whereas any values above 120 are considered edges. # Canny Edge Detection uses gradient values as thresholds # The first threshold gradient canny = cv2.Canny(image, 50, 120) cv2_imshow('Canny', canny.astype('float32'))
Perspective Transform (Non-Affine)
import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('images/scan.jpg') cv2_imshow('Original', image) # Cordinates of the 4 corners of the original image points_A = np.float32([[320,15], [700,215], [85,610], [530,780]]) # Cordinates of the 4 corners of the desired output # We use a ratio of an A4 Paper 1 : 1.41 points_B = np.float32([[0,0], [420,0], [0,594], [420,594]]) # Use the two sets of four points to compute # the Perspective Transformation matrix, M M = cv2.getPerspectiveTransform(points_A, points_B) warped = cv2.warpPerspective(image, M, (420,594)) cv2_imshow('warpPerspective', warped) ## In affine transforms you only need 3 coordiantes to obtain the correct transform import cv2 import numpy as np import matplotlib.pyplot as plt image = cv2.imread('images/ex2.jpg') rows,cols,ch = image.shape cv2_imshow('Original', image) # Cordinates of the 4 corners of the original image points_A = np.float32([[320,15], [700,215], [85,610]]) # Cordinates of the 4 corners of the desired output # We use a ratio of an A4 Paper 1 : 1.41 points_B = np.float32([[0,0], [420,0], [0,594]]) # Use the two sets of four points to compute # the Perspective Transformation matrix, M M = cv2.getAffineTransform(points_A, points_B) warped = cv2.warpAffine(image, M, (cols, rows)) cv2_imshow('warpPerspective', warped)
imageをスケッチ
import cv2 import numpy as np # Our sketch generating function def sketch(image): # Convert image to grayscale img_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # Clean up image using Guassian Blur img_gray_blur = cv2.GaussianBlur(img_gray, (5,5), 0) # Extract edges canny_edges = cv2.Canny(img_gray_blur, 10, 70) # Do an invert binarize the image ret, mask = cv2.threshold(canny_edges, 70, 255, cv2.THRESH_BINARY_INV) return mask cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() cv2.imshow('Our Live Sketcher', sketch(frame)) if cv2.waitKey(1) == 13: #13 is the Enter Key break # Release camera and close windows cap.release() cv2.destroyAllWindows()
https://www.udemy.com/master-computer-vision-with-opencv-in-python/