getting a dark image using make_lupton_rgb - python

I am using astropy visualization to make a colored image of M66 in this case.
Before doing anything I learnt that I have to cast my RGB .fts array with numpy.float_()
forCasting = np.float_()
### READING
b = fits.open("data/"+"M66-Blue.fts")[0].data
r = fits.open("data/"+"M66-Red.fts")[0].data
g = fits.open("data/"+"M66-Green.fts")[0].data
### CASTING
r = np.array(r,forCasting)
g = np.array(g,forCasting)
b = np.array(b,forCasting)
so that I could proceed with my stretch like :
stretch = SqrtStretch() + ZScaleInterval()
r = stretch(b)
g = stretch(r)
b = stretch(g)
plt.imshow(r, origin='lower')
plt.show()
plt.imshow(g, origin='lower')
plt.show()
plt.imshow(b, origin='lower')
plt.show()
Then I just use the method make_lupton_rgb from astropy.visualizaion as follow, but I have a super dark image that I cannot distinguish anything. Does anybody know why I have a dark final image here? Do you have any suggestions?
### SAVING
# rgb_default = make_lupton_rgb(r, g, b, minimum=1000, stretch=900, Q=100, filename="provafinale.png")
rgb_default = make_lupton_rgb(r, g, b, filename="provafinale.png")
plt.imshow(rgb_default, origin='lower')
plt.show()
Thanks!

It looks like you have to set stretch and Q arguments of make_lupton_rgb.
The default values are stretch=5 and Q=8, that gives dark result.
I have no experience with astropy or with Astronomy.
I just played with the arguments, and got bright image using stretch=1 and Q=0.
rgb_default = make_lupton_rgb(r, g, b, minimum=0, stretch=1, Q=0, filename="provafinale.png")
I tried computing minimum and stretch using np.percentile, for linear stretching the output.
I tested the code using an m8_050507_9i9m image from index_fits.
Here is the code I used for testing:
import numpy as np
from astropy.io import fits
from astropy.visualization import SqrtStretch
from astropy.visualization import ZScaleInterval
from astropy.visualization import make_lupton_rgb
from matplotlib import pyplot as plt
forCasting = np.float_()
### READING
# http://www.mistisoftware.com/astronomy/index_fits.htm
r = fits.open("m8_050507_9i9m_R.FIT")[0].data
g = fits.open("m8_050507_9i9m_G.FIT")[0].data
b = fits.open("m8_050507_9i9m_B.FIT")[0].data
# Crop the top and the right margin (contains black pixels)
r = r[:, :-200]
g = g[:, :-200]
b = b[:, :-200]
### CASTING
r = np.array(r,forCasting)
g = np.array(g,forCasting)
b = np.array(b,forCasting)
stretch = SqrtStretch() + ZScaleInterval()
r = stretch(b)
g = stretch(r)
b = stretch(g)
plt.imshow(r, origin='lower')
plt.imshow(g, origin='lower')
plt.imshow(b, origin='lower')
### SAVING
# https://docs.astropy.org/en/stable/api/astropy.visualization.make_lupton_rgb.html
# astropy.visualization.make_lupton_rgb(image_r, image_g, image_b, minimum=0, stretch=5, Q=8, fil/ename=None)[source]
# Return a Red/Green/Blue color image from up to 3 images using an asinh stretch.
# The input images can be int or float, and in any range or bit-depth.
lo_val, up_val = np.percentile(np.hstack((r.flatten(), g.flatten(), b.flatten())), (0.5, 99.5)) # Get the value of lower and upper 0.5% of all pixels
stretch_val = up_val - lo_val
rgb_default = make_lupton_rgb(r, g, b, minimum=lo_val, stretch=stretch_val, Q=0, filename="provafinale.png")
# Cut the top rows - contains black pixels
rgb_default = rgb_default[100:, :, :]
plt.imshow(rgb_default, origin='lower')
plt.show()
Result:

Related

Why are these codes not visually showing the right colors extracted from the image?

