Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.37 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from os.path import getsize
  4. from IPython.display import Markdown as md
  5. from matplotlib import pyplot as plt
  6. from skimage.exposure import rescale_intensity
  7.  
  8. name = "stitch.jpg"
  9. image = cv2.imread(name,cv2.IMREAD_GRAYSCALE)
  10. height, width = image.shape
  11.  
  12. image_sobel=np.zeros(image.shape, dtype="float32")
  13.  
  14. sobelx = np.array(
  15.     [[-1,-2,-1],
  16.      [0,0,0],
  17.      [1,2,1]])
  18. sobely = np.array(
  19.     [[-1,0,1],
  20.      [-2,0,2],
  21.      [-1,0,1]])
  22.  
  23. for h in range(height-2):
  24.     for w in range(width-2):
  25.         a = np.float32(image[h:h+3,w:w+3]) # submatriz 3x3 ao redor do pixel image[h+1,w+1]
  26.         dx=np.sum(np.multiply(a,sobelx)) # derivada sobel em relacao a x
  27.         dy=np.sum(np.multiply(a,sobely)) # derivada sobel em relacao a y
  28.         image_sobel[h+1][w+1]=np.sqrt(np.power(dx,2) + np.power(dy,2))
  29.  
  30. # normalizando intensidades dos pixels para dentro do range [0,255]
  31. # https://www.pyimagesearch.com/2016/07/25/convolutions-with-opencv-and-python/
  32. image_sobel = rescale_intensity(image_sobel, in_range=(0, 255))
  33. image_sobel = (image_sobel * 255).astype("uint8")
  34.  
  35. image_roberts=np.zeros(image.shape, dtype="float32")
  36.  
  37. robertsx = np.array(
  38.     [[1,0],
  39.      [0,-1]])
  40. robertsy = np.array(
  41.     [[-1,0],
  42.      [0,1]])
  43.  
  44. for h in range(height-1):
  45.     for w in range(width-1):
  46.         a = np.float32(image[h:h+2,w:w+2]) # submatriz 2x2 ao redor do pixel image[h+1,w+1]
  47.         dx=np.sum(np.multiply(a,robertsx))
  48.         dy=np.sum(np.multiply(a,robertsy))
  49.         image_roberts[h+1][w+1]=np.sqrt(np.power(dx,2) + np.power(dy,2))
  50.  
  51. # normalizando intensidades dos pixels para dentro do range [0,255]
  52. image_roberts = rescale_intensity(image_roberts, in_range=(0, 255))
  53. image_roberts = (image_roberts * 255).astype("uint8")
  54.  
  55. fig=plt.figure(figsize=(25,25))
  56. fig.add_subplot(1,3,1)
  57. plt.title("Imagem original cinza")
  58. plt.imshow(image,cmap="gray")
  59. plt.xticks([]),plt.yticks([])
  60. fig.add_subplot(1,3,2)
  61. plt.title("Resultado do filtro Sobel")
  62. plt.imshow(image_sobel,cmap="gray")
  63. plt.xticks([]),plt.yticks([])
  64. fig.add_subplot(1,3,3)
  65. plt.title("Resultado do filtro Roberts")
  66. plt.imshow(image_roberts,cmap="gray")
  67. plt.xticks([]),plt.yticks([])
  68. plt.show()
  69.  
  70. cv2.imshow("Imagem original cinza",image)
  71. cv2.imshow("Resultado do filtro Sobel",image_sobel)
  72. cv2.imshow("Resultado do filtro Roberts",image_roberts)
  73. cv2.waitKey(0)
  74. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement