Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.50 KB | None | 0 0
  1. import numpy as np
  2. import scipy
  3. import scipy.signal as sg
  4. import scipy.ndimage as ndimage
  5. import scipy.ndimage.filters as filters
  6. import cv2
  7. import os
  8. import math
  9. from matplotlib import pyplot as plt
  10.  
  11.  
  12. output_path = './results'
  13.  
  14. def main(img, label):
  15.  
  16.     smooth_5, smooth_10 = gaussian_smooth(img)
  17.     gradient_5, magnitude_5, R_5 = sobel_edge_detection(smooth_5)
  18.     gradient_10, magnitude_10, R_10 = sobel_edge_detection(smooth_10)
  19.  
  20.     R_10[R_10 < 50] = 0
  21.  
  22.     nms_3 = nms(R_10, 3)
  23.     nms_30 = nms(R_10, 30)
  24.  
  25.  
  26.     output_3 = np.copy(img)
  27.     output_30 = np.copy(img)
  28.  
  29.     coordinates_3 = np.argwhere(nms_3 > 0)
  30.     coordinates_30 = np.argwhere(nms_30 > 0)
  31.  
  32.     for coordinate in coordinates_3:
  33.         cv2.circle(output_3, (coordinate[1], coordinate[0]), 5, (0, 0, 200), -1)  
  34.  
  35.     for coordinate in coordinates_30:
  36.         cv2.circle(output_30, (coordinate[1], coordinate[0]), 5, (0, 0, 200), -1)  
  37.  
  38.  
  39.     if (label == 'original'):
  40.  
  41.         saveimg('kernel_5.jpg', smooth_5)
  42.         saveimg('kernel_10.jpg', smooth_10)
  43.  
  44.         # savepltimg('gradient_5.jpg', gradient_5, 'hsv')
  45.         gmap_5 = hsv2rgb(gradient_5, magnitude_5, 16)
  46.         saveimg('gradient_5.jpg', gmap_5)
  47.         savepltimg('magnitude_5.jpg', magnitude_5, 'gray')
  48.         # savepltimg('gradient_10.jpg', gradient_10, 'hsv')
  49.         gmap_10 = hsv2rgb(gradient_10, magnitude_10, 8)
  50.         saveimg('gradient_10.jpg', gmap_10)
  51.         savepltimg('magnitude_10.jpg', magnitude_10, 'gray')
  52.  
  53.         # savepltimg('tensor_10.jpg', R_10, 'gray')
  54.         savepltimg('nms_3.jpg', nms_3, 'gray')
  55.         savepltimg('nms_30.jpg', nms_30, 'gray')
  56.  
  57.  
  58.     print(label, nms_3.sum())
  59.     saveimg('%s_3.jpg' % label, output_3)
  60.     print(label, nms_30.sum())
  61.     saveimg('%s_30.jpg' % label, output_30)
  62.  
  63.  
  64.  
  65.  
  66. def gaussian_smooth(img):
  67.     row, col = np.meshgrid(np.arange(-2, 3), np.arange(-2, 3))
  68.     kernel_5 = (1 / (2 * np.pi * 25)) * np.exp(-(row**2 + col**2) / (2 * 25))
  69.     kernel_5 /= kernel_5.sum()
  70.  
  71.     R = img[:, :, 0]
  72.     G = img[:, :, 1]
  73.     B = img[:, :, 2]
  74.  
  75.  
  76.     smooth_5R = sg.convolve2d(R, kernel_5, boundary='symm', mode='same')
  77.     smooth_5G = sg.convolve2d(G, kernel_5, boundary='symm', mode='same')
  78.     smooth_5B = sg.convolve2d(B, kernel_5, boundary='symm', mode='same')
  79.     smooth_5 = np.stack((smooth_5R, smooth_5G, smooth_5B), axis=2)
  80.  
  81.     row, col = np.meshgrid(np.arange(-5, 5), np.arange(-5, 5))
  82.     kernel_10 = (1 / (2 * np.pi * 25)) * np.exp(-(row**2 + col**2) / (2 * 25))
  83.     kernel_10 /= kernel_10.sum()
  84.  
  85.     smooth_10R = sg.convolve2d(R, kernel_10, boundary='symm', mode='same')
  86.     smooth_10G = sg.convolve2d(G, kernel_10, boundary='symm', mode='same')
  87.     smooth_10B = sg.convolve2d(B, kernel_10, boundary='symm', mode='same')
  88.     smooth_10 = np.stack((smooth_10R, smooth_10G, smooth_10B), axis=2)
  89.  
  90.  
  91.     return smooth_5, smooth_10
  92.  
  93. def sobel_edge_detection(img):
  94.  
  95.     gray = cv2.cvtColor(np.float32(img), cv2.COLOR_BGR2GRAY)
  96.     sobel_operator_x = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) / 8
  97.     sobel_operator_y = np.array([[1, 2, 1], [0, 0, 0], [-1, -2, -1]]) / 8
  98.     Gx = sg.convolve2d(gray, sobel_operator_x, boundary='symm', mode='same')
  99.     Gy = sg.convolve2d(gray, sobel_operator_y, boundary='symm', mode='same')
  100.     magnitude = np.sqrt(Gx**2 + Gy**2)
  101.     gradient = np.arctan2(Gx, Gy)
  102.     R_map = structure_tensor(gray, Gx, Gy)
  103.    
  104.     return gradient, magnitude, R_map
  105.  
  106. def structure_tensor(img, gradient_x, gradient_y):
  107.     row, col = np.meshgrid(np.arange(-5, 5), np.arange(-5, 5))
  108.     kernel_10 = (1 / (2 * np.pi * 25)) * np.exp(-(row**2 + col**2) / (2 * 25))
  109.     kernel_10 /= kernel_10.sum()
  110.  
  111.     Ixx = np.square(gradient_x)
  112.     Ixy = np.multiply(gradient_x, gradient_y)
  113.     Iyy = np.square(gradient_y)
  114.  
  115.     Axx = sg.convolve2d(Ixx, kernel_10, boundary='symm', mode='same')
  116.     Axy = sg.convolve2d(Ixy, kernel_10, boundary='symm', mode='same')
  117.     Ayy = sg.convolve2d(Iyy, kernel_10, boundary='symm', mode='same')
  118.  
  119.     det = np.multiply(Axx, Ayy) - np.square(Axy)    
  120.     trace = Axx + Ayy
  121.     k = 0.05
  122.  
  123.     return det - k * (trace**2)
  124.    
  125.  
  126. def nms(R_map, window_size):
  127.     data_max = filters.maximum_filter(R_map, window_size)
  128.     diff = np.absolute(data_max - R_map)
  129.     # np.savetxt('diff_%d.csv' % window_size, diff, delimiter=',')
  130.     # np.savetxt('Rmap_%d.csv' % window_size, R_map, delimiter=',')
  131.     # np.savetxt('Dmax_%d.csv' % window_size, data_max, delimiter=',')
  132.     output = np.zeros(R_map.shape, dtype=np.uint8)
  133.     output[np.logical_and(diff < 1e-3, R_map > 50)] = 1
  134.     # print(output.sum())
  135.     return output
  136.  
  137. def saveimg(filename, ref):
  138.     filepath = os.path.join(output_path, filename)
  139.     cv2.imwrite(filepath, ref)
  140.  
  141. def savepltimg(filename, ref, colormap=None):
  142.     filepath = os.path.join(output_path, filename)
  143.     plt.figure()
  144.     plt.imshow(ref, cmap=colormap, alpha=1.0)
  145.     plt.savefig(filepath)
  146.  
  147. def hsv2rgb(gradient, magnitude, threshold):
  148.  
  149.     norm = plt.Normalize(-np.pi, np.pi)
  150.     # print(norm(gradient))
  151.     output = cv2.applyColorMap((norm(gradient) * 255).astype(np.uint8), cv2.COLORMAP_HSV)
  152.     output[magnitude < threshold] = [0, 0, 0]
  153.    
  154.     return output
  155.  
  156. if __name__ == '__main__':
  157.     original = cv2.imread('original.jpg')
  158.  
  159.     rotate = ndimage.rotate(original, 45, reshape=False)
  160.     scale = cv2.resize(original, None, fx=0.5, fy=0.5)
  161.  
  162.  
  163.     if not os.path.isdir(output_path):
  164.         os.mkdir(output_path)
  165.    
  166.     main(original, 'original')
  167.     main(rotate, 'rotate')
  168.     main(scale, 'scale')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement