i am trying to find the histogram of an input image. But instead of seeing a histogram, the code runs then stops without showing anything. Can someone point out to me why this is happening?
import pylab as plt
import matplotlib.image as mpimg
import numpy as np
img = np.uint8(mpimg.imread('jack.jpg'))
# convert to grayscale
# do for individual channels R, G, B, A for nongrayscale images
img = np.uint8((0.2126* img[:,:,0]) + \
np.uint8(0.7152 * img[:,:,1]) +\
np.uint8(0.0722 * img[:,:,2]))
plt.histogram(img,10)
plt.show()
You are confusing histogram with hist. And yes, plt.histogram is a call to numpy.histogram.
Try this:
import pylab as plt
import matplotlib.image as mpimg
import numpy as np
img = np.uint8(mpimg.imread('jack.jpg'))
# convert to grayscale
# do for individual channels R, G, B, A for nongrayscale images
img = np.uint8(0.2126 * img[:,:,0]) +\
np.uint8(0.7152 * img[:,:,1]) +\
np.uint8(0.0722 * img[:,:,2])
plt.hist(img,10)
plt.show()
[edit to answer comment]
According to the documentation (links above on the functions names), np.histogram will Compute the histogram of a set of data, returning:
hist : array The values of the histogram. [...]
bin_edges : array of dtype
float Return the bin edges (length(hist)+1).
And plt.hist will Compute and draw the histogram of x, returning a tuple (n, bins, patches).
Related
I would like to save just the wavelet image (no ticks nor labels) shown here to a png file.
I tried to follow the solution posted here for saving a spectrogram plot, but this approach is not working for me.
This is what I get:
This is the code that I have used:
import librosa
import librosa.display
import os
import pywt
import matplotlib.pyplot as plt
import soundfile as sf
import skimage.io
from tftb.generators import anasing
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np
from ssqueezepy import cwt
from ssqueezepy.visuals import plot, imshow
# Set the path to the Binary Class dataset
fulldatasetpath = 'G:/AudioFile'
input_file = (r'G:/Audiofile.wav')
[data1, sample_rate1] = sf.read(input_file)
#[sample_rate1, data1] = wav.read(input_file);
duration = len(data1)/sample_rate1
time = np.arange(0, duration, 1/sample_rate1) #time vector
#%%############## Take CWT & plot ##################################################
Wx, scales = cwt(data1, 'morlet')
imshow(Wx, abs=1)
plt.show()
Wx = abs(Wx)
#%%############## SAVE TO IMAGE ###########################################
def scale_minmax(X, min=0.0, max=1.0):
X_std = (X - X.min()) / (X.max() - X.min())
X_scaled = X_std * (max - min) + min
return X_scaled
wave1 = np.log(Wx + 1e-9) # add small number to avoid log(0)
# min-max scale to fit inside 8-bit range
img = scale_minmax(Wx, 0, 255).astype(np.uint8)
img = np.flip(img, axis=0) # put low frequencies at the bottom in image
img = 255-img # invert. make black==more energy
out = 'out.png'
# save as PNG
skimage.io.imsave(out, img)
You can set the position of the axis to cover the entire figure, and you can also play with figsize. For example:
import matplotlib.pyplot as plt
import numpy as np
from ssqueezepy import imshow
# test image
img = np.zeros((500, 40000, 3), dtype=int)
for i in range(img.shape[1]):
img[:, i, 0] = int(abs(1 - 2 * i / img.shape[1]) * 255)
# create a figure and set the size
f = plt.figure(figsize=(8, 4))
# add a new axis into which ssqueezepy is going to plot
ax = f.add_subplot()
imshow(img)
# turn off tick labels
ax.axis(False)
# make the axis to cover the entire figure
ax.set_position([0, 0, 1, 1])
f.savefig("result.png")
Image
I want to make a colormap used in the attached image colorbar. So far I tried the code given below but didn't get the result I was looking for.
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
img = plt.imread('Climat.png')
colors_from_img = img[:, 0, :]
my_cmap = LinearSegmentedColormap.from_list('my_cmap', colors_from_img, N=651)
y = random_sample((100, 100))
imshow(y, cmap=my_cmap);plt.colorbar()
Looking for your suggestions. Thank you in advance.
bicarlsen has given you the correct direction. Restrict the points from which you extract the colors to the colored rectangles:
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
import numpy as np
img = plt.imread('Climat.png')
colors_from_img = img[80::88, 30, :]
my_cmap = LinearSegmentedColormap.from_list('my_cmap', colors_from_img[::-1], N=len(colors_from_img))
y = np.random.random_sample((100, 100))
plt.imshow(y, cmap=my_cmap)
plt.colorbar()
plt.show()
Sample output:
P.S.: Initially, I thought a more general approach with
colors_from_img = np.unique(img[:, 30, :], axis=0)
was possible but as the input image is rasterized, all kinds of mixed colors are present where the black lines separate colored rectangles.
I am trying to write a code to generate an RBG image of the Pillars of Creation. For that I am using fits file corresponding to red, blue and green, and trying to use make_lupton_rbg to generate the RBG image. However I am getting full green image. I believe, I have to make adjustments to Q and stretch values, but I can't find anything to give it a good color (as seen in the pictures).
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.visualization import make_lupton_rgb
forc=np.float_()
r=fits.open("./673nmos.fits")[0].data
g=fits.open("./656nmos.fits")[0].data
b=fits.open("./502nmos.fits")[0].data
r = np.array(r,forc)
g = np.array(g,forc)
b = np.array(b,forc)
rgb_default = make_lupton_rgb(r,g,b,Q=1,stretch=0.1,filename="pillar.png")
plt.imshow(rgb_default, origin='lower')
plt.show()
The fits file were download from here
This is the output I am getting
And this is the output I should get (or at least something like it)
Scaling the r, g, and b arrays based on their relative brightnesses and using a high linear stretch gets much closer:
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.visualization import make_lupton_rgb
forc=np.float_()
r=fits.open("/path/to/673nmos/673nmos.fits")[0].data
g=fits.open("/path/to/656nmos/656nmos.fits")[0].data
b=fits.open("/path/to/502nmos/502nmos.fits")[0].data
r = np.array(r,forc)
g = np.array(g,forc)
b = np.array(b,forc)
rgb_default = make_lupton_rgb(r*5,g*0.75,b*8,Q=0.001,stretch=300,filename="pillar.png")
plt.imshow(rgb_default, origin='lower')
plt.show()
But clearly the linear stretching handles the spikes poorly, this can be compensated for by simply threshold filtering before applying make_lupton_rgb:
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
from astropy.visualization import make_lupton_rgb
forc=np.float_()
r=fits.open("/path/to/673nmos/673nmos.fits")[0].data
g=fits.open("/path/to/656nmos/656nmos.fits")[0].data
b=fits.open("/path/to/502nmos/502nmos.fits")[0].data
r = np.array(r,forc)*5
g = np.array(g,forc)*0.75
b = np.array(b,forc)*8
t = 250
r[r > t] = t
g[g > t] = t
b[b > t] = t
rgb_default = make_lupton_rgb(r,g,b,Q=0.001,stretch=300,filename="pillar.png")
plt.figure(figsize=(8,8))
plt.imshow(rgb_default, origin='lower')
plt.show()
I wonder how to use Python to compute the gradients of the image. The gradients include x and y direction. I want to get an x gradient map of the image and a y gradient map of the image. Can anyone tell me how to do this?
Thanks~
I think you mean this:
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
# Create a black image
img=np.zeros((640,480))
# ... and make a white rectangle in it
img[100:-100,80:-80]=1
# See how it looks
plt.imshow(img,cmap=plt.cm.gray)
plt.show()
# Rotate it for extra fun
img=ndimage.rotate(img,25,mode='constant')
# Have another look
plt.imshow(img,cmap=plt.cm.gray)
plt.show()
# Get x-gradient in "sx"
sx = ndimage.sobel(img,axis=0,mode='constant')
# Get y-gradient in "sy"
sy = ndimage.sobel(img,axis=1,mode='constant')
# Get square root of sum of squares
sobel=np.hypot(sx,sy)
# Hopefully see some edges
plt.imshow(sobel,cmap=plt.cm.gray)
plt.show()
Or you can define the x and y gradient convolution kernels yourself and call the convolve() function:
# Create a black image
img=np.zeros((640,480))
# ... and make a white rectangle in it
img[100:-100,80:-80]=1
# Define kernel for x differences
kx = np.array([[1,0,-1],[2,0,-2],[1,0,-1]])
# Define kernel for y differences
ky = np.array([[1,2,1] ,[0,0,0], [-1,-2,-1]])
# Perform x convolution
x=ndimage.convolve(img,kx)
# Perform y convolution
y=ndimage.convolve(img,ky)
sobel=np.hypot(x,y)
plt.imshow(sobel,cmap=plt.cm.gray)
plt.show()
you can use opencv to compute x and y gradients as below:
import numpy as np
import cv2
img = cv2.imread('Desert.jpg')
kernely = np.array([[1,1,1],[0,0,0],[-1,-1,-1]])
kernelx = np.array([[1,0,-1],[1,0,-1],[1,0,-1]])
edges_x = cv2.filter2D(img,cv2.CV_8U,kernelx)
edges_y = cv2.filter2D(img,cv2.CV_8U,kernely)
cv2.imshow('Gradients_X',edges_x)
cv2.imshow('Gradients_Y',edges_y)
cv2.waitKey(0)
We can do it with scikit-image filters module functions too, as shown below:
import matplotlib.pylab as plt
from skimage.io import imread
from skimage.color import rgb2gray
from skimage import filters
im = rgb2gray(imread('../images/cameraman.jpg')) # RGB image to gray scale
plt.gray()
plt.figure(figsize=(20,20))
plt.subplot(221)
plt.imshow(im)
plt.title('original', size=20)
plt.subplot(222)
edges_y = filters.sobel_h(im)
plt.imshow(edges_y)
plt.title('sobel_x', size=20)
plt.subplot(223)
edges_x = filters.sobel_v(im)
plt.imshow(edges_x)
plt.title('sobel_y', size=20)
plt.subplot(224)
edges = filters.sobel(im)
plt.imshow(edges)
plt.title('sobel', size=20)
plt.show()
Consider the following code:
import numpy as np
rand_matrix = np.random.rand(10,10)
which generates a 10x10 random matrix.
Following code to display as colour map:
import matplotlib.pyplot as plt
plt.imshow(rand_matrix)
plt.show()
I would like to get the RGB numpy array (no axis) from the object obtained from plt.imshow
In other words, if I save the image generated from plt.show, I would like to get the 3D RGB numpy array obtained from:
import matplotlib.image as mpimg
img=mpimg.imread('rand_matrix.png')
But without the need to save and load the image, which is computationally very expensive.
Thank you.
You can save time by saving to a io.BytesIO instead of to a file:
import io
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
def ax_to_array(ax, **kwargs):
fig = ax.figure
frameon = ax.get_frame_on()
ax.set_frame_on(False)
with io.BytesIO() as memf:
extent = ax.get_window_extent()
extent = extent.transformed(fig.dpi_scale_trans.inverted())
plt.axis('off')
fig.savefig(memf, format='PNG', bbox_inches=extent, **kwargs)
memf.seek(0)
arr = mpimg.imread(memf)[::-1,...]
ax.set_frame_on(frameon)
return arr.copy()
rand_matrix = np.random.rand(10,10)
fig, ax = plt.subplots()
ax.imshow(rand_matrix)
result = ax_to_array(ax)
# view using matplotlib
plt.show()
# view using PIL
result = (result * 255).astype('uint8')
img = Image.fromarray(result)
img.show()