Skip to content

Commit 23ea31c

Browse files
committed
Code Files Added
1 parent a61bd2c commit 23ea31c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+134118
-0
lines changed

Module 2/1/convolution.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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()

Module 2/1/edge_detection.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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()

Module 2/1/histogram.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+

Module 2/1/image_filters.py

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+

Module 2/1/vignette.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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()

Module 2/10/find_fund_matrix.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import argparse
2+
3+
import cv2
4+
import numpy as np
5+
6+
def build_arg_parser():
7+
parser = argparse.ArgumentParser(description='Find fundamental matrix \
8+
using the two input stereo images and draw epipolar lines')
9+
parser.add_argument("--img-left", dest="img_left", required=True,
10+
help="Image captured from the left view")
11+
parser.add_argument("--img-right", dest="img_right", required=True,
12+
help="Image captured from the right view")
13+
parser.add_argument("--feature-type", dest="feature_type",
14+
required=True, help="Feature extractor that will be used; can be either 'sift' or 'surf'")
15+
return parser
16+
17+
def draw_lines(img_left, img_right, lines, pts_left, pts_right):
18+
h,w = img_left.shape
19+
img_left = cv2.cvtColor(img_left, cv2.COLOR_GRAY2BGR)
20+
img_right = cv2.cvtColor(img_right, cv2.COLOR_GRAY2BGR)
21+
22+
for line, pt_left, pt_right in zip(lines, pts_left, pts_right):
23+
x_start,y_start = map(int, [0, -line[2]/line[1] ])
24+
x_end,y_end = map(int, [w, -(line[2]+line[0]*w)/line[1] ])
25+
color = tuple(np.random.randint(0,255,2).tolist())
26+
cv2.line(img_left, (x_start,y_start), (x_end,y_end), color,1)
27+
cv2.circle(img_left, tuple(pt_left), 5, color, -1)
28+
cv2.circle(img_right, tuple(pt_right), 5, color, -1)
29+
30+
return img_left, img_right
31+
32+
def get_descriptors(gray_image, feature_type):
33+
if feature_type == 'surf':
34+
feature_extractor = cv2.SURF()
35+
36+
elif feature_type == 'sift':
37+
feature_extractor = cv2.SIFT()
38+
39+
else:
40+
raise TypeError("Invalid feature type; should be either 'surf' or 'sift'")
41+
42+
keypoints, descriptors = feature_extractor.detectAndCompute(gray_image, None)
43+
return keypoints, descriptors
44+
45+
if __name__=='__main__':
46+
args = build_arg_parser().parse_args()
47+
img_left = cv2.imread(args.img_left,0) # left image
48+
img_right = cv2.imread(args.img_right,0) # right image
49+
feature_type = args.feature_type
50+
51+
if feature_type not in ['sift', 'surf']:
52+
raise TypeError("Invalid feature type; has to be either 'sift' or 'surf'")
53+
54+
scaling_factor = 1.0
55+
img_left = cv2.resize(img_left, None, fx=scaling_factor,
56+
fy=scaling_factor, interpolation=cv2.INTER_AREA)
57+
img_right = cv2.resize(img_right, None, fx=scaling_factor,
58+
fy=scaling_factor, interpolation=cv2.INTER_AREA)
59+
60+
kps_left, des_left = get_descriptors(img_left, feature_type)
61+
kps_right, des_right = get_descriptors(img_right, feature_type)
62+
63+
# FLANN parameters
64+
FLANN_INDEX_KDTREE = 0
65+
index_params = dict(algorithm = FLANN_INDEX_KDTREE, trees = 5)
66+
search_params = dict(checks=50)
67+
68+
# Get the matches based on the descriptors
69+
flann = cv2.FlannBasedMatcher(index_params, search_params)
70+
matches = flann.knnMatch(des_left, des_right, k=2)
71+
72+
pts_left_image = []
73+
pts_right_image = []
74+
75+
# ratio test to retain only the good matches
76+
for i,(m,n) in enumerate(matches):
77+
if m.distance < 0.7*n.distance:
78+
pts_left_image.append(kps_left[m.queryIdx].pt)
79+
pts_right_image.append(kps_right[m.trainIdx].pt)
80+
81+
pts_left_image = np.float32(pts_left_image)
82+
pts_right_image = np.float32(pts_right_image)
83+
F, mask = cv2.findFundamentalMat(pts_left_image, pts_right_image, cv2.FM_LMEDS)
84+
85+
# Selecting only the inliers
86+
pts_left_image = pts_left_image[mask.ravel()==1]
87+
pts_right_image = pts_right_image[mask.ravel()==1]
88+
89+
# Drawing the lines on left image and the corresponding feature points on the right image
90+
lines1 = cv2.computeCorrespondEpilines(pts_right_image.reshape(-1,1,2), 2, F)
91+
lines1 = lines1.reshape(-1,3)
92+
img_left_lines, img_right_pts = draw_lines(img_left, img_right, lines1, pts_left_image, pts_right_image)
93+
94+
# Drawing the lines on right image and the corresponding feature points on the left image
95+
lines2 = cv2.computeCorrespondEpilines(pts_left_image.reshape(-1,1,2), 1,F)
96+
lines2 = lines2.reshape(-1,3)
97+
img_right_lines, img_left_pts = draw_lines(img_right, img_left, lines2, pts_right_image, pts_left_image)
98+
99+
cv2.imshow('Epi lines on left image', img_left_lines)
100+
cv2.imshow('Feature points on right image', img_right_pts)
101+
cv2.imshow('Epi lines on right image', img_right_lines)
102+
cv2.imshow('Feature points on left image', img_left_pts)
103+
cv2.waitKey()
104+
cv2.destroyAllWindows()

Module 2/10/stereo_match.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import argparse
2+
3+
import cv2
4+
import numpy as np
5+
6+
def build_arg_parser():
7+
parser = argparse.ArgumentParser(description='Reconstruct the 3D map from \
8+
the two input stereo images. Output will be saved in \'output.ply\'')
9+
parser.add_argument("--image-left", dest="image_left", required=True,
10+
help="Input image captured from the left")
11+
parser.add_argument("--image-right", dest="image_right", required=True,
12+
help="Input image captured from the right")
13+
parser.add_argument("--output-file", dest="output_file", required=True,
14+
help="Output filename (without the extension) where the point cloud will be saved")
15+
return parser
16+
17+
def create_output(vertices, colors, filename):
18+
colors = colors.reshape(-1, 3)
19+
vertices = np.hstack([vertices.reshape(-1,3), colors])
20+
21+
ply_header = '''ply
22+
format ascii 1.0
23+
element vertex %(vert_num)d
24+
property float x
25+
property float y
26+
property float z
27+
property uchar red
28+
property uchar green
29+
property uchar blue
30+
end_header
31+
'''
32+
33+
with open(filename, 'w') as f:
34+
f.write(ply_header % dict(vert_num=len(vertices)))
35+
np.savetxt(f, vertices, '%f %f %f %d %d %d')
36+
37+
if __name__ == '__main__':
38+
args = build_arg_parser().parse_args()
39+
image_left = cv2.imread(args.image_left)
40+
image_right = cv2.imread(args.image_right)
41+
output_file = args.output_file + '.ply'
42+
43+
if image_left.shape[0] != image_right.shape[0] or \
44+
image_left.shape[1] != image_right.shape[1]:
45+
raise TypeError("Input images must be of the same size")
46+
47+
# downscale images for faster processing
48+
image_left = cv2.pyrDown(image_left)
49+
image_right = cv2.pyrDown(image_right)
50+
51+
# disparity range is tuned for 'aloe' image pair
52+
win_size = 1
53+
min_disp = 16
54+
max_disp = min_disp * 9
55+
num_disp = max_disp - min_disp # Needs to be divisible by 16
56+
stereo = cv2.StereoSGBM(minDisparity = min_disp,
57+
numDisparities = num_disp,
58+
SADWindowSize = win_size,
59+
uniquenessRatio = 10,
60+
speckleWindowSize = 100,
61+
speckleRange = 32,
62+
disp12MaxDiff = 1,
63+
P1 = 8*3*win_size**2,
64+
P2 = 32*3*win_size**2,
65+
fullDP = True
66+
)
67+
68+
print "\nComputing the disparity map ..."
69+
disparity_map = stereo.compute(image_left, image_right).astype(np.float32) / 16.0
70+
71+
print "\nGenerating the 3D map ..."
72+
h, w = image_left.shape[:2]
73+
focal_length = 0.8*w
74+
75+
# Perspective transformation matrix
76+
Q = np.float32([[1, 0, 0, -w/2.0],
77+
[0,-1, 0, h/2.0],
78+
[0, 0, 0, -focal_length],
79+
[0, 0, 1, 0]])
80+
81+
points_3D = cv2.reprojectImageTo3D(disparity_map, Q)
82+
colors = cv2.cvtColor(image_left, cv2.COLOR_BGR2RGB)
83+
mask_map = disparity_map > disparity_map.min()
84+
output_points = points_3D[mask_map]
85+
output_colors = colors[mask_map]
86+
87+
print "\nCreating the output file ...\n"
88+
create_output(output_points, output_colors, output_file)
89+
90+
#cv2.imshow('Left Image', image_left)
91+
#cv2.imshow('Right Image', image_right)
92+
#cv2.imshow('Disparity Map', (disparity_map - min_disp) / num_disp)
93+
#cv2.waitKey()
94+
#cv2.destroyAllWindows()
95+

0 commit comments

Comments
 (0)