What IS a .fits file, as in, what is a .fits array? - python

I'm basically trying to plot some images based on a given set of parameters of a .fits file. However, this made me curious: what IS a .fits array? When I type in img[2400,3456] or some random values in the array, I get some output.
I guess my question is more conceptual than code-based, but, it boils down to this: what IS a .fits file, and what do the arrays and the outputs represent?

A FITS file consists of header-data units. A header-data unit contains an ASCII-type header with
keyword-value-comment triples plus either binary FITS tables or (hyperdimensional) image cubes.
Each entry in a table of a binary FITS table may itself contain hyperdimensional image cubes. An array
is some slice through some dimensions of any of these cubes.
Now as a shortcut to images stored in the first (a.k.a primary) header-data unit, many viewers
allow to indicate in square brackets some indices of windows into these images (which in most
common cases is based on the equivalent support by the cfitsio library).

Related

How to correlate different MRI sequences images in NIFTI format?

I am new on Medical Imaging. I am dealing with MRI images, namely T2 and DWI.
I uploaded both images with nib.load, yet each sequence image has a different number of slices (volume, depth?). If I select one slice (z coordinate), how can get the corresponding slice on the other sequence image? ITK does it correctly, so maybe something in the NIFTI header could help?
Thank you so much for reading! I also tried interpolation, but it did not work.
If ITK does it correctly, why not just use ITK?
If you insist on hand-rolling your own index <-> physical space conversion routines, take a look at how ITK computes those matrices and the publicly accessible methods which use them. For being specific to NIFTI, take a look at ITK's NIFTI reader which sets the relevant metadata in itk::Image.
Reading the NIFTI, namely affine, and extracting the translation Transformation Matrix to create a mapping from the T2 to the DWI voxels.
Hint: nibabel.

Converting GeoJSON to a raster image using rasterio

I am hoping for advice as to how I can convert GeoJSON geometry into a tif image using rasterio. I have tried a lot of things, but all of them do not rasterize all the shapes found in the GeoJSON (more like 80% of the file is rasterized). How can I ensure all the geometry is rasterized and is of adequate size? Let me know if my question is unclear.
Your problem stems from the fact that when you rasterize a shape, using gdal_translate for example, you must determine the resolution of the raster, and it must be chosen in accordance to the "size" of your feature vectors if you want your raster to retain enough information.
If you do not want to lose too many details in the rasterization process, I guess a good rule of thumb would be to set the resolution to be lower than the typical size of your individual features. For example, if your features look like squares ~1km large, a "good" resolution would be in the 10-100m range.
A way to programmatically get the typical size of your features would be to compute their minimum_rotated_rectangle for example: https://shapely.readthedocs.io/en/stable/manual.html#object.minimum_rotated_rectangle

Python crashes when reading U16 and U8 PCIDSK layers to numpy arrays with GDAL

The PCIDSK (normally .pix) format allows for bands with multiple data types to be held in the same file. My files contain "U16", "U8" and "BIT" channels.
GDAL is able to interpret the dataset correctly:
ds = gdal.Open("myfile.pix")
for i in range(1,ds.RasterCount+1):
print ds.GetRasterBand(i).DataType, ds.GetRasterBand(i).GetDescription()
It is interpreting the "U16" band as gdalconst.GDT_UInt16 (2), and the "U8" and "BIT" as gdalconst.GDT_Byte (1). I can see all the bands from the original file, in the correct order, with proper band names.
The problem is when I try to extract the data to numpy arrays. pythonw.exe crashes (not even an error in the console) when I try the usual ds.GetRasterBand(1).ReadAsArray().
I noticed that I was able to extract some data correctly by specifying the region to extract but it still crashing when the region includes one specific pixel, which is different from file to file. The given pixel is problematic for all "U16" and "U8" layers.
For a file 9494 (X) x 9609 (Y), it fails only at:
ds.GetRasterBand(1).ReadAsArray(8704,9472,1,1)
For a file 9193 (X) x 9293 (Y), it fails only at:
ds.GetRasterBand(1).ReadAsArray(5376,9216,1,1)
There is nothing notable about the pixels in the original data. I noticed that the unreadable locations would be located at edge of a tile 256x256 tile interleaved in the original.
It seems to be able to handle the "BIT" layers correctly with .ReadAsArray(), but not the "U16" and "U8".
The machine has plenty of memory, and I able to read and create other datasets that are much larger.
As a side note, if any knows how to create PCIDSK files with multiple data types, it would be useful for me to create a working example of the problem.
Using Python 2.7.11, Numpy 1.10.2, gdal bindings 1.11.3.

relation between .jpg and corresponding .asc file

I had a python assignment in which I was required to do certain operations on a map whose .jgp as well as .asc file was provided to me. I know that to every colored pixel in the map there is a numerical data assigned to it in the file. I was able to solve the questions in the assignment and generate the resulting .asc file that was required to be submitted. I just want to know if it is possible to assign colors to the numbers in the final .asc file and generate a jpg map of it. Also, please tell me how the assignment of the numbers to the pixels is done.

How to cluster a set of image files to different folders based on their content

I have a set of images in a folder, where each image either has a square shape or a triangle shape on a white background (like this and this). I would like to separate those images into different folders (note that I don't care about detecting whether image is a square/triangle etc. I just want to separate those two).
I am planning to use more complex shapes in the future (e.g. pentagons, or other non-geometric shapes) so I am looking for an unsupervised approach. But the main task will always be clustering a set of images into different folders.
What is the easiest/best way to do it? I looked at image clustering algorithms, but they do clustering of colors/shapes inside the image. In my case I simply want to separate those image files based on the shapes that have.
Any pointers/help is appreciated.
You can follow this method:
1. Create a look-up tables with shape you are using in the images
2. Do template matching on the images stored in a single folder
3. According to the result of template matching just store them in different folders
4. You can create folders beforehand and just replace the strings in program according to the usage.
I hope this helps
It's really going to depend on what your data set looks like (e.g., what your shape images look like), and how robust you want your solution to be. The tricky part is going to be extracting features from each shape image the produce a clustering result that you're satisfied with. A few ideas:
You could compute SIFT features for each images and then cluster the images based on those features: http://en.wikipedia.org/wiki/Scale-invariant_feature_transform
If you don't want to go the SIFT route, you could try something like HOG: http://en.wikipedia.org/wiki/Histogram_of_oriented_gradients
A somewhat more naive approach - If the shapes are always the same scale, and the background color is fixed you could get rid of the background cluster the images based on shape area (e.g., number of pixels taken up by the shape).

Categories