Created
June 14, 2017 12:07
-
-
Save hackintoshrao/96f4e2a6133997430497d87079927b12 to your computer and use it in GitHub Desktop.
Histogram of color instensity distribution of image
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import cv2 | |
| import matplotlib.pyplot as plt | |
| import matplotlib.image as mpimg | |
| image = mpimg.imread('cutout1.jpg') | |
| # Define a function to compute color histogram features | |
| def color_hist(img, nbins=32, bins_range=(0, 256)): | |
| # Compute the histogram of the RGB channels separately | |
| rhist = np.histogram(image[:,:,0], bins=32, range=(0, 256)) | |
| ghist = np.histogram(image[:,:,1], bins=32, range=(0, 256)) | |
| bhist = np.histogram(image[:,:,2], bins=32, range=(0, 256)) | |
| bin_edges = rhist[1] | |
| # Generating bin centers | |
| bin_centers = (bin_edges[1] + bin_edges[0: len(bin_edges) - 1]) / 2 | |
| # Concatenate the histograms into a single feature vector | |
| hist_features = np.concatenate((rhist[0], ghist[0], bhist[0])) | |
| # Return the individual histograms, bin_centers and feature vector | |
| return rhist, ghist, bhist, bin_centers, hist_features | |
| rh, gh, bh, bincen, feature_vec = color_hist(image, nbins=32, bins_range=(0, 256)) | |
| # Plot a figure with all three bar charts | |
| if rh is not None: | |
| fig = plt.figure(figsize=(12,3)) | |
| plt.subplot(131) | |
| plt.bar(bincen, rh[0]) | |
| plt.xlim(0, 256) | |
| plt.title('R Histogram') | |
| plt.subplot(132) | |
| plt.bar(bincen, gh[0]) | |
| plt.xlim(0, 256) | |
| plt.title('G Histogram') | |
| plt.subplot(133) | |
| plt.bar(bincen, bh[0]) | |
| plt.xlim(0, 256) | |
| plt.title('B Histogram') | |
| fig.tight_layout() | |
| else: | |
| print('Your function is returning None for at least one variable...') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment