|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import numpy as np |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import time |
| 7 | + |
| 8 | +from image_processor import * |
| 9 | + |
| 10 | +print (TCOLORS.PURPLE + "Custom Corner Detector" + TCOLORS.NORMAL) |
| 11 | + |
| 12 | +COLOR_OUT = True |
| 13 | + |
| 14 | +#FILENAME = 'chessboard_skew.jpg' |
| 15 | +#FILENAME = 'checkerboard2.png' |
| 16 | +FILENAME = "lena1.png" |
| 17 | + |
| 18 | +#Generate the Gaussian Envelope |
| 19 | +#Constants |
| 20 | +#SIGMA=1.6 |
| 21 | + |
| 22 | +BLOCK_SIZE = 3 |
| 23 | +#BLOCK_SIZE = 5 |
| 24 | +#BLOCK_SIZE = 7 |
| 25 | + |
| 26 | + |
| 27 | +K_VALUE = 0.04 |
| 28 | +#K_VALUE = 0.15 |
| 29 | + |
| 30 | +THRESHOLD = 50000 |
| 31 | + |
| 32 | + |
| 33 | +#SIGMA = float(BLOCK_SIZE) / 3.0 |
| 34 | +SIGMA = float(BLOCK_SIZE) / 6.0 |
| 35 | + |
| 36 | +img = cv2.imread(FILENAME) |
| 37 | +gray = rgb2gray(img) |
| 38 | + |
| 39 | + |
| 40 | +cstm_out = cv2.imread(FILENAME) |
| 41 | +ocv_out = cv2.imread(FILENAME) |
| 42 | + |
| 43 | +if not COLOR_OUT: |
| 44 | + cstm_out = rgb2gray(cstm_out) |
| 45 | + ocv_out = rgb2gray(cstm_out) |
| 46 | + |
| 47 | +print "Image Width: %d Image Height: %d" % (len(gray[0]), len(gray)) |
| 48 | +print "Sigma: %f" % SIGMA |
| 49 | +print "Envelope Size: %d" % BLOCK_SIZE |
| 50 | + |
| 51 | +print "Generating Gaussian Array...", |
| 52 | +start_time = time.time() |
| 53 | +#gaussian_array = gen_deviation_array(sigma = SIGMA, length = BLOCK_SIZE) |
| 54 | +gaussian_array = gen_2d_deviation_array(sigma = SIGMA, length = BLOCK_SIZE) |
| 55 | +elapsed_time = time.time() - start_time |
| 56 | +print "Done: Elapsed Time: %.3f" % elapsed_time |
| 57 | +print "Gausian Distribution:\n%s" % str(gaussian_array) |
| 58 | + |
| 59 | +print "Creating the X and Y image derivatives...", |
| 60 | +start_time = time.time() |
| 61 | +image_x, image_y = gen_image_derivatives(gray) |
| 62 | +elapsed_time = time.time() - start_time |
| 63 | +print "Done: Elapsed Time: %.3f" % elapsed_time |
| 64 | + |
| 65 | +print "Generate <Ix^2*W(u,v)>, <IxIy*W(u,v)>, <Iy^2*W(u,v)>...", |
| 66 | +start_time = time.time() |
| 67 | +a, bc, d = generate_matrix_values(image_x, image_y, gaussian_array) |
| 68 | +elapsed_time = time.time() - start_time |
| 69 | +print "Done: Elapsed Time: %.3f" % elapsed_time |
| 70 | + |
| 71 | +print "Find the R Values:...", |
| 72 | +start_time = time.time() |
| 73 | +corners, det, trc = generate_mc_debug(a, bc, d, K_VALUE, THRESHOLD) |
| 74 | + |
| 75 | +elapsed_time = time.time() - start_time |
| 76 | +print "Done: Elapsed Time: %.3f" % elapsed_time |
| 77 | + |
| 78 | +print "Display original image, gray image, DX Image, DY Image and Gaussian Array" |
| 79 | + |
| 80 | +print "Apply to gray image" |
| 81 | +height = len(gray) |
| 82 | +width = len(gray[0]) |
| 83 | + |
| 84 | +A = np.ndarray(shape=(len(a), len(a[0]))) |
| 85 | +BC = np.ndarray(shape=(len(a), len(a[0]))) |
| 86 | +D = np.ndarray(shape=(len(a), len(a[0]))) |
| 87 | + |
| 88 | +for y in range(height): |
| 89 | + for x in range(width): |
| 90 | + A[y,x] = int(a[y,x]) |
| 91 | + BC[y,x] = int(bc[y,x]) |
| 92 | + D[y,x] = int(d[y,x]) |
| 93 | + |
| 94 | + |
| 95 | +ocv_gray = np.float32(gray) |
| 96 | +ocv = cv2.cornerHarris(ocv_gray, |
| 97 | + BLOCK_SIZE, |
| 98 | + 3, |
| 99 | + K_VALUE) |
| 100 | + |
| 101 | +ocv = cv2.dilate(ocv,None) |
| 102 | + |
| 103 | +if COLOR_OUT: |
| 104 | + corners = cv2.dilate(corners,None) |
| 105 | + cstm_out[corners == 255]=[0, 255, 0] |
| 106 | + ocv_out[ocv > (0.01 * ocv.max())]=[0, 255, 0] |
| 107 | + |
| 108 | +else: |
| 109 | + cstm_out[corners == 255]=[255] |
| 110 | + ocv_out[ocv > (0.01 * ocv.max())]=[255] |
| 111 | + |
| 112 | + |
| 113 | +fig = plt.figure() |
| 114 | +a=fig.add_subplot(5,3,1) |
| 115 | +plt.title("Original") |
| 116 | +plt.imshow(gray, cmap='Greys_r') |
| 117 | +plt.axis('off') |
| 118 | + |
| 119 | +a=fig.add_subplot(5,3,2) |
| 120 | +plt.title("Gaussian") |
| 121 | +plt.imshow(gaussian_array, cmap='Greys_r') |
| 122 | +plt.axis('off') |
| 123 | + |
| 124 | +a=fig.add_subplot(5,3,4) |
| 125 | +plt.title("X Derivative") |
| 126 | +plt.imshow(image_x, cmap='Greys_r') |
| 127 | +plt.axis('off') |
| 128 | + |
| 129 | +a=fig.add_subplot(5,3,5) |
| 130 | +plt.title("Y Derivative") |
| 131 | +plt.imshow(image_y, cmap='Greys_r') |
| 132 | +plt.axis('off') |
| 133 | + |
| 134 | +f=fig.add_subplot(5,3,7) |
| 135 | +plt.title("A = <Ix^2 * w(u, v)>") |
| 136 | +plt.imshow(A, cmap="Greys_r") |
| 137 | +plt.axis('off') |
| 138 | + |
| 139 | +f=fig.add_subplot(5,3,8) |
| 140 | +plt.title("B and C = <IxIy * w(u, v)>") |
| 141 | +plt.imshow(BC, cmap="Greys_r") |
| 142 | +plt.axis('off') |
| 143 | + |
| 144 | +f=fig.add_subplot(5,3,9) |
| 145 | +plt.title("D = <Iy^2 * w(u,v)") |
| 146 | +plt.imshow(D, cmap="Greys_r") |
| 147 | +plt.axis('off') |
| 148 | + |
| 149 | +f=fig.add_subplot(5,3,10) |
| 150 | +plt.title("Determinate(M)") |
| 151 | +plt.imshow(det, cmap="Greys_r") |
| 152 | +plt.axis('off') |
| 153 | + |
| 154 | +f=fig.add_subplot(5,3,11) |
| 155 | +plt.title("k * trace(M) ^ 2") |
| 156 | +plt.imshow(trc, cmap="Greys_r") |
| 157 | +plt.axis('off') |
| 158 | + |
| 159 | +if COLOR_OUT: |
| 160 | + f=fig.add_subplot(5,3,13) |
| 161 | + plt.title("Custom HCD Result") |
| 162 | + plt.imshow(cstm_out) |
| 163 | + plt.axis('off') |
| 164 | + |
| 165 | + f=fig.add_subplot(5,3,14) |
| 166 | + plt.title("Openc CV HCD Result") |
| 167 | + plt.imshow(ocv_out) |
| 168 | + plt.axis('off') |
| 169 | + |
| 170 | +else: |
| 171 | + f=fig.add_subplot(5,3,13) |
| 172 | + plt.title("Custom HCD Result") |
| 173 | + #plt.imshow(corners, cmap="Greys_r") |
| 174 | + plt.imshow(cstm_out, cmap="Greys_r") |
| 175 | + plt.axis('off') |
| 176 | + |
| 177 | + f=fig.add_subplot(5,3,14) |
| 178 | + plt.title("Openc CV HCD Result") |
| 179 | + plt.imshow(ocv_out, cmap="Greys_r") |
| 180 | + plt.axis('off') |
| 181 | + |
| 182 | +plt.show() |
| 183 | + |
0 commit comments