1from __future__ import print_function 2import cv2 as cv 3import numpy as np 4import argparse 5 6source_window = 'Source image' 7corners_window = 'Corners detected' 8max_thresh = 255 9 10def cornerHarris_demo(val): 11 thresh = val 12 13 # Detector parameters 14 blockSize = 2 15 apertureSize = 3 16 k = 0.04 17 18 # Detecting corners 19 dst = cv.cornerHarris(src_gray, blockSize, apertureSize, k) 20 21 # Normalizing 22 dst_norm = np.empty(dst.shape, dtype=np.float32) 23 cv.normalize(dst, dst_norm, alpha=0, beta=255, norm_type=cv.NORM_MINMAX) 24 dst_norm_scaled = cv.convertScaleAbs(dst_norm) 25 26 # Drawing a circle around corners 27 for i in range(dst_norm.shape[0]): 28 for j in range(dst_norm.shape[1]): 29 if int(dst_norm[i,j]) > thresh: 30 cv.circle(dst_norm_scaled, (j,i), 5, (0), 2) 31 32 # Showing the result 33 cv.namedWindow(corners_window) 34 cv.imshow(corners_window, dst_norm_scaled) 35 36# Load source image and convert it to gray 37parser = argparse.ArgumentParser(description='Code for Harris corner detector tutorial.') 38parser.add_argument('--input', help='Path to input image.', default='building.jpg') 39args = parser.parse_args() 40 41src = cv.imread(cv.samples.findFile(args.input)) 42if src is None: 43 print('Could not open or find the image:', args.input) 44 exit(0) 45 46src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY) 47 48# Create a window and a trackbar 49cv.namedWindow(source_window) 50thresh = 200 # initial threshold 51cv.createTrackbar('Threshold: ', source_window, thresh, max_thresh, cornerHarris_demo) 52cv.imshow(source_window, src) 53cornerHarris_demo(thresh) 54 55cv.waitKey() 56