├── Chapter01 ├── Old │ ├── affine.py │ ├── color_spaces.py │ ├── geometric_transformations.py │ ├── projective.py │ ├── read_display_save.py │ └── warping.py ├── affine.py ├── color_spaces.py ├── geometric_transformations.py ├── projective.py ├── read_display_save.py └── warping.py ├── Chapter02 ├── Old │ ├── convolution.py │ ├── edge_detection.py │ ├── histogram.py │ ├── image_filters.py │ └── vignette.py ├── convolution.py ├── edge_detection.py ├── histogram.py ├── image_filters.py └── vignette.py ├── Chapter03 ├── Old │ ├── bilateral_filter.py │ ├── cartoonize.py │ ├── keyboard.py │ ├── median_filter.py │ ├── mouse_simple.py │ ├── mouse_webcam.py │ └── webcam.py ├── bilateral_filter.py ├── cartoonize.py ├── keyboard.py ├── median_filter.py ├── mouse_simple.py ├── mouse_webcam.py └── webcam.py ├── Chapter04 ├── Old │ ├── cascade_files │ │ ├── haarcascade_eye.xml │ │ ├── haarcascade_frontalface_alt.xml │ │ ├── haarcascade_mcs_leftear.xml │ │ ├── haarcascade_mcs_mouth.xml │ │ ├── haarcascade_mcs_nose.xml │ │ └── haarcascade_mcs_rightear.xml │ ├── ear_detector.py │ ├── eye_detector.py │ ├── eye_sunglasses.py │ ├── face_detector.py │ ├── face_hannibal.py │ ├── face_skull.py │ ├── mouth_detector.py │ ├── mouth_moustache.py │ ├── nose_detector.py │ └── pupil_detector.py ├── ear_detector.py ├── eye_detector.py ├── eye_sunglasses.py ├── face_detector.py ├── face_hannibal.py ├── face_skull.py ├── haarcascade_eye.xml ├── haarcascade_frontalface_alt.xml ├── haarcascade_mcs_leftear.xml ├── haarcascade_mcs_mouth.xml ├── haarcascade_mcs_nose.xml ├── haarcascade_mcs_rightear.xml ├── mouth_detector.py ├── mouth_moustache.py ├── nose_detector.py └── pupil_detector.py ├── Chapter05 ├── Old │ ├── brief_with_fast.py │ ├── brief_with_star.py │ ├── fast.py │ ├── good_features_to_track.py │ ├── harris_corners.py │ ├── orb.py │ ├── sift_features.py │ └── surf_features.py ├── brief_with_fast.py ├── brief_with_star.py ├── fast.py ├── good_features_to_track.py ├── harris_corners.py ├── orb.py ├── sift_features.py └── surf_features.py ├── Chapter06 ├── Old │ ├── feature_matching.py │ └── pano_stitch.py ├── feature_matching.py └── pano_stitch.py ├── Chapter07 ├── object_removal.py ├── seam_carver_compress.py └── seam_carver_expand.py ├── Chapter08 ├── contour_approx.py ├── convexity_defects_ver1.py ├── convexity_defects_ver2.py ├── grabcut.py ├── shape_censor.py └── shape_matcher.py ├── Chapter09 ├── Old │ ├── background_subtraction.py │ ├── camshift.py │ ├── colorspaces.py │ ├── frame_diff.py │ └── optical_flow.py ├── background_subtraction.py ├── camshift.py ├── colorspaces.py ├── frame_diff.py └── optical_flow.py ├── Chapter10 ├── Old │ ├── classify_data.py │ ├── create_features.py │ ├── dense_detector.py │ └── training.py ├── classify_data.py ├── create_features.py ├── dense_detector.py └── training.py ├── Chapter11 ├── find_fund_matrix.py └── stereo_match.py ├── Chapter12 ├── augmented_reality.py ├── augmented_reality_circular_motion.py ├── augmented_reality_dancing.py ├── augmented_reality_linear_motion.py ├── augmented_reality_variable_height.py └── pose_estimation.py ├── LICENSE └── README.md /Chapter01/Old/affine.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/prateek.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | #src_points = np.float32([[0,0], [cols-1,0], [0,rows-1]]) 8 | #dst_points = np.float32([[0,0], [int(0.6*(cols-1)),0], [int(0.4*(cols-1)),rows-1]]) 9 | src_points = np.float32([[0,0], [cols-1,0], [0,rows-1]]) 10 | dst_points = np.float32([[cols-1,0], [0,0], [cols-1,rows-1]]) 11 | 12 | affine_matrix = cv2.getAffineTransform(src_points, dst_points) 13 | 14 | img_output = cv2.warpAffine(img, affine_matrix, (cols,rows)) 15 | 16 | cv2.imshow('Input', img) 17 | cv2.imshow('Output', img_output) 18 | cv2.waitKey() 19 | -------------------------------------------------------------------------------- /Chapter01/Old/color_spaces.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | img = cv2.imread('../images/input.jpg') 4 | 5 | # Original image 6 | cv2.imshow('Original image', img) 7 | 8 | # Convert to grayscale 9 | gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 10 | cv2.imshow('Grayscale image', gray_img) 11 | 12 | # Convert to YUV 13 | yuv_img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) 14 | cv2.imshow('YUV image', yuv_img) 15 | 16 | # Displaying the channels of YUV separately 17 | #cv2.imshow('Y channel', yuv_img[:,:,0]) 18 | #cv2.imshow('U channel', yuv_img[:,:,1]) 19 | #cv2.imshow('V channel', yuv_img[:,:,2]) 20 | 21 | # Convert to HSV 22 | hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 23 | cv2.imshow('HSV image', hsv_img) 24 | 25 | # Displaying the channels of HSV separately 26 | cv2.imshow('H channel', hsv_img[:,:,0]) 27 | cv2.imshow('S channel', hsv_img[:,:,1]) 28 | cv2.imshow('V channel', hsv_img[:,:,2]) 29 | 30 | cv2.waitKey() 31 | -------------------------------------------------------------------------------- /Chapter01/Old/geometric_transformations.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input.jpg') 5 | cv2.imshow('Original', img) 6 | 7 | ########## 8 | # Translation 9 | 10 | num_rows, num_cols = img.shape[:2] 11 | 12 | translation_matrix = np.float32([ [1,0,70], [0,1,110] ]) 13 | img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110)) 14 | translation_matrix = np.float32([ [1,0,-30], [0,1,-50] ]) 15 | img_translation = cv2.warpAffine(img_translation, translation_matrix, (num_cols + 70 + 30, num_rows + 110 + 50)) 16 | #cv2.imshow('Translation', img_translation) 17 | 18 | ########## 19 | # Rotation 20 | 21 | # cropped 22 | rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 30, 1) 23 | img_rotation = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows)) 24 | #cv2.imshow('Rotation', img_rotation) 25 | 26 | # not cropped 27 | translation_matrix = np.float32([ [1,0,int(0.5*num_cols)], [0,1,int(0.5*num_rows)] ]) 28 | img_translation = cv2.warpAffine(img, translation_matrix, (2*num_cols, 2*num_rows)) 29 | 30 | rotation_matrix = cv2.getRotationMatrix2D((num_cols, num_rows), 30, 1) 31 | img_rotation = cv2.warpAffine(img_translation, rotation_matrix, (2*num_cols, 2*num_rows)) 32 | 33 | #cv2.imshow('Rotation', img_rotation) 34 | 35 | ########## 36 | 37 | # Scaling 38 | img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_LINEAR) 39 | cv2.imshow('Scaling - Linear Interpolation', img_scaled) 40 | img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_CUBIC) 41 | cv2.imshow('Scaling - Cubic Interpolation', img_scaled) 42 | img_scaled = cv2.resize(img,(450, 400), interpolation = cv2.INTER_AREA) 43 | cv2.imshow('Scaling - Skewed Size', img_scaled) 44 | 45 | cv2.waitKey() 46 | -------------------------------------------------------------------------------- /Chapter01/Old/projective.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/IMG_2374.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | #src_points = np.float32([[0,0], [cols-1,0], [0,rows-1], [cols-1,rows-1]]) 8 | #dst_points = np.float32([[0,0], [cols-1,0], [int(0.33*cols),rows-1], [int(0.66*cols),rows-1]]) 9 | #src_points = np.float32([[0,0], [0,rows-1], [cols/2,0], [cols/2,rows-1]]) 10 | #dst_points = np.float32([[0,100], [0,rows-101], [cols/2,0], [cols/2,rows-1]]) 11 | src_points = np.float32([[261,1064], [1598,847], [1201,2218], [3087,1583]]) 12 | dst_points = np.float32([[0,0], [480,0], [0,640], [480,640]]) 13 | 14 | projective_matrix = cv2.getPerspectiveTransform(src_points, dst_points) 15 | 16 | img_output = cv2.warpPerspective(img, projective_matrix, (480,640)) 17 | 18 | #cv2.imshow('Input', img) 19 | cv2.imshow('Output', img_output) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter01/Old/read_display_save.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | img = cv2.imread('../images/input.jpg') 4 | cv2.imshow('Input image', img) 5 | 6 | # Load the image in grayscale mode 7 | gray_img = cv2.imread('../images/input.jpg', cv2.IMREAD_GRAYSCALE) 8 | cv2.imshow('Grayscale', gray_img) 9 | 10 | # Saving an image 11 | #cv2.imwrite('../images/output.jpg', gray_img) 12 | cv2.waitKey() 13 | -------------------------------------------------------------------------------- /Chapter01/Old/warping.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import math 4 | 5 | img = cv2.imread('../images/prateek.jpg', cv2.IMREAD_GRAYSCALE) 6 | rows, cols = img.shape 7 | 8 | ##################### 9 | # Vertical wave 10 | 11 | img_output = np.zeros(img.shape, dtype=img.dtype) 12 | 13 | for i in range(rows): 14 | for j in range(cols): 15 | offset_x = int(25.0 * math.sin(2 * 3.14 * i / 180)) 16 | offset_y = 0 17 | if j+offset_x < rows: 18 | img_output[i,j] = img[i,(j+offset_x)%cols] 19 | else: 20 | img_output[i,j] = 0 21 | 22 | cv2.imshow('Input', img) 23 | cv2.imshow('Vertical wave', img_output) 24 | 25 | ##################### 26 | # Horizontal wave 27 | 28 | img_output = np.zeros(img.shape, dtype=img.dtype) 29 | 30 | for i in range(rows): 31 | for j in range(cols): 32 | offset_x = 0 33 | offset_y = int(16.0 * math.sin(2 * 3.14 * j / 150)) 34 | if i+offset_y < rows: 35 | img_output[i,j] = img[(i+offset_y)%rows,j] 36 | else: 37 | img_output[i,j] = 0 38 | 39 | cv2.imshow('Horizontal wave', img_output) 40 | 41 | ##################### 42 | # Both horizontal and vertical wave 43 | 44 | img_output = np.zeros(img.shape, dtype=img.dtype) 45 | 46 | for i in range(rows): 47 | for j in range(cols): 48 | offset_x = int(20.0 * math.sin(2 * 3.14 * i / 150)) 49 | offset_y = int(20.0 * math.cos(2 * 3.14 * j / 150)) 50 | if i+offset_y < rows and j+offset_x < cols: 51 | img_output[i,j] = img[(i+offset_y)%rows,(j+offset_x)%cols] 52 | else: 53 | img_output[i,j] = 0 54 | 55 | cv2.imshow('Multidirectional wave', img_output) 56 | 57 | ##################### 58 | # Concave effect 59 | 60 | img_output = np.zeros(img.shape, dtype=img.dtype) 61 | 62 | for i in range(rows): 63 | for j in range(cols): 64 | offset_x = int(128.0 * math.sin(2 * 3.14 * i / (2*cols))) 65 | offset_y = 0 66 | if j+offset_x < cols: 67 | img_output[i,j] = img[i,(j+offset_x)%cols] 68 | else: 69 | img_output[i,j] = 0 70 | 71 | cv2.imshow('Concave', img_output) 72 | 73 | cv2.waitKey() 74 | 75 | -------------------------------------------------------------------------------- /Chapter01/affine.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/prateek.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | #src_points = np.float32([[0,0], [cols-1,0], [0,rows-1]]) 8 | #dst_points = np.float32([[0,0], [int(0.6*(cols-1)),0], [int(0.4*(cols-1)),rows-1]]) 9 | src_points = np.float32([[0,0], [cols-1,0], [0,rows-1]]) 10 | dst_points = np.float32([[cols-1,0], [0,0], [cols-1,rows-1]]) 11 | 12 | affine_matrix = cv2.getAffineTransform(src_points, dst_points) 13 | 14 | img_output = cv2.warpAffine(img, affine_matrix, (cols,rows)) 15 | 16 | cv2.imshow('Input', img) 17 | cv2.imshow('Output', img_output) 18 | cv2.waitKey() 19 | -------------------------------------------------------------------------------- /Chapter01/color_spaces.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | img = cv2.imread('../images/input.jpg') 4 | 5 | # Original image 6 | cv2.imshow('Original image', img) 7 | 8 | # Convert to grayscale 9 | gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 10 | cv2.imshow('Grayscale image', gray_img) 11 | 12 | # Convert to YUV 13 | yuv_img = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) 14 | cv2.imshow('YUV image', yuv_img) 15 | 16 | # Displaying the channels of YUV separately 17 | #cv2.imshow('Y channel', yuv_img[:,:,0]) 18 | #cv2.imshow('U channel', yuv_img[:,:,1]) 19 | #cv2.imshow('V channel', yuv_img[:,:,2]) 20 | 21 | # Convert to HSV 22 | hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 23 | cv2.imshow('HSV image', hsv_img) 24 | 25 | # Displaying the channels of HSV separately 26 | cv2.imshow('H channel', hsv_img[:,:,0]) 27 | cv2.imshow('S channel', hsv_img[:,:,1]) 28 | cv2.imshow('V channel', hsv_img[:,:,2]) 29 | 30 | cv2.waitKey() 31 | -------------------------------------------------------------------------------- /Chapter01/geometric_transformations.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input.jpg') 5 | cv2.imshow('Original', img) 6 | 7 | ########## 8 | # Translation 9 | 10 | num_rows, num_cols = img.shape[:2] 11 | 12 | translation_matrix = np.float32([ [1,0,70], [0,1,110] ]) 13 | img_translation = cv2.warpAffine(img, translation_matrix, (num_cols + 70, num_rows + 110)) 14 | translation_matrix = np.float32([ [1,0,-30], [0,1,-50] ]) 15 | img_translation = cv2.warpAffine(img_translation, translation_matrix, (num_cols + 70 + 30, num_rows + 110 + 50)) 16 | #cv2.imshow('Translation', img_translation) 17 | 18 | ########## 19 | # Rotation 20 | 21 | # cropped 22 | rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), 30, 1) 23 | img_rotation = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows)) 24 | #cv2.imshow('Rotation', img_rotation) 25 | 26 | # not cropped 27 | translation_matrix = np.float32([ [1,0,int(0.5*num_cols)], [0,1,int(0.5*num_rows)] ]) 28 | img_translation = cv2.warpAffine(img, translation_matrix, (2*num_cols, 2*num_rows)) 29 | 30 | rotation_matrix = cv2.getRotationMatrix2D((num_cols, num_rows), 30, 1) 31 | img_rotation = cv2.warpAffine(img_translation, rotation_matrix, (2*num_cols, 2*num_rows)) 32 | 33 | #cv2.imshow('Rotation', img_rotation) 34 | 35 | ########## 36 | 37 | # Scaling 38 | img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_LINEAR) 39 | cv2.imshow('Scaling - Linear Interpolation', img_scaled) 40 | img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_CUBIC) 41 | cv2.imshow('Scaling - Cubic Interpolation', img_scaled) 42 | img_scaled = cv2.resize(img,(450, 400), interpolation = cv2.INTER_AREA) 43 | cv2.imshow('Scaling - Skewed Size', img_scaled) 44 | 45 | cv2.waitKey() 46 | -------------------------------------------------------------------------------- /Chapter01/projective.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/IMG_2374.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | #src_points = np.float32([[0,0], [cols-1,0], [0,rows-1], [cols-1,rows-1]]) 8 | #dst_points = np.float32([[0,0], [cols-1,0], [int(0.33*cols),rows-1], [int(0.66*cols),rows-1]]) 9 | #src_points = np.float32([[0,0], [0,rows-1], [cols/2,0], [cols/2,rows-1]]) 10 | #dst_points = np.float32([[0,100], [0,rows-101], [cols/2,0], [cols/2,rows-1]]) 11 | src_points = np.float32([[261,1064], [1598,847], [1201,2218], [3087,1583]]) 12 | dst_points = np.float32([[0,0], [480,0], [0,640], [480,640]]) 13 | 14 | projective_matrix = cv2.getPerspectiveTransform(src_points, dst_points) 15 | 16 | img_output = cv2.warpPerspective(img, projective_matrix, (480,640)) 17 | 18 | #cv2.imshow('Input', img) 19 | cv2.imshow('Output', img_output) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter01/read_display_save.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | img = cv2.imread('../images/input.jpg') 4 | cv2.imshow('Input image', img) 5 | 6 | # Load the image in grayscale mode 7 | gray_img = cv2.imread('../images/input.jpg', cv2.IMREAD_GRAYSCALE) 8 | cv2.imshow('Grayscale', gray_img) 9 | 10 | # Saving an image 11 | #cv2.imwrite('../images/output.jpg', gray_img) 12 | cv2.waitKey() 13 | -------------------------------------------------------------------------------- /Chapter01/warping.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | import math 4 | 5 | img = cv2.imread('../images/prateek.jpg', cv2.IMREAD_GRAYSCALE) 6 | rows, cols = img.shape 7 | 8 | ##################### 9 | # Vertical wave 10 | 11 | img_output = np.zeros(img.shape, dtype=img.dtype) 12 | 13 | for i in range(rows): 14 | for j in range(cols): 15 | offset_x = int(25.0 * math.sin(2 * 3.14 * i / 180)) 16 | offset_y = 0 17 | if j+offset_x < rows: 18 | img_output[i,j] = img[i,(j+offset_x)%cols] 19 | else: 20 | img_output[i,j] = 0 21 | 22 | cv2.imshow('Input', img) 23 | cv2.imshow('Vertical wave', img_output) 24 | 25 | ##################### 26 | # Horizontal wave 27 | 28 | img_output = np.zeros(img.shape, dtype=img.dtype) 29 | 30 | for i in range(rows): 31 | for j in range(cols): 32 | offset_x = 0 33 | offset_y = int(16.0 * math.sin(2 * 3.14 * j / 150)) 34 | if i+offset_y < rows: 35 | img_output[i,j] = img[(i+offset_y)%rows,j] 36 | else: 37 | img_output[i,j] = 0 38 | 39 | cv2.imshow('Horizontal wave', img_output) 40 | 41 | ##################### 42 | # Both horizontal and vertical wave 43 | 44 | img_output = np.zeros(img.shape, dtype=img.dtype) 45 | 46 | for i in range(rows): 47 | for j in range(cols): 48 | offset_x = int(20.0 * math.sin(2 * 3.14 * i / 150)) 49 | offset_y = int(20.0 * math.cos(2 * 3.14 * j / 150)) 50 | if i+offset_y < rows and j+offset_x < cols: 51 | img_output[i,j] = img[(i+offset_y)%rows,(j+offset_x)%cols] 52 | else: 53 | img_output[i,j] = 0 54 | 55 | cv2.imshow('Multidirectional wave', img_output) 56 | 57 | ##################### 58 | # Concave effect 59 | 60 | img_output = np.zeros(img.shape, dtype=img.dtype) 61 | 62 | for i in range(rows): 63 | for j in range(cols): 64 | offset_x = int(128.0 * math.sin(2 * 3.14 * i / (2*cols))) 65 | offset_y = 0 66 | if j+offset_x < cols: 67 | img_output[i,j] = img[i,(j+offset_x)%cols] 68 | else: 69 | img_output[i,j] = 0 70 | 71 | cv2.imshow('Concave', img_output) 72 | 73 | cv2.waitKey() 74 | 75 | -------------------------------------------------------------------------------- /Chapter02/Old/convolution.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_sharp_edges.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | kernel_identity = np.array([[0,0,0], [0,1,0], [0,0,0]]) 8 | kernel_3x3 = np.ones((3,3), np.float32) / 9.0 9 | kernel_5x5 = np.ones((5,5), np.float32) / 25.0 10 | 11 | cv2.imshow('Original', img) 12 | 13 | output = cv2.filter2D(img, -1, kernel_identity) 14 | cv2.imshow('Identity filter', output) 15 | 16 | output = cv2.filter2D(img, -1, kernel_3x3) 17 | cv2.imshow('3x3 filter', output) 18 | 19 | output = cv2.filter2D(img, -1, kernel_5x5) 20 | cv2.imshow('5x5 filter', output) 21 | 22 | cv2.waitKey() 23 | -------------------------------------------------------------------------------- /Chapter02/Old/edge_detection.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_train.jpg', cv2.IMREAD_GRAYSCALE) 5 | rows, cols = img.shape 6 | 7 | sobel_horizontal_1 = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) 8 | sobel_horizontal_2 = cv2.Sobel(img, cv2.CV_64F, 2, 0, ksize=5) 9 | sobel_horizontal_3 = cv2.Sobel(img, cv2.CV_64F, 3, 0, ksize=5) 10 | sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) 11 | laplacian = cv2.Laplacian(img, cv2.CV_64F) 12 | canny = cv2.Canny(img, 50, 240) 13 | 14 | cv2.imshow('Original', img) 15 | #cv2.imshow('Sobel horizontal 1', sobel_horizontal_1) 16 | #cv2.imshow('Sobel horizontal 2', sobel_horizontal_2) 17 | #cv2.imshow('Sobel horizontal 3', sobel_horizontal_3) 18 | #cv2.imshow('Sobel vertical', sobel_vertical) 19 | cv2.imshow('Laplacian', laplacian) 20 | cv2.imshow('Canny', canny) 21 | 22 | cv2.waitKey() 23 | -------------------------------------------------------------------------------- /Chapter02/Old/histogram.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_histogram.jpg', 0) 5 | histeq = cv2.equalizeHist(img) 6 | 7 | #cv2.imshow('Input', img) 8 | #cv2.imshow('Histogram equalized', histeq) 9 | 10 | ################## 11 | # Histogram equalization of color images 12 | 13 | img = cv2.imread('../images/input_histogram_color.jpg') 14 | 15 | img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) 16 | img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0]) 17 | 18 | img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) 19 | 20 | cv2.imshow('Color input image', img) 21 | cv2.imshow('Histogram equalized', img_output) 22 | 23 | cv2.waitKey() 24 | 25 | -------------------------------------------------------------------------------- /Chapter02/Old/image_filters.py: -------------------------------------------------------------------------------- 1 | # http://lodev.org/cgtutor/filtering.html 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | #img = cv2.imread('../images/input_sharp_edges.jpg', cv2.IMREAD_GRAYSCALE) 7 | img = cv2.imread('../images/input_tree.jpg') 8 | rows, cols = img.shape[:2] 9 | #cv2.imshow('Original', img) 10 | 11 | ################### 12 | # Motion Blur 13 | size = 15 14 | kernel_motion_blur = np.zeros((size, size)) 15 | kernel_motion_blur[int((size-1)/2), :] = np.ones(size) 16 | kernel_motion_blur = kernel_motion_blur / size 17 | output = cv2.filter2D(img, -1, kernel_motion_blur) 18 | #cv2.imshow('Motion Blur', output) 19 | 20 | ################### 21 | # Sharpening 22 | kernel_sharpen_1 = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) 23 | kernel_sharpen_2 = np.array([[1,1,1], [1,-7,1], [1,1,1]]) 24 | kernel_sharpen_3 = np.array([[-1,-1,-1,-1,-1], 25 | [-1,2,2,2,-1], 26 | [-1,2,8,2,-1], 27 | [-1,2,2,2,-1], 28 | [-1,-1,-1,-1,-1]]) / 8.0 29 | output_1 = cv2.filter2D(img, -1, kernel_sharpen_1) 30 | output_2 = cv2.filter2D(img, -1, kernel_sharpen_2) 31 | output_3 = cv2.filter2D(img, -1, kernel_sharpen_3) 32 | #cv2.imshow('Sharpening', output_1) 33 | #cv2.imshow('Excessive Sharpening', output_2) 34 | #cv2.imshow('Edge Enhancement', output_3) 35 | 36 | ################### 37 | # Embossing 38 | img_emboss_input = cv2.imread('../images/input_house.jpg') 39 | kernel_emboss_1 = np.array([[0,-1,-1], 40 | [1,0,-1], 41 | [1,1,0]]) 42 | kernel_emboss_2 = np.array([[-1,-1,0], 43 | [-1,0,1], 44 | [0,1,1]]) 45 | kernel_emboss_3 = np.array([[1,0,0], 46 | [0,0,0], 47 | [0,0,-1]]) 48 | gray_img = cv2.cvtColor(img_emboss_input,cv2.COLOR_BGR2GRAY) 49 | output_1 = cv2.filter2D(gray_img, -1, kernel_emboss_1) 50 | output_2 = cv2.filter2D(gray_img, -1, kernel_emboss_2) 51 | output_3 = cv2.filter2D(gray_img, -1, kernel_emboss_3) 52 | cv2.imshow('Input', img_emboss_input) 53 | cv2.imshow('Embossing - South West', output_1 + 128) 54 | cv2.imshow('Embossing - South East', output_2 + 128) 55 | cv2.imshow('Embossing - North West', output_3 + 128) 56 | 57 | ################### 58 | # Erosion and dilation 59 | 60 | img = cv2.imread('../images/input_morphology.png',0) 61 | kernel = np.ones((5,5), np.uint8) 62 | img_erosion = cv2.erode(img, kernel, iterations=1) 63 | img_dilation = cv2.dilate(img, kernel, iterations=1) 64 | #cv2.imshow('Input', img) 65 | #cv2.imshow('Erosion', img_erosion) 66 | #cv2.imshow('Dilation', img_dilation) 67 | 68 | cv2.waitKey() 69 | 70 | -------------------------------------------------------------------------------- /Chapter02/Old/vignette.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_flowers.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | kernel_x = cv2.getGaussianKernel(cols,200) 8 | kernel_y = cv2.getGaussianKernel(rows,200) 9 | kernel = kernel_y * kernel_x.T 10 | mask = 255 * kernel / np.linalg.norm(kernel) 11 | output = np.copy(img) 12 | 13 | for i in range(3): 14 | output[:,:,i] = output[:,:,i] * mask 15 | 16 | #cv2.imshow('Original', img) 17 | #cv2.imshow('Vignette', output) 18 | 19 | ################ 20 | # Shifting the focus 21 | 22 | kernel_x = cv2.getGaussianKernel(int(1.5*cols),200) 23 | kernel_y = cv2.getGaussianKernel(int(1.5*rows),200) 24 | kernel = kernel_y * kernel_x.T 25 | mask = 255 * kernel / np.linalg.norm(kernel) 26 | mask = mask[int(0.5*rows):, int(0.5*cols):] 27 | output = np.copy(img) 28 | 29 | for i in range(3): 30 | output[:,:,i] = output[:,:,i] * mask 31 | 32 | cv2.imshow('Input', img) 33 | cv2.imshow('Vignette with shifted focus', output) 34 | 35 | 36 | cv2.waitKey() 37 | -------------------------------------------------------------------------------- /Chapter02/convolution.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_sharp_edges.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | kernel_identity = np.array([[0,0,0], [0,1,0], [0,0,0]]) 8 | kernel_3x3 = np.ones((3,3), np.float32) / 9.0 9 | kernel_5x5 = np.ones((5,5), np.float32) / 25.0 10 | 11 | cv2.imshow('Original', img) 12 | 13 | output = cv2.filter2D(img, -1, kernel_identity) 14 | cv2.imshow('Identity filter', output) 15 | 16 | output = cv2.filter2D(img, -1, kernel_3x3) 17 | cv2.imshow('3x3 filter', output) 18 | 19 | output = cv2.filter2D(img, -1, kernel_5x5) 20 | cv2.imshow('5x5 filter', output) 21 | 22 | cv2.waitKey() 23 | -------------------------------------------------------------------------------- /Chapter02/edge_detection.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_train.jpg', cv2.IMREAD_GRAYSCALE) 5 | rows, cols = img.shape 6 | 7 | sobel_horizontal_1 = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) 8 | sobel_horizontal_2 = cv2.Sobel(img, cv2.CV_64F, 2, 0, ksize=5) 9 | sobel_horizontal_3 = cv2.Sobel(img, cv2.CV_64F, 3, 0, ksize=5) 10 | sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) 11 | laplacian = cv2.Laplacian(img, cv2.CV_64F) 12 | canny = cv2.Canny(img, 50, 240) 13 | 14 | cv2.imshow('Original', img) 15 | #cv2.imshow('Sobel horizontal 1', sobel_horizontal_1) 16 | #cv2.imshow('Sobel horizontal 2', sobel_horizontal_2) 17 | #cv2.imshow('Sobel horizontal 3', sobel_horizontal_3) 18 | #cv2.imshow('Sobel vertical', sobel_vertical) 19 | cv2.imshow('Laplacian', laplacian) 20 | cv2.imshow('Canny', canny) 21 | 22 | cv2.waitKey() 23 | -------------------------------------------------------------------------------- /Chapter02/histogram.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_histogram.jpg', 0) 5 | histeq = cv2.equalizeHist(img) 6 | 7 | #cv2.imshow('Input', img) 8 | #cv2.imshow('Histogram equalized', histeq) 9 | 10 | ################## 11 | # Histogram equalization of color images 12 | 13 | img = cv2.imread('../images/input_histogram_color.jpg') 14 | 15 | img_yuv = cv2.cvtColor(img, cv2.COLOR_BGR2YUV) 16 | img_yuv[:,:,0] = cv2.equalizeHist(img_yuv[:,:,0]) 17 | 18 | img_output = cv2.cvtColor(img_yuv, cv2.COLOR_YUV2BGR) 19 | 20 | cv2.imshow('Color input image', img) 21 | cv2.imshow('Histogram equalized', img_output) 22 | 23 | cv2.waitKey() 24 | 25 | -------------------------------------------------------------------------------- /Chapter02/image_filters.py: -------------------------------------------------------------------------------- 1 | # http://lodev.org/cgtutor/filtering.html 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | #img = cv2.imread('../images/input_sharp_edges.jpg', cv2.IMREAD_GRAYSCALE) 7 | img = cv2.imread('../images/input_tree.jpg') 8 | rows, cols = img.shape[:2] 9 | #cv2.imshow('Original', img) 10 | 11 | ################### 12 | # Motion Blur 13 | size = 15 14 | kernel_motion_blur = np.zeros((size, size)) 15 | kernel_motion_blur[int((size-1)/2), :] = np.ones(size) 16 | kernel_motion_blur = kernel_motion_blur / size 17 | output = cv2.filter2D(img, -1, kernel_motion_blur) 18 | #cv2.imshow('Motion Blur', output) 19 | 20 | ################### 21 | # Sharpening 22 | kernel_sharpen_1 = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]]) 23 | kernel_sharpen_2 = np.array([[1,1,1], [1,-7,1], [1,1,1]]) 24 | kernel_sharpen_3 = np.array([[-1,-1,-1,-1,-1], 25 | [-1,2,2,2,-1], 26 | [-1,2,8,2,-1], 27 | [-1,2,2,2,-1], 28 | [-1,-1,-1,-1,-1]]) / 8.0 29 | output_1 = cv2.filter2D(img, -1, kernel_sharpen_1) 30 | output_2 = cv2.filter2D(img, -1, kernel_sharpen_2) 31 | output_3 = cv2.filter2D(img, -1, kernel_sharpen_3) 32 | #cv2.imshow('Sharpening', output_1) 33 | #cv2.imshow('Excessive Sharpening', output_2) 34 | #cv2.imshow('Edge Enhancement', output_3) 35 | 36 | ################### 37 | # Embossing 38 | img_emboss_input = cv2.imread('../images/input_house.jpg') 39 | kernel_emboss_1 = np.array([[0,-1,-1], 40 | [1,0,-1], 41 | [1,1,0]]) 42 | kernel_emboss_2 = np.array([[-1,-1,0], 43 | [-1,0,1], 44 | [0,1,1]]) 45 | kernel_emboss_3 = np.array([[1,0,0], 46 | [0,0,0], 47 | [0,0,-1]]) 48 | gray_img = cv2.cvtColor(img_emboss_input,cv2.COLOR_BGR2GRAY) 49 | output_1 = cv2.filter2D(gray_img, -1, kernel_emboss_1) 50 | output_2 = cv2.filter2D(gray_img, -1, kernel_emboss_2) 51 | output_3 = cv2.filter2D(gray_img, -1, kernel_emboss_3) 52 | cv2.imshow('Input', img_emboss_input) 53 | cv2.imshow('Embossing - South West', output_1 + 128) 54 | cv2.imshow('Embossing - South East', output_2 + 128) 55 | cv2.imshow('Embossing - North West', output_3 + 128) 56 | 57 | ################### 58 | # Erosion and dilation 59 | 60 | img = cv2.imread('../images/input_morphology.png',0) 61 | kernel = np.ones((5,5), np.uint8) 62 | img_erosion = cv2.erode(img, kernel, iterations=1) 63 | img_dilation = cv2.dilate(img, kernel, iterations=1) 64 | #cv2.imshow('Input', img) 65 | #cv2.imshow('Erosion', img_erosion) 66 | #cv2.imshow('Dilation', img_dilation) 67 | 68 | cv2.waitKey() 69 | 70 | -------------------------------------------------------------------------------- /Chapter02/vignette.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_flowers.jpg') 5 | rows, cols = img.shape[:2] 6 | 7 | kernel_x = cv2.getGaussianKernel(cols,200) 8 | kernel_y = cv2.getGaussianKernel(rows,200) 9 | kernel = kernel_y * kernel_x.T 10 | mask = 255 * kernel / np.linalg.norm(kernel) 11 | output = np.copy(img) 12 | 13 | for i in range(3): 14 | output[:,:,i] = output[:,:,i] * mask 15 | 16 | #cv2.imshow('Original', img) 17 | #cv2.imshow('Vignette', output) 18 | 19 | ################ 20 | # Shifting the focus 21 | 22 | kernel_x = cv2.getGaussianKernel(int(1.5*cols),200) 23 | kernel_y = cv2.getGaussianKernel(int(1.5*rows),200) 24 | kernel = kernel_y * kernel_x.T 25 | mask = 255 * kernel / np.linalg.norm(kernel) 26 | mask = mask[int(0.5*rows):, int(0.5*cols):] 27 | output = np.copy(img) 28 | 29 | for i in range(3): 30 | output[:,:,i] = output[:,:,i] * mask 31 | 32 | cv2.imshow('Input', img) 33 | cv2.imshow('Vignette with shifted focus', output) 34 | 35 | 36 | cv2.waitKey() 37 | -------------------------------------------------------------------------------- /Chapter03/Old/bilateral_filter.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_bilateral_filter.jpg') 5 | img = cv2.resize(img, None, fx=0.4, fy=0.4, interpolation=cv2.INTER_AREA) 6 | 7 | img_gaussian = cv2.GaussianBlur(img, (13,13), 0) 8 | img_bilateral = cv2.bilateralFilter(img, 13, 70, 50) 9 | 10 | cv2.imshow('Input', img) 11 | cv2.imshow('Gaussian filter', img_gaussian) 12 | cv2.imshow('Bilateral filter', img_bilateral) 13 | cv2.waitKey() 14 | -------------------------------------------------------------------------------- /Chapter03/Old/cartoonize.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def cartoonize_image(img, ds_factor=4, sketch_mode=False): 5 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 6 | img_gray = cv2.medianBlur(img_gray, 7) 7 | 8 | edges = cv2.Laplacian(img_gray, cv2.CV_8U, ksize=5) 9 | ret, mask = cv2.threshold(edges, 100, 255, cv2.THRESH_BINARY_INV) 10 | 11 | if sketch_mode: 12 | return cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) 13 | #img_sketch = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) 14 | #kernel = np.ones((3,3), np.uint8) 15 | #img_eroded = cv2.erode(img_sketch, kernel, iterations=1) 16 | #return cv2.medianBlur(img_eroded, 5) 17 | 18 | img_small = cv2.resize(img, None, fx=1.0/ds_factor, fy=1.0/ds_factor, interpolation=cv2.INTER_AREA) 19 | 20 | num_repetitions = 10 21 | sigma_color = 5 22 | sigma_space = 7 23 | size = 5 24 | for i in xrange(num_repetitions): 25 | img_small = cv2.bilateralFilter(img_small, size, sigma_color, sigma_space) 26 | 27 | img_output = cv2.resize(img_small, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_LINEAR) 28 | 29 | dst = np.zeros(img_gray.shape) 30 | dst = cv2.bitwise_and(img_output, img_output, mask=mask) 31 | 32 | return dst 33 | 34 | if __name__=='__main__': 35 | cap = cv2.VideoCapture(0) 36 | 37 | cur_char = -1 38 | prev_char = -1 39 | 40 | while True: 41 | ret, frame = cap.read() 42 | frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 43 | 44 | c = cv2.waitKey(1) 45 | if c == 27: 46 | break 47 | 48 | if c > -1 and c != prev_char: 49 | cur_char = c 50 | 51 | prev_char = c 52 | 53 | if cur_char == ord('s'): 54 | cv2.imshow('Cartoonize', cartoonize_image(frame, sketch_mode=True)) 55 | elif cur_char == ord('c'): 56 | cv2.imshow('Cartoonize', cartoonize_image(frame, sketch_mode=False)) 57 | else: 58 | cv2.imshow('Cartoonize', frame) 59 | 60 | cap.release() 61 | cv2.destroyAllWindows() 62 | 63 | -------------------------------------------------------------------------------- /Chapter03/Old/keyboard.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import cv2 4 | 5 | def argument_parser(): 6 | parser = argparse.ArgumentParser(description="Change colorspace of the \ 7 | input video stream using keyboard controls. The control keys are: \ 8 | Grayscale - 'g', YUV - 'y', HSV - 'h'") 9 | return parser 10 | 11 | if __name__=='__main__': 12 | args = argument_parser().parse_args() 13 | 14 | cap = cv2.VideoCapture(0) 15 | 16 | cur_char = -1 17 | prev_char = -1 18 | 19 | while True: 20 | ret, frame = cap.read() 21 | frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 22 | 23 | c = cv2.waitKey(1) 24 | 25 | if c == 27: 26 | break 27 | 28 | if c > -1 and c != prev_char: 29 | cur_char = c 30 | 31 | prev_char = c 32 | 33 | if cur_char == ord('g'): 34 | output = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 35 | 36 | elif cur_char == ord('y'): 37 | output = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV) 38 | 39 | elif cur_char == ord('h'): 40 | output = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 41 | 42 | else: 43 | output = frame 44 | 45 | cv2.imshow('Webcam', output) 46 | 47 | cap.release() 48 | cv2.destroyAllWindows() 49 | -------------------------------------------------------------------------------- /Chapter03/Old/median_filter.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_median_filter.png') 5 | output = cv2.medianBlur(img, 7) 6 | cv2.imshow('Input', img) 7 | cv2.imshow('Median filter', output) 8 | cv2.waitKey() 9 | -------------------------------------------------------------------------------- /Chapter03/Old/mouse_simple.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def detect_quadrant(event, x, y, flags, param): 5 | if event == cv2.EVENT_LBUTTONDOWN: 6 | if x > width/2: 7 | if y > height/2: 8 | point_top_left = (width/2, height/2) 9 | point_bottom_right = (width-1, height-1) 10 | else: 11 | point_top_left = (width/2, 0) 12 | point_bottom_right = (width-1, height/2) 13 | 14 | else: 15 | if y > height/2: 16 | point_top_left = (0, height/2) 17 | point_bottom_right = (width/2, height-1) 18 | else: 19 | point_top_left = (0, 0) 20 | point_bottom_right = (width/2, height/2) 21 | 22 | cv2.rectangle(img, (0,0), (width-1,height-1), (255,255,255), -1) 23 | cv2.rectangle(img, point_top_left, point_bottom_right, (0,100,0), -1) 24 | 25 | if __name__=='__main__': 26 | width, height = 640, 480 27 | img = np.ones((height, width, 3), dtype='uint8') * 255 28 | cv2.namedWindow('Input window') 29 | cv2.setMouseCallback('Input window', detect_quadrant) 30 | 31 | while True: 32 | cv2.imshow('Input window', img) 33 | c = cv2.waitKey(10) 34 | if c == 27: 35 | break 36 | 37 | cv2.destroyAllWindows() 38 | -------------------------------------------------------------------------------- /Chapter03/Old/mouse_webcam.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def draw_rectangle(event, x, y, flags, params): 5 | global x_init, y_init, drawing, top_left_pt, bottom_right_pt 6 | 7 | if event == cv2.EVENT_LBUTTONDOWN: 8 | drawing = True 9 | x_init, y_init = x, y 10 | 11 | elif event == cv2.EVENT_MOUSEMOVE: 12 | if drawing: 13 | top_left_pt, bottom_right_pt = (x_init,y_init), (x,y) 14 | img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] 15 | 16 | elif event == cv2.EVENT_LBUTTONUP: 17 | drawing = False 18 | top_left_pt, bottom_right_pt = (x_init,y_init), (x,y) 19 | img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] 20 | 21 | if __name__=='__main__': 22 | drawing = False 23 | top_left_pt, bottom_right_pt = (-1,-1), (-1,-1) 24 | 25 | cap = cv2.VideoCapture(0) 26 | cv2.namedWindow('Webcam') 27 | cv2.setMouseCallback('Webcam', draw_rectangle) 28 | 29 | while True: 30 | ret, frame = cap.read() 31 | img = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 32 | (x0,y0), (x1,y1) = top_left_pt, bottom_right_pt 33 | img[y0:y1, x0:x1] = 255 - img[y0:y1, x0:x1] 34 | 35 | cv2.imshow('Webcam', img) 36 | 37 | c = cv2.waitKey(1) 38 | if c == 27: 39 | break 40 | 41 | cv2.destroyAllWindows() 42 | 43 | -------------------------------------------------------------------------------- /Chapter03/Old/webcam.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | cap = cv2.VideoCapture(0) 4 | 5 | while True: 6 | ret, frame = cap.read() 7 | frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 8 | cv2.imshow('Input', frame) 9 | 10 | c = cv2.waitKey(0) 11 | if c == 27: 12 | break 13 | 14 | cap.release() 15 | cv2.destroyAllWindows() 16 | -------------------------------------------------------------------------------- /Chapter03/bilateral_filter.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_bilateral_filter.jpg') 5 | img = cv2.resize(img, None, fx=0.4, fy=0.4, interpolation=cv2.INTER_AREA) 6 | 7 | img_gaussian = cv2.GaussianBlur(img, (13,13), 0) 8 | img_bilateral = cv2.bilateralFilter(img, 13, 70, 50) 9 | 10 | cv2.imshow('Input', img) 11 | cv2.imshow('Gaussian filter', img_gaussian) 12 | cv2.imshow('Bilateral filter', img_bilateral) 13 | cv2.waitKey() 14 | -------------------------------------------------------------------------------- /Chapter03/cartoonize.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def cartoonize_image(img, ds_factor=4, sketch_mode=False): 5 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 6 | img_gray = cv2.medianBlur(img_gray, 7) 7 | 8 | edges = cv2.Laplacian(img_gray, cv2.CV_8U, ksize=5) 9 | ret, mask = cv2.threshold(edges, 100, 255, cv2.THRESH_BINARY_INV) 10 | 11 | if sketch_mode: 12 | return cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) 13 | #img_sketch = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) 14 | #kernel = np.ones((3,3), np.uint8) 15 | #img_eroded = cv2.erode(img_sketch, kernel, iterations=1) 16 | #return cv2.medianBlur(img_eroded, 5) 17 | 18 | img_small = cv2.resize(img, None, fx=1.0/ds_factor, fy=1.0/ds_factor, interpolation=cv2.INTER_AREA) 19 | 20 | num_repetitions = 10 21 | sigma_color = 5 22 | sigma_space = 7 23 | size = 5 24 | for i in xrange(num_repetitions): 25 | img_small = cv2.bilateralFilter(img_small, size, sigma_color, sigma_space) 26 | 27 | img_output = cv2.resize(img_small, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_LINEAR) 28 | 29 | dst = np.zeros(img_gray.shape) 30 | dst = cv2.bitwise_and(img_output, img_output, mask=mask) 31 | 32 | return dst 33 | 34 | if __name__=='__main__': 35 | cap = cv2.VideoCapture(0) 36 | 37 | cur_char = -1 38 | prev_char = -1 39 | 40 | while True: 41 | ret, frame = cap.read() 42 | frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 43 | 44 | c = cv2.waitKey(1) 45 | if c == 27: 46 | break 47 | 48 | if c > -1 and c != prev_char: 49 | cur_char = c 50 | 51 | prev_char = c 52 | 53 | if cur_char == ord('s'): 54 | cv2.imshow('Cartoonize', cartoonize_image(frame, sketch_mode=True)) 55 | elif cur_char == ord('c'): 56 | cv2.imshow('Cartoonize', cartoonize_image(frame, sketch_mode=False)) 57 | else: 58 | cv2.imshow('Cartoonize', frame) 59 | 60 | cap.release() 61 | cv2.destroyAllWindows() 62 | 63 | -------------------------------------------------------------------------------- /Chapter03/keyboard.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import cv2 4 | 5 | def argument_parser(): 6 | parser = argparse.ArgumentParser(description="Change colorspace of the \ 7 | input video stream using keyboard controls. The control keys are: \ 8 | Grayscale - 'g', YUV - 'y', HSV - 'h'") 9 | return parser 10 | 11 | if __name__=='__main__': 12 | args = argument_parser().parse_args() 13 | 14 | cap = cv2.VideoCapture(0) 15 | 16 | cur_char = -1 17 | prev_char = -1 18 | 19 | while True: 20 | ret, frame = cap.read() 21 | frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 22 | 23 | c = cv2.waitKey(1) 24 | 25 | if c == 27: 26 | break 27 | 28 | if c > -1 and c != prev_char: 29 | cur_char = c 30 | 31 | prev_char = c 32 | 33 | if cur_char == ord('g'): 34 | output = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 35 | 36 | elif cur_char == ord('y'): 37 | output = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV) 38 | 39 | elif cur_char == ord('h'): 40 | output = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 41 | 42 | else: 43 | output = frame 44 | 45 | cv2.imshow('Webcam', output) 46 | 47 | cap.release() 48 | cv2.destroyAllWindows() 49 | -------------------------------------------------------------------------------- /Chapter03/median_filter.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_median_filter.png') 5 | output = cv2.medianBlur(img, 7) 6 | cv2.imshow('Input', img) 7 | cv2.imshow('Median filter', output) 8 | cv2.waitKey() 9 | -------------------------------------------------------------------------------- /Chapter03/mouse_simple.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def detect_quadrant(event, x, y, flags, param): 5 | if event == cv2.EVENT_LBUTTONDOWN: 6 | if x > width/2: 7 | if y > height/2: 8 | point_top_left = (width/2, height/2) 9 | point_bottom_right = (width-1, height-1) 10 | else: 11 | point_top_left = (width/2, 0) 12 | point_bottom_right = (width-1, height/2) 13 | 14 | else: 15 | if y > height/2: 16 | point_top_left = (0, height/2) 17 | point_bottom_right = (width/2, height-1) 18 | else: 19 | point_top_left = (0, 0) 20 | point_bottom_right = (width/2, height/2) 21 | 22 | cv2.rectangle(img, (0,0), (width-1,height-1), (255,255,255), -1) 23 | cv2.rectangle(img, point_top_left, point_bottom_right, (0,100,0), -1) 24 | 25 | if __name__=='__main__': 26 | width, height = 640, 480 27 | img = np.ones((height, width, 3), dtype='uint8') * 255 28 | cv2.namedWindow('Input window') 29 | cv2.setMouseCallback('Input window', detect_quadrant) 30 | 31 | while True: 32 | cv2.imshow('Input window', img) 33 | c = cv2.waitKey(10) 34 | if c == 27: 35 | break 36 | 37 | cv2.destroyAllWindows() 38 | -------------------------------------------------------------------------------- /Chapter03/mouse_webcam.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def draw_rectangle(event, x, y, flags, params): 5 | global x_init, y_init, drawing, top_left_pt, bottom_right_pt 6 | 7 | if event == cv2.EVENT_LBUTTONDOWN: 8 | drawing = True 9 | x_init, y_init = x, y 10 | 11 | elif event == cv2.EVENT_MOUSEMOVE: 12 | if drawing: 13 | top_left_pt = (min(x_init, x), min(y_init, y)) 14 | bottom_right_pt = (max(x_init, x), max(y_init, y)) 15 | img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] 16 | 17 | elif event == cv2.EVENT_LBUTTONUP: 18 | drawing = False 19 | top_left_pt = (min(x_init, x), min(y_init, y)) 20 | bottom_right_pt = (max(x_init, x), max(y_init, y)) 21 | img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] 22 | 23 | if __name__=='__main__': 24 | drawing = False 25 | top_left_pt, bottom_right_pt = (-1,-1), (-1,-1) 26 | 27 | cap = cv2.VideoCapture(0) 28 | cv2.namedWindow('Webcam') 29 | cv2.setMouseCallback('Webcam', draw_rectangle) 30 | 31 | while True: 32 | ret, frame = cap.read() 33 | img = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 34 | (x0,y0), (x1,y1) = top_left_pt, bottom_right_pt 35 | img[y0:y1, x0:x1] = 255 - img[y0:y1, x0:x1] 36 | 37 | cv2.imshow('Webcam', img) 38 | 39 | c = cv2.waitKey(1) 40 | if c == 27: 41 | break 42 | 43 | cv2.destroyAllWindows() 44 | 45 | -------------------------------------------------------------------------------- /Chapter03/webcam.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | cap = cv2.VideoCapture(0) 4 | 5 | while True: 6 | ret, frame = cap.read() 7 | frame = cv2.resize(frame, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA) 8 | cv2.imshow('Input', frame) 9 | 10 | c = cv2.waitKey(0) 11 | if c == 27: 12 | break 13 | 14 | cap.release() 15 | cv2.destroyAllWindows() 16 | -------------------------------------------------------------------------------- /Chapter04/Old/cascade_files/haarcascade_mcs_leftear.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/OpenCV-with-Python-By-Example/f00a526509c790dbc25064dd62785d3b2d7a0e8b/Chapter04/Old/cascade_files/haarcascade_mcs_leftear.xml -------------------------------------------------------------------------------- /Chapter04/Old/cascade_files/haarcascade_mcs_rightear.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/OpenCV-with-Python-By-Example/f00a526509c790dbc25064dd62785d3b2d7a0e8b/Chapter04/Old/cascade_files/haarcascade_mcs_rightear.xml -------------------------------------------------------------------------------- /Chapter04/Old/ear_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | left_ear_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_leftear.xml') 5 | right_ear_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_rightear.xml') 6 | 7 | if left_ear_cascade.empty(): 8 | raise IOError('Unable to load the left ear cascade classifier xml file') 9 | 10 | if right_ear_cascade.empty(): 11 | raise IOError('Unable to load the right ear cascade classifier xml file') 12 | 13 | img = cv2.imread('../images/input_ear_3.jpg') 14 | 15 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 16 | 17 | left_ear = left_ear_cascade.detectMultiScale(gray, 1.3, 5) 18 | right_ear = right_ear_cascade.detectMultiScale(gray, 1.3, 5) 19 | 20 | for (x,y,w,h) in left_ear: 21 | cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 3) 22 | 23 | for (x,y,w,h) in right_ear: 24 | cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 3) 25 | 26 | cv2.imshow('Ear Detector', img) 27 | cv2.waitKey() 28 | cv2.destroyAllWindows() 29 | -------------------------------------------------------------------------------- /Chapter04/Old/eye_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | eye_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_eye.xml') 6 | 7 | if face_cascade.empty(): 8 | raise IOError('Unable to load the face cascade classifier xml file') 9 | 10 | if eye_cascade.empty(): 11 | raise IOError('Unable to load the eye cascade classifier xml file') 12 | 13 | cap = cv2.VideoCapture(0) 14 | ds_factor = 0.5 15 | 16 | while True: 17 | ret, frame = cap.read() 18 | frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA) 19 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 20 | 21 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 22 | for (x,y,w,h) in faces: 23 | roi_gray = gray[y:y+h, x:x+w] 24 | roi_color = frame[y:y+h, x:x+w] 25 | eyes = eye_cascade.detectMultiScale(roi_gray) 26 | for (x_eye,y_eye,w_eye,h_eye) in eyes: 27 | center = (int(x_eye + 0.5*w_eye), int(y_eye + 0.5*h_eye)) 28 | radius = int(0.3 * (w_eye + h_eye)) 29 | color = (0, 255, 0) 30 | thickness = 3 31 | cv2.circle(roi_color, center, radius, color, thickness) 32 | 33 | cv2.imshow('Eye Detector', frame) 34 | 35 | c = cv2.waitKey(1) 36 | if c == 27: 37 | break 38 | 39 | cap.release() 40 | cv2.destroyAllWindows() 41 | -------------------------------------------------------------------------------- /Chapter04/Old/eye_sunglasses.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | eye_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_eye.xml') 6 | 7 | if face_cascade.empty(): 8 | raise IOError('Unable to load the face cascade classifier xml file') 9 | 10 | if eye_cascade.empty(): 11 | raise IOError('Unable to load the eye cascade classifier xml file') 12 | 13 | img = cv2.imread('../images/input_sunglasses.jpg') 14 | sunglasses_img = cv2.imread('../images/eye_sunglasses_1.jpg') 15 | 16 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 17 | 18 | centers = [] 19 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 20 | 21 | for (x,y,w,h) in faces: 22 | roi_gray = gray[y:y+h, x:x+w] 23 | roi_color = img[y:y+h, x:x+w] 24 | eyes = eye_cascade.detectMultiScale(roi_gray) 25 | for (x_eye,y_eye,w_eye,h_eye) in eyes: 26 | #cv2.rectangle(roi_color, (x_eye,y_eye), (x_eye+w_eye,y_eye+h_eye), (0,255,0), 3) 27 | centers.append((x + int(x_eye + 0.5*w_eye), y + int(y_eye + 0.5*h_eye))) 28 | 29 | # Overlay sunglasses 30 | sunglasses_width = 2.12 * abs(centers[1][0] - centers[0][0]) 31 | overlay_img = np.ones(img.shape, np.uint8) * 255 32 | h, w = sunglasses_img.shape[:2] 33 | scaling_factor = sunglasses_width / w 34 | overlay_sunglasses = cv2.resize(sunglasses_img, None, fx=scaling_factor, 35 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 36 | 37 | x = centers[0][0] if centers[0][0] < centers[1][0] else centers[1][0] 38 | x -= 0.26*overlay_sunglasses.shape[1] 39 | y += 0.85*overlay_sunglasses.shape[0] 40 | h, w = overlay_sunglasses.shape[:2] 41 | overlay_img[y:y+h, x:x+w] = overlay_sunglasses 42 | 43 | # Create mask 44 | gray_sunglasses = cv2.cvtColor(overlay_img, cv2.COLOR_BGR2GRAY) 45 | ret, mask = cv2.threshold(gray_sunglasses, 110, 255, cv2.THRESH_BINARY) 46 | mask_inv = cv2.bitwise_not(mask) 47 | temp = cv2.bitwise_and(img, img, mask=mask) 48 | temp2 = cv2.bitwise_and(overlay_img, overlay_img, mask=mask_inv) 49 | final_img = cv2.add(temp, temp2) 50 | 51 | cv2.imshow('Eye Detector', img) 52 | cv2.imshow('Sunglasses', final_img) 53 | cv2.waitKey() 54 | cv2.destroyAllWindows() 55 | -------------------------------------------------------------------------------- /Chapter04/Old/face_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | 6 | if face_cascade.empty(): 7 | raise IOError('Unable to load the face cascade classifier xml file') 8 | 9 | cap = cv2.VideoCapture(0) 10 | scaling_factor = 0.5 11 | 12 | while True: 13 | ret, frame = cap.read() 14 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 15 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 16 | 17 | face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) 18 | for (x,y,w,h) in face_rects: 19 | cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) 20 | 21 | cv2.imshow('Face Detector', frame) 22 | 23 | c = cv2.waitKey(1) 24 | if c == 27: 25 | break 26 | 27 | cap.release() 28 | cv2.destroyAllWindows() 29 | -------------------------------------------------------------------------------- /Chapter04/Old/face_hannibal.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | 6 | face_mask = cv2.imread('../images/mask_hannibal.png') 7 | h_mask, w_mask = face_mask.shape[:2] 8 | 9 | if face_cascade.empty(): 10 | raise IOError('Unable to load the face cascade classifier xml file') 11 | 12 | cap = cv2.VideoCapture(0) 13 | scaling_factor = 0.5 14 | 15 | while True: 16 | ret, frame = cap.read() 17 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | 20 | face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) 21 | for (x,y,w,h) in face_rects: 22 | if h > 0 and w > 0: 23 | x = int(x + 0.1*w) 24 | y = int(y + 0.4*h) 25 | w = int(0.8 * w) 26 | h = int(0.8 * h) 27 | 28 | frame_roi = frame[y:y+h, x:x+w] 29 | face_mask_small = cv2.resize(face_mask, (w, h), interpolation=cv2.INTER_AREA) 30 | 31 | gray_mask = cv2.cvtColor(face_mask_small, cv2.COLOR_BGR2GRAY) 32 | ret, mask = cv2.threshold(gray_mask, 50, 255, cv2.THRESH_BINARY) 33 | mask_inv = cv2.bitwise_not(mask) 34 | masked_face = cv2.bitwise_and(face_mask_small, face_mask_small, mask=mask) 35 | masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv) 36 | frame[y:y+h, x:x+w] = cv2.add(masked_face, masked_frame) 37 | 38 | cv2.imshow('Face Detector', frame) 39 | 40 | c = cv2.waitKey(1) 41 | if c == 27: 42 | break 43 | 44 | cap.release() 45 | cv2.destroyAllWindows() 46 | -------------------------------------------------------------------------------- /Chapter04/Old/face_skull.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | 6 | face_mask = cv2.imread('../images/mask_skull.png') 7 | h_mask, w_mask = face_mask.shape[:2] 8 | 9 | if face_cascade.empty(): 10 | raise IOError('Unable to load the face cascade classifier xml file') 11 | 12 | cap = cv2.VideoCapture(0) 13 | scaling_factor = 0.5 14 | 15 | while True: 16 | ret, frame = cap.read() 17 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | 20 | face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) 21 | for (x,y,w,h) in face_rects: 22 | if h > 0 and w > 0: 23 | h, w = int(1.4*h), int(1.0*w) 24 | y -= 0.1*h 25 | frame_roi = frame[y:y+h, x:x+w] 26 | face_mask_small = cv2.resize(face_mask, (w, h), interpolation=cv2.INTER_AREA) 27 | 28 | gray_mask = cv2.cvtColor(face_mask_small, cv2.COLOR_BGR2GRAY) 29 | ret, mask = cv2.threshold(gray_mask, 180, 255, cv2.THRESH_BINARY_INV) 30 | mask_inv = cv2.bitwise_not(mask) 31 | masked_face = cv2.bitwise_and(face_mask_small, face_mask_small, mask=mask) 32 | masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv) 33 | frame[y:y+h, x:x+w] = cv2.add(masked_face, masked_frame) 34 | 35 | cv2.imshow('Face Detector', frame) 36 | 37 | c = cv2.waitKey(1) 38 | if c == 27: 39 | break 40 | 41 | cap.release() 42 | cv2.destroyAllWindows() 43 | -------------------------------------------------------------------------------- /Chapter04/Old/mouth_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | mouth_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_mouth.xml') 5 | 6 | if mouth_cascade.empty(): 7 | raise IOError('Unable to load the mouth cascade classifier xml file') 8 | 9 | cap = cv2.VideoCapture(0) 10 | ds_factor = 0.5 11 | 12 | while True: 13 | ret, frame = cap.read() 14 | frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA) 15 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 16 | 17 | mouth_rects = mouth_cascade.detectMultiScale(gray, 1.7, 11) 18 | for (x,y,w,h) in mouth_rects: 19 | y = int(y - 0.15*h) 20 | cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) 21 | break 22 | 23 | cv2.imshow('Mouth Detector', frame) 24 | 25 | c = cv2.waitKey(1) 26 | if c == 27: 27 | break 28 | 29 | cap.release() 30 | cv2.destroyAllWindows() 31 | -------------------------------------------------------------------------------- /Chapter04/Old/mouth_moustache.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | mouth_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_mouth.xml') 5 | 6 | moustache_mask = cv2.imread('../images/moustache.png') 7 | h_mask, w_mask = moustache_mask.shape[:2] 8 | 9 | if mouth_cascade.empty(): 10 | raise IOError('Unable to load the mouth cascade classifier xml file') 11 | 12 | cap = cv2.VideoCapture(0) 13 | scaling_factor = 0.5 14 | 15 | while True: 16 | ret, frame = cap.read() 17 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | 20 | mouth_rects = mouth_cascade.detectMultiScale(gray, 1.3, 5) 21 | if len(mouth_rects) > 0: 22 | (x,y,w,h) = mouth_rects[0] 23 | h, w = int(0.6*h), int(1.2*w) 24 | x -= 0.05*w 25 | y -= 0.55*h 26 | frame_roi = frame[y:y+h, x:x+w] 27 | moustache_mask_small = cv2.resize(moustache_mask, (w, h), interpolation=cv2.INTER_AREA) 28 | 29 | gray_mask = cv2.cvtColor(moustache_mask_small, cv2.COLOR_BGR2GRAY) 30 | ret, mask = cv2.threshold(gray_mask, 50, 255, cv2.THRESH_BINARY_INV) 31 | mask_inv = cv2.bitwise_not(mask) 32 | masked_mouth = cv2.bitwise_and(moustache_mask_small, moustache_mask_small, mask=mask) 33 | masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv) 34 | frame[y:y+h, x:x+w] = cv2.add(masked_mouth, masked_frame) 35 | 36 | cv2.imshow('Moustache', frame) 37 | 38 | c = cv2.waitKey(1) 39 | if c == 27: 40 | break 41 | 42 | cap.release() 43 | cv2.destroyAllWindows() 44 | -------------------------------------------------------------------------------- /Chapter04/Old/nose_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | nose_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_nose.xml') 5 | 6 | if nose_cascade.empty(): 7 | raise IOError('Unable to load the nose cascade classifier xml file') 8 | 9 | cap = cv2.VideoCapture(0) 10 | ds_factor = 0.5 11 | 12 | while True: 13 | ret, frame = cap.read() 14 | frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA) 15 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 16 | 17 | nose_rects = nose_cascade.detectMultiScale(gray, 1.3, 5) 18 | for (x,y,w,h) in nose_rects: 19 | cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) 20 | break 21 | 22 | cv2.imshow('Nose Detector', frame) 23 | 24 | c = cv2.waitKey(1) 25 | if c == 27: 26 | break 27 | 28 | cap.release() 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /Chapter04/Old/pupil_detector.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | img = cv2.imread('../images/input_pupil.jpg') 7 | scaling_factor = 0.7 8 | 9 | img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 10 | cv2.imshow('Input', img) 11 | gray = cv2.cvtColor(~img, cv2.COLOR_BGR2GRAY) 12 | 13 | ret, thresh_gray = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY) 14 | contours, hierarchy = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 15 | 16 | for contour in contours: 17 | area = cv2.contourArea(contour) 18 | rect = cv2.boundingRect(contour) 19 | x, y, width, height = rect 20 | radius = 0.25 * (width + height) 21 | 22 | area_condition = (100 <= area <= 200) 23 | symmetry_condition = (abs(1 - float(width)/float(height)) <= 0.2) 24 | fill_condition = (abs(1 - (area / (math.pi * math.pow(radius, 2.0)))) <= 0.3) 25 | 26 | if area_condition and symmetry_condition and fill_condition: 27 | cv2.circle(img, (int(x + radius), int(y + radius)), int(1.3*radius), (0,180,0), -1) 28 | 29 | cv2.imshow('Pupil Detector', img) 30 | 31 | c = cv2.waitKey() 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Chapter04/ear_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | left_ear_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_leftear.xml') 5 | right_ear_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_rightear.xml') 6 | 7 | if left_ear_cascade.empty(): 8 | raise IOError('Unable to load the left ear cascade classifier xml file') 9 | 10 | if right_ear_cascade.empty(): 11 | raise IOError('Unable to load the right ear cascade classifier xml file') 12 | 13 | img = cv2.imread('../images/input_ear_3.jpg') 14 | 15 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 16 | 17 | left_ear = left_ear_cascade.detectMultiScale(gray, 1.3, 5) 18 | right_ear = right_ear_cascade.detectMultiScale(gray, 1.3, 5) 19 | 20 | for (x,y,w,h) in left_ear: 21 | cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 3) 22 | 23 | for (x,y,w,h) in right_ear: 24 | cv2.rectangle(img, (x,y), (x+w,y+h), (0,255,0), 3) 25 | 26 | cv2.imshow('Ear Detector', img) 27 | cv2.waitKey() 28 | cv2.destroyAllWindows() 29 | -------------------------------------------------------------------------------- /Chapter04/eye_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | eye_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_eye.xml') 6 | 7 | if face_cascade.empty(): 8 | raise IOError('Unable to load the face cascade classifier xml file') 9 | 10 | if eye_cascade.empty(): 11 | raise IOError('Unable to load the eye cascade classifier xml file') 12 | 13 | cap = cv2.VideoCapture(0) 14 | ds_factor = 0.5 15 | 16 | while True: 17 | ret, frame = cap.read() 18 | frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA) 19 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 20 | 21 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 22 | for (x,y,w,h) in faces: 23 | roi_gray = gray[y:y+h, x:x+w] 24 | roi_color = frame[y:y+h, x:x+w] 25 | eyes = eye_cascade.detectMultiScale(roi_gray) 26 | for (x_eye,y_eye,w_eye,h_eye) in eyes: 27 | center = (int(x_eye + 0.5*w_eye), int(y_eye + 0.5*h_eye)) 28 | radius = int(0.3 * (w_eye + h_eye)) 29 | color = (0, 255, 0) 30 | thickness = 3 31 | cv2.circle(roi_color, center, radius, color, thickness) 32 | 33 | cv2.imshow('Eye Detector', frame) 34 | 35 | c = cv2.waitKey(1) 36 | if c == 27: 37 | break 38 | 39 | cap.release() 40 | cv2.destroyAllWindows() 41 | -------------------------------------------------------------------------------- /Chapter04/eye_sunglasses.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | eye_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_eye.xml') 6 | 7 | if face_cascade.empty(): 8 | raise IOError('Unable to load the face cascade classifier xml file') 9 | 10 | if eye_cascade.empty(): 11 | raise IOError('Unable to load the eye cascade classifier xml file') 12 | 13 | img = cv2.imread('../images/input_sunglasses.jpg') 14 | sunglasses_img = cv2.imread('../images/eye_sunglasses_1.jpg') 15 | 16 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 17 | 18 | centers = [] 19 | faces = face_cascade.detectMultiScale(gray, 1.3, 5) 20 | 21 | for (x,y,w,h) in faces: 22 | roi_gray = gray[y:y+h, x:x+w] 23 | roi_color = img[y:y+h, x:x+w] 24 | eyes = eye_cascade.detectMultiScale(roi_gray) 25 | for (x_eye,y_eye,w_eye,h_eye) in eyes: 26 | #cv2.rectangle(roi_color, (x_eye,y_eye), (x_eye+w_eye,y_eye+h_eye), (0,255,0), 3) 27 | centers.append((x + int(x_eye + 0.5*w_eye), y + int(y_eye + 0.5*h_eye))) 28 | 29 | if len(centers) > 0: 30 | # Overlay sunglasses 31 | sunglasses_width = 2.12 * abs(centers[1][0] - centers[0][0]) 32 | overlay_img = np.ones(img.shape, np.uint8) * 255 33 | h, w = sunglasses_img.shape[:2] 34 | scaling_factor = sunglasses_width / w 35 | overlay_sunglasses = cv2.resize(sunglasses_img, None, fx=scaling_factor, 36 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 37 | 38 | x = centers[0][0] if centers[0][0] < centers[1][0] else centers[1][0] 39 | x -= 0.26*overlay_sunglasses.shape[1] 40 | y += 0.85*overlay_sunglasses.shape[0] 41 | h, w = overlay_sunglasses.shape[:2] 42 | overlay_img[y:y+h, x:x+w] = overlay_sunglasses 43 | 44 | # Create mask 45 | gray_sunglasses = cv2.cvtColor(overlay_img, cv2.COLOR_BGR2GRAY) 46 | ret, mask = cv2.threshold(gray_sunglasses, 110, 255, cv2.THRESH_BINARY) 47 | mask_inv = cv2.bitwise_not(mask) 48 | temp = cv2.bitwise_and(img, img, mask=mask) 49 | temp2 = cv2.bitwise_and(overlay_img, overlay_img, mask=mask_inv) 50 | final_img = cv2.add(temp, temp2) 51 | 52 | cv2.imshow('Eye Detector', img) 53 | cv2.imshow('Sunglasses', final_img) 54 | cv2.waitKey() 55 | cv2.destroyAllWindows() 56 | -------------------------------------------------------------------------------- /Chapter04/face_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | 6 | if face_cascade.empty(): 7 | raise IOError('Unable to load the face cascade classifier xml file') 8 | 9 | cap = cv2.VideoCapture(0) 10 | scaling_factor = 0.5 11 | 12 | while True: 13 | ret, frame = cap.read() 14 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 15 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 16 | 17 | face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) 18 | for (x,y,w,h) in face_rects: 19 | cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) 20 | 21 | cv2.imshow('Face Detector', frame) 22 | 23 | c = cv2.waitKey(1) 24 | if c == 27: 25 | break 26 | 27 | cap.release() 28 | cv2.destroyAllWindows() 29 | -------------------------------------------------------------------------------- /Chapter04/face_hannibal.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | 6 | face_mask = cv2.imread('../images/mask_hannibal.png') 7 | h_mask, w_mask = face_mask.shape[:2] 8 | 9 | if face_cascade.empty(): 10 | raise IOError('Unable to load the face cascade classifier xml file') 11 | 12 | cap = cv2.VideoCapture(0) 13 | scaling_factor = 0.5 14 | 15 | while True: 16 | ret, frame = cap.read() 17 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | 20 | face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) 21 | for (x,y,w,h) in face_rects: 22 | if h > 0 and w > 0: 23 | x = int(x + 0.1*w) 24 | y = int(y + 0.4*h) 25 | w = int(0.8 * w) 26 | h = int(0.8 * h) 27 | 28 | frame_roi = frame[y:y+h, x:x+w] 29 | face_mask_small = cv2.resize(face_mask, (w, h), interpolation=cv2.INTER_AREA) 30 | 31 | gray_mask = cv2.cvtColor(face_mask_small, cv2.COLOR_BGR2GRAY) 32 | ret, mask = cv2.threshold(gray_mask, 50, 255, cv2.THRESH_BINARY) 33 | mask_inv = cv2.bitwise_not(mask) 34 | masked_face = cv2.bitwise_and(face_mask_small, face_mask_small, mask=mask) 35 | masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv) 36 | frame[y:y+h, x:x+w] = cv2.add(masked_face, masked_frame) 37 | 38 | cv2.imshow('Face Detector', frame) 39 | 40 | c = cv2.waitKey(1) 41 | if c == 27: 42 | break 43 | 44 | cap.release() 45 | cv2.destroyAllWindows() 46 | -------------------------------------------------------------------------------- /Chapter04/face_skull.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | face_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_frontalface_alt.xml') 5 | 6 | face_mask = cv2.imread('../images/mask_skull.png') 7 | h_mask, w_mask = face_mask.shape[:2] 8 | 9 | if face_cascade.empty(): 10 | raise IOError('Unable to load the face cascade classifier xml file') 11 | 12 | cap = cv2.VideoCapture(0) 13 | scaling_factor = 0.5 14 | 15 | while True: 16 | ret, frame = cap.read() 17 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | 20 | face_rects = face_cascade.detectMultiScale(gray, 1.3, 5) 21 | for (x,y,w,h) in face_rects: 22 | if h > 0 and w > 0: 23 | h, w = int(1.4*h), int(1.0*w) 24 | y -= 0.1*h 25 | frame_roi = frame[y:y+h, x:x+w] 26 | face_mask_small = cv2.resize(face_mask, (w, h), interpolation=cv2.INTER_AREA) 27 | 28 | gray_mask = cv2.cvtColor(face_mask_small, cv2.COLOR_BGR2GRAY) 29 | ret, mask = cv2.threshold(gray_mask, 180, 255, cv2.THRESH_BINARY_INV) 30 | mask_inv = cv2.bitwise_not(mask) 31 | masked_face = cv2.bitwise_and(face_mask_small, face_mask_small, mask=mask) 32 | masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv) 33 | frame[y:y+h, x:x+w] = cv2.add(masked_face, masked_frame) 34 | 35 | cv2.imshow('Face Detector', frame) 36 | 37 | c = cv2.waitKey(1) 38 | if c == 27: 39 | break 40 | 41 | cap.release() 42 | cv2.destroyAllWindows() 43 | -------------------------------------------------------------------------------- /Chapter04/haarcascade_mcs_leftear.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/OpenCV-with-Python-By-Example/f00a526509c790dbc25064dd62785d3b2d7a0e8b/Chapter04/haarcascade_mcs_leftear.xml -------------------------------------------------------------------------------- /Chapter04/haarcascade_mcs_rightear.xml: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/OpenCV-with-Python-By-Example/f00a526509c790dbc25064dd62785d3b2d7a0e8b/Chapter04/haarcascade_mcs_rightear.xml -------------------------------------------------------------------------------- /Chapter04/mouth_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | mouth_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_mouth.xml') 5 | 6 | if mouth_cascade.empty(): 7 | raise IOError('Unable to load the mouth cascade classifier xml file') 8 | 9 | cap = cv2.VideoCapture(0) 10 | ds_factor = 0.5 11 | 12 | while True: 13 | ret, frame = cap.read() 14 | frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA) 15 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 16 | 17 | mouth_rects = mouth_cascade.detectMultiScale(gray, 1.7, 11) 18 | for (x,y,w,h) in mouth_rects: 19 | y = int(y - 0.15*h) 20 | cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) 21 | break 22 | 23 | cv2.imshow('Mouth Detector', frame) 24 | 25 | c = cv2.waitKey(1) 26 | if c == 27: 27 | break 28 | 29 | cap.release() 30 | cv2.destroyAllWindows() 31 | -------------------------------------------------------------------------------- /Chapter04/mouth_moustache.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | mouth_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_mouth.xml') 5 | 6 | moustache_mask = cv2.imread('../images/moustache.png') 7 | h_mask, w_mask = moustache_mask.shape[:2] 8 | 9 | if mouth_cascade.empty(): 10 | raise IOError('Unable to load the mouth cascade classifier xml file') 11 | 12 | cap = cv2.VideoCapture(0) 13 | scaling_factor = 0.5 14 | 15 | while True: 16 | ret, frame = cap.read() 17 | frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | 20 | mouth_rects = mouth_cascade.detectMultiScale(gray, 1.3, 5) 21 | if len(mouth_rects) > 0: 22 | (x,y,w,h) = mouth_rects[0] 23 | h, w = int(0.6*h), int(1.2*w) 24 | x -= 0.05*w 25 | y -= 0.55*h 26 | frame_roi = frame[y:y+h, x:x+w] 27 | moustache_mask_small = cv2.resize(moustache_mask, (w, h), interpolation=cv2.INTER_AREA) 28 | 29 | gray_mask = cv2.cvtColor(moustache_mask_small, cv2.COLOR_BGR2GRAY) 30 | ret, mask = cv2.threshold(gray_mask, 50, 255, cv2.THRESH_BINARY_INV) 31 | mask_inv = cv2.bitwise_not(mask) 32 | masked_mouth = cv2.bitwise_and(moustache_mask_small, moustache_mask_small, mask=mask) 33 | masked_frame = cv2.bitwise_and(frame_roi, frame_roi, mask=mask_inv) 34 | frame[y:y+h, x:x+w] = cv2.add(masked_mouth, masked_frame) 35 | 36 | cv2.imshow('Moustache', frame) 37 | 38 | c = cv2.waitKey(1) 39 | if c == 27: 40 | break 41 | 42 | cap.release() 43 | cv2.destroyAllWindows() 44 | -------------------------------------------------------------------------------- /Chapter04/nose_detector.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | nose_cascade = cv2.CascadeClassifier('cascade_files/haarcascade_mcs_nose.xml') 5 | 6 | if nose_cascade.empty(): 7 | raise IOError('Unable to load the nose cascade classifier xml file') 8 | 9 | cap = cv2.VideoCapture(0) 10 | ds_factor = 0.5 11 | 12 | while True: 13 | ret, frame = cap.read() 14 | frame = cv2.resize(frame, None, fx=ds_factor, fy=ds_factor, interpolation=cv2.INTER_AREA) 15 | gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 16 | 17 | nose_rects = nose_cascade.detectMultiScale(gray, 1.3, 5) 18 | for (x,y,w,h) in nose_rects: 19 | cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3) 20 | break 21 | 22 | cv2.imshow('Nose Detector', frame) 23 | 24 | c = cv2.waitKey(1) 25 | if c == 27: 26 | break 27 | 28 | cap.release() 29 | cv2.destroyAllWindows() 30 | -------------------------------------------------------------------------------- /Chapter04/pupil_detector.py: -------------------------------------------------------------------------------- 1 | import math 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | img = cv2.imread('../images/input_pupil.jpg') 7 | scaling_factor = 0.7 8 | 9 | img = cv2.resize(img, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA) 10 | cv2.imshow('Input', img) 11 | gray = cv2.cvtColor(~img, cv2.COLOR_BGR2GRAY) 12 | 13 | ret, thresh_gray = cv2.threshold(gray, 220, 255, cv2.THRESH_BINARY) 14 | contours, hierarchy = cv2.findContours(thresh_gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 15 | 16 | for contour in contours: 17 | area = cv2.contourArea(contour) 18 | rect = cv2.boundingRect(contour) 19 | x, y, width, height = rect 20 | radius = 0.25 * (width + height) 21 | 22 | area_condition = (100 <= area <= 200) 23 | symmetry_condition = (abs(1 - float(width)/float(height)) <= 0.2) 24 | fill_condition = (abs(1 - (area / (math.pi * math.pow(radius, 2.0)))) <= 0.3) 25 | 26 | if area_condition and symmetry_condition and fill_condition: 27 | cv2.circle(img, (int(x + radius), int(y + radius)), int(1.3*radius), (0,180,0), -1) 28 | 29 | cv2.imshow('Pupil Detector', img) 30 | 31 | c = cv2.waitKey() 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Chapter05/Old/brief_with_fast.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | gray_image = cv2.imread('../images/input_brief.jpg', 0) 5 | 6 | # Initiate FAST detector 7 | fast = cv2.FastFeatureDetector() 8 | 9 | # Initiate BRIEF extractor 10 | brief = cv2.DescriptorExtractor_create("BRIEF") 11 | 12 | # find the keypoints with STAR 13 | keypoints = fast.detect(gray_image, None) 14 | 15 | # compute the descriptors with BRIEF 16 | keypoints, descriptors = brief.compute(gray_image, keypoints) 17 | 18 | gray_keypoints = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 19 | cv2.imshow('BRIEF keypoints', gray_keypoints) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter05/Old/brief_with_star.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | gray_image = cv2.imread('../images/input_brief.jpg', 0) 5 | 6 | # Initialize STAR detector 7 | star = cv2.FeatureDetector_create("STAR") 8 | 9 | # Initialize BRIEF extractor 10 | brief = cv2.DescriptorExtractor_create("BRIEF") 11 | 12 | # find the keypoints with STAR 13 | keypoints = star.detect(gray_image, None) 14 | 15 | # compute the descriptors with BRIEF 16 | keypoints, descriptors = brief.compute(gray_image, keypoints) 17 | 18 | gray_keypoints = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 19 | cv2.imshow('BRIEF keypoints', gray_keypoints) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter05/Old/fast.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | gray_image = cv2.imread('../images/input_fast.jpg', 0) 5 | 6 | fast = cv2.FastFeatureDetector() 7 | 8 | # Detect keypoints 9 | keypoints = fast.detect(gray_image, None) 10 | print "Number of keypoints with non max suppression:", len(keypoints) 11 | 12 | # Draw keypoints on top of the input image 13 | img_keypoints_with_nonmax = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 14 | cv2.imshow('FAST keypoints - with non max suppression', img_keypoints_with_nonmax) 15 | 16 | # Disable nonmaxSuppression 17 | fast.setBool('nonmaxSuppression', False) 18 | 19 | # Detect keypoints again 20 | keypoints = fast.detect(gray_image, None) 21 | 22 | print "Total Keypoints without nonmaxSuppression:", len(keypoints) 23 | 24 | # Draw keypoints on top of the input image 25 | img_keypoints_without_nonmax = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 26 | cv2.imshow('FAST keypoints - without non max suppression', img_keypoints_without_nonmax) 27 | cv2.waitKey() -------------------------------------------------------------------------------- /Chapter05/Old/good_features_to_track.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_harris_GFTT_box.jpg') 5 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 6 | 7 | corners = cv2.goodFeaturesToTrack(gray, 7, 0.05, 25) 8 | corners = np.float32(corners) 9 | 10 | for item in corners: 11 | x, y = item[0] 12 | cv2.circle(img, (x,y), 5, 255, -1) 13 | 14 | cv2.imshow("Top 'k' features", img) 15 | cv2.waitKey() 16 | -------------------------------------------------------------------------------- /Chapter05/Old/harris_corners.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_harris_GFTT_box.jpg') 5 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 6 | 7 | gray = np.float32(gray) 8 | 9 | # for the polygon image 10 | #dst = cv2.cornerHarris(gray,4,5,0.02) 11 | 12 | # for the box image 13 | dst = cv2.cornerHarris(gray,4,5,0.04) # limited corners 14 | #dst = cv2.cornerHarris(gray,9,5,0.04) # bottom right corner 15 | #dst = cv2.cornerHarris(gray,14,5,0.04) # top left corner 16 | 17 | #result is dilated for marking the corners, not important 18 | dst = cv2.dilate(dst,None) 19 | 20 | # Threshold for an optimal value, it may vary depending on the image. 21 | img[dst>0.01*dst.max()]=[0,0,0] 22 | 23 | cv2.imshow('Harris Corners',img) 24 | cv2.waitKey() 25 | -------------------------------------------------------------------------------- /Chapter05/Old/orb.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | input_image = cv2.imread('../images/input_sift_surf_orb_fishing.jpg') 5 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 6 | 7 | # Initiate ORB object 8 | orb = cv2.ORB() 9 | 10 | # find the keypoints with ORB 11 | keypoints = orb.detect(gray_image, None) 12 | 13 | # compute the descriptors with ORB 14 | keypoints, descriptors = orb.compute(gray_image, keypoints) 15 | 16 | # draw only the location of the keypoints without size or orientation 17 | final_keypoints = cv2.drawKeypoints(input_image, keypoints, color=(0,255,0), flags=0) 18 | 19 | cv2.imshow('ORB keypoints', final_keypoints) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter05/Old/sift_features.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | input_image = cv2.imread('../images/input_sift_surf_orb_fishing.jpg') 5 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 6 | 7 | sift = cv2.SIFT() 8 | keypoints = sift.detect(gray_image, None) 9 | 10 | input_image = cv2.drawKeypoints(input_image, keypoints, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) 11 | 12 | cv2.imshow('SIFT features', input_image) 13 | cv2.waitKey() 14 | 15 | ###### 16 | 17 | # To detect and compute at the same time 18 | #keypoints, descriptors = sift.detectAndCompute(gray_image, None) 19 | -------------------------------------------------------------------------------- /Chapter05/Old/surf_features.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_sift_surf_orb_fishing.jpg') 5 | gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 6 | 7 | surf = cv2.SURF() 8 | 9 | # If we don't set this threshold, there will be too many keypoints 10 | surf.hessianThreshold = 15000 11 | 12 | kp, des = surf.detectAndCompute(gray, None) 13 | 14 | img = cv2.drawKeypoints(img, kp, None, (0,255,0), 4) 15 | 16 | cv2.imshow('SURF features', img) 17 | cv2.waitKey() 18 | -------------------------------------------------------------------------------- /Chapter05/brief_with_fast.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | gray_image = cv2.imread('../images/input_brief.jpg', 0) 5 | 6 | # Initiate FAST detector 7 | fast = cv2.FastFeatureDetector() 8 | 9 | # Initiate BRIEF extractor 10 | brief = cv2.DescriptorExtractor_create("BRIEF") 11 | 12 | # find the keypoints with STAR 13 | keypoints = fast.detect(gray_image, None) 14 | 15 | # compute the descriptors with BRIEF 16 | keypoints, descriptors = brief.compute(gray_image, keypoints) 17 | 18 | gray_keypoints = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 19 | cv2.imshow('BRIEF keypoints', gray_keypoints) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter05/brief_with_star.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | gray_image = cv2.imread('../images/input_brief.jpg', 0) 5 | 6 | # Initialize STAR detector 7 | star = cv2.FeatureDetector_create("STAR") 8 | 9 | # Initialize BRIEF extractor 10 | brief = cv2.DescriptorExtractor_create("BRIEF") 11 | 12 | # find the keypoints with STAR 13 | keypoints = star.detect(gray_image, None) 14 | 15 | # compute the descriptors with BRIEF 16 | keypoints, descriptors = brief.compute(gray_image, keypoints) 17 | 18 | gray_keypoints = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 19 | cv2.imshow('BRIEF keypoints', gray_keypoints) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter05/fast.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | gray_image = cv2.imread('../images/input_fast.jpg', 0) 5 | 6 | fast = cv2.FastFeatureDetector() 7 | 8 | # Detect keypoints 9 | keypoints = fast.detect(gray_image, None) 10 | print "Number of keypoints with non max suppression:", len(keypoints) 11 | 12 | # Draw keypoints on top of the input image 13 | img_keypoints_with_nonmax = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 14 | cv2.imshow('FAST keypoints - with non max suppression', img_keypoints_with_nonmax) 15 | 16 | # Disable nonmaxSuppression 17 | fast.setBool('nonmaxSuppression', False) 18 | 19 | # Detect keypoints again 20 | keypoints = fast.detect(gray_image, None) 21 | 22 | print "Total Keypoints without nonmaxSuppression:", len(keypoints) 23 | 24 | # Draw keypoints on top of the input image 25 | img_keypoints_without_nonmax = cv2.drawKeypoints(gray_image, keypoints, color=(0,255,0)) 26 | cv2.imshow('FAST keypoints - without non max suppression', img_keypoints_without_nonmax) 27 | cv2.waitKey() -------------------------------------------------------------------------------- /Chapter05/good_features_to_track.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_harris_GFTT_box.jpg') 5 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 6 | 7 | corners = cv2.goodFeaturesToTrack(gray, 7, 0.05, 25) 8 | corners = np.float32(corners) 9 | 10 | for item in corners: 11 | x, y = item[0] 12 | cv2.circle(img, (x,y), 5, 255, -1) 13 | 14 | cv2.imshow("Top 'k' features", img) 15 | cv2.waitKey() 16 | -------------------------------------------------------------------------------- /Chapter05/harris_corners.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_harris_GFTT_box.jpg') 5 | gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 6 | 7 | gray = np.float32(gray) 8 | 9 | # for the polygon image 10 | #dst = cv2.cornerHarris(gray,4,5,0.02) 11 | 12 | # for the box image 13 | dst = cv2.cornerHarris(gray,4,5,0.04) # limited corners 14 | #dst = cv2.cornerHarris(gray,9,5,0.04) # bottom right corner 15 | #dst = cv2.cornerHarris(gray,14,5,0.04) # top left corner 16 | 17 | #result is dilated for marking the corners, not important 18 | dst = cv2.dilate(dst,None) 19 | 20 | # Threshold for an optimal value, it may vary depending on the image. 21 | img[dst>0.01*dst.max()]=[0,0,0] 22 | 23 | cv2.imshow('Harris Corners',img) 24 | cv2.waitKey() 25 | -------------------------------------------------------------------------------- /Chapter05/orb.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | input_image = cv2.imread('../images/input_sift_surf_orb_fishing.jpg') 5 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 6 | 7 | # Initiate ORB object 8 | orb = cv2.ORB() 9 | 10 | # find the keypoints with ORB 11 | keypoints = orb.detect(gray_image, None) 12 | 13 | # compute the descriptors with ORB 14 | keypoints, descriptors = orb.compute(gray_image, keypoints) 15 | 16 | # draw only the location of the keypoints without size or orientation 17 | final_keypoints = cv2.drawKeypoints(input_image, keypoints, color=(0,255,0), flags=0) 18 | 19 | cv2.imshow('ORB keypoints', final_keypoints) 20 | cv2.waitKey() 21 | -------------------------------------------------------------------------------- /Chapter05/sift_features.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | input_image = cv2.imread('../images/input_sift_surf_orb_fishing.jpg') 5 | gray_image = cv2.cvtColor(input_image, cv2.COLOR_BGR2GRAY) 6 | 7 | sift = cv2.SIFT() 8 | keypoints = sift.detect(gray_image, None) 9 | 10 | input_image = cv2.drawKeypoints(input_image, keypoints, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) 11 | 12 | cv2.imshow('SIFT features', input_image) 13 | cv2.waitKey() 14 | 15 | ###### 16 | 17 | # To detect and compute at the same time 18 | #keypoints, descriptors = sift.detectAndCompute(gray_image, None) 19 | -------------------------------------------------------------------------------- /Chapter05/surf_features.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | img = cv2.imread('../images/input_sift_surf_orb_fishing.jpg') 5 | gray= cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 6 | 7 | surf = cv2.SURF() 8 | 9 | # If we don't set this threshold, there will be too many keypoints 10 | surf.hessianThreshold = 15000 11 | 12 | kp, des = surf.detectAndCompute(gray, None) 13 | 14 | img = cv2.drawKeypoints(img, kp, None, (0,255,0), 4) 15 | 16 | cv2.imshow('SURF features', img) 17 | cv2.waitKey() 18 | -------------------------------------------------------------------------------- /Chapter06/Old/feature_matching.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def draw_matches(img1, keypoints1, img2, keypoints2, matches): 5 | rows1, cols1 = img1.shape[:2] 6 | rows2, cols2 = img2.shape[:2] 7 | 8 | # Create a new output image that concatenates the two images together 9 | output_img = np.zeros((max([rows1,rows2]), cols1+cols2, 3), dtype='uint8') 10 | output_img[:rows1, :cols1, :] = np.dstack([img1, img1, img1]) 11 | output_img[:rows2, cols1:cols1+cols2, :] = np.dstack([img2, img2, img2]) 12 | 13 | # Draw connecting lines between matching keypoints 14 | for match in matches: 15 | # Get the matching keypoints for each of the images 16 | img1_idx = match.queryIdx 17 | img2_idx = match.trainIdx 18 | 19 | (x1, y1) = keypoints1[img1_idx].pt 20 | (x2, y2) = keypoints2[img2_idx].pt 21 | 22 | # Draw a small circle at both co-ordinates and then draw a line 23 | radius = 4 24 | colour = (0,255,0) # green 25 | thickness = 1 26 | cv2.circle(output_img, (int(x1),int(y1)), radius, colour, thickness) 27 | cv2.circle(output_img, (int(x2)+cols1,int(y2)), radius, colour, thickness) 28 | cv2.line(output_img, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), colour, thickness) 29 | 30 | return output_img 31 | 32 | if __name__=='__main__': 33 | img1 = cv2.imread('../images/input_schoolbus_partial.jpg', 0) # queryImage 34 | img2 = cv2.imread('../images/input_schoolbus_full.jpg', 0) # trainImage 35 | 36 | #img1 = cv2.imread(sys.argv[1], 0) # query image (rotated subregion) 37 | #img2 = cv2.imread(sys.argv[2], 0) # train image (full image) 38 | 39 | # Initialize ORB detector 40 | orb = cv2.ORB() 41 | 42 | # Extract keypoints and descriptors 43 | keypoints1, descriptors1 = orb.detectAndCompute(img1, None) 44 | keypoints2, descriptors2 = orb.detectAndCompute(img2, None) 45 | 46 | # Create Brute Force matcher object 47 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 48 | 49 | # Match descriptors 50 | matches = bf.match(descriptors1, descriptors2) 51 | 52 | # Sort them in the order of their distance 53 | matches = sorted(matches, key = lambda x:x.distance) 54 | 55 | # Draw first 'n' matches 56 | img3 = draw_matches(img1, keypoints1, img2, keypoints2, matches[:30]) 57 | 58 | cv2.imshow('Matched keypoints', img3) 59 | cv2.waitKey() 60 | 61 | -------------------------------------------------------------------------------- /Chapter06/Old/pano_stitch.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import argparse 3 | 4 | import cv2 5 | import numpy as np 6 | 7 | def argument_parser(): 8 | parser = argparse.ArgumentParser(description='Stitch two images together') 9 | parser.add_argument("--query-image", dest="query_image", required=True, 10 | help="First image that needs to be stitched") 11 | parser.add_argument("--train-image", dest="train_image", required=True, 12 | help="Second image that needs to be stitched") 13 | parser.add_argument("--min-match-count", dest="min_match_count", type=int, 14 | required=False, default=10, help="Minimum number of matches required") 15 | return parser 16 | 17 | # Warp img2 to img1 using the homography matrix H 18 | def warpImages(img1, img2, H): 19 | rows1, cols1 = img1.shape[:2] 20 | rows2, cols2 = img2.shape[:2] 21 | 22 | list_of_points_1 = np.float32([[0,0], [0,rows1], [cols1,rows1], [cols1,0]]).reshape(-1,1,2) 23 | temp_points = np.float32([[0,0], [0,rows2], [cols2,rows2], [cols2,0]]).reshape(-1,1,2) 24 | list_of_points_2 = cv2.perspectiveTransform(temp_points, H) 25 | list_of_points = np.concatenate((list_of_points_1, list_of_points_2), axis=0) 26 | 27 | [x_min, y_min] = np.int32(list_of_points.min(axis=0).ravel() - 0.5) 28 | [x_max, y_max] = np.int32(list_of_points.max(axis=0).ravel() + 0.5) 29 | translation_dist = [-x_min, -y_min] 30 | H_translation = np.array([[1, 0, translation_dist[0]], [0, 1, translation_dist[1]], [0,0,1]]) 31 | 32 | output_img = cv2.warpPerspective(img2, H_translation.dot(H), (x_max-x_min, y_max-y_min)) 33 | output_img[translation_dist[1]:rows1+translation_dist[1], translation_dist[0]:cols1+translation_dist[0]] = img1 34 | 35 | return output_img 36 | 37 | if __name__=='__main__': 38 | #img1 = cv2.imread('../images/pano_query_image.jpg', 0) # queryImage 39 | #img2 = cv2.imread('../images/pano_train_image.jpg', 0) # trainImage 40 | args = argument_parser().parse_args() 41 | img1 = cv2.imread(args.query_image, 0) 42 | img2 = cv2.imread(args.train_image, 0) 43 | min_match_count = args.min_match_count 44 | 45 | #cv2.imshow('Query image', img1) 46 | #cv2.imshow('Train image', img2) 47 | cv2.imshow('Frame 1', img1) 48 | cv2.imshow('Frame 2', img2) 49 | 50 | # Initialize the SIFT detector 51 | sift = cv2.SIFT() 52 | 53 | # Extract the keypoints and descriptors 54 | keypoints1, descriptors1 = sift.detectAndCompute(img1, None) 55 | keypoints2, descriptors2 = sift.detectAndCompute(img2, None) 56 | 57 | # Initialize parameters for Flann based matcher 58 | FLANN_INDEX_KDTREE = 0 59 | index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) 60 | search_params = dict(checks = 50) 61 | 62 | # Initialize the Flann based matcher object 63 | flann = cv2.FlannBasedMatcher(index_params, search_params) 64 | 65 | # Compute the matches 66 | matches = flann.knnMatch(descriptors1, descriptors2, k=2) 67 | 68 | # Store all the good matches as per Lowe's ratio test 69 | good_matches = [] 70 | for m1,m2 in matches: 71 | if m1.distance < 0.7*m2.distance: 72 | good_matches.append(m1) 73 | 74 | if len(good_matches) > min_match_count: 75 | src_pts = np.float32([ keypoints1[good_match.queryIdx].pt for good_match in good_matches ]).reshape(-1,1,2) 76 | dst_pts = np.float32([ keypoints2[good_match.trainIdx].pt for good_match in good_matches ]).reshape(-1,1,2) 77 | 78 | M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) 79 | result = warpImages(img2, img1, M) 80 | cv2.imshow('Stitched output', result) 81 | 82 | cv2.waitKey() 83 | 84 | else: 85 | print "We don't have enough number of matches between the two images." 86 | print "Found only %d matches. We need at least %d matches." % (len(good_matches), min_match_count) 87 | 88 | -------------------------------------------------------------------------------- /Chapter06/feature_matching.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def draw_matches(img1, keypoints1, img2, keypoints2, matches): 5 | rows1, cols1 = img1.shape[:2] 6 | rows2, cols2 = img2.shape[:2] 7 | 8 | # Create a new output image that concatenates the two images together 9 | output_img = np.zeros((max([rows1,rows2]), cols1+cols2, 3), dtype='uint8') 10 | output_img[:rows1, :cols1, :] = np.dstack([img1, img1, img1]) 11 | output_img[:rows2, cols1:cols1+cols2, :] = np.dstack([img2, img2, img2]) 12 | 13 | # Draw connecting lines between matching keypoints 14 | for match in matches: 15 | # Get the matching keypoints for each of the images 16 | img1_idx = match.queryIdx 17 | img2_idx = match.trainIdx 18 | 19 | (x1, y1) = keypoints1[img1_idx].pt 20 | (x2, y2) = keypoints2[img2_idx].pt 21 | 22 | # Draw a small circle at both co-ordinates and then draw a line 23 | radius = 4 24 | colour = (0,255,0) # green 25 | thickness = 1 26 | cv2.circle(output_img, (int(x1),int(y1)), radius, colour, thickness) 27 | cv2.circle(output_img, (int(x2)+cols1,int(y2)), radius, colour, thickness) 28 | cv2.line(output_img, (int(x1),int(y1)), (int(x2)+cols1,int(y2)), colour, thickness) 29 | 30 | return output_img 31 | 32 | if __name__=='__main__': 33 | img1 = cv2.imread('../images/input_schoolbus_partial.jpg', 0) # queryImage 34 | img2 = cv2.imread('../images/input_schoolbus_full.jpg', 0) # trainImage 35 | 36 | #img1 = cv2.imread(sys.argv[1], 0) # query image (rotated subregion) 37 | #img2 = cv2.imread(sys.argv[2], 0) # train image (full image) 38 | 39 | # Initialize ORB detector 40 | orb = cv2.ORB() 41 | 42 | # Extract keypoints and descriptors 43 | keypoints1, descriptors1 = orb.detectAndCompute(img1, None) 44 | keypoints2, descriptors2 = orb.detectAndCompute(img2, None) 45 | 46 | # Create Brute Force matcher object 47 | bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True) 48 | 49 | # Match descriptors 50 | matches = bf.match(descriptors1, descriptors2) 51 | 52 | # Sort them in the order of their distance 53 | matches = sorted(matches, key = lambda x:x.distance) 54 | 55 | # Draw first 'n' matches 56 | img3 = draw_matches(img1, keypoints1, img2, keypoints2, matches[:30]) 57 | 58 | cv2.imshow('Matched keypoints', img3) 59 | cv2.waitKey() 60 | 61 | -------------------------------------------------------------------------------- /Chapter06/pano_stitch.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import argparse 3 | 4 | import cv2 5 | import numpy as np 6 | 7 | def argument_parser(): 8 | parser = argparse.ArgumentParser(description='Stitch two images together') 9 | parser.add_argument("--query-image", dest="query_image", required=True, 10 | help="First image that needs to be stitched") 11 | parser.add_argument("--train-image", dest="train_image", required=True, 12 | help="Second image that needs to be stitched") 13 | parser.add_argument("--min-match-count", dest="min_match_count", type=int, 14 | required=False, default=10, help="Minimum number of matches required") 15 | return parser 16 | 17 | # Warp img2 to img1 using the homography matrix H 18 | def warpImages(img1, img2, H): 19 | rows1, cols1 = img1.shape[:2] 20 | rows2, cols2 = img2.shape[:2] 21 | 22 | list_of_points_1 = np.float32([[0,0], [0,rows1], [cols1,rows1], [cols1,0]]).reshape(-1,1,2) 23 | temp_points = np.float32([[0,0], [0,rows2], [cols2,rows2], [cols2,0]]).reshape(-1,1,2) 24 | list_of_points_2 = cv2.perspectiveTransform(temp_points, H) 25 | list_of_points = np.concatenate((list_of_points_1, list_of_points_2), axis=0) 26 | 27 | [x_min, y_min] = np.int32(list_of_points.min(axis=0).ravel() - 0.5) 28 | [x_max, y_max] = np.int32(list_of_points.max(axis=0).ravel() + 0.5) 29 | translation_dist = [-x_min, -y_min] 30 | H_translation = np.array([[1, 0, translation_dist[0]], [0, 1, translation_dist[1]], [0,0,1]]) 31 | 32 | output_img = cv2.warpPerspective(img2, H_translation.dot(H), (x_max-x_min, y_max-y_min)) 33 | output_img[translation_dist[1]:rows1+translation_dist[1], translation_dist[0]:cols1+translation_dist[0]] = img1 34 | 35 | return output_img 36 | 37 | if __name__=='__main__': 38 | #img1 = cv2.imread('../images/pano_query_image.jpg', 0) # queryImage 39 | #img2 = cv2.imread('../images/pano_train_image.jpg', 0) # trainImage 40 | args = argument_parser().parse_args() 41 | img1 = cv2.imread(args.query_image, 0) 42 | img2 = cv2.imread(args.train_image, 0) 43 | min_match_count = args.min_match_count 44 | 45 | #cv2.imshow('Query image', img1) 46 | #cv2.imshow('Train image', img2) 47 | cv2.imshow('Frame 1', img1) 48 | cv2.imshow('Frame 2', img2) 49 | 50 | # Initialize the SIFT detector 51 | sift = cv2.SIFT() 52 | 53 | # Extract the keypoints and descriptors 54 | keypoints1, descriptors1 = sift.detectAndCompute(img1, None) 55 | keypoints2, descriptors2 = sift.detectAndCompute(img2, None) 56 | 57 | # Initialize parameters for Flann based matcher 58 | FLANN_INDEX_KDTREE = 0 59 | index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5) 60 | search_params = dict(checks = 50) 61 | 62 | # Initialize the Flann based matcher object 63 | flann = cv2.FlannBasedMatcher(index_params, search_params) 64 | 65 | # Compute the matches 66 | matches = flann.knnMatch(descriptors1, descriptors2, k=2) 67 | 68 | # Store all the good matches as per Lowe's ratio test 69 | good_matches = [] 70 | for m1,m2 in matches: 71 | if m1.distance < 0.7*m2.distance: 72 | good_matches.append(m1) 73 | 74 | if len(good_matches) > min_match_count: 75 | src_pts = np.float32([ keypoints1[good_match.queryIdx].pt for good_match in good_matches ]).reshape(-1,1,2) 76 | dst_pts = np.float32([ keypoints2[good_match.trainIdx].pt for good_match in good_matches ]).reshape(-1,1,2) 77 | 78 | M, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0) 79 | result = warpImages(img2, img1, M) 80 | cv2.imshow('Stitched output', result) 81 | 82 | cv2.waitKey() 83 | 84 | else: 85 | print "We don't have enough number of matches between the two images." 86 | print "Found only %d matches. We need at least %d matches." % (len(good_matches), min_match_count) 87 | 88 | -------------------------------------------------------------------------------- /Chapter07/object_removal.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | def draw_rectangle(event, x, y, flags, params): 7 | global x_init, y_init, drawing, top_left_pt, bottom_right_pt, img_orig 8 | 9 | if event == cv2.EVENT_LBUTTONDOWN: 10 | drawing = True 11 | x_init, y_init = x, y 12 | 13 | elif event == cv2.EVENT_MOUSEMOVE: 14 | if drawing: 15 | top_left_pt, bottom_right_pt = (x_init,y_init), (x,y) 16 | img[y_init:y, x_init:x] = 255 - img_orig[y_init:y, x_init:x] 17 | cv2.rectangle(img, top_left_pt, bottom_right_pt, (0,255,0), 2) 18 | 19 | elif event == cv2.EVENT_LBUTTONUP: 20 | drawing = False 21 | top_left_pt, bottom_right_pt = (x_init,y_init), (x,y) 22 | img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] 23 | cv2.rectangle(img, top_left_pt, bottom_right_pt, (0,255,0), 2) 24 | rect_final = (x_init, y_init, x-x_init, y-y_init) 25 | remove_object(img_orig, rect_final) 26 | 27 | def compute_energy_matrix_modified(img, rect_roi): 28 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 29 | sobel_x = cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=3) 30 | sobel_y = cv2.Sobel(gray,cv2.CV_64F,0,1,ksize=3) 31 | abs_sobel_x = cv2.convertScaleAbs(sobel_x) 32 | abs_sobel_y = cv2.convertScaleAbs(sobel_y) 33 | energy_matrix = cv2.addWeighted(abs_sobel_x, 0.5, abs_sobel_y, 0.5, 0) 34 | x,y,w,h = rect_roi 35 | energy_matrix[y:y+h, x:x+w] = 0 36 | 37 | return energy_matrix 38 | 39 | def compute_energy_matrix(img): 40 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 41 | sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) 42 | sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) 43 | abs_sobel_x = cv2.convertScaleAbs(sobel_x) 44 | abs_sobel_y = cv2.convertScaleAbs(sobel_y) 45 | return cv2.addWeighted(abs_sobel_x, 0.5, abs_sobel_y, 0.5, 0) 46 | 47 | def find_vertical_seam(img, energy): 48 | rows, cols = img.shape[:2] 49 | 50 | seam = np.zeros(img.shape[0]) 51 | dist_to = np.zeros(img.shape[:2]) + sys.maxint 52 | dist_to[0,:] = np.zeros(img.shape[1]) 53 | edge_to = np.zeros(img.shape[:2]) 54 | 55 | for row in xrange(rows-1): 56 | for col in xrange(cols): 57 | if col != 0: 58 | if dist_to[row+1, col-1] > dist_to[row, col] + energy[row+1, col-1]: 59 | dist_to[row+1, col-1] = dist_to[row, col] + energy[row+1, col-1] 60 | edge_to[row+1, col-1] = 1 61 | 62 | if dist_to[row+1, col] > dist_to[row, col] + energy[row+1, col]: 63 | dist_to[row+1, col] = dist_to[row, col] + energy[row+1, col] 64 | edge_to[row+1, col] = 0 65 | 66 | if col != cols-1: 67 | if dist_to[row+1, col+1] > dist_to[row, col] + energy[row+1, col+1]: 68 | dist_to[row+1, col+1] = dist_to[row, col] + energy[row+1, col+1] 69 | edge_to[row+1, col+1] = -1 70 | 71 | # Retrace the min-path and update the 'seam' vector 72 | seam[rows-1] = np.argmin(dist_to[rows-1, :]) 73 | for i in (x for x in reversed(xrange(rows)) if x > 0): 74 | seam[i-1] = seam[i] + edge_to[i, int(seam[i])] 75 | 76 | return seam 77 | 78 | def add_vertical_seam(img, seam, num_iter): 79 | seam = seam + num_iter 80 | rows, cols = img.shape[:2] 81 | zero_col_mat = np.zeros((rows,1,3), dtype=np.uint8) 82 | img_extended = np.hstack((img, zero_col_mat)) 83 | 84 | for row in xrange(rows): 85 | for col in xrange(cols, int(seam[row]), -1): 86 | img_extended[row, col] = img[row, col-1] 87 | 88 | for i in range(3): 89 | v1 = img_extended[row, int(seam[row])-1, i] 90 | v2 = img_extended[row, int(seam[row])+1, i] 91 | img_extended[row, int(seam[row]), i] = (int(v1)+int(v2))/2 92 | 93 | return img_extended 94 | 95 | def remove_vertical_seam(img, seam): 96 | rows, cols = img.shape[:2] 97 | for row in xrange(rows): 98 | for col in xrange(int(seam[row]), cols-1): 99 | img[row, col] = img[row, col+1] 100 | 101 | img = img[:, 0:cols-1] 102 | return img 103 | 104 | def remove_object(img, rect_roi): 105 | num_seams = rect_roi[2] + 10 106 | energy = compute_energy_matrix_modified(img, rect_roi) 107 | 108 | for i in xrange(num_seams): 109 | seam = find_vertical_seam(img, energy) 110 | img = remove_vertical_seam(img, seam) 111 | x,y,w,h = rect_roi 112 | energy = compute_energy_matrix_modified(img, (x,y,w-i,h)) 113 | print 'Number of seams removed =', i+1 114 | 115 | img_output = np.copy(img) 116 | img_carved_backup = np.copy(img) 117 | 118 | for i in xrange(num_seams): 119 | seam = find_vertical_seam(img, energy) 120 | img = remove_vertical_seam(img, seam) 121 | img_output = add_vertical_seam(img_output, seam, i) 122 | energy = compute_energy_matrix(img) 123 | print 'Number of seams added =', i+1 124 | 125 | cv2.imshow('Input', img_input) 126 | cv2.imshow('Output', img_output) 127 | cv2.waitKey() 128 | 129 | if __name__=='__main__': 130 | img_input = cv2.imread(sys.argv[1]) 131 | 132 | drawing = False 133 | img = np.copy(img_input) 134 | img_orig = np.copy(img_input) 135 | 136 | cv2.namedWindow('Input') 137 | cv2.setMouseCallback('Input', draw_rectangle) 138 | 139 | while True: 140 | cv2.imshow('Input', img) 141 | c = cv2.waitKey(1) 142 | if c == 27: 143 | break 144 | 145 | cv2.destroyAllWindows() 146 | -------------------------------------------------------------------------------- /Chapter07/seam_carver_compress.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | def overlay_vertical_seam(img, seam): 7 | img_seam_overlay = np.copy(img) 8 | x_coords, y_coords = np.transpose([(i,int(j)) for i,j in enumerate(seam)]) 9 | img_seam_overlay[x_coords, y_coords] = (0,255,0) 10 | 11 | return img_seam_overlay 12 | 13 | def compute_energy_matrix(img): 14 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 15 | sobel_x = cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=3) 16 | sobel_y = cv2.Sobel(gray,cv2.CV_64F,0,1,ksize=3) 17 | abs_sobel_x = cv2.convertScaleAbs(sobel_x) 18 | abs_sobel_y = cv2.convertScaleAbs(sobel_y) 19 | return cv2.addWeighted(abs_sobel_x, 0.5, abs_sobel_y, 0.5, 0) 20 | 21 | def find_vertical_seam(img, energy): 22 | rows, cols = img.shape[:2] 23 | 24 | seam = np.zeros(img.shape[0]) 25 | dist_to = np.zeros(img.shape[:2]) + sys.maxint 26 | dist_to[0,:] = np.zeros(img.shape[1]) 27 | edge_to = np.zeros(img.shape[:2]) 28 | 29 | for row in xrange(rows-1): 30 | for col in xrange(cols): 31 | if col != 0: 32 | if dist_to[row+1, col-1] > dist_to[row, col] + energy[row+1, col-1]: 33 | dist_to[row+1, col-1] = dist_to[row, col] + energy[row+1, col-1] 34 | edge_to[row+1, col-1] = 1 35 | 36 | if dist_to[row+1, col] > dist_to[row, col] + energy[row+1, col]: 37 | dist_to[row+1, col] = dist_to[row, col] + energy[row+1, col] 38 | edge_to[row+1, col] = 0 39 | 40 | if col != cols-1: 41 | if dist_to[row+1, col+1] > dist_to[row, col] + energy[row+1, col+1]: 42 | dist_to[row+1, col+1] = dist_to[row, col] + energy[row+1, col+1] 43 | edge_to[row+1, col+1] = -1 44 | 45 | # Retrace the min-path and update the 'seam' vector 46 | seam[rows-1] = np.argmin(dist_to[rows-1, :]) 47 | for i in (x for x in reversed(xrange(rows)) if x > 0): 48 | seam[i-1] = seam[i] + edge_to[i, int(seam[i])] 49 | 50 | return seam 51 | 52 | def remove_vertical_seam(img, seam): 53 | rows, cols = img.shape[:2] 54 | for row in xrange(rows): 55 | for col in xrange(int(seam[row]), cols-1): 56 | img[row, col] = img[row, col+1] 57 | 58 | img = img[:, 0:cols-1] 59 | return img 60 | 61 | if __name__=='__main__': 62 | img_input = cv2.imread(sys.argv[1]) 63 | num_seams = int(sys.argv[2]) 64 | img = np.copy(img_input) 65 | img_overlay_seam = np.copy(img_input) 66 | energy = compute_energy_matrix(img) 67 | 68 | for i in xrange(num_seams): 69 | seam = find_vertical_seam(img, energy) 70 | img_overlay_seam = overlay_vertical_seam(img_overlay_seam, seam) 71 | img = remove_vertical_seam(img, seam) 72 | energy = compute_energy_matrix(img) 73 | print 'Number of seams removed =', i+1 74 | 75 | cv2.imshow('Input', img_input) 76 | cv2.imshow('Seams', img_overlay_seam) 77 | cv2.imshow('Output', img) 78 | cv2.waitKey() 79 | -------------------------------------------------------------------------------- /Chapter07/seam_carver_expand.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | def compute_energy_matrix(img): 7 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 8 | sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) 9 | sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) 10 | abs_sobel_x = cv2.convertScaleAbs(sobel_x) 11 | abs_sobel_y = cv2.convertScaleAbs(sobel_y) 12 | return cv2.addWeighted(abs_sobel_x, 0.5, abs_sobel_y, 0.5, 0) 13 | 14 | def find_vertical_seam(img, energy): 15 | rows, cols = img.shape[:2] 16 | 17 | seam = np.zeros(img.shape[0]) 18 | dist_to = np.zeros(img.shape[:2]) + sys.maxint 19 | dist_to[0,:] = np.zeros(img.shape[1]) 20 | edge_to = np.zeros(img.shape[:2]) 21 | 22 | for row in xrange(rows-1): 23 | for col in xrange(cols): 24 | if col != 0: 25 | if dist_to[row+1, col-1] > dist_to[row, col] + energy[row+1, col-1]: 26 | dist_to[row+1, col-1] = dist_to[row, col] + energy[row+1, col-1] 27 | edge_to[row+1, col-1] = 1 28 | 29 | if dist_to[row+1, col] > dist_to[row, col] + energy[row+1, col]: 30 | dist_to[row+1, col] = dist_to[row, col] + energy[row+1, col] 31 | edge_to[row+1, col] = 0 32 | 33 | if col != cols-1: 34 | if dist_to[row+1, col+1] > dist_to[row, col] + energy[row+1, col+1]: 35 | dist_to[row+1, col+1] = dist_to[row, col] + energy[row+1, col+1] 36 | edge_to[row+1, col+1] = -1 37 | 38 | # Retrace the min-path and update the 'seam' vector 39 | seam[rows-1] = np.argmin(dist_to[rows-1, :]) 40 | for i in (x for x in reversed(xrange(rows)) if x > 0): 41 | seam[i-1] = seam[i] + edge_to[i, int(seam[i])] 42 | 43 | return seam 44 | 45 | def add_vertical_seam(img, seam, num_iter): 46 | seam = seam + num_iter 47 | rows, cols = img.shape[:2] 48 | zero_col_mat = np.zeros((rows,1,3), dtype=np.uint8) 49 | img_extended = np.hstack((img, zero_col_mat)) 50 | 51 | for row in xrange(rows): 52 | for col in xrange(cols, int(seam[row]), -1): 53 | img_extended[row, col] = img[row, col-1] 54 | 55 | for i in range(3): 56 | v1 = img_extended[row, int(seam[row])-1, i] 57 | v2 = img_extended[row, int(seam[row])+1, i] 58 | img_extended[row, int(seam[row]), i] = (int(v1)+int(v2))/2 59 | 60 | return img_extended 61 | 62 | def remove_vertical_seam(img, seam): 63 | rows, cols = img.shape[:2] 64 | for row in xrange(rows): 65 | for col in xrange(int(seam[row]), cols-1): 66 | img[row, col] = img[row, col+1] 67 | 68 | img = img[:, 0:cols-1] 69 | return img 70 | 71 | if __name__=='__main__': 72 | img_input = cv2.imread(sys.argv[1]) 73 | num_seams = int(sys.argv[2]) 74 | img = np.copy(img_input) 75 | img_output = np.copy(img_input) 76 | energy = compute_energy_matrix(img) 77 | 78 | for i in xrange(num_seams): 79 | seam = find_vertical_seam(img, energy) 80 | img = remove_vertical_seam(img, seam) 81 | img_output = add_vertical_seam(img_output, seam, i) 82 | energy = compute_energy_matrix(img) 83 | print 'Number of seams added =', i+1 84 | 85 | cv2.imshow('Input', img_input) 86 | cv2.imshow('Output', img_output) 87 | cv2.waitKey() 88 | -------------------------------------------------------------------------------- /Chapter08/contour_approx.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | def get_all_contours(img): 7 | ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 8 | ret, thresh = cv2.threshold(ref_gray, 127, 255, 0) 9 | contours, hierarchy = cv2.findContours(thresh, 1, 2) 10 | return contours 11 | 12 | if __name__=='__main__': 13 | #img = cv2.imread('../images/input_nike_logo_shapes.png') 14 | img = cv2.imread(sys.argv[1]) 15 | input_contours = get_all_contours(img) 16 | 17 | factor = 0.00001 18 | while True: 19 | output_img = np.zeros(img.shape, np.uint8) + 255 20 | 21 | for contour in input_contours: 22 | epsilon = factor * cv2.arcLength(contour, True) 23 | approx = cv2.approxPolyDP(contour, epsilon, True) 24 | 25 | cv2.drawContours(output_img, [approx], -1, (0,0,0), 3) 26 | 27 | cv2.imshow('Output', output_img) 28 | c = cv2.waitKey() 29 | if c == 27: 30 | break 31 | 32 | factor *= 0.75 33 | 34 | -------------------------------------------------------------------------------- /Chapter08/convexity_defects_ver1.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | # Input is a color image 7 | def get_contours(img): 8 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 9 | ret, thresh = cv2.threshold(img_gray, 127, 255, 0) 10 | contours, hierarchy = cv2.findContours(thresh, 2, 1) 11 | return contours 12 | 13 | if __name__=='__main__': 14 | img = cv2.imread(sys.argv[1]) 15 | 16 | for contour in get_contours(img): 17 | hull = cv2.convexHull(contour, returnPoints=False) 18 | defects = cv2.convexityDefects(contour,hull) 19 | 20 | if defects is None: continue 21 | 22 | for i in range(defects.shape[0]): 23 | start_defect, end_defect, far_defect, _ = defects[i,0] 24 | start = tuple(contour[start_defect][0]) 25 | end = tuple(contour[end_defect][0]) 26 | far = tuple(contour[far_defect][0]) 27 | cv2.circle(img, far, 5, [128,0,0], -1) 28 | cv2.drawContours(img, [contour], -1, (0,0,0), 3) 29 | 30 | cv2.imshow('Convexity defects',img) 31 | cv2.waitKey(0) 32 | cv2.destroyAllWindows() 33 | -------------------------------------------------------------------------------- /Chapter08/convexity_defects_ver2.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | # Input is a color image 7 | def get_contours(img): 8 | img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 9 | ret, thresh = cv2.threshold(img_gray, 127, 255, 0) 10 | contours, hierarchy = cv2.findContours(thresh, 2, 1) 11 | return contours 12 | 13 | if __name__=='__main__': 14 | img = cv2.imread(sys.argv[1]) 15 | 16 | for contour in get_contours(img): 17 | orig_contour = contour 18 | epsilon = 0.01 * cv2.arcLength(contour, True) 19 | contour = cv2.approxPolyDP(contour, epsilon, True) 20 | 21 | hull = cv2.convexHull(contour, returnPoints=False) 22 | defects = cv2.convexityDefects(contour,hull) 23 | 24 | if defects is None: continue 25 | 26 | for i in range(defects.shape[0]): 27 | start_defect, end_defect, far_defect, _ = defects[i,0] 28 | start = tuple(contour[start_defect][0]) 29 | end = tuple(contour[end_defect][0]) 30 | far = tuple(contour[far_defect][0]) 31 | cv2.circle(img, far, 7, [255,0,0], -1) 32 | cv2.drawContours(img, [orig_contour], -1, (0,0,0), 3) 33 | 34 | cv2.imshow('Convexity defects',img) 35 | cv2.waitKey(0) 36 | cv2.destroyAllWindows() 37 | -------------------------------------------------------------------------------- /Chapter08/grabcut.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def draw_rectangle(event, x, y, flags, params): 5 | global x_init, y_init, drawing, top_left_pt, bottom_right_pt, img_orig 6 | 7 | if event == cv2.EVENT_LBUTTONDOWN: 8 | drawing = True 9 | x_init, y_init = x, y 10 | 11 | elif event == cv2.EVENT_MOUSEMOVE: 12 | if drawing: 13 | top_left_pt, bottom_right_pt = (x_init,y_init), (x,y) 14 | img[y_init:y, x_init:x] = 255 - img_orig[y_init:y, x_init:x] 15 | cv2.rectangle(img, top_left_pt, bottom_right_pt, (0,255,0), 2) 16 | 17 | elif event == cv2.EVENT_LBUTTONUP: 18 | drawing = False 19 | top_left_pt, bottom_right_pt = (x_init,y_init), (x,y) 20 | img[y_init:y, x_init:x] = 255 - img[y_init:y, x_init:x] 21 | cv2.rectangle(img, top_left_pt, bottom_right_pt, (0,255,0), 2) 22 | rect_final = (x_init, y_init, x-x_init, y-y_init) 23 | run_grabcut(img_orig, rect_final) 24 | 25 | def run_grabcut(img_orig, rect_final): 26 | mask = np.zeros(img_orig.shape[:2],np.uint8) 27 | x,y,w,h = rect_final 28 | mask[y:y+h, x:x+w] = 1 29 | 30 | bgdModel = np.zeros((1,65),np.float64) 31 | fgdModel = np.zeros((1,65),np.float64) 32 | 33 | cv2.grabCut(img_orig,mask,rect_final,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT) 34 | 35 | mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8') 36 | img_orig = img_orig*mask2[:,:,np.newaxis] 37 | 38 | cv2.imshow('Output', img_orig) 39 | 40 | if __name__=='__main__': 41 | drawing = False 42 | top_left_pt, bottom_right_pt = (-1,-1), (-1,-1) 43 | 44 | img_orig = cv2.imread('../images/input_hand.jpg') 45 | img = img_orig.copy() 46 | 47 | cv2.namedWindow('Input') 48 | cv2.setMouseCallback('Input', draw_rectangle) 49 | 50 | while True: 51 | cv2.imshow('Input', img) 52 | c = cv2.waitKey(1) 53 | if c == 27: 54 | break 55 | 56 | cv2.destroyAllWindows() 57 | 58 | -------------------------------------------------------------------------------- /Chapter08/shape_censor.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | def get_all_contours(img): 7 | ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 8 | ret, thresh = cv2.threshold(ref_gray, 127, 255, 0) 9 | contours, hierarchy = cv2.findContours(thresh, 1, 2) 10 | return contours 11 | 12 | if __name__=='__main__': 13 | img = cv2.imread('../images/input_censor.png') 14 | 15 | img_orig = np.copy(img) 16 | input_contours = get_all_contours(img) 17 | solidity_values = [] 18 | 19 | for contour in input_contours: 20 | area_contour = cv2.contourArea(contour) 21 | convex_hull = cv2.convexHull(contour) 22 | area_hull = cv2.contourArea(convex_hull) 23 | solidity = float(area_contour)/area_hull 24 | solidity_values.append(solidity) 25 | 26 | # Clustering 27 | criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0) 28 | flags = cv2.KMEANS_RANDOM_CENTERS 29 | solidity_values = np.array(solidity_values).reshape((len(solidity_values),1)).astype('float32') 30 | compactness, labels, centers = cv2.kmeans(solidity_values, 2, criteria, 10, flags) 31 | 32 | closest_class = np.argmin(centers) 33 | output_contours = [] 34 | for i in solidity_values[labels==closest_class]: 35 | index = np.where(solidity_values==i)[0][0] 36 | output_contours.append(input_contours[index]) 37 | 38 | cv2.drawContours(img, output_contours, -1, (0,0,0), 3) 39 | cv2.imshow('Output', img) 40 | 41 | # Censoring 42 | for contour in output_contours: 43 | rect = cv2.minAreaRect(contour) 44 | box = cv2.cv.BoxPoints(rect) 45 | box = np.int0(box) 46 | cv2.drawContours(img_orig,[box],0,(0,0,0),-1) 47 | 48 | cv2.imshow('Censored', img_orig) 49 | cv2.waitKey() 50 | 51 | -------------------------------------------------------------------------------- /Chapter08/shape_matcher.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | def get_ref_contour(img): 7 | ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 8 | ret, thresh = cv2.threshold(ref_gray, 127, 255, 0) 9 | contours, hierarchy = cv2.findContours(thresh, 1, 2) 10 | 11 | for contour in contours: 12 | area = cv2.contourArea(contour) 13 | img_area = img.shape[0] * img.shape[1] 14 | if 0.05 < area/float(img_area) < 0.8: 15 | return contour 16 | 17 | def get_all_contours(img): 18 | ref_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 19 | ret, thresh = cv2.threshold(ref_gray, 127, 255, 0) 20 | contours, hierarchy = cv2.findContours(thresh, 1, 2) 21 | return contours 22 | 23 | if __name__=='__main__': 24 | img1 = cv2.imread('../images/input_nike_logo_ref.jpg') 25 | img2 = cv2.imread('../images/input_nike_logo_shapes.png') 26 | 27 | ref_contour = get_ref_contour(img1) 28 | input_contours = get_all_contours(img2) 29 | 30 | closest_contour = input_contours[0] 31 | min_dist = sys.maxint 32 | for contour in input_contours: 33 | ret = cv2.matchShapes(ref_contour, contour, 1, 0.0) 34 | if ret < min_dist: 35 | min_dist = ret 36 | closest_contour = contour 37 | 38 | cv2.drawContours(img2, [closest_contour], -1, (0,0,0), 3) 39 | cv2.imshow('Output', img2) 40 | cv2.waitKey() 41 | 42 | -------------------------------------------------------------------------------- /Chapter09/Old/background_subtraction.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def get_frame(cap, scaling_factor): 5 | ret, frame = cap.read() 6 | frame = cv2.resize(frame, None, fx=scaling_factor, 7 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 8 | return frame 9 | 10 | if __name__=='__main__': 11 | cap = cv2.VideoCapture(0) 12 | bgSubtractor = cv2.BackgroundSubtractorMOG() 13 | history = 100 14 | 15 | while True: 16 | frame = get_frame(cap, 0.5) 17 | mask = bgSubtractor.apply(frame, learningRate=1.0/history) 18 | mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) 19 | cv2.imshow('Input frame', frame) 20 | cv2.imshow('Moving Objects', mask & frame) 21 | c = cv2.waitKey(10) 22 | if c == 27: 23 | break 24 | 25 | cap.release() 26 | cv2.destroyAllWindows() 27 | -------------------------------------------------------------------------------- /Chapter09/Old/camshift.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | class ObjectTracker(object): 7 | def __init__(self): 8 | self.cap = cv2.VideoCapture(0) 9 | ret, self.frame = self.cap.read() 10 | self.scaling_factor = 0.5 11 | self.frame = cv2.resize(self.frame, None, fx=self.scaling_factor, fy=self.scaling_factor, interpolation=cv2.INTER_AREA) 12 | cv2.namedWindow('Object Tracker') 13 | cv2.setMouseCallback('Object Tracker', self.mouse_event) 14 | 15 | self.selection = None 16 | self.drag_start = None 17 | self.tracking_state = 0 18 | 19 | def mouse_event(self, event, x, y, flags, param): 20 | x, y = np.int16([x, y]) # BUG 21 | if event == cv2.EVENT_LBUTTONDOWN: 22 | self.drag_start = (x, y) 23 | self.tracking_state = 0 24 | if self.drag_start: 25 | if flags & cv2.EVENT_FLAG_LBUTTON: 26 | h, w = self.frame.shape[:2] 27 | xo, yo = self.drag_start 28 | x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y])) 29 | x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y])) 30 | self.selection = None 31 | if x1-x0 > 0 and y1-y0 > 0: 32 | self.selection = (x0, y0, x1, y1) 33 | else: 34 | self.drag_start = None 35 | if self.selection is not None: 36 | self.tracking_state = 1 37 | 38 | def start_tracking(self): 39 | while True: 40 | ret, self.frame = self.cap.read() 41 | self.frame = cv2.resize(self.frame, None, fx=self.scaling_factor, fy=self.scaling_factor, interpolation=cv2.INTER_AREA) 42 | vis = self.frame.copy() 43 | hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) 44 | mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.))) 45 | 46 | if self.selection: 47 | x0, y0, x1, y1 = self.selection 48 | self.track_window = (x0, y0, x1-x0, y1-y0) 49 | hsv_roi = hsv[y0:y1, x0:x1] 50 | mask_roi = mask[y0:y1, x0:x1] 51 | hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] ) 52 | cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX); 53 | self.hist = hist.reshape(-1) 54 | 55 | vis_roi = vis[y0:y1, x0:x1] 56 | cv2.bitwise_not(vis_roi, vis_roi) 57 | vis[mask == 0] = 0 58 | 59 | if self.tracking_state == 1: 60 | self.selection = None 61 | prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1) 62 | prob &= mask 63 | term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) 64 | track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit) 65 | 66 | cv2.ellipse(vis, track_box, (0, 255, 0), 2) 67 | 68 | cv2.imshow('Object Tracker', vis) 69 | 70 | c = cv2.waitKey(5) 71 | if c == 27: 72 | break 73 | 74 | cv2.destroyAllWindows() 75 | 76 | if __name__ == '__main__': 77 | ObjectTracker().start_tracking() 78 | -------------------------------------------------------------------------------- /Chapter09/Old/colorspaces.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def get_frame(cap, scaling_factor): 5 | ret, frame = cap.read() 6 | frame = cv2.resize(frame, None, fx=scaling_factor, 7 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 8 | return frame 9 | 10 | if __name__=='__main__': 11 | cap = cv2.VideoCapture(0) 12 | scaling_factor = 0.5 13 | 14 | while True: 15 | frame = get_frame(cap, scaling_factor) 16 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 17 | 18 | # define range of skin color in HSV 19 | lower = np.array([0,70,60]) 20 | upper = np.array([50,150,255]) 21 | 22 | # define blue range 23 | #lower = np.array([60,100,100]) 24 | #upper = np.array([180,255,255]) 25 | 26 | # Threshold the HSV image to get only blue color 27 | mask = cv2.inRange(hsv, lower, upper) 28 | 29 | # Bitwise-AND mask and original image 30 | res = cv2.bitwise_and(frame, frame, mask=mask) 31 | res = cv2.medianBlur(res, 5) 32 | 33 | cv2.imshow('Original image', frame) 34 | cv2.imshow('Color Detector', res) 35 | c = cv2.waitKey(5) 36 | if c == 27: 37 | break 38 | 39 | cv2.destroyAllWindows() 40 | -------------------------------------------------------------------------------- /Chapter09/Old/frame_diff.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | def frame_diff(prev_frame, cur_frame, next_frame): 4 | diff_frames1 = cv2.absdiff(next_frame, cur_frame) 5 | diff_frames2 = cv2.absdiff(cur_frame, prev_frame) 6 | return cv2.bitwise_and(diff_frames1, diff_frames2) 7 | 8 | def get_frame(cap): 9 | ret, frame = cap.read() 10 | frame = cv2.resize(frame, None, fx=scaling_factor, 11 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 12 | return cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) 13 | 14 | if __name__=='__main__': 15 | cap = cv2.VideoCapture(0) 16 | scaling_factor = 0.5 17 | 18 | prev_frame = get_frame(cap) 19 | cur_frame = get_frame(cap) 20 | next_frame = get_frame(cap) 21 | 22 | while True: 23 | cv2.imshow("Object Movement", frame_diff(prev_frame, cur_frame, next_frame)) 24 | 25 | prev_frame = cur_frame 26 | cur_frame = next_frame 27 | next_frame = get_frame(cap) 28 | 29 | key = cv2.waitKey(10) 30 | if key == 27: 31 | break 32 | 33 | cv2.destroyAllWindows() 34 | -------------------------------------------------------------------------------- /Chapter09/Old/optical_flow.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def start_tracking(): 5 | cap = cv2.VideoCapture(0) 6 | scaling_factor = 0.5 7 | num_frames_to_track = 5 8 | num_frames_jump = 2 9 | tracking_paths = [] 10 | frame_index = 0 11 | tracking_params = dict(winSize = (11, 11), maxLevel = 2, 12 | criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) 13 | 14 | while True: 15 | ret, frame = cap.read() 16 | frame = cv2.resize(frame, None, fx=scaling_factor, 17 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | output_img = frame.copy() 20 | 21 | if len(tracking_paths) > 0: 22 | prev_img, current_img = prev_gray, frame_gray 23 | feature_points_0 = np.float32([tp[-1] for tp in tracking_paths]).reshape(-1, 1, 2) 24 | feature_points_1, _, _ = cv2.calcOpticalFlowPyrLK(prev_img, current_img, feature_points_0, 25 | None, **tracking_params) 26 | feature_points_0_rev, _, _ = cv2.calcOpticalFlowPyrLK(current_img, prev_img, feature_points_1, 27 | None, **tracking_params) 28 | diff_feature_points = abs(feature_points_0-feature_points_0_rev).reshape(-1, 2).max(-1) 29 | good_points = diff_feature_points < 1 30 | new_tracking_paths = [] 31 | 32 | for tp, (x, y), good_points_flag in zip(tracking_paths, 33 | feature_points_1.reshape(-1, 2), good_points): 34 | if not good_points_flag: 35 | continue 36 | 37 | tp.append((x, y)) 38 | if len(tp) > num_frames_to_track: 39 | del tp[0] 40 | 41 | new_tracking_paths.append(tp) 42 | cv2.circle(output_img, (x, y), 3, (0, 255, 0), -1) 43 | 44 | tracking_paths = new_tracking_paths 45 | cv2.polylines(output_img, [np.int32(tp) for tp in tracking_paths], False, (0, 150, 0)) 46 | 47 | if not frame_index % num_frames_jump: 48 | mask = np.zeros_like(frame_gray) 49 | mask[:] = 255 50 | for x, y in [np.int32(tp[-1]) for tp in tracking_paths]: 51 | cv2.circle(mask, (x, y), 6, 0, -1) 52 | 53 | feature_points = cv2.goodFeaturesToTrack(frame_gray, mask = mask, maxCorners = 500, 54 | qualityLevel = 0.3, minDistance = 7, blockSize = 7) 55 | 56 | if feature_points is not None: 57 | for x, y in np.float32(feature_points).reshape(-1, 2): 58 | tracking_paths.append([(x, y)]) 59 | 60 | frame_index += 1 61 | prev_gray = frame_gray 62 | cv2.imshow('Optical Flow', output_img) 63 | 64 | c = cv2.waitKey(1) 65 | if c == 27: 66 | break 67 | 68 | if __name__ == '__main__': 69 | start_tracking() 70 | cv2.destroyAllWindows() 71 | 72 | -------------------------------------------------------------------------------- /Chapter09/background_subtraction.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def get_frame(cap, scaling_factor): 5 | ret, frame = cap.read() 6 | frame = cv2.resize(frame, None, fx=scaling_factor, 7 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 8 | return frame 9 | 10 | if __name__=='__main__': 11 | cap = cv2.VideoCapture(0) 12 | bgSubtractor = cv2.BackgroundSubtractorMOG() 13 | history = 100 14 | 15 | while True: 16 | frame = get_frame(cap, 0.5) 17 | mask = bgSubtractor.apply(frame, learningRate=1.0/history) 18 | mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) 19 | cv2.imshow('Input frame', frame) 20 | cv2.imshow('Moving Objects', mask & frame) 21 | c = cv2.waitKey(10) 22 | if c == 27: 23 | break 24 | 25 | cap.release() 26 | cv2.destroyAllWindows() 27 | -------------------------------------------------------------------------------- /Chapter09/camshift.py: -------------------------------------------------------------------------------- 1 | import sys 2 | 3 | import cv2 4 | import numpy as np 5 | 6 | class ObjectTracker(object): 7 | def __init__(self): 8 | self.cap = cv2.VideoCapture(0) 9 | ret, self.frame = self.cap.read() 10 | self.scaling_factor = 0.5 11 | self.frame = cv2.resize(self.frame, None, fx=self.scaling_factor, fy=self.scaling_factor, interpolation=cv2.INTER_AREA) 12 | cv2.namedWindow('Object Tracker') 13 | cv2.setMouseCallback('Object Tracker', self.mouse_event) 14 | 15 | self.selection = None 16 | self.drag_start = None 17 | self.tracking_state = 0 18 | 19 | def mouse_event(self, event, x, y, flags, param): 20 | x, y = np.int16([x, y]) # BUG 21 | if event == cv2.EVENT_LBUTTONDOWN: 22 | self.drag_start = (x, y) 23 | self.tracking_state = 0 24 | if self.drag_start: 25 | if flags & cv2.EVENT_FLAG_LBUTTON: 26 | h, w = self.frame.shape[:2] 27 | xo, yo = self.drag_start 28 | x0, y0 = np.maximum(0, np.minimum([xo, yo], [x, y])) 29 | x1, y1 = np.minimum([w, h], np.maximum([xo, yo], [x, y])) 30 | self.selection = None 31 | if x1-x0 > 0 and y1-y0 > 0: 32 | self.selection = (x0, y0, x1, y1) 33 | else: 34 | self.drag_start = None 35 | if self.selection is not None: 36 | self.tracking_state = 1 37 | 38 | def start_tracking(self): 39 | while True: 40 | ret, self.frame = self.cap.read() 41 | self.frame = cv2.resize(self.frame, None, fx=self.scaling_factor, fy=self.scaling_factor, interpolation=cv2.INTER_AREA) 42 | vis = self.frame.copy() 43 | hsv = cv2.cvtColor(self.frame, cv2.COLOR_BGR2HSV) 44 | mask = cv2.inRange(hsv, np.array((0., 60., 32.)), np.array((180., 255., 255.))) 45 | 46 | if self.selection: 47 | x0, y0, x1, y1 = self.selection 48 | self.track_window = (x0, y0, x1-x0, y1-y0) 49 | hsv_roi = hsv[y0:y1, x0:x1] 50 | mask_roi = mask[y0:y1, x0:x1] 51 | hist = cv2.calcHist( [hsv_roi], [0], mask_roi, [16], [0, 180] ) 52 | cv2.normalize(hist, hist, 0, 255, cv2.NORM_MINMAX); 53 | self.hist = hist.reshape(-1) 54 | 55 | vis_roi = vis[y0:y1, x0:x1] 56 | cv2.bitwise_not(vis_roi, vis_roi) 57 | vis[mask == 0] = 0 58 | 59 | if self.tracking_state == 1: 60 | self.selection = None 61 | prob = cv2.calcBackProject([hsv], [0], self.hist, [0, 180], 1) 62 | prob &= mask 63 | term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 1 ) 64 | track_box, self.track_window = cv2.CamShift(prob, self.track_window, term_crit) 65 | 66 | cv2.ellipse(vis, track_box, (0, 255, 0), 2) 67 | 68 | cv2.imshow('Object Tracker', vis) 69 | 70 | c = cv2.waitKey(5) 71 | if c == 27: 72 | break 73 | 74 | cv2.destroyAllWindows() 75 | 76 | if __name__ == '__main__': 77 | ObjectTracker().start_tracking() 78 | -------------------------------------------------------------------------------- /Chapter09/colorspaces.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def get_frame(cap, scaling_factor): 5 | ret, frame = cap.read() 6 | frame = cv2.resize(frame, None, fx=scaling_factor, 7 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 8 | return frame 9 | 10 | if __name__=='__main__': 11 | cap = cv2.VideoCapture(0) 12 | scaling_factor = 0.5 13 | 14 | while True: 15 | frame = get_frame(cap, scaling_factor) 16 | hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 17 | 18 | # define range of skin color in HSV 19 | lower = np.array([0,70,60]) 20 | upper = np.array([50,150,255]) 21 | 22 | # define blue range 23 | #lower = np.array([60,100,100]) 24 | #upper = np.array([180,255,255]) 25 | 26 | # Threshold the HSV image to get only blue color 27 | mask = cv2.inRange(hsv, lower, upper) 28 | 29 | # Bitwise-AND mask and original image 30 | res = cv2.bitwise_and(frame, frame, mask=mask) 31 | res = cv2.medianBlur(res, 5) 32 | 33 | cv2.imshow('Original image', frame) 34 | cv2.imshow('Color Detector', res) 35 | c = cv2.waitKey(5) 36 | if c == 27: 37 | break 38 | 39 | cv2.destroyAllWindows() 40 | -------------------------------------------------------------------------------- /Chapter09/frame_diff.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | 3 | def frame_diff(prev_frame, cur_frame, next_frame): 4 | diff_frames1 = cv2.absdiff(next_frame, cur_frame) 5 | diff_frames2 = cv2.absdiff(cur_frame, prev_frame) 6 | return cv2.bitwise_and(diff_frames1, diff_frames2) 7 | 8 | def get_frame(cap): 9 | ret, frame = cap.read() 10 | frame = cv2.resize(frame, None, fx=scaling_factor, 11 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 12 | return cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) 13 | 14 | if __name__=='__main__': 15 | cap = cv2.VideoCapture(0) 16 | scaling_factor = 0.5 17 | 18 | prev_frame = get_frame(cap) 19 | cur_frame = get_frame(cap) 20 | next_frame = get_frame(cap) 21 | 22 | while True: 23 | cv2.imshow("Object Movement", frame_diff(prev_frame, cur_frame, next_frame)) 24 | 25 | prev_frame = cur_frame 26 | cur_frame = next_frame 27 | next_frame = get_frame(cap) 28 | 29 | key = cv2.waitKey(10) 30 | if key == 27: 31 | break 32 | 33 | cv2.destroyAllWindows() 34 | -------------------------------------------------------------------------------- /Chapter09/optical_flow.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | def start_tracking(): 5 | cap = cv2.VideoCapture(0) 6 | scaling_factor = 0.5 7 | num_frames_to_track = 5 8 | num_frames_jump = 2 9 | tracking_paths = [] 10 | frame_index = 0 11 | tracking_params = dict(winSize = (11, 11), maxLevel = 2, 12 | criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) 13 | 14 | while True: 15 | ret, frame = cap.read() 16 | frame = cv2.resize(frame, None, fx=scaling_factor, 17 | fy=scaling_factor, interpolation=cv2.INTER_AREA) 18 | frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 19 | output_img = frame.copy() 20 | 21 | if len(tracking_paths) > 0: 22 | prev_img, current_img = prev_gray, frame_gray 23 | feature_points_0 = np.float32([tp[-1] for tp in tracking_paths]).reshape(-1, 1, 2) 24 | feature_points_1, _, _ = cv2.calcOpticalFlowPyrLK(prev_img, current_img, feature_points_0, 25 | None, **tracking_params) 26 | feature_points_0_rev, _, _ = cv2.calcOpticalFlowPyrLK(current_img, prev_img, feature_points_1, 27 | None, **tracking_params) 28 | diff_feature_points = abs(feature_points_0-feature_points_0_rev).reshape(-1, 2).max(-1) 29 | good_points = diff_feature_points < 1 30 | new_tracking_paths = [] 31 | 32 | for tp, (x, y), good_points_flag in zip(tracking_paths, 33 | feature_points_1.reshape(-1, 2), good_points): 34 | if not good_points_flag: 35 | continue 36 | 37 | tp.append((x, y)) 38 | if len(tp) > num_frames_to_track: 39 | del tp[0] 40 | 41 | new_tracking_paths.append(tp) 42 | cv2.circle(output_img, (x, y), 3, (0, 255, 0), -1) 43 | 44 | tracking_paths = new_tracking_paths 45 | cv2.polylines(output_img, [np.int32(tp) for tp in tracking_paths], False, (0, 150, 0)) 46 | 47 | if not frame_index % num_frames_jump: 48 | mask = np.zeros_like(frame_gray) 49 | mask[:] = 255 50 | for x, y in [np.int32(tp[-1]) for tp in tracking_paths]: 51 | cv2.circle(mask, (x, y), 6, 0, -1) 52 | 53 | feature_points = cv2.goodFeaturesToTrack(frame_gray, mask = mask, maxCorners = 500, 54 | qualityLevel = 0.3, minDistance = 7, blockSize = 7) 55 | 56 | if feature_points is not None: 57 | for x, y in np.float32(feature_points).reshape(-1, 2): 58 | tracking_paths.append([(x, y)]) 59 | 60 | frame_index += 1 61 | prev_gray = frame_gray 62 | cv2.imshow('Optical Flow', output_img) 63 | 64 | c = cv2.waitKey(1) 65 | if c == 27: 66 | break 67 | 68 | if __name__ == '__main__': 69 | start_tracking() 70 | cv2.destroyAllWindows() 71 | 72 | -------------------------------------------------------------------------------- /Chapter10/Old/classify_data.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import argparse 4 | import cPickle as pickle 5 | 6 | import cv2 7 | import numpy as np 8 | 9 | import create_features as cf 10 | from training import ClassifierTrainer 11 | 12 | def build_arg_parser(): 13 | parser = argparse.ArgumentParser(description='Extracts features \ 14 | from each line and classifies the data') 15 | parser.add_argument("--input-image", dest="input_image", required=True, 16 | help="Input image to be classified") 17 | parser.add_argument("--svm-file", dest="svm_file", required=True, 18 | help="File containing the trained SVM model") 19 | parser.add_argument("--codebook-file", dest="codebook_file", 20 | required=True, help="File containing the codebook") 21 | return parser 22 | 23 | class ImageClassifier(object): 24 | def __init__(self, svm_file, codebook_file): 25 | with open(svm_file, 'r') as f: 26 | self.svm = pickle.load(f) 27 | 28 | with open(codebook_file, 'r') as f: 29 | self.kmeans, self.centroids = pickle.load(f) 30 | 31 | def getImageTag(self, img): 32 | img = cf.resize_to_size(img) 33 | feature_vector = cf.FeatureExtractor().get_feature_vector(img, self.kmeans, self.centroids) 34 | image_tag = self.svm.classify(feature_vector) 35 | return image_tag 36 | 37 | if __name__=='__main__': 38 | args = build_arg_parser().parse_args() 39 | svm_file = args.svm_file 40 | codebook_file = args.codebook_file 41 | input_image = cv2.imread(args.input_image) 42 | 43 | print "Output class:", ImageClassifier(svm_file, codebook_file).getImageTag(input_image) 44 | -------------------------------------------------------------------------------- /Chapter10/Old/create_features.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import argparse 4 | import cPickle as pickle 5 | import json 6 | 7 | import cv2 8 | import numpy as np 9 | from sklearn.cluster import KMeans 10 | 11 | def build_arg_parser(): 12 | parser = argparse.ArgumentParser(description='Creates features for given images') 13 | parser.add_argument("--samples", dest="cls", nargs="+", action="append", 14 | required=True, help="Folders containing the training images. \ 15 | The first element needs to be the class label.") 16 | parser.add_argument("--codebook-file", dest='codebook_file', required=True, 17 | help="Base file name to store the codebook") 18 | parser.add_argument("--feature-map-file", dest='feature_map_file', required=True, 19 | help="Base file name to store the feature map") 20 | parser.add_argument("--scale-image", dest="scale", type=int, default=150, 21 | help="Scales the longer dimension of the image down to this size.") 22 | 23 | return parser 24 | 25 | def load_input_map(label, input_folder): 26 | combined_data = [] 27 | 28 | if not os.path.isdir(input_folder): 29 | print "The folder " + input_folder + " doesn't exist" 30 | raise IOError 31 | 32 | for root, dirs, files in os.walk(input_folder): 33 | for filename in (x for x in files if x.endswith('.jpg')): 34 | combined_data.append({'label': label, 'image': os.path.join(root, filename)}) 35 | 36 | return combined_data 37 | 38 | class FeatureExtractor(object): 39 | def extract_image_features(self, img): 40 | kps = DenseDetector().detect(img) 41 | kps, fvs = SIFTExtractor().compute(img, kps) 42 | return fvs 43 | 44 | def get_centroids(self, input_map, num_samples_to_fit=10): 45 | kps_all = [] 46 | 47 | count = 0 48 | cur_label = '' 49 | for item in input_map: 50 | if count >= num_samples_to_fit: 51 | if cur_label != item['label']: 52 | count = 0 53 | else: 54 | continue 55 | 56 | count += 1 57 | 58 | if count == num_samples_to_fit: 59 | print "Built centroids for", item['label'] 60 | 61 | cur_label = item['label'] 62 | img = cv2.imread(item['image']) 63 | img = resize_to_size(img, 150) 64 | 65 | num_dims = 128 66 | fvs = self.extract_image_features(img) 67 | kps_all.extend(fvs) 68 | 69 | kmeans, centroids = Quantizer().quantize(kps_all) 70 | return kmeans, centroids 71 | 72 | def get_feature_vector(self, img, kmeans, centroids): 73 | return Quantizer().get_feature_vector(img, kmeans, centroids) 74 | 75 | def extract_feature_map(input_map, kmeans, centroids): 76 | feature_map = [] 77 | 78 | for item in input_map: 79 | temp_dict = {} 80 | temp_dict['label'] = item['label'] 81 | 82 | print "Extracting features for", item['image'] 83 | img = cv2.imread(item['image']) 84 | img = resize_to_size(img, 150) 85 | 86 | temp_dict['feature_vector'] = FeatureExtractor().get_feature_vector( 87 | img, kmeans, centroids) 88 | 89 | if temp_dict['feature_vector'] is not None: 90 | feature_map.append(temp_dict) 91 | 92 | return feature_map 93 | 94 | class Quantizer(object): 95 | def __init__(self, num_clusters=32): 96 | self.num_dims = 128 97 | self.extractor = SIFTExtractor() 98 | self.num_clusters = num_clusters 99 | self.num_retries = 10 100 | 101 | def quantize(self, datapoints): 102 | kmeans = KMeans(self.num_clusters, 103 | n_init=max(self.num_retries, 1), 104 | max_iter=10, tol=1.0) 105 | 106 | res = kmeans.fit(datapoints) 107 | centroids = res.cluster_centers_ 108 | return kmeans, centroids 109 | 110 | def normalize(self, input_data): 111 | sum_input = np.sum(input_data) 112 | if sum_input > 0: 113 | return input_data / sum_input 114 | else: 115 | return input_data 116 | 117 | def get_feature_vector(self, img, kmeans, centroids): 118 | kps = DenseDetector().detect(img) 119 | kps, fvs = self.extractor.compute(img, kps) 120 | labels = kmeans.predict(fvs) 121 | fv = np.zeros(self.num_clusters) 122 | 123 | for i, item in enumerate(fvs): 124 | fv[labels[i]] += 1 125 | 126 | fv_image = np.reshape(fv, ((1, fv.shape[0]))) 127 | return self.normalize(fv_image) 128 | 129 | class DenseDetector(object): 130 | def __init__(self, step_size=20, feature_scale=40, img_bound=20): 131 | self.detector = cv2.FeatureDetector_create("Dense") 132 | self.detector.setInt("initXyStep", step_size) 133 | self.detector.setInt("initFeatureScale", feature_scale) 134 | self.detector.setInt("initImgBound", img_bound) 135 | 136 | def detect(self, img): 137 | return self.detector.detect(img) 138 | 139 | class SIFTExtractor(object): 140 | def compute(self, image, kps): 141 | if image is None: 142 | print "Not a valid image" 143 | raise TypeError 144 | 145 | gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 146 | kps, des = cv2.SIFT().compute(gray_image, kps) 147 | return kps, des 148 | 149 | # Resize the shorter dimension to 'new_size' 150 | # while maintaining the aspect ratio 151 | def resize_to_size(input_image, new_size=150): 152 | h, w = input_image.shape[0], input_image.shape[1] 153 | ds_factor = new_size / float(h) 154 | if w < h: 155 | ds_factor = new_size / float(w) 156 | new_size = (int(w * ds_factor), int(h * ds_factor)) 157 | return cv2.resize(input_image, new_size) 158 | 159 | if __name__=='__main__': 160 | args = build_arg_parser().parse_args() 161 | 162 | input_map = [] 163 | for cls in args.cls: 164 | assert len(cls) >= 2, "Format for classes is `