Advertisement
akariya

ECE558 Project1 Problem 1

Sep 22nd, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. import cv2
  2. import argparse
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5.  
  6. # function to find neighbourDirections
  7. def getNeighbourDirections(type_of_neighbours):
  8.     if type_of_neighbours == 'left':
  9.         return [0,-1]
  10.     if type_of_neighbours == 'right':
  11.         return [0,1]
  12.     if type_of_neighbours == 'top':
  13.         return [-1,0]
  14.     if type_of_neighbours == 'bottom':
  15.         return [1,0]
  16.     if type_of_neighbours == 'top-left':
  17.         return [-1,-1]
  18.     if type_of_neighbours == 'top-right':
  19.         return [-1,1]
  20.     if type_of_neighbours == 'bottom-left':
  21.         return [1,-1]
  22.     if type_of_neighbours == 'bottom-right':
  23.         return [1,1]
  24.  
  25.  
  26. # function to plot histogram for grayscale image
  27. def plotHist(img):
  28.    
  29.     y, x = img.shape
  30.     compute = np.zeros([y,x])
  31.     img = img.astype(np.int16)
  32.     i = 0
  33.     while(i < y):
  34.         j = 0
  35.         while(j < x):
  36.             if(i+neighbourDirections[0]>=0 and j+neighbourDirections[1]>=0 and i+neighbourDirections[0]<y and j+neighbourDirections[1]<x):
  37.                 compute[i][j] = (img[i][j]-img[i+neighbourDirections[0]][j+neighbourDirections[1]]) ^ 2
  38.             j += 1
  39.         i += 1
  40.     plt.hist(compute.flatten(), bins=np.arange(255))
  41.     plt.show()
  42.  
  43. # function to plot histogram for image with 3 channels
  44. def plotHist3d(img):
  45.    
  46.     y, x, c= img.shape
  47.     compute = np.zeros([y,x])
  48.     img = img.astype(np.int16)
  49.     i = 0
  50.     while(i < y):
  51.         j = 0
  52.         while(j < x):
  53.             if(i+neighbourDirections[0]>=0 and j+neighbourDirections[1]>=0 and i+neighbourDirections[0]<y and j+neighbourDirections[1]<x):
  54.                 compute[i][j] += (img[i][j][0]-img[i+neighbourDirections[0]][j+neighbourDirections[1]][0]) ^ 2
  55.                 compute[i][j] += (img[i][j][1]-img[i+neighbourDirections[0]][j+neighbourDirections[1]][1]) ^ 2
  56.                 compute[i][j] += (img[i][j][2]-img[i+neighbourDirections[0]][j+neighbourDirections[1]][2]) ^ 2
  57.             j += 1
  58.         i += 1
  59.  
  60.     plt.hist(compute.flatten(), bins=np.arange(256))
  61.     plt.show()
  62.  
  63. # driver code
  64. parser = argparse.ArgumentParser(description='Visualizing and checking the smoothness prior')
  65. parser.add_argument('-i', action='store', dest='img_path',help='Enter image path (relative path)')
  66. parser.add_argument('-n', action='store', dest='type_of_neighbours',help='Enter type of neighbours to process')
  67. args = parser.parse_args()
  68. img_path = args.img_path
  69. type_of_neighbours = args.type_of_neighbours
  70. neighbourDirections = getNeighbourDirections(type_of_neighbours)
  71.  
  72. imgColor = cv2.imread(img_path)
  73. imgGray = cv2.cvtColor(imgColor, cv2.COLOR_BGR2GRAY)
  74. hsv = cv2.cvtColor(imgColor, cv2.COLOR_BGR2HSV)
  75. lab = cv2.cvtColor(imgColor, cv2.COLOR_BGR2LAB)
  76. plotHist(imgGray)
  77. plotHist3d(imgColor)
  78. plotHist3d(hsv)
  79. plotHist3d(lab)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement