Select Git revision
crash_dump.te
main.py 4.28 KiB
import cv2
import numpy as np
def show(name, img, x, y):
windowStartX = 10
windowStartY = 50
windowXoffset = 5
windowYoffset = 40
w = img.shape[0] + windowXoffset
h = img.shape[1] + windowYoffset
cv2.namedWindow(name)
cv2.moveWindow(name, windowStartX + w * x, windowStartY + h * y)
cv2.imshow(name, img)
def harrisResponseImage(img):
## Compute the spatial derivatives in x and y direction.
dIdx = cv2.Sobel(img, cv2.CV_64F, 1, 0, 3)
dIdy = cv2.Sobel(img, cv2.CV_64F, 0, 1, 3)
# show("dI/dx", abs(dIdx), 1, 0)
# show("dI/dy", abs(dIdy), 2, 0)
##########################################################
## Compute Ixx, Iyy, and Ixy with
## Ixx = (dI/dx) * (dI/dx),
## Iyy = (dI/dy) * (dI/dy),
## Ixy = (dI/dx) * (dI/dy).
## Note: The multiplication between the images is element-wise (not a matrix
## multiplication)!!
Ixx = dIdx * dIdx
Iyy = dIdy * dIdy
Ixy = dIdx * dIdy
# show("Ixx", abs(dIdx), 0, 1)
# show("Iyy", abs(dIdy), 1, 1)
# show("Ixy", abs(dIdx), 2, 1)
##########################################################
## Compute the images A,B, and C by blurring the
## images Ixx, Iyy, and Ixy with a
## Gaussian filter of size 3x3 and standard deviation of 1.
kernelSize = (3, 3)
sdev = 1
A = cv2.GaussianBlur(Ixx, kernelSize, sdev)
B = cv2.GaussianBlur(Iyy, kernelSize, sdev)
C = cv2.GaussianBlur(Ixy, kernelSize, sdev)
# show("A", abs(A) * 5, 0, 1);
# show("B", abs(B) * 5, 1, 1);
# show("C", abs(C) * 5, 2, 1);
##########################################################
## TODO 1.4
## Compute the harris response with the following formula:
## R = Det - k * Trace*Trace
## Det = A * B - C * C
## Trace = A + B
k = 0.06
Det = A * B - C * C
Trace = A + B
response = Det - k*(Trace**2)
## Normalize the response image
dbg = (response - np.min(response)) / (np.max(response) - np.min(response))
dbg = dbg.astype(np.float32)
# show("Harris Response", dbg, 0, 2)
##########################################################
cv2.imwrite("dIdx.png", (abs(dIdx) * 255.0))
cv2.imwrite("dIdy.png", (abs(dIdy) * 255.0))
cv2.imwrite("A.png", (abs(A) * 5 * 255.0))
cv2.imwrite("B.png", (abs(B) * 5 * 255.0))
cv2.imwrite("C.png", (abs(C) * 5 * 255.0))
cv2.imwrite("response.png", np.uint8(dbg * 255.0))
cv2.waitKey(0)
return response
def harrisKeypoints(response, threshold = 0.1):
## TODO 2.1
## Generate a keypoint for a pixel,
## if the response is larger than the threshold
## and it is a local maximum.
##
## Don't generate keypoints at the image border.
## Note: Keypoints are stored with (x,y) and images are accessed with (y,x)!!
points = []
points_idx = np.argwhere(response>threshold)
for each_point in points_idx:
points.append(cv2.KeyPoint(each_point[1].astype(float), each_point[0].astype(float), 1))
return points
def harrisEdges(input, response, edge_threshold=-0.01):
## TODO 3.1
## Set edge pixels to red.
##
## A pixel belongs to an edge, if the response is smaller than a threshold
## and it is a minimum in x or y direction.
##
## Don't generate edges at the image border.
result = input.copy()
edges_idx = np.argwhere(response < edge_threshold)
for each_pixel in edges_idx:
result[each_pixel[0], each_pixel[1]] = (0,0, 255)
return result
def main():
input_img = cv2.imread('blox.jpg') ## read the image
input_gray = cv2.cvtColor(input_img, cv2.COLOR_BGR2GRAY) ## convert to grayscale
input_gray = (input_gray - np.min(input_gray)) / (np.max(input_gray) - np.min(input_gray)) ## normalize
input_gray = input_gray.astype(np.float32) ## convert to float32 for filtering
## Obtain Harris Response, corners and edges
response = harrisResponseImage(input_gray)
points = harrisKeypoints(response)
edges = harrisEdges(input_img, response)
imgKeypoints1 = cv2.drawKeypoints(input_img, points, outImage=None, color=(0, 255, 0))
show("Harris Keypoints", imgKeypoints1, 1, 2)
show("Harris Edges", edges, 2, 2)
cv2.waitKey(0)
cv2.imwrite("edges.png", edges)
cv2.imwrite("corners.png", imgKeypoints1)
if __name__ == '__main__':
main()