Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import numpy as np
- from os.path import getsize
- from IPython.display import Markdown as md
- from matplotlib import pyplot as plt
- from skimage.exposure import rescale_intensity
- name = "stitch.jpg"
- image = cv2.imread(name,cv2.IMREAD_GRAYSCALE)
- height, width = image.shape
- image_sobel=np.zeros(image.shape, dtype="float32")
- sobelx = np.array(
- [[-1,-2,-1],
- [0,0,0],
- [1,2,1]])
- sobely = np.array(
- [[-1,0,1],
- [-2,0,2],
- [-1,0,1]])
- for h in range(height-2):
- for w in range(width-2):
- a = np.float32(image[h:h+3,w:w+3]) # submatriz 3x3 ao redor do pixel image[h+1,w+1]
- dx=np.sum(np.multiply(a,sobelx)) # derivada sobel em relacao a x
- dy=np.sum(np.multiply(a,sobely)) # derivada sobel em relacao a y
- image_sobel[h+1][w+1]=np.sqrt(np.power(dx,2) + np.power(dy,2))
- # normalizando intensidades dos pixels para dentro do range [0,255]
- # https://www.pyimagesearch.com/2016/07/25/convolutions-with-opencv-and-python/
- image_sobel = rescale_intensity(image_sobel, in_range=(0, 255))
- image_sobel = (image_sobel * 255).astype("uint8")
- image_roberts=np.zeros(image.shape, dtype="float32")
- robertsx = np.array(
- [[1,0],
- [0,-1]])
- robertsy = np.array(
- [[-1,0],
- [0,1]])
- for h in range(height-1):
- for w in range(width-1):
- a = np.float32(image[h:h+2,w:w+2]) # submatriz 2x2 ao redor do pixel image[h+1,w+1]
- dx=np.sum(np.multiply(a,robertsx))
- dy=np.sum(np.multiply(a,robertsy))
- image_roberts[h+1][w+1]=np.sqrt(np.power(dx,2) + np.power(dy,2))
- # normalizando intensidades dos pixels para dentro do range [0,255]
- image_roberts = rescale_intensity(image_roberts, in_range=(0, 255))
- image_roberts = (image_roberts * 255).astype("uint8")
- fig=plt.figure(figsize=(25,25))
- fig.add_subplot(1,3,1)
- plt.title("Imagem original cinza")
- plt.imshow(image,cmap="gray")
- plt.xticks([]),plt.yticks([])
- fig.add_subplot(1,3,2)
- plt.title("Resultado do filtro Sobel")
- plt.imshow(image_sobel,cmap="gray")
- plt.xticks([]),plt.yticks([])
- fig.add_subplot(1,3,3)
- plt.title("Resultado do filtro Roberts")
- plt.imshow(image_roberts,cmap="gray")
- plt.xticks([]),plt.yticks([])
- plt.show()
- cv2.imshow("Imagem original cinza",image)
- cv2.imshow("Resultado do filtro Sobel",image_sobel)
- cv2.imshow("Resultado do filtro Roberts",image_roberts)
- cv2.waitKey(0)
- cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement