ndimage script mis-behaving - python

I have a script that reads in image data, and then iterates over the images with the median filter in scipy.ndimage. From the iteration i create new arrays.
However when i attempt to run the script with
run filtering.py
The filtering does not seem to work. The new arrays (month_f) are the same as the old ones.
import matplotlib.pyplot as plt
import numpy as numpy
from scipy import ndimage
import Image as Image
# Get images
#Load images
jan1999 = Image.open('jan1999.tif')
mar1999 = Image.open('mar1999.tif')
may1999 = Image.open('may1999.tif')
sep1999 = Image.open('sep1999.tif')
dec1999 = Image.open('dec1999.tif')
jan2000 = Image.open('jan2000.tif')
feb2000 = Image.open('feb2000.tif')
#Compute numpy arrays
jan1999 = numpy.array(jan1999)
mar1999 = numpy.array(mar1999)
may1999 = numpy.array(may1999)
sep1999 = numpy.array(sep1999)
dec1999 = numpy.array(dec1999)
jan2000 = numpy.array(jan2000)
feb2000 = numpy.array(feb2000)
########### Put arrays into a list
months = [jan1999, mar1999, may1999, sep1999, dec1999, jan2000, feb2000]
############ Filtering = 3,3
months_f = []
for image in months:
image = scipy.ndimage.median_filter(image, size=(5,5))
months_f.append(image)
Any help would be much appreciated :)

This is rather a comment but due to reputation limits I'm not able to write one.
The way you import your modules is a bit strange. Especially "import .. as" with the idential name. I think a more pythonian way would be
import matplotlib.pyplot as plt
import numpy as np
from scipy import ndimage
from PIL import Image
and then call
image = ndimage.median_filter(image, size=(...))
When I run your steps with a RGB test image it seems to work.
What does jan1999.shape return?

Related

How can I extract the information from a tree using viewer?

from astropy.io import fits
import matplotlib.pyplot as plt
from astrodendro import Dendrogram, pp_catalog
import numpy as np
fname = "/home/citlali/Documentos/Servicio/m/m8093-1901/manga-8093-1901.Pipe3D.cube.fits.gz"
image = fits.open(fname)
DATOS = image["FLUX_ELINES"].data
img = DATOS[45, :, :]
d = Dendrogram.compute(img, min_value=0, min_delta=0, min_npix=0.1)
d.trunk[0]
v = d.viewer()
v.show()
This is my code and when I plotted I got this where I select whatever regions I want (for example, red and green). So, I'm looking for extract the information of those regions but I don't know how to do it. If anyone have an idea o know a function, I will appreciate it. Also it has to be atuomated.
Thank you.

Python "make_transforms" shows list index error while aligning fits images

I am trying to align 4 fits (V2.fits,V3.fits,V4.fits and, V5.fits) with respect to a reference fits file (V1.fits). I tried the following by employing the python fits_align package :
from fits_align.ident import make_transforms
from fits_align.align import affineremap
from glob import glob
from astropy.io import fits
from numpy import shape
import os
img_list = sorted(glob(os.path.join("*.fits")))
ref_image = img_list[0]
images_to_align = img_list[1:]
print(img_list) # List of all images
print(ref_image) # Reference image
print(images_to_align) # Images to align
identifications = make_transforms(ref_image, images_to_align)
aligned_images = [ref_image]
for id in identifications:
if id.ok:
alignedimg = affineremap(id.ukn.filepath, id.trans, outdir=tmpdir)
aligned_images.append(alignedimg)
I get the following index error:
What could be the reasons for this error? please suggest possible solutions

How do I write a function that takes in two different image arrays, plots the "difference image," and returns the difference image array?

So I have these 2 images I downloaded from a fits file. Here is the code located below. Now I want to create a function that takes in both those image arrays and subtracts each array from eachother and then plots the difference as a new picture. Can anyone help me out? I'm really stuck on it been working at it for 4 hours to no avail.
import numpy as np
import matplotlib.pyplot as plt
from astropy.io import fits
Image_file = fits.open('https://raw.githubusercontent.com/msu-cmse-courses/cmse202-S21-student/master/data/m42_40min_ir.fits')
fourty_min_ir = Image_file[0].data
type(fourty_min_ir)
Image_file = fits.open('https://raw.githubusercontent.com/msu-cmse-courses/cmse202-S21-student/master/data/m42_40min_red.fits')
fourty_min_red = Image_file[0].data
type(fourty_min_red)
results_img=fourty_min_ir-fourty_min_red
plt.imshow(results_img)
plt.show()

how to create a new interlaced image to two picture with python opencv and anaconda

I would like to create a new interlaced image where the odd rows belong to one image and the even rows to the other image. I am trying to do that with python and openCv and numpy! Reading the one images and with a loop i try to write the values in the odd and even rows. I don't know how to do that. Do you have some tips?p
Here is an example
import numpy as np
from matplotlib import pyplot as plt
imgshape = (100,100,3)
blue = np.zeros(imgshape)
red = np.zeros(imgshape)
blue[:,:,2] = np.ones(blue.shape[:2])
red[:,:,0] = np.ones(blue.shape[:2])
mix = np.zeros(imgshape)
oddrows = [i for i in range(blue.shape[0]) if i %2==1]
evenrows = [i for i in range(blue.shape[0]) if i%2==0]
mix[oddrows] = red[oddrows]
mix[evenrows] = blue[evenrows]
plt.imshow(mix)

Importing images for manifold Isomap

There are 192 x 144 pixel images. They should be imported to a Python list so that the items in the list are NDArray instances. New dataframe should be created from the list and that dataframe should be given to Isomap. iso.fit(df) fails with the errors
array = array.astype(np.float64)
ValueError: setting an array element with a sequence.
I have spent more than one day trying to figure out how the NDArrays should be processed and the dataframe loaded with them. No luck. Any help would be appreciated.
import pandas as pd
from scipy import misc
import glob
from sklearn import manifold
samples = []
for filename in glob.glob('Datasets/ALOI/32/*.png'):
img = misc.imread(filename, mode='I')
samples.append(img)
df = pd.DataFrame.from_records(samples, coerce_float=True)
iso = manifold.Isomap(n_neighbors=6, n_components=3)
iso.fit(df)
If those are gray scale images from the ALOI, you probably want to treat each pixel's brightness as a feature. Therefore, you should flatten the img array with img.reshape(-1). The revised code follows:
import pandas as pd
from scipy import misc
import glob
from sklearn import manifold
samples = []
for filename in glob.glob('Datasets/ALOI/32/*.png'):
img = misc.imread(filename, mode='I')
# the following line changed
samples.append(img.reshape(-1))
df = pd.DataFrame.from_records(samples, coerce_float=True)
iso = manifold.Isomap(n_neighbors=6, n_components=3)
iso.fit(df)

Categories

Resources