26 test_harris_standalone.py <imagefile name>
40 import matplotlib.pyplot
as plt
43 from scipy.interpolate
import griddata
47 debug_level = logging.DEBUG
48 logging.basicConfig(stream=sys.stdout, level=debug_level)
51 kernels = [
"MedianFilter",
"HarrisDetector",
"Histogram",
"CannyEdgeDetector",
"HoughLineTransform",
52 "Demosaicing",
"GammaCorrection",
"Sobel",
"GaussianBlur",
"Laplacian",
"Remap",
"PyrUp",
53 "PyrDown",
"Rotate",
"WarpAffine",
"HOGDescriptor"]
55 frame = cv.imread(cv.samples.findFile(
"CARLA.jpg"))
57 sys.exit(
"Could not read the image.")
59 orig_frame = cv.cvtColor(frame, cv.COLOR_RGB2GRAY)
66 table = [((i / 255) ** invGamma) * 255
for i
in range(256)]
67 table = np.array(table, np.uint8)
69 return cv.LUT(src, table)
76 toc_capture = time.perf_counter()
80 elapsed_times = [[0
for j
in range(REPEATS)]
for i
in range(len(kernels))]
81 results = [[0
for j
in range(REPEATS)]
for i
in range(len(kernels))]
84 for i
in range(len(kernels)):
85 frame = orig_frame.copy()
86 for j
in range(REPEATS):
87 start_time = time.time()
91 results[i][j] = cv.medianBlur(frame, 9)
97 results[i][j] = cv.cornerHarris(frame, blockSize, ksize , freeParameter)
99 results[i][j] = cv.dilate(results[i][j],
None)
102 tmp[results[i][j]>0.01*results[i][j].max()]=[255]
111 hist = cv.calcHist(tmp, channels, mask, histSize, ranges)
118 canny = cv.Canny(frame, t_lower, t_upper)
119 results[i][j] = canny
121 lines = cv.HoughLines(canny, 1, np.pi / 180, 150,
None, 0, 0)
123 results[i][j] = cv.demosaicing(frame, cv.COLOR_BayerRG2BGR_VNG);
127 x = cv.Sobel(frame, cv.CV_64F, 1, 0, ksize=5)
128 y = cv.Sobel(frame, cv.CV_64F, 0, 1, ksize=5)
129 absx= cv.convertScaleAbs(x)
130 absy = cv.convertScaleAbs(y)
131 results[i][j] = cv.addWeighted(absx, 0.5, absy, 0.5,0)
133 results[i][j] = cv.GaussianBlur(frame,(35,35),cv.BORDER_DEFAULT)
137 results[i][j] = cv.Laplacian(frame, ddepth, ksize=kernel_size)
149 destination = np.array([
159 grid_x, grid_y = np.mgrid[0:783:784j, 0:783:784j]
160 grid_z = griddata(destination, source, (grid_x, grid_y), method=
'cubic')
161 map_x = np.append([], [ar[:,1]
for ar
in grid_z]).reshape(784,784)
162 map_y = np.append([], [ar[:,0]
for ar
in grid_z]).reshape(784,784)
163 map_x_32 = map_x.astype(
'float32')
164 map_y_32 = map_y.astype(
'float32')
165 results[i][j] = cv.remap(frame, map_x_32, map_y_32, cv.INTER_CUBIC)
167 rows, cols = map(int, frame.shape)
168 results[i][j] = cv.pyrUp(frame, ( cols*2, rows*2 ))
170 rows, cols = map(int, frame.shape)
171 results[i][j] = cv.pyrDown(frame, ( cols/2, rows/2 ))
173 results[i][j] = cv.rotate(frame, cv.ROTATE_90_CLOCKWISE)
175 rows, cols = frame.shape[:2]
177 input_pts = np.float32([[0,0], [cols-1,0], [0,rows-1]])
178 output_pts = np.float32([[cols-1,0], [0,0], [cols-1,rows-1]])
179 input_pts = np.float32([[50, 50],
182 output_pts = np.float32([[10, 100],
186 M = cv.getAffineTransform(input_pts , output_pts)
188 results[i][j] = cv.warpAffine(frame, M, (cols,rows))
190 hog = cv.HOGDescriptor()
191 h = hog.compute(frame)
194 end_time = time.time()
195 elapsed_times[i][j] = (end_time - start_time)
197 for i
in range(len(kernels)):
198 image_name =
"CARLA_out_kernel_" + kernels[i] +
".jpg"
199 cv.imwrite(image_name, results[i][0])
200 logging.info(
"First saved image of " + kernels[i] +
": " + image_name)
202 elapsed_times = np.sort(elapsed_times)
203 average_elapsed_time = sum(elapsed_times) / REPEATS
205 print(
"Time required to submit a trivial function call:")
206 print(
" Average: {}".format(average_elapsed_time))
212 elapsed_times = np.transpose(elapsed_times)
213 df = pd.DataFrame(elapsed_times)
215 ax = sns.boxplot(data=df)
216 ax.set_xticklabels(kernels)
217 ax.legend(loc=
'best')
218 ax.tick_params(axis=
'x', labelrotation=30)
220 ax.set_xlabel(
"Computer Vision Kernel", fontsize = 20)
221 ax.set_ylabel(
"Exec. Time (s)", fontsize = 20)
228 lines = cv.HoughLinesP(canny, 1, np.pi/180.0, 120, minLineLength=10, maxLineGap=250)
230 x1, y1, x2, y2 = line[0]
232 cv.line(frame,(x1,y1),(x2,y2),(0,0,255),2)
233 cv.imwrite(
'file.png', frame)
def gammaCorrection(src, gamma)