so I am working on a program to extract up to 4 of the most common colors, from a picture. Right now, I'm working on it visually showing the most common colors, however, after reading the image, I am:
unable to get an output of the correct rgb codes (it's not outputting it for me)
and
the chart that pops up either shows all black, or shows 3 random colors that are not in the picture.
Any tips or help? I've tried anything that I can, I am not sure why it cannot read the colors well. Thank you.
The code:
import matplotlib.image as img
import matplotlib.pyplot as plt
from scipy.cluster.vq import whiten
from scipy.cluster.vq import kmeans
import pandas as pd
import numpy as np
bimage = img.imread('Images/build2.jpg') #read image (this part works)
print(bimage.shape)
r = []
g = []
b = []
for row in bimage:
for temp_r, temp_g, temp_b in row:
r.append(temp_r)
g.append(temp_g)
b.append(temp_b)
bimage_df = pd.DataFrame({'red': r,
'green': g,
'blue': b})
bimage_df['scaled_color_red'] = whiten(bimage_df['red']) #supposed to give color codes
bimage_df['scaled_color_blue'] = whiten(bimage_df['blue'])
bimage_df['scaled_color_green'] = whiten(bimage_df['green'])
cluster_centers, _ = kmeans(bimage_df[['scaled_color_red', #to find most common colors
'scaled_color_blue',
'scaled_color_green']], 3)
dominant_colors = []
red_std, green_std, blue_std = bimage_df[['red',
'green',
'blue']].std()
for cluster_center in cluster_centers:
red_scaled, green_scaled, blue_scaled = cluster_center
dominant_colors.append((
red_scaled * red_std / 255,
green_scaled * green_std / 255,
blue_scaled * blue_std / 255
))
plt.imshow([dominant_colors])
plt.show()
The image I used:
I have tried using this method for an output and another type of chart too, but that gave me all black or purple, unrelated colors. I had referred to geeks4geeks for this, could not troubleshoot either. Any help would be greatly appreciated.
The major issue is the usage of whiten method that is not adequate for the sample image:
whiten documentation:
Before running k-means, it is beneficial to rescale each feature dimension of the observation set by its standard deviation (i.e. “whiten” it - as in “white noise” where each frequency has equal power). Each feature is divided by its standard deviation across all observations to give it unit variance.
The normalization method assumes normal distribution of the noise.
The sample image is not a natural image (has no noise), and the normalization procedure does not feat the given image.
Instead of normalization, it is recommended to convert the image to LAB color space, where color distances better match the perceptual distances.
Keeping the colors in RGB format may work good enough...
Swapping the green and the blue channels is another issue.
Instead of using a for loop, we may use NumPy array operations (it's not a bug, just faster):
fimage = bimage.astype(float) # Convert image from uint8 to float (kmeans requires floats).
r = fimage[:, :, 0].flatten().tolist() # Convert red elements to list
g = fimage[:, :, 1].flatten().tolist() # Convert grenn elements to list
b = fimage[:, :, 2].flatten().tolist() # Convert blue elements to list
bimage_df = pd.DataFrame({'red': r,
'green': g,
'blue': b})
Apply kmeans with 100 iterations (the default is 20, and may not be enough):
cluster_centers, _ = kmeans(bimage_df[['red', #Find rhe 4 most common colors
'green',
'blue']], 4, iter=100) # The default is 20 iterations, use 100 iterations for better convergence
Before using plt.imshow we have to convert the colors to uint8 type (we may also convert to range [0, 1]), otherwize the displayed colors are going to be white (saturated).
dominant_colors = np.round(cluster_centers).astype(np.uint8) # Round and convert to uint8
plt.imshow([dominant_colors])
plt.show()
Code sample:
import matplotlib.image as img
import matplotlib.pyplot as plt
#from scipy.cluster.vq import whiten
from scipy.cluster.vq import kmeans
import pandas as pd
import numpy as np
bimage = img.imread('Images/build2.jpg') #read image (this part works)
print(bimage.shape)
#r = []
#g = []
#b = []
#for row in bimage:
# for temp_r, temp_g, temp_b in row:
# r.append(temp_r)
# g.append(temp_g)
# b.append(temp_b)
# Use NumPy array operations, instead of using a for loop.
fimage = bimage.astype(float) # Convert image from uint8 to float (kmeans requires floats).
r = fimage[:, :, 0].flatten().tolist() # Convert red elements to list
g = fimage[:, :, 1].flatten().tolist() # Convert grenn elements to list
b = fimage[:, :, 2].flatten().tolist() # Convert blue elements to list
bimage_df = pd.DataFrame({'red': r,
'green': g,
'blue': b})
# Don't use whiten
#bimage_df['scaled_color_red'] = whiten(bimage_df['red']) #supposed to give color codes
#bimage_df['scaled_color_blue'] = whiten(bimage_df['blue'])
#bimage_df['scaled_color_green'] = whiten(bimage_df['green'])
#cluster_centers, _ = kmeans(bimage_df[['scaled_color_red', #to find most common colors
# 'scaled_color_blue',
# 'scaled_color_green']], 3)
cluster_centers, _ = kmeans(bimage_df[['red', #Find the 4 most common colors
'green',
'blue']], 4, iter=100) # The default is 20 iterations, use 100 iterations for better convergence
dominant_colors = np.round(cluster_centers).astype(np.uint8) # Round and convert to uint8
print(dominant_colors)
# Since whiten is not used, we don't need the STD
#red_std, green_std, blue_std = bimage_df[['red',
# 'green',
# 'blue']].std()
#for cluster_center in cluster_centers:
# red_scaled, green_scaled, blue_scaled = cluster_center
# dominant_colors.append((
# red_scaled * red_std / 255,
# green_scaled * green_std / 255,
# blue_scaled * blue_std / 255
# ))
plt.imshow([dominant_colors])
plt.show()
Result:

position overlay precisely in matplotlib

I am trying to overlay precisely one image patch to a larger patch of the image
I have the coordinates where I'd like to put the patch and overlay the image but I don't know how to do with matplotlib.
I know it's possible with PILLOW (as explained here)but since I am using matplotlib for everything I'd be happy to stick to it.
For this example it would be moving the 'red patch' into the rectangle where 'it's supposed' to be.
Here is the code that I used for that:
temp_k = img_arr[0][
np.min(kernel[:, 1]) : np.max(kernel[:, 1]),
np.min(kernel[:, 0]) : np.max(kernel[:, 0]),
]
temp_w = img_arr[1][
np.min(window[:, 1]) : np.max(window[:, 1]),
np.min(window[:, 0]) : np.max(window[:, 0]),
]
w, h = temp_k.shape[::-1]
res = cv2.matchTemplate(temp_w, temp_k, cv2.TM_CCORR_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
cv2.rectangle(temp_w, top_left, bottom_right, 255, 1)
plt.imshow(temp_w, cmap="bone")
plt.imshow(temp_k, cmap="magma", alpha=0.6)
plt.plot(max_loc[0], max_loc[1], "yo")
plt.savefig("../images/test.png")
plt.tight_layout()
Does anyone has an idea how to do that ?
Thanks in advance.
Just as with Pillow, you need to tell Matplotlib where to place data. If you omit that, it will assume a default extent of [0,xs,ys,0], basically plotting it in the top-left corner as shown on your image.
Generating some example data:
import matplotlib.pyplot as plt
import numpy as np
n = 32
m = n // 2
o = n // 4
a = np.random.randn(n,n)
b = np.random.randn(m,m)
a_extent = [0,n,n,0]
b_extent = [o, o+m, o+m, o]
# a_extent = [0, 32, 32, 0]
# b_extent = [8, 24, 24, 8]
Plotting with:
fig, ax = plt.subplots(figsize=(5,5), constrained_layout=False, dpi=86, facecolor="w")
ax.imshow(a, cmap="bone", extent=a_extent)
ax.autoscale(False)
ax.imshow(b, cmap="magma", extent=b_extent)
Will result in:

Point in Polygon in a numpy array

I am using skimage. I need to create a mask equal in area to an image. The mask will have a region which will hide part of the image. I am building it as in the sample below but this is very slow and am sure there is a pythonic way of doing it. Could anyone highlight this please?
Code am using presently:
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import skimage as sk
sourceimage = './sample.jpg'
img = np.copy(io.imread(sourceimage, as_gray=True))
mask = np.full(img.shape, 1)
maskpolygon = [(1,200),(300,644),(625,490),(625,1)]
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
pgon = Polygon(maskpolygon)
for r in range(mask.shape[0]):
for c in range(mask.shape[1]):
p = Point(r,c)
if pgon.contains(p):
mask[r,c] = 0
Expected result is like (for a 9x9 image - but I am working on 700x700)
[1,1,1,1,1,1,1,1,1]
[1,1,1,1,1,1,1,1,1]
[1,1,0,0,1,1,1,1,1]
[1,1,0,0,1,1,1,1,1]
[1,1,0,0,0,0,1,1,1]
[1,1,0,0,0,0,0,1,1]
[1,1,1,0,0,0,0,1,1]
[1,1,1,1,0,0,1,1,1]
[1,1,1,1,1,1,1,1,1]
I can invert 1's and 0's to show/hide region.
Thank you.
I have been able to resolve this thanks to #HansHirse.
Below is how I worked it out
sourceimage = './sample.jpg'
figuresize = (100, 100)
from skimage.draw import polygon
#open source and create a copy
img = np.copy(io.imread(sourceimage, as_gray=True))
mask = np.full(img.shape, 0)
maskpolygon = [(1,1), (280,1),(625, 280),(460, 621),(15, 625)]
maskpolygonr = [x[0] for x in maskpolygon]
maskpolygonc = [x[1] for x in maskpolygon]
rr, cc = polygon(maskpolygonr, maskpolygonc)
mask[rr ,cc] = 1
masked_image = img * mask
# show step by step what is happening
fig, axs = plt.subplots(nrows = 3, ncols = 1, sharex=True, sharey = True, figsize=figuresize )
ax = axs.ravel()
ax[0].imshow(img)#, cmap=plt.cm.gray)
ax[1].imshow(mask)#, cmap=plt.cm.gray)
ax[2].imshow(masked_image)#, cmap=plt.cm.gray)

FFT low-pass filter

Be warned, this is a newbie question.
I acquired some noisy data (a 1x200 pixel sclice from a grayscale image), for which I am trying to build a simple FFT low-pass filter. I do understand the general principle of the Fourier Transform, but I ran into trouble trying to implement it.
My script works well on example data, but behaves in a strange manner on my data.
I think I must be mixing dimensions at some point, but it's been quite a few long hours and I cannot find where! I suspect that, because the output (please see below) of print(signal.shape) is different between the example and real data. Furthermore, scipy.fftpack.rfft(signal) seems to do nothing to my signal instead of computing the function in the frequency domain, as it should.
My script:
(will run out-of-the-box using example data, just by copy-pasting everything below #example data)
import cv2
import numpy as np
from scipy.fftpack import rfft, irfft, fftfreq, fft, ifft
import matplotlib as mpl
import matplotlib.pyplot as plt
#===========================================
#GETTING DATA AND SETTING CONSTANTS
#===========================================
REACH = 100
COURSE = 180
CENTER = (cx, cy)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
gray2 = gray.copy()
#drawing initial vector
cv2.line(gray, (cx, cy + REACH), (cx, cy - REACH), 0, 5)
cv2.circle(gray, (cx, cy + REACH), 10, 0, -1)
cv2.circle(gray, (cx, cy), REACH, 0, 5)
#flooding contour with white
cv2.drawContours(gray2, contours, index, 255, -1)
#real data
signal = gray2[(cy - REACH):(cy + REACH), (cx-0.5):(cx+0.5)]
time = np.linspace(0, 2*REACH, num=200)
#example data
time = np.linspace(0,10,2000)
signal = np.cos(5*np.pi*time) + np.cos(7*np.pi*time)
#=============================================
#THE FFT TRANSFORM & FILTERING
#=============================================
#signal filtering
f_signal = rfft(signal)
W = fftfreq(signal.size, d=time[1]-time[0])
cut_f_signal = f_signal.copy()
cut_f_signal[(W>5)] = 0
cut_signal = irfft(cut_f_signal)
#==================================
#FROM HERE ITS ONLY PLOTTING
#==================================
print(signal.shape)
plt.figure(figsize=(8,8))
ax1 = plt.subplot(321)
ax1.plot(signal)
ax1.set_title("Original Signal", color='green', fontsize=16)
ax2 = plt.subplot(322)
ax2.plot(np.abs(f_signal))
plt.xlim(0,100)
ax2.set_title("FFT Signal", color='green', fontsize=16)
ax3 = plt.subplot(323)
ax3.plot(cut_f_signal)
plt.xlim(0,100)
ax3.set_title("Filtered FFT Signal", color='green', fontsize=16)
ax4 = plt.subplot(324)
ax4.plot(cut_signal)
ax4.set_title("Filtered Signal", color='green', fontsize=16)
for i in [ax1,ax2,ax3,ax4]:
i.tick_params(labelsize=16, labelcolor='green')
plt.tight_layout()
plt.show()
The result on real data:
parameters:
signal = gray2[(cy - REACH):(cy + REACH), (cx-0.5):(cx+0.5)]
time = np.linspace(0, 2*REACH, num=200)
filtering parameter:
cut_f_signal[(W<0.05)] = 0
Output:
output of signal.shape is (200L, 1L)
The result on example data:
parameters:
signal = np.cos(5*np.pi*time) + np.cos(7*np.pi*time)
time = np.linspace(0,10,2000)
filtering parameter:
cut_f_signal[(W>5)] = 0
Output:
output of signal.shape is (2000L,)
So I began to think about that, and after a time I realized the stupidity in my ways. My base data is an image, and I take a slice of it to generate the above signal.
So instead of trying to implement a less-than-satisfying home-brewed FFT filter to smoooth the signal, it is in fact much better and easier to smooth the image with one of the numerous battle-tested filters (gaussian, bilateral, etc.) available in equally numerous image libs (in my case, OpenCV)...
Thanks to the people that took the time to try and help!

Shape recognition with numpy/scipy (perhaps watershed)

My goal is to trace drawings that have a lot of separate shapes in them and to split these shapes into individual images. It is black on white. I'm quite new to numpy,opencv&co - but here is my current thought:
scan for black pixels
black pixel found -> watershed
find watershed boundary (as polygon path)
continue searching, but ignore points within the already found boundaries
I'm not very good at these kind of things, is there a better way?
First I tried to find the rectangular bounding box of the watershed results (this is more or less a collage of examples):
from numpy import *
import numpy as np
from scipy import ndimage
np.set_printoptions(threshold=np.nan)
a = np.zeros((512, 512)).astype(np.uint8) #unsigned integer type needed by watershed
y, x = np.ogrid[0:512, 0:512]
m1 = ((y-200)**2 + (x-100)**2 < 30**2)
m2 = ((y-350)**2 + (x-400)**2 < 20**2)
m3 = ((y-260)**2 + (x-200)**2 < 20**2)
a[m1+m2+m3]=1
markers = np.zeros_like(a).astype(int16)
markers[0, 0] = 1
markers[200, 100] = 2
markers[350, 400] = 3
markers[260, 200] = 4
res = ndimage.watershed_ift(a.astype(uint8), markers)
unique(res)
B = argwhere(res.astype(uint8))
(ystart, xstart), (ystop, xstop) = B.min(0), B.max(0) + 1
tr = a[ystart:ystop, xstart:xstop]
print tr
Somehow, when I use the original array (a) then argwhere seems to work, but after the watershed (res) it just outputs the complete array again.
The next step could be to find the polygon path around the shape, but the bounding box would be great for now!
Please help!
#Hooked has already answered most of your question, but I was in the middle of writing this up when he answered, so I'll post it in the hopes that it's still useful...
You're trying to jump through a few too many hoops. You don't need watershed_ift.
You use scipy.ndimage.label to differentiate separate objects in a boolean array and scipy.ndimage.find_objects to find the bounding box of each object.
Let's break things down a bit.
import numpy as np
from scipy import ndimage
import matplotlib.pyplot as plt
def draw_circle(grid, x0, y0, radius):
ny, nx = grid.shape
y, x = np.ogrid[:ny, :nx]
dist = np.hypot(x - x0, y - y0)
grid[dist < radius] = True
return grid
# Generate 3 circles...
a = np.zeros((512, 512), dtype=np.bool)
draw_circle(a, 100, 200, 30)
draw_circle(a, 400, 350, 20)
draw_circle(a, 200, 260, 20)
# Label the objects in the array.
labels, numobjects = ndimage.label(a)
# Now find their bounding boxes (This will be a tuple of slice objects)
# You can use each one to directly index your data.
# E.g. a[slices[0]] gives you the original data within the bounding box of the
# first object.
slices = ndimage.find_objects(labels)
#-- Plotting... -------------------------------------
fig, ax = plt.subplots()
ax.imshow(a)
ax.set_title('Original Data')
fig, ax = plt.subplots()
ax.imshow(labels)
ax.set_title('Labeled objects')
fig, axes = plt.subplots(ncols=numobjects)
for ax, sli in zip(axes.flat, slices):
ax.imshow(labels[sli], vmin=0, vmax=numobjects)
tpl = 'BBox:\nymin:{0.start}, ymax:{0.stop}\nxmin:{1.start}, xmax:{1.stop}'
ax.set_title(tpl.format(*sli))
fig.suptitle('Individual Objects')
plt.show()
Hopefully that makes it a bit clearer how to find the bounding boxes of the objects.
Use the ndimage library from scipy. The function label places a unique tag on each block of pixels that are within a threshold. This identifies the unique clusters (shapes). Starting with your definition of a:
from scipy import ndimage
image_threshold = .5
label_array, n_features = ndimage.label(a>image_threshold)
# Plot the resulting shapes
import pylab as plt
plt.subplot(121)
plt.imshow(a)
plt.subplot(122)
plt.imshow(label_array)
plt.show()

Categories