Skip to content

Instantly share code, notes, and snippets.

@skt7
Created January 26, 2018 20:31
Show Gist options
  • Save skt7/32da40e9b1e8ad8d7fc49dfcba3667b3 to your computer and use it in GitHub Desktop.
Save skt7/32da40e9b1e8ad8d7fc49dfcba3667b3 to your computer and use it in GitHub Desktop.
3D plot for image pixels using python matplotlib
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import cv2
#read image
img = cv2.imread('colors.jpg')
#convert from BGR to RGB
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#get rgb values from image to 1D array
r, g, b = cv2.split(img)
r = r.flatten()
g = g.flatten()
b = b.flatten()
#plotting
fig = plt.figure()
ax = Axes3D(fig)
ax.scatter(r, g, b)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment