# -*- coding: utf-8 -*- """ Created on Thu Mar 14 10:36:01 2019 @author: Student """ import os import numpy as np from matplotlib import pyplot as plt from skimage import io from skimage import color from scipy import signal from scipy import ndimage as nd def our_gradient(image): gradient_x = np.zeros(image.shape) gradient_y = np.zeros(image.shape) gradient_x[:, 1:-1] = image[:, 2:] - image[:, 0:-2] gradient_y[1:-1, :] = image[2: , :] - image[0:-2 , :] return [gradient_y, gradient_x] def run(): current_path = os.path.abspath(os.path.dirname(__file__)) image_path = os.path.join(current_path, '1.jpg') image = io.imread(image_path) image = color.rgb2gray(image) # #gradients = np.gradient(image) # gradients = our_gradient(image) # gradient_y, gradient_x = gradients[0], gradients[1] # # gradient_mag = np.sqrt(np.square(gradient_x) + np.square(gradient_y)) # gradient_angle = np.arctan2(gradient_y, gradient_x) # # plt.figure() # plt.subplot(2,3,1) # plt.imshow(image, cmap='gray') # plt.axis('off') # plt.subplot(2,3,2) # plt.imshow(gradient_x, cmap='gray') # plt.axis('off') # plt.subplot(2,3,3) # plt.imshow(gradient_y, cmap='gray') # plt.axis('off') # plt.subplot(2,3,4) # plt.imshow(gradient_mag, cmap = 'gray') # plt.axis('off') # plt.subplot(2,3,5) # plt.imshow(gradient_angle, cmap = 'gray') #### przedstawic w odpowiedniej przestrzeni barw, tab okresowa # plt.axis('off') # plt.show() # Zad. # Dokonaj filtracji obrazu filtrem usredniajacym o wielkosci maski (21,21). # Pokaz obraz oryginalny obok obrazu po filtracji. # # mask = np.ones((13,13)) # filtered_image = signal.convolve2d(image, mask) # gauss_image = nd.gaussian_filter(image, sigma=2) #### funkcja, ktora generuje maske gaussa, # #### przyjmie jako wejscie sigme oraz rozmiar maski # median_image = signal.medfilt(image, 13) # # # plt.figure() # plt.subplot(2,2,1) # plt.imshow(image, cmap='gray') # plt.axis('off') # plt.subplot(2,2,2) # plt.imshow(filtered_image, cmap='gray') # plt.axis('off') # plt.subplot(2,2,3) # plt.imshow(gauss_image, cmap='gray') # plt.axis('off') # plt.subplot(2,2,4) # plt.imshow(median_image, cmap='gray') # plt.axis('off') # plt.show() # laplasjan = np.array([ # [0, 1, 0], # [1, -4, 1], # [0, 1, 0] # ]) # # laplasjan_image = signal.convolve2d(image, laplasjan) # # plt.figure() # plt.subplot(1, 2, 1) # plt.imshow(image, cmap='gray') # plt.axis('off') # plt.subplot(1, 2, 2) # plt.imshow(laplasjan_image, cmap='gray') # plt.axis('off') # plt.show() # Zad. # Zdefiniuj obie maski Sobela. Dokonaj obliczenia gradientu # za pomocą tych masek oraz przefiltruj obraz za pomocą # tych masek. Wyniki zaprezentuj. # grad_x = np.array([ # [0, 0, 0], # [1, 0 ,1], # [0, 0, 0] # ]) # sobel_X = np.array([ [-1, 0 ,1], [-2, 0 ,2], [-1, 0 ,1] ]) sobel_Y = sobel_X.T im_sobel_x_conv = signal.convolve2d(image, sobel_X) im_sobel_x_corr = signal.correlate2d(image, sobel_X) plt.figure() plt.subplot(1,2,1) plt.imshow(im_sobel_x_conv, cmap='gray') plt.axis('off') plt.subplot(1,2,2) plt.imshow(im_sobel_x_corr, cmap='gray') plt.axis('off') plt.show() if __name__ == "__main__": run()