Skip to content
Snippets Groups Projects
Select Git revision
  • 550f985c968d21b9f4f9820b13391ea04b2aed3c
  • master default protected
  • android-msm-bullhead-3.10-nougat_kgdb_less_changes
  • android-msm-bullhead-3.10-nougat_kgdb
  • android-msm-bullhead-3.10-nougat_klist
  • android-4.4
  • android-msm-vega-4.4-oreo-daydream
  • android-msm-wahoo-4.4-p-preview-5
  • android-msm-wahoo-4.4-pie
  • android-msm-marlin-3.18-p-preview-5
  • android-msm-marlin-3.18-pie
  • android-msm-wahoo-2018.07-oreo-m2
  • android-msm-wahoo-2018.07-oreo-m4
  • android-msm-wahoo-4.4-p-preview-4
  • android-msm-bullhead-3.10-oreo-m6
  • android-msm-angler-3.10-oreo-m6
  • android-msm-marlin-3.18-p-preview-4
  • android-msm-stargazer-3.18-oreo-wear-dr
  • android-msm-catshark-3.18-oreo-wear-dr
  • android-msm-wahoo-4.4-oreo-m2
  • android-msm-wahoo-4.4-oreo-m4
  • android-daydreamos-8.0.0_r0.5
  • android-8.1.0_r0.92
  • android-8.1.0_r0.91
  • android-daydreamos-8.0.0_r0.4
  • android-p-preview-5_r0.2
  • android-p-preview-5_r0.1
  • android-9.0.0_r0.5
  • android-9.0.0_r0.4
  • android-9.0.0_r0.2
  • android-9.0.0_r0.1
  • android-8.1.0_r0.81
  • android-8.1.0_r0.80
  • android-8.1.0_r0.78
  • android-8.1.0_r0.76
  • android-8.1.0_r0.75
  • android-8.1.0_r0.72
  • android-8.1.0_r0.70
  • android-p-preview-4_r0.2
  • android-p-preview-4_r0.1
  • android-wear-8.0.0_r0.30
41 results

ray_cs.c

Blame
  • 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()