├── README.md ├── blob.jpg ├── book1.jpg ├── book2.jpg ├── first-image.jpg ├── times-square.jpg ├── __pycache__ └── utils.cpython-37.pyc ├── blob_detection.py ├── utils.py ├── homography_book.py ├── homography.py ├── perspective-correction.py ├── homography2.py ├── virtual-billboard.py └── kaze_vbow.py /README.md: -------------------------------------------------------------------------------- 1 | # opencv_codes 2 | -------------------------------------------------------------------------------- /blob.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anil-matcha/opencv_codes/master/blob.jpg -------------------------------------------------------------------------------- /book1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anil-matcha/opencv_codes/master/book1.jpg -------------------------------------------------------------------------------- /book2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anil-matcha/opencv_codes/master/book2.jpg -------------------------------------------------------------------------------- /first-image.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anil-matcha/opencv_codes/master/first-image.jpg -------------------------------------------------------------------------------- /times-square.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anil-matcha/opencv_codes/master/times-square.jpg -------------------------------------------------------------------------------- /__pycache__/utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Anil-matcha/opencv_codes/master/__pycache__/utils.cpython-37.pyc -------------------------------------------------------------------------------- /blob_detection.py: -------------------------------------------------------------------------------- 1 | # Standard imports 2 | import cv2 3 | import numpy as np; 4 | 5 | params = cv2.SimpleBlobDetector_Params() 6 | params.filterByConvexity = True 7 | params.minConvexity = 0.1 8 | params.filterByCircularity = True 9 | params.minCircularity = 0.1 10 | params.filterByArea = True 11 | params.minArea = 100 12 | params.filterByInertia = True 13 | params.minInertiaRatio = 0.01 14 | params.minThreshold = 10; 15 | params.maxThreshold = 225; 16 | 17 | im = cv2.imread("blob.jpg", cv2.IMREAD_GRAYSCALE) 18 | detector = cv2.SimpleBlobDetector_create(params) 19 | keypoints = detector.detect(im) 20 | im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,255,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) 21 | cv2.imshow("Keypoints", im_with_keypoints) 22 | cv2.waitKey(0) -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def mouse_handler(event, x, y, flags, data) : 5 | 6 | if event == cv2.EVENT_LBUTTONDOWN : 7 | cv2.circle(data['im'], (x,y),3, (0,0,255), 5, 16); 8 | cv2.imshow("Image", data['im']); 9 | if len(data['points']) < 4 : 10 | data['points'].append([x,y]) 11 | 12 | def get_four_points(im): 13 | 14 | # Set up data to send to mouse handler 15 | data = {} 16 | data['im'] = im.copy() 17 | data['points'] = [] 18 | 19 | #Set the callback function for any mouse event 20 | cv2.imshow("Image",im) 21 | cv2.setMouseCallback("Image", mouse_handler, data) 22 | cv2.waitKey(0) 23 | 24 | # Convert array to np.array 25 | points = np.vstack(data['points']).astype(float) 26 | 27 | return points 28 | -------------------------------------------------------------------------------- /homography_book.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | if __name__ == '__main__' : 7 | 8 | # Read source image. 9 | im_src = cv2.imread('book2.jpg') 10 | # Four corners of the book in source image 11 | pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]], dtype=float) 12 | 13 | 14 | # Read destination image. 15 | im_dst = cv2.imread('book1.jpg') 16 | # Four corners of the book in destination image. 17 | pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]], dtype=float) 18 | 19 | # Calculate Homography 20 | h, status = cv2.findHomography(pts_src, pts_dst) 21 | 22 | # Warp source image to destination based on homography 23 | im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0])) 24 | 25 | # Display images 26 | cv2.imshow("Source Image", im_src) 27 | cv2.imshow("Destination Image", im_dst) 28 | cv2.imshow("Warped Source Image", im_out) 29 | 30 | cv2.waitKey(0) 31 | -------------------------------------------------------------------------------- /homography.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def mouseHandler(event,x,y,flags,param): 5 | global im_temp, pts_src 6 | 7 | if event == cv2.EVENT_LBUTTONDOWN: 8 | cv2.circle(im_temp,(x,y),3,(0,255,255),5,cv2.LINE_AA) 9 | cv2.imshow("Image", im_temp) 10 | if len(pts_src) < 4: 11 | pts_src = np.append(pts_src,[(x,y)],axis=0) 12 | 13 | 14 | # Read in the image. 15 | im_src = cv2.imread("book1.jpg") 16 | 17 | # Destination image 18 | height, width = 400, 300 19 | im_dst = np.zeros((height,width,3),dtype=np.uint8) 20 | 21 | 22 | # Create a list of points. 23 | pts_dst = np.empty((0,2)) 24 | pts_dst = np.append(pts_dst, [(0,0)], axis=0) 25 | pts_dst = np.append(pts_dst, [(width-1,0)], axis=0) 26 | pts_dst = np.append(pts_dst, [(width-1,height-1)], axis=0) 27 | pts_dst = np.append(pts_dst, [(0,height-1)], axis=0) 28 | 29 | # Create a window 30 | cv2.namedWindow("Image", 1) 31 | 32 | im_temp = im_src 33 | pts_src = np.empty((0,2)) 34 | 35 | cv2.setMouseCallback("Image",mouseHandler) 36 | 37 | 38 | cv2.imshow("Image", im_temp) 39 | cv2.waitKey(0) 40 | 41 | tform, status = cv2.findHomography(pts_src, pts_dst) 42 | im_dst = cv2.warpPerspective(im_src, tform,(width,height)) 43 | 44 | cv2.imshow("Image", im_dst) 45 | cv2.waitKey(0) -------------------------------------------------------------------------------- /perspective-correction.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import cv2 4 | import numpy as np 5 | from utils import get_four_points 6 | 7 | 8 | if __name__ == '__main__' : 9 | 10 | # Read in the image. 11 | im_src = cv2.imread("book1.jpg") 12 | 13 | # Destination image 14 | size = (300,400,3) 15 | 16 | im_dst = np.zeros(size, np.uint8) 17 | 18 | 19 | pts_dst = np.array( 20 | [ 21 | [0,0], 22 | [size[0] - 1, 0], 23 | [size[0] - 1, size[1] -1], 24 | [0, size[1] - 1 ] 25 | ], dtype=float 26 | ) 27 | 28 | 29 | print(''' 30 | Click on the four corners of the book -- top left first and 31 | bottom left last -- and then hit ENTER 32 | ''') 33 | 34 | # Show image and wait for 4 clicks. 35 | cv2.imshow("Image", im_src) 36 | pts_src = get_four_points(im_src); 37 | 38 | # Calculate the homography 39 | h, status = cv2.findHomography(pts_src, pts_dst) 40 | 41 | # Warp source image to destination 42 | im_dst = cv2.warpPerspective(im_src, h, size[0:2]) 43 | 44 | # Show output 45 | cv2.imshow("Image", im_dst) 46 | cv2.waitKey(0) 47 | 48 | 49 | -------------------------------------------------------------------------------- /homography2.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import sys 4 | 5 | def mouseHandler(event,x,y,flags,param): 6 | global im_temp, pts_dst 7 | 8 | if event == cv2.EVENT_LBUTTONDOWN: 9 | cv2.circle(im_temp,(x,y),3,(0,255,255),5,cv2.LINE_AA) 10 | cv2.imshow("Image", im_temp) 11 | if len(pts_dst) < 4: 12 | pts_dst = np.append(pts_dst,[(x,y)],axis=0) 13 | 14 | 15 | # Read in the image. 16 | im_src = cv2.imread("book1.jpg") 17 | height, width = im_src.shape[:2] 18 | 19 | # Create a list of points. 20 | pts_src = np.empty((0,2),dtype=np.int32) 21 | pts_src = np.append(pts_src, [(0,0)], axis=0) 22 | pts_src = np.append(pts_src, [(width-1,0)], axis=0) 23 | pts_src = np.append(pts_src, [(width-1,height-1)], axis=0) 24 | pts_src = np.append(pts_src, [(0,height-1)], axis=0) 25 | 26 | # Destination image 27 | im_dst = cv2.imread("book2.jpg") 28 | 29 | # Create a window 30 | cv2.namedWindow("Image", 1) 31 | 32 | im_temp = im_dst 33 | pts_dst = np.empty((0,2),dtype=np.int32) 34 | 35 | cv2.setMouseCallback("Image",mouseHandler) 36 | 37 | 38 | cv2.imshow("Image", im_temp) 39 | cv2.waitKey(0) 40 | 41 | tform, status = cv2.findHomography(pts_src, pts_dst) 42 | im_temp = cv2.warpPerspective(im_src, tform,(width,height)) 43 | 44 | cv2.fillConvexPoly(im_dst, pts_dst, 0, cv2.LINE_AA) 45 | im_dst = im_dst + im_temp 46 | 47 | cv2.imshow("Image", im_dst) 48 | cv2.waitKey(0) 49 | -------------------------------------------------------------------------------- /virtual-billboard.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import cv2 4 | import numpy as np 5 | from utils import mouse_handler 6 | from utils import get_four_points 7 | import sys 8 | 9 | 10 | if __name__ == '__main__' : 11 | 12 | # Read source image. 13 | im_src = cv2.imread('first-image.jpg'); 14 | size = im_src.shape 15 | 16 | # Create a vector of source points. 17 | pts_src = np.array( 18 | [ 19 | [0,0], 20 | [size[1] - 1, 0], 21 | [size[1] - 1, size[0] -1], 22 | [0, size[0] - 1 ] 23 | ],dtype=float 24 | ); 25 | 26 | 27 | # Read destination image 28 | im_dst = cv2.imread('times-square.jpg'); 29 | 30 | # Get four corners of the billboard 31 | print('Click on four corners of a billboard and then press ENTER') 32 | pts_dst = get_four_points(im_dst) 33 | 34 | # Calculate Homography between source and destination points 35 | h, status = cv2.findHomography(pts_src, pts_dst); 36 | 37 | # Warp source image 38 | im_temp = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0])) 39 | 40 | # Black out polygonal area in destination image. 41 | cv2.fillConvexPoly(im_dst, pts_dst.astype(int), 0, 16); 42 | 43 | # Add warped source image to destination image. 44 | im_dst = im_dst + im_temp; 45 | 46 | # Display image. 47 | cv2.imshow("Image", im_dst); 48 | cv2.waitKey(0); -------------------------------------------------------------------------------- /kaze_vbow.py: -------------------------------------------------------------------------------- 1 | import cv2, os 2 | from sklearn.cluster import KMeans 3 | from sklearn.neighbors import NearestNeighbors 4 | import numpy as np 5 | 6 | extractor = cv2.AKAZE_create() 7 | 8 | def build_histogram(descriptor_list, cluster_alg): 9 | histogram = np.zeros(len(cluster_alg.cluster_centers_)) 10 | cluster_result = cluster_alg.predict(descriptor_list) 11 | for i in cluster_result: 12 | histogram[i] += 1.0 13 | return histogram 14 | 15 | def gray(image): 16 | gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 17 | return gray 18 | 19 | def features(image, extractor): 20 | keypoints, descriptors = extractor.detectAndCompute(image, None) 21 | return keypoints, descriptors 22 | 23 | kmeans = KMeans(n_clusters = 800) 24 | preprocessed_image = [] 25 | files = [x for x in os.listdir() if "jpg" in x] 26 | print(files) 27 | images = [cv2.imread(img) for img in files] 28 | descriptor_list = np.array([]) 29 | for image in images: 30 | image = gray(image) 31 | keypoint, descriptor = features(image, extractor) 32 | if len(descriptor_list) == 0: 33 | descriptor_list = np.array(descriptor) 34 | else: 35 | descriptor_list = np.vstack((descriptor_list, descriptor)) 36 | kmeans.fit(descriptor_list) 37 | for image in images: 38 | image = gray(image) 39 | keypoint, descriptor = features(image, extractor) 40 | if (descriptor is not None): 41 | histogram = build_histogram(descriptor, kmeans) 42 | preprocessed_image.append(histogram) 43 | 44 | data = cv2.imread("book1.jpg") 45 | data = gray(data) 46 | keypoint, descriptor = features(data, extractor) 47 | histogram = build_histogram(descriptor, kmeans) 48 | neighbor = NearestNeighbors(n_neighbors = 5) 49 | neighbor.fit(preprocessed_image) 50 | dist, result = neighbor.kneighbors([histogram]) 51 | print([files[i] for i in result[0]]) --------------------------------------------------------------------------------