Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import cv2
- import argparse
- import numpy as np
- from matplotlib import pyplot as plt
- # function to find neighbourDirections
- def getNeighbourDirections(type_of_neighbours):
- if type_of_neighbours == 'left':
- return [0,-1]
- if type_of_neighbours == 'right':
- return [0,1]
- if type_of_neighbours == 'top':
- return [-1,0]
- if type_of_neighbours == 'bottom':
- return [1,0]
- if type_of_neighbours == 'top-left':
- return [-1,-1]
- if type_of_neighbours == 'top-right':
- return [-1,1]
- if type_of_neighbours == 'bottom-left':
- return [1,-1]
- if type_of_neighbours == 'bottom-right':
- return [1,1]
- # function to plot histogram for grayscale image
- def plotHist(img):
- y, x = img.shape
- compute = np.zeros([y,x])
- img = img.astype(np.int16)
- i = 0
- while(i < y):
- j = 0
- while(j < x):
- if(i+neighbourDirections[0]>=0 and j+neighbourDirections[1]>=0 and i+neighbourDirections[0]<y and j+neighbourDirections[1]<x):
- compute[i][j] = (img[i][j]-img[i+neighbourDirections[0]][j+neighbourDirections[1]]) ^ 2
- j += 1
- i += 1
- plt.hist(compute.flatten(), bins=np.arange(255))
- plt.show()
- # function to plot histogram for image with 3 channels
- def plotHist3d(img):
- y, x, c= img.shape
- compute = np.zeros([y,x])
- img = img.astype(np.int16)
- i = 0
- while(i < y):
- j = 0
- while(j < x):
- if(i+neighbourDirections[0]>=0 and j+neighbourDirections[1]>=0 and i+neighbourDirections[0]<y and j+neighbourDirections[1]<x):
- compute[i][j] += (img[i][j][0]-img[i+neighbourDirections[0]][j+neighbourDirections[1]][0]) ^ 2
- compute[i][j] += (img[i][j][1]-img[i+neighbourDirections[0]][j+neighbourDirections[1]][1]) ^ 2
- compute[i][j] += (img[i][j][2]-img[i+neighbourDirections[0]][j+neighbourDirections[1]][2]) ^ 2
- j += 1
- i += 1
- plt.hist(compute.flatten(), bins=np.arange(256))
- plt.show()
- # driver code
- parser = argparse.ArgumentParser(description='Visualizing and checking the smoothness prior')
- parser.add_argument('-i', action='store', dest='img_path',help='Enter image path (relative path)')
- parser.add_argument('-n', action='store', dest='type_of_neighbours',help='Enter type of neighbours to process')
- args = parser.parse_args()
- img_path = args.img_path
- type_of_neighbours = args.type_of_neighbours
- neighbourDirections = getNeighbourDirections(type_of_neighbours)
- imgColor = cv2.imread(img_path)
- imgGray = cv2.cvtColor(imgColor, cv2.COLOR_BGR2GRAY)
- hsv = cv2.cvtColor(imgColor, cv2.COLOR_BGR2HSV)
- lab = cv2.cvtColor(imgColor, cv2.COLOR_BGR2LAB)
- plotHist(imgGray)
- plotHist3d(imgColor)
- plotHist3d(hsv)
- plotHist3d(lab)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement