def detect_features(image): """ Computer Vision 600.461/661 Assignment 2 Args: image (numpy.ndarray): The input image to detect features on. Note: this is NOT the image name or image path. Returns: pixel_coords (list of tuples): A list of (row,col) tuples of detected feature locations in the image """ harris_threshold = 0.5 sobel_vertical_kernel = [[-1, 0, 1], [-2, 0, 2], [-1, 0, 1] ] sobel_horizontal_kernel = np.rot90(sobel_vertical_kernel) I_x = convolve2d(image, sobel_vertical_kernel, mode='same', boundary='symm') I_y = convolve2d(image, sobel_horizontal_kernel, mode='same', boundary='symm') I_xx = I_x * I_x I_yy = I_y * I_y I_xy = I_x * I_y I_xx = gaussian_filter(I_xx, 3) I_yy = gaussian_filter(I_yy, 3) I_xy = gaussian_filter(I_xy, 3) R = (I_xx * I_yy - I_xy**2) - K*(I_xx + I_yy)**2 corners = nonmaxsuppts(R, 5, harris_threshold) return corners
def detect_features(image, kernel_size=5, sigma=1.5, nLimit=8, radius=3, filename=None, color_image=None): """ Computer Vision 600.461/661 Assignment 2 Args: image (numpy.ndarray): The input image to detect features on. Note: this is NOT the image name or image path. Returns: pixel_coords (list of tuples): A list of (row,col) tuples of detected feature locations in the image """ pixel_coords = list() image.astype(float) gx, gy = gauss_derivative_kernels(kernel_size, sigma) I_x = signal.convolve2d(image, gx, mode='same') I_y = signal.convolve2d(image, gy, mode='same') I_x2 = I_x * I_x I_y2 = I_y * I_y I_xy = I_x * I_y window = np.array([[1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1], [1, 1, 1, 1, 1]]) a = signal.convolve2d(I_x2, window, mode='same') b = signal.convolve2d(2.0 * I_xy, window, mode='same') c = signal.convolve2d(I_y2, window, mode='same') temp = np.sqrt(b * b + (a - c)**2) lambda1 = 0.5 * (a + c + temp) lambda2 = 0.5 * (a + c - temp) R = lambda1 * lambda2 - 0.04 * (lambda1 + lambda2)**2 all_r = [] for row in R.tolist(): all_r.extend([x for x in row]) result = heapq.nlargest(len(all_r) / nLimit, all_r) threshold = result[-1] pixel_coords = nonmaxsuppts(R, radius, threshold) if filename != None: img = image.copy() if color_image != None: img = color_image.copy() for pixel in pixel_coords: if color_image != None: cv2.circle(img, (pixel[1], pixel[0]), 3, (255, 0, 0), -1) else: cv2.circle(img, (pixel[1], pixel[0]), 3, (255, 255, 255)) cv2.imwrite(filename, img) return pixel_coords
def detect_features(image, nonmax_radius, corner_thresh_ratio): """ Input: image nonmax_radius: radius of window for non-maximum suppression corner_thresh_ration: threshold of the normalized corner strength """ # define key parameters window_size = 9 offset = (window_size - 1) / 2 k = 0.05 # create a list to store corner coordinates corner_list = [] #create corner strength image height, width = image.shape[:2] corner_strength = np.zeros((height, width)) # compute Gaussian gradients dy, dx = Gaussian_derivative(image) # compute second moments Iyy = dy**2 Iyx = dy * dx Ixx = dx**2 #compute corner strength image for y in range(offset, height - offset): for x in range(offset, width - offset): #compute second moments a_window = Iyy[y - offset:y + offset + 1, x - offset:x + offset + 1] b_window = Ixx[y - offset:y + offset + 1, x - offset:x + offset + 1] c_window = Iyx[y - offset:y + offset + 1, x - offset:x + offset + 1] a = a_window.sum() b = b_window.sum() c = c_window.sum() # det equals multiplication of two eigenvalues det = (a * b) - c**2 # trace equals sum of two eigenvalues trace = a + b # compute r r = det - k * (trace**2) # fill the corner strength image corner_strength[y][x] = r # normalize corner strength corner_strength = (corner_strength - np.min(corner_strength)) * 10.0 / ( np.max(corner_strength - np.min(corner_strength))) # apply threshold to find corner points and highlight on the image corner_thresh = corner_thresh_ratio * np.mean(corner_strength) corner_list = nonmaxsuppts(corner_strength, nonmax_radius, corner_thresh) return corner_list
def detect_features(image): imgNew = image.copy() imgNew = cv2.cvtColor(imgNew, cv2.COLOR_RGB2GRAY) row, col = imgNew.shape imgNew = cv2.GaussianBlur(imgNew, (5, 5), 0) sobelCol = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) sobelRow = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) gradMatCol = cv2.filter2D(src=imgNew, ddepth=cv2.CV_64F, kernel=sobelCol) gradMatRow = cv2.filter2D(src=imgNew, ddepth=cv2.CV_64F, kernel=sobelRow) Ixx = gradMatCol**2 Iyy = gradMatRow**2 Ixy = gradMatCol * gradMatRow cornernessMat = np.zeros((row, col), np.float64) for y in range(1, row - 1): for x in range(1, col - 1): windowIxx = Ixx[y - 1:y + 1 + 1, x - 1:x + 1 + 1] windowIxy = Ixy[y - 1:y + 1 + 1, x - 1:x + 1 + 1] windowIyy = Iyy[y - 1:y + 1 + 1, x - 1:x + 1 + 1] Sxx = windowIxx.sum() Sxy = windowIxy.sum() Syy = windowIyy.sum() det = (Sxx * Syy) - (Sxy**2) trace = Sxx + Syy cornernessMat[y, x] = det - 0.04 * (trace**2) temp = np.amax(cornernessMat) pixel_coords = nonmaxsuppts.nonmaxsuppts(cornernessMat, 10, temp * 0.1) #0.06 print len(pixel_coords) return pixel_coords # pic = cv2.imread('bikes1.png') # out = detect_features(pic) # # for ls in out: # cv2.circle(img=pic, center=(ls[1], ls[0]), radius=5, color=(0, 0, 255)) # # image.itemset((ls[0], ls[1], 0), 0) # # image.itemset((ls[0], ls[1], 1), 0) # # image.itemset((ls[0], ls[1], 2), 255) # cv2.imshow('image', pic) # cv2.waitKey(0)
def detect_features(img, harris_threshold=0.5): I_x = convolve2d(img, sobel_vertical_kernel, mode='same', boundary='symm') I_y = convolve2d(img, sobel_horizontal_kernel, mode='same', boundary='symm') I_xx = I_x * I_x I_yy = I_y * I_y I_xy = I_x * I_y I_xx = gaussian_filter(I_xx, 3) I_yy = gaussian_filter(I_yy, 3) I_xy = gaussian_filter(I_xy, 3) R = (I_xx * I_yy - I_xy**2) - K*(I_xx + I_yy)**2 corners = nonmaxsuppts(R, 5, harris_threshold) return corners
def detect_features(image, nonmaxsupptsRadius=None, nonmaxsupptsThreshold=None): """ Computer Vision 600.461/661 Assignment 2 Args: image (numpy.ndarray): The input image to detect features on. Note: this is NOT the image name or image path. Returns: pixel_coords (list of tuples): A list of (row,col) tuples of detected feature locations in the image """ # if only one argument is given # default nonmaxsupptsRadius is 20 # default nonmaxsupptsThreshold is 0.08 if (nonmaxsupptsRadius == None) and (nonmaxsupptsThreshold == None): imgNew = image.copy() imgNew = cv2.cvtColor(imgNew, cv2.COLOR_RGB2GRAY) row, col = imgNew.shape imgNew = cv2.GaussianBlur(imgNew, (5, 5), 0) # self-implement np.gradient # gradMatRow = np.zeros((row, col), np.float64) # gradMatCol = np.zeros((row, col), np.float64) # cornernessMat = np.zeros((row, col), np.float64) # for i in range(1,row-1): # for j in range(1,col-1): # gradMatRow[i,j] = (imgNew[i + 1, j] - imgNew[i - 1, j])/2.0 # gradMatCol[i,j] = (imgNew[i,j+1]-imgNew[i,j-1])/2.0 # for i in range(1,row-1): # gradMatCol[i,0] = imgNew[i,1]-imgNew[i,0] # gradMatCol[i, col-1] = imgNew[i, col-1] - imgNew[i, col-2] # gradMatRow[i, 0] = (imgNew[i + 1, 0] - imgNew[i - 1, 0]) / 2.0 # for j in range(1,col-1): # gradMatRow[0, j] = imgNew[1, j]-imgNew[0, j] # gradMatRow[row-1, j] = imgNew[row-1, j] - imgNew[row-2, j] # gradMatCol[0, j] = (imgNew[0, j + 1] - imgNew[0, j - 1]) / 2.0 # gradMatRow[0, 0] = imgNew[1, 0] - imgNew[0, 0] # gradMatRow[row - 1, 0] = imgNew[row - 1, 0] - imgNew[row - 2, 0] # gradMatRow[0, col - 1] = imgNew[1, col - 1] - imgNew[0, col - 1] # gradMatRow[row - 1, col - 1] = imgNew[row - 1, col - 1] - imgNew[row - 2, col - 1] # gradMatCol[0, 0] = imgNew[0, 1] - imgNew[0, 0] # gradMatCol[row - 1, 0] = imgNew[row - 1, 1] - imgNew[row - 1, 0] # gradMatCol[0, col - 1] = imgNew[0, col - 2] - imgNew[0, col - 1] # gradMatCol[row - 1, col - 1] = imgNew[row - 1, col - 1] - imgNew[row - 1, col - 2] # gradMatRow,gradMatCol = np.gradient(imgNew) sobelCol = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) sobelRow = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) gradMatCol = cv2.filter2D(src=imgNew, ddepth=cv2.CV_64F, kernel=sobelCol) gradMatRow = cv2.filter2D(src=imgNew, ddepth=cv2.CV_64F, kernel=sobelRow) Ixx = gradMatCol**2 Iyy = gradMatRow**2 Ixy = gradMatCol * gradMatRow cornernessMat = np.zeros((row, col), np.float64) for y in range(1, row - 1): for x in range(1, col - 1): Sxx = Ixx[y - 1:y + 1 + 1, x - 1:x + 1 + 1].sum() Sxy = Ixy[y - 1:y + 1 + 1, x - 1:x + 1 + 1].sum() Syy = Iyy[y - 1:y + 1 + 1, x - 1:x + 1 + 1].sum() det = (Sxx * Syy) - (Sxy**2) cornernessMat[y, x] = det - 0.04 * ((Sxx + Syy)**2) maxVal = np.max(cornernessMat) pixel_coords = nonmaxsuppts.nonmaxsuppts(cornernessMat, 20, 0.08 * maxVal) else: imgNew = image.copy() imgNew = cv2.cvtColor(imgNew, cv2.COLOR_RGB2GRAY) row, col = imgNew.shape imgNew = cv2.GaussianBlur(imgNew, (5, 5), 0) # self-implement np.gradient # gradMatRow = np.zeros((row, col), np.float64) # gradMatCol = np.zeros((row, col), np.float64) # cornernessMat = np.zeros((row, col), np.float64) # for i in range(1,row-1): # for j in range(1,col-1): # gradMatRow[i,j] = (imgNew[i + 1, j] - imgNew[i - 1, j])/2.0 # gradMatCol[i,j] = (imgNew[i,j+1]-imgNew[i,j-1])/2.0 # for i in range(1,row-1): # gradMatCol[i,0] = imgNew[i,1]-imgNew[i,0] # gradMatCol[i, col-1] = imgNew[i, col-1] - imgNew[i, col-2] # gradMatRow[i, 0] = (imgNew[i + 1, 0] - imgNew[i - 1, 0]) / 2.0 # for j in range(1,col-1): # gradMatRow[0, j] = imgNew[1, j]-imgNew[0, j] # gradMatRow[row-1, j] = imgNew[row-1, j] - imgNew[row-2, j] # gradMatCol[0, j] = (imgNew[0, j + 1] - imgNew[0, j - 1]) / 2.0 # gradMatRow[0, 0] = imgNew[1, 0] - imgNew[0, 0] # gradMatRow[row - 1, 0] = imgNew[row - 1, 0] - imgNew[row - 2, 0] # gradMatRow[0, col - 1] = imgNew[1, col - 1] - imgNew[0, col - 1] # gradMatRow[row - 1, col - 1] = imgNew[row - 1, col - 1] - imgNew[row - 2, col - 1] # gradMatCol[0, 0] = imgNew[0, 1] - imgNew[0, 0] # gradMatCol[row - 1, 0] = imgNew[row - 1, 1] - imgNew[row - 1, 0] # gradMatCol[0, col - 1] = imgNew[0, col - 2] - imgNew[0, col - 1] # gradMatCol[row - 1, col - 1] = imgNew[row - 1, col - 1] - imgNew[row - 1, col - 2] #gradMatRow,gradMatCol = np.gradient(imgNew) sobelCol = np.array([[1, 0, -1], [2, 0, -2], [1, 0, -1]]) sobelRow = np.array([[-1, -2, -1], [0, 0, 0], [1, 2, 1]]) gradMatCol = cv2.filter2D(src=imgNew, ddepth=cv2.CV_64F, kernel=sobelCol) gradMatRow = cv2.filter2D(src=imgNew, ddepth=cv2.CV_64F, kernel=sobelRow) Ixx = gradMatCol**2 Iyy = gradMatRow**2 Ixy = gradMatCol * gradMatRow cornernessMat = np.zeros((row, col), np.float64) for y in range(1, row - 1): for x in range(1, col - 1): Sxx = Ixx[y - 1:y + 1 + 1, x - 1:x + 1 + 1].sum() Sxy = Ixy[y - 1:y + 1 + 1, x - 1:x + 1 + 1].sum() Syy = Iyy[y - 1:y + 1 + 1, x - 1:x + 1 + 1].sum() det = (Sxx * Syy) - (Sxy**2) cornernessMat[y, x] = det - 0.04 * ((Sxx + Syy)**2) maxVal = np.max(cornernessMat) pixel_coords = nonmaxsuppts.nonmaxsuppts( cornernessMat, nonmaxsupptsRadius, nonmaxsupptsThreshold * maxVal) print len(pixel_coords) for ls in pixel_coords: image.itemset((ls[0], ls[1], 0), 0) image.itemset((ls[0], ls[1], 1), 0) image.itemset((ls[0], ls[1], 2), 255) return pixel_coords
def detect_features(image): """ Computer Vision 600.461/661 Assignment 2 Args: image (numpy.ndarray): The input image to detect features on. Note: this is NOT the image name or image path. Returns: pixel_coords (list of tuples): A list of (row,col) tuples of detected feature locations in the image """ #Determine Ix & Iy Ix = np.zeros((len(image), len(image[0]))) filters.gaussian_filter(image, (5,5), (0,1), Ix) Iy = np.zeros((len(image), len(image[0]))) filters.gaussian_filter(image, (5,5), (1,0), Iy) #sobel_x = np.matrix([[-1, -2, 0, 2, 1], [-2, -3, 0, 3, 2], [-3, -5, 0, 5, 3], [-2, -3, 0, 3, 2], [-1, -2, 0, 2, 1]]) #sobel_y = np.matrix([[1, 2, 3, 2, 1], [2, 3, 5, 3, 2], [0, 0, 0, 0, 0], [-2, -3, -5, -3, -2], [-1, -2, -3, -2, -1]]) #Ix = cv2.filter2D(image, -1, sobel_x) #Iy = cv2.filter2D(image, -1, sobel_y) Ix2 = Ix * Ix Ixy = Ix * Iy Iy2 = Iy * Iy a = {} b = {} c = {} lambda1 = {} lambda2 = {} R = np.zeros((len(image), len(image[0]))) for x in range(0, len(image)): for y in range(0, len(image[0])): a[(x,y)] = 0 b[(x,y)] = 0 c[(x,y)] = 0 if x > 2 and x < len(image) - 3 and y > 2 and y < len(image[0]) - 3: for i in range(-2,3): for j in range(-2,3): a[(x,y)] += Ix2[x+i][y+j] b[(x,y)] += (2 * Ixy[x+i][y+j]) c[(x,y)] += Iy2[x+i][y+j] #a[(x,y)] += (Ix[x+i][y+j] ** 2) #b[(x,y)] += 2 * (Ix[x+i][y+j] * Iy[x+i][y+j]) #c[(x,y)] += (Iy[x+i][y+j] ** 2) lambda1[(x,y)] = .5 * (a[(x,y)] + c[(x,y)] + math.sqrt((b[(x,y)] ** 2) + ((a[(x,y)] - c[(x,y)]) ** 2))) lambda2[(x,y)] = .5 * (a[(x,y)] + c[(x,y)] - math.sqrt((b[(x,y)] ** 2) + ((a[(x,y)] - c[(x,y)]) ** 2))) R[x,y] = (lambda1[(x,y)] * lambda2[(x,y)]) - .05 * ((lambda1[(x,y)] + lambda2[(x,y)]) ** 2) '''for x in range(0, len(image)): for y in range(0, len(image[0])): lambda1[(x,y)] = .5 * (a[(x,y)] + c[(x,y)] + math.sqrt((b[(x,y)] ** 2) + ((a[(x,y)] - c[(x,y)]) ** 2))) lambda2[(x,y)] = .5 * (a[(x,y)] + c[(x,y)] - math.sqrt((b[(x,y)] ** 2) + ((a[(x,y)] - c[(x,y)]) ** 2))) R[x][y] = (lambda1[(x,y)] * lambda2[(x,y)]) - .05 * ((lambda1[(x,y)] + lambda2[(x,y)]) ** 2) ''' #create image of features found cv2.imwrite("R.png", R) print "pre nonmax suppts: ", datetime.datetime.now().time() pixel_coords = nonmaxsuppts(R, 5, np.amax(R)*0.1) print "post nonmax suppts: ", datetime.datetime.now().time() #mark the features found on top of the gray-scale version of the original image for cord in pixel_coords: #image[cord[0]][cord[1]] = 255 cv2.circle(image, (cord[1],cord[0]), 1, (0,255,0), -1) cv2.imwrite("over.png", image) return pixel_coords