-
Notifications
You must be signed in to change notification settings - Fork 7
/
detect_corners.py
135 lines (94 loc) · 4.33 KB
/
detect_corners.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import numpy as np
import cv2
import sys
import copy as cp
import matplotlib.pyplot as plt
import argparse
import os
import random
from corners import harris_corners,shi_tomasi
#Checks if the path exists
def check_path(path):
if not os.path.exists(path):
print('ERROR! The given path does not exist.')
sys.exit(0)
#Finds corners : harris and shitomasi
def find_corners(img):
img_dup = cp.copy(img)
img_dup1 = cp.copy(img)
harris = harris_corners(img)
shitomasi,silhouette = shi_tomasi(img_dup)
#Display different corner detection methods side by side
out1 = np.concatenate((harris,shitomasi),axis=1)
out2 = np.concatenate((img_dup1,silhouette),axis=1)
out3 = np.concatenate((out1,out2),axis=0)
#cv2.imshow('Left: Harris, Right: Shi-Tomasi',out1)
#cv2.imshow('Important points',out2)
cv2.imshow('Corners',out3)
return harris,shitomasi,silhouette,out3
def main():
if len(sys.argv) < 2:
print('ERROR: Please check https://github.com/nishagandhi/detect_those_corners for sample usage of this script or run : python detect_corners.py --help')
sys.exit(0)
parser = argparse.ArgumentParser()
parser.add_argument('--input_type',default=2,required=True,type=int,help='Specify input type : 0-Image, 1-Webcam, 2-Image Folder')
parser.add_argument('--img_path',default='./images/eagle.jpeg',type=str,help='Specify image path (OPTIONAL)')
parser.add_argument('--folder_path',default='./images/',type=str,help='Specify folder path (OPTIONAL)')
parser.add_argument('--camera',default=0,type=int,help='Specify the camera you want to use (OPTIONAL)')
parser.add_argument('--save',default=False,type=bool,help='Specify True for saving the output (OPTIONAL)')
parser.add_argument('--output_path',default='output/',type=str,help='Specify output path if you want to save(OPTIONAL)')
args = parser.parse_args()
#Image
if args.input_type==0:
check_path(args.img_path)
img = cv2.imread(args.img_path)
harris,shitomasi,silhoutte,out3 = find_corners(img)
#Save the output
if args.save==True:
temp = str(random.randint(1,101))
#Uncomment these to save all outputs
'''
cv2.imwrite(args.output_path+'Harris_'+temp+'.jpg',harris)
cv2.imwrite(args.output_path+'shitomasi_'+temp+'.jpg',shitomasi)
cv2.imwrite(args.output_path+'silhouette_'+temp+'.jpg',silhouette)
'''
cv2.imwrite(args.output_path+'corners'+temp+'.jpg',out3)
cv2.waitKey(0)
#Webcam
elif args.input_type==1:
cap = cv2.VideoCapture(args.camera)
width = int(cap.get(3))
height = int(cap.get(4))
temp = str(random.randint(1,101))
writer1 = cv2.VideoWriter(args.output_path+'out_'+temp+'.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30,(width,height))
writer2 = cv2.VideoWriter(args.output_path+'out_'+temp+'.avi',cv2.VideoWriter_fourcc('M','J','P','G'),30,(width,height))
while(True):
ret,frame = cap.read()
frame1 = cp.copy(frame)
harris,shitomasi,silhouette,out3= find_corners(frame)
if args.save==True:
#To write out3, please provide proper height and width. You can add more writers.
writer1.write(frame1)
writer2.write(silhouette)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cap.release()
#Folder of images
else:
check_path(args.folder_path)
for img in os.listdir(args.folder_path):
img = cv2.imread(args.folder_path+img)
harris,shitomasi,silhouette,out3 = find_corners(img)
temp = str(random.randint(1,101))
if args.save==True:
#Uncomment these to save all outputs
'''
cv2.imwrite(args.output_path+'Harris_'+temp+'.jpg',harris)
cv2.imwrite(args.output_path+'shitomasi_'+temp+'.jpg',shitomasi)
cv2.imwrite(args.output_path+'silhouette_'+temp+'.jpg',silhouette)
'''
cv2.imwrite(args.output_path+'corners'+temp+'.jpg',out3)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == "__main__":
main()