Facebook
From Buff Echidna, 5 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 224
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Thu Mar 14 10:36:01 2019
  4.  
  5. @author: Student
  6. """
  7. import os
  8. import numpy as np
  9. from matplotlib import pyplot as plt
  10. from skimage import io
  11. from skimage import color
  12. from scipy import signal
  13. from scipy import ndimage as nd
  14.  
  15. def our_gradient(image):
  16.    
  17.     gradient_x = np.zeros(image.shape)
  18.     gradient_y = np.zeros(image.shape)
  19.     gradient_x[:, 1:-1] = image[:, 2:] - image[:, 0:-2]
  20.     gradient_y[1:-1, :] = image[2: , :] - image[0:-2 , :]
  21.     return [gradient_y, gradient_x]
  22.  
  23. def run():
  24.     current_path = os.path.abspath(os.path.dirname(__file__))
  25.     image_path = os.path.join(current_path, '1.jpg')
  26.  
  27.     image = io.imread(image_path)
  28.     image = color.rgb2gray(image)
  29.    
  30. #    #gradients = np.gradient(image)
  31. #    gradients = our_gradient(image)
  32. #    gradient_y, gradient_x = gradients[0], gradients[1]
  33. #    
  34. #    gradient_mag = np.sqrt(np.square(gradient_x) + np.square(gradient_y))
  35. #    gradient_angle = np.arctan2(gradient_y, gradient_x)
  36. #    
  37. #    plt.figure()
  38. #    plt.subplot(2,3,1)
  39. #    plt.imshow(image, cmap='gray')
  40. #    plt.axis('off')
  41. #    plt.subplot(2,3,2)
  42. #    plt.imshow(gradient_x, cmap='gray')
  43. #    plt.axis('off')
  44. #    plt.subplot(2,3,3)
  45. #    plt.imshow(gradient_y, cmap='gray')
  46. #    plt.axis('off')
  47. #    plt.subplot(2,3,4)
  48. #    plt.imshow(gradient_mag, cmap = 'gray')
  49. #    plt.axis('off')
  50. #    plt.subplot(2,3,5)
  51. #    plt.imshow(gradient_angle, cmap = 'gray') #### przedstawic w odpowiedniej przestrzeni barw, tab okresowa
  52. #    plt.axis('off')
  53. #    plt.show()
  54.  
  55. #    Zad.
  56. #    Dokonaj filtracji obrazu filtrem usredniajacym o wielkosci maski (21,21).
  57. #    Pokaz obraz oryginalny obok obrazu po filtracji.
  58. #
  59. #    mask = np.ones((13,13))
  60. #    filtered_image = signal.convolve2d(image, mask)
  61. #    gauss_image = nd.gaussian_filter(image, sigma=2) #### funkcja, ktora generuje maske gaussa,
  62. #                                                     #### przyjmie jako wejscie sigme oraz rozmiar maski
  63. #    median_image = signal.medfilt(image, 13)
  64. #    
  65. #    
  66. #    plt.figure()
  67. #    plt.subplot(2,2,1)
  68. #    plt.imshow(image, cmap='gray')
  69. #    plt.axis('off')
  70. #    plt.subplot(2,2,2)
  71. #    plt.imshow(filtered_image, cmap='gray')
  72. #    plt.axis('off')    
  73. #    plt.subplot(2,2,3)
  74. #    plt.imshow(gauss_image, cmap='gray')
  75. #    plt.axis('off')    
  76. #    plt.subplot(2,2,4)
  77. #    plt.imshow(median_image, cmap='gray')
  78. #    plt.axis('off')  
  79. #    plt.show()
  80.  
  81. #    laplasjan = np.array([
  82. #            [0, 1, 0],
  83. #            [1, -4, 1],
  84. #            [0, 1, 0]
  85. #            ])
  86. #    
  87. #    laplasjan_image = signal.convolve2d(image, laplasjan)
  88. #    
  89. #    plt.figure()
  90. #    plt.subplot(1, 2, 1)
  91. #    plt.imshow(image, cmap='gray')
  92. #    plt.axis('off')
  93. #    plt.subplot(1, 2, 2)
  94. #    plt.imshow(laplasjan_image, cmap='gray')
  95. #    plt.axis('off')
  96. #    plt.show()
  97.  
  98. #    Zad.
  99. #    Zdefiniuj obie maski Sobela. Dokonaj obliczenia gradientu
  100. #    za pomocą tych masek oraz przefiltruj obraz za pomocą
  101. #    tych masek. Wyniki zaprezentuj.
  102.  
  103. #    grad_x = np.array([
  104. #            [0, 0, 0],
  105. #            [1, 0 ,1],
  106. #            [0, 0, 0]
  107. #            ])
  108. #    
  109.  
  110.     sobel_X = np.array([
  111.             [-1, 0 ,1],
  112.             [-2, 0 ,2],
  113.             [-1, 0 ,1]
  114.             ])  
  115.    
  116.     sobel_Y = sobel_X.T
  117.    
  118.     im_sobel_x_conv = signal.convolve2d(image, sobel_X)
  119.     im_sobel_x_corr = signal.correlate2d(image, sobel_X)
  120.     plt.figure()
  121.     plt.subplot(1,2,1)
  122.     plt.imshow(im_sobel_x_conv, cmap='gray')
  123.     plt.axis('off')
  124.     plt.subplot(1,2,2)
  125.     plt.imshow(im_sobel_x_corr, cmap='gray')
  126.     plt.axis('off')
  127.     plt.show()
  128.    
  129. if __name__ == "__main__":
  130.     run()