Resize CSV data using Python and Keras - python

I have CSV files that I need to feed to a Deep-Learning network. Currently my CSV files are of size 360*480, but the network restricts them to be of size 224*224. I am using Python and Keras for the deep-learning part. So how can I resize the matrices?
I was thinking that since aspect ratio is 3:4, so if I resize them to 224:(224*4/3) = 224:299, and then crop the width of the matrix to 224, it could serve the purpose. But I cannot find a suitable function to do that. Please suggest.

I think you're looking for cv.resize() if you're using images.
If not, try numpy.ndarray.resize()

Image processing
If you want to do nontrivial alterations to the data as images (i.e. interpolating between pixel values, assuming that they represent photographs) then you might want to use proper image processing libraries for that. You'd need to treat them not as raw matrixes (csv of numbers) but convert them to rgb images, do the transformations you desire, and convert them back to a numpy matrix.
OpenCV (https://docs.opencv.org/3.4/da/d6e/tutorial_py_geometric_transformations.html)
or Pillow (https://pillow.readthedocs.io/en/3.1.x/reference/Image.html) might be useful to do that.

I found a short and simple way to solve this. This uses the Python Image Library/Pillow.
import numpy as np
import pylab as pl
from PIL import Image
matrix = np.array(list(csv.reader(open('./path/mat.csv', "r"), delimiter=","))).astype("uint8") #read csv
imgObj = Image.fromarray(matrix) #convert matrix to Image object
resized_imgObj = img.resize((224,224)) #resize Image object
imgObj.show()
resized_imgObj.show()
resized_matrix = np.asarray(img) #convert Image object to matrix
While numpy module also has a resize function, but it is not as useful as the aforementioned way.
When I tried it, the resized matrix had lost all the intricacies and aesthetic aspect of the original matrix. This is probably due to the fact that numpy.ndarray.resize doesn't interpolate and missing entries are filled with zeros.
So, for this case Image.resize() is more useful.

You could also convert the csv file to a list, truncate the list, and then convert the list to a numpy array and then use np.reshape.

Related

How can I efficiently simulate image compression artifacts in Python?

I'm training a neural net using simulated images, and one of the things that happens in real life is low quality JPEG compression. It fuzzes up sharp edges in a particular way. Does anyone have an efficient way to simulate these effects? By that I mean create a corrupted version of a clean input. The images are grayscale, stored as numpy arrays.
Thanks to the answers in the comments, here is a solution which saves the image as JPEG and reads it back in, all in memory using standard python libraries.
import io
import imageio
# Image is 2D numpy array, q is quality 0-100
def jpegBlur(im,q):
buf = io.BytesIO()
imageio.imwrite(buf,im,format='jpg',quality=q)
s = buf.getbuffer()
return imageio.imread(s,format='jpg')
In my function I also pre- and post-scaled the image to convert from float64 to uint8 and back again, but this is the basic idea.

cvtColor "code" for 16-bit grayscale images

I have unsigned 16-bit grayscale tiff images as numpy arrays. I want to do some image processing on these images using OpenCV. I am converting the numpy array to Mat format using cv2.cvtColor(src, code). As far as the documentation goes, I am having a hard time finding the right code argument to correctly convert 16-bit grayscale images without losing any information.
Previously, I read the images directly using cv2.imread(src, cv2.IMREAD_UNCHANGED). However, I don't have the original image files now, only the pickled numpy array. I am looking for the code in cvtColor which does a similar thing as cv2.IMREAD_UNCHANGED
Your question is tough to follow. All you appear to have are some files containing pickled Numpy arrays, correct?
If so, you don't need any imread(), you just need to unpickle the files and you will have Numpy arrays of the type that OpenCV uses to hold images. Check their dtype is np.uint16 and their shape appears correct.

Medical imaging (PyRadiomics) with .nii.gz files

I am trying to implement the package:
https://pyradiomics.readthedocs.io/en/latest/usage.html
It looks super simple, but they expect .nrrd files.
My files are .nii.gz. How do I solve this?
Also, have anyone tried to apply PyRadiomics on TCIA data? if so, can I see your github or Jupyter Notebook?
Thanks a lot.
You could turn NII into numpy array firstly and then turn it into NRRD with using:
nrrd and nibabel
import numpy as np
import nibabel as nib
import nrrd
# Download NII
example_filename = "image.nii.gz"
image = nib.load(example_filename)
# Turn into numpy array
array = np.array(img.dataobj)
# Save NRRD
nrrd_path_to = "image.nrrd"
nrrd.write(image_path_to, array)
Although the examples are in .nrrd, PyRadiomics uses SimpleITK for image operations. This allows PyRadiomics to support a whole range of image formats, including .nii.gz. You don't have to convert them.
The DWIConverter converts diffusion-weighted MR images in DICOM series into nrrd format for analysis in Slicer. It parses the DICOM header to extract necessary information about measurement frame, diffusion weighting directions, b-values, etc, and write out a nrrd image. For non-diffusion weighted DICOM images, it loads in an entire DICOM series and writes out a single DICOM volume in a .nhdr/.raw pair.
So that trying to convert your .nii.gz inside DICOM files for the nrrd format is a possibility by using this tools. Also, you can look at the SlicerDMRI that is a similar module.

Numpy array to image is blurry

I'm trying to save a numpy array as a png but the output is always blurry or the size of the image is very small. I've tried looking around SO and various other sources but I haven't been able to figure this one out.
(1) Using the following technique I get an image of the appropriate size (800x600px) but the matrix is blurry:
import matplotlib.pyplot as plt
plt.imshow(matrix)
plt.savefig(filename)
(2) Using another technique, the image is very small (60x60px) but the matrix is not blurry:
import matplotlib
matplotlib.image.imsave('name.png', array)
(3) After running my python program to produce a numpy array A, if I then run the command
matshow(A)
and then I hit the save icon to save the image, the result is an image of a larger size (800x600px) and the matrix is not blurry.
Using technique (3) wouldn't be an issue, but the thing is I have a lot of numpy arrays that I need to save as images, so it would be very time consuming to use technique (3) for each array.
Note: I can't post the images because I don't have enough reputation, so here is a link to them instead: https://www.dropbox.com/sh/mohtpm97xyy8ahi/AACgIpY5ECBDIbcnNRS9kd8La?dl=0
If anyone has had success producing clearer images of a larger size, like those in technique 3, any insight would be much appreciated.

What is the difference between a PIL Image from PIL.Image.open and a NumPy Image matplotlib.image.imread ?

I would like to know why I should or shouldn't use a matplotlib image over a PIL image. I know that matplotlib uses PIL to load any image that is not a PNG, but what is the advantage in having it in a numpy array over the PIL backend representation?
The PIL API includes function for performing a wide range of image manipulations. But of course, it does not provide a function for all conceivable operations. Numpy is useful when you have a mathematical operation to perform on the image which is not built into the PIL API. PIL has a way of altering pixels one-by-one but because if its reliance on Python loops it can be a very slow way to manipulate a large image (or many images).
Numpy math is relatively fast and it has an expressive syntax which
can make coding new image manipulations easier. Moreover, scipy has many additional image manipulating functions which can be applied to numpy arrays.
Here are a few examples:
emboss
converting rgb to hsv
replacing a color

Categories