How to plot a 3D histogram of RGB image in python - python

I want to plot a 3D histogram of my RGB image.
Below is my code:
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
from scipy.misc import imread
import pylab
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
img1 = imread('image.jpg')
img_reshaped = img1.reshape(img1.shape[0] * img1.shape[1], img1.shape[2])
hist, edges = np.histogramdd(img_reshaped, bins=(100, 100, 100))
Please tell me how to plot the hist histogram that I have obtained.

Have you taken a look at the 3d histogram example from the matplotlib gallery?
see: http://matplotlib.org/examples/mplot3d/hist3d_demo.html

Related

Convert cmap values to RGB for PIL.Image

I want to use PIL.Image to save a figure and I want to use matplotlib cmaps to map the data to a color. I have tried the following:
import matplotlib
matplotlib.use('TkAgg')
import matplotlib as mpl
import matplotlib.pyplot as plt
from matplotlib import cm
import numpy as np
from PIL import Image
M, N = 255, 255
data = np.arange(M*N).reshape((M, N))
cmap_name = 'autumn_r'
cmap_name = cmap_name
cmap = plt.get_cmap(cmap_name)
norm = mpl.colors.Normalize()
scalarMap = cm.ScalarMappable(norm=norm, cmap=cmap)
plt.imshow(data, cmap=cmap)
plt.show()
colors = scalarMap.to_rgba(data)
image = Image.fromarray((colors[:, :, :3]*256).astype(np.uint8))
image.show()
Which plots this in matplotlib:
However, it plots this in the Image:
How can I get PIL.Image to show the same colors as matplotlib?
If its possible to also add the alpha channel, that will be useful
You need to give PIL the same normalisation and cmap you give matplotlib, so it can do the same mapping from 2D array -> normalised -> mapped to cmap.
I rewrote your sample code to be a bit simpler:
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import cm
from PIL import Image
M, N = 255, 255
data = np.arange(M*N).reshape((M, N))
cmap = cm.autumn_r
plt.imshow(data, cmap=cmap)
norm = mpl.colors.Normalize()
Then your answer is:
Image.fromarray(np.uint8(cmap(norm(data))*255)).show()
(Found the solution here, might be a dupe.)

Align image with Sinusoidal projection correctly using imshow and cartopy

I'm trying to plot an image with a Sinusoidal projection using imshow but if I use the transform=ccrs.Sinusoidal(central_longitude=128) it's not working at all and using the transform=ccrs.PlateCarree() the coastlines and image are not aligned. This is my code:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
plt.figure(figsize=(8, 8))
ax = plt.axes(projection=ccrs.PlateCarree())
img_extent = (122.8109, 133.2922, 32.8286, 43.1708)
img = plt.imread('Korea.A2004004.0445.250m.jpg')
ax.coastlines(resolution='10m')
ax.imshow(img, origin='upper', extent=img_extent, transform=ccrs.PlateCarree())
plt.show()
Image is from: https://eoimages.gsfc.nasa.gov/images/imagerecords/69000/69679/Korea.A2004004.0445.250m.jpg

how to plot a 2d image in 3d plot for calculating depth of object as z axis?

I want to plot a 2D image in 3D map for calculating depth of object and for same I have made a code but that code is giving me an error for calculating distance z as depth.
error is shape mismatch: objects cannot be broadcast to a single shape
import cv2
import numpy as np
import math
import scipy.ndimage as ndimage
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
image2=cv2.imread('i1.jpg')
image2 = image2[:,:,1] # get the first channel
rows, cols = image2.shape
x, y= np.meshgrid(range(cols), range(rows)[::-1])
blurred = ndimage.gaussian_filter(image2,(5, 5))
fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(221)
ax.imshow(image2, cmap='gray')
ax = fig.add_subplot(222, projection='3d')
ax.elev= 5
ax.plot_surface(x,y,z,image2)
ax = fig.add_subplot(223)
ax.imshow(blurred, cmap='gray')
ax = fig.add_subplot(224, projection='3d')
ax.elev= 5
ax.plot_surface(x,y,z,blurred)
plt.show()

Generate 3D Surface Map from Skimage Elevation Map (2D numpy.ndarray)

In the skimage Segmentation tutorial, a 3D surface plot of the elevation map generated from the sobel function was plotted.
>>> from skimage.filters import sobel
>>> elevation_map = sobel(coins)
Question: elevation_map appears to be a 2D numpy.ndarray. How do we generate the 3D map shown using this?
This is likely produced using Paraview/VTK;
Try to play around the following:
from skimage import data
from skimage.filters import sobel
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from matplotlib import cm
from scipy.ndimage import zoom
coins = data.coins()
coins = zoom(coins, 10)
elevation_map = sobel(coins)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
m, n=elevation_map.shape
X, Y = np.meshgrid(np.arange(n), np.arange(m))
ax.plot_surface(X, Y, elevation_map, cmap=cm.viridis, antialiased=False)
ax.axis("off")
ax.set_facecolor('black')
plt.show()

Set width of plot area, matplotlib

The texts on the right on this pyplot graph are clipped, how can I expand the plot area without changing the x-axis?
Minimal example code (similar to but not identical to example image)
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mp
n40=146-1.07*40
n90=146-1.07*90
ageAxis =np.array([10, 40, 90])
Normal=np.array([n40, n40, n90])
plt.plot(ageAxis,Normal)
plt.text(90.2,50,'long text here that will be clipped')
ax = plt.gca()
ax.set_ylim([0,165])
ax.set_xlim([0,90])
fig= plt.gcf()
# set size fig.set_size_inches(20, 10.5)
plt.show()
It seems that it can be done with a combination of set_size_inches and subplots_adjust
Not elegant, I think, but it works:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mp
n40=146-1.07*40
n90=146-1.07*90
ageAxis =np.array([10, 40, 90])
Normal=np.array([n40, n40, n90])
plt.plot(ageAxis,Normal)
plt.text(90.2,50,'long text here that will be clipped')
ax = plt.gca()
ax.set_ylim([0,165])
ax.set_xlim([0,90])
fig= plt.gcf()
fig.set_size_inches(10, 5.5) # set a suitable size
plt.subplots_adjust(right=0.75) # adjust plot area
plt.show()

Categories