2

Essentially my task involves converting a 2-Dimensional temperature field (a 2D matrix) to an image (such that resolution is preserved), then do some image processing on the image and then convert it back to temperature field. For example, if my Temperature field was a grid containing 25x25 cells, my image should be having a resolution of 25x25 pixels.

Also, since my temperature field will have float values, I want the floating values to be preserved in the image and be able to extract the same afterwards.

I tried the following: I have a numpy array that represents some physical field (like Temperature) and I want to convert this to an image for further processing. But while creating the image, matplotlib 's cmap seems to be the only option in python that preserves the resolution.

But when I save the numpy array into colormap, the resulting image seems to be a 4-channel image while I want a 3 channel image.

Is there a way out for my problem?

The code is used was:

import matplotlib as plt
cmap = plt.cm.jet
norm = plt.Normalize(vmin=temp.min(), vmax=temp.max())

# Map the normalized data to colors.  
image = cmap(norm(temp))

# Save the image
plt.imsave('temperature2image.png', image)

# Display the image
plt.pcolor(image)

##### PROCESS THE IMAGE #################

temp_after_processing = plt.imread('processed_image.png')

EDIT: Also, can someone else tell me if there is a way to convert the 3 channel or 4 channel image back to a 2dimensional matrix containing temperature values?

Bharath Ram
  • 190
  • 1
  • 12
  • You could remove the alpha channel afterwards, as in https://stackoverflow.com/questions/9166400/convert-rgba-png-to-rgb-with-pil – ImportanceOfBeingErnest Sep 13 '19 at 13:56
  • use to_rgb function to convert to rgb. colormap by default converts the value to rgba. – abhilb Sep 13 '19 at 14:05
  • Also, Is there a way to map the processed 3 channel image back to the 2 Dimensional matrix containing temperature values? – Bharath Ram Sep 13 '19 at 14:08
  • 1
    You can slice off the extra channel like `image[:, :, :3]`... but what is the image processing you're doing? Maybe you can just do that on the array. Or do it on a grayscale image (i.e. an ordinary 2D array). Either of these is probably easier than turning the 3-channel image back into a monochrome one (i.e. a 2D array). – Matt Hall Sep 13 '19 at 16:05
  • I want to perform some computer vision tasks on the image. Yes, using GrayScale images instead of converting to 3 channel image is one possibility. But leaving that out, I am wondering if there is a way to convert back the 3 channel image to a 2D matrix. – Bharath Ram Sep 13 '19 at 17:13
  • I took a matrix, normalised it (linear normalisation), saved it as a png file using plt.imsave(). Then, I reopened the same image, converted 3 channel image back to 2D matrix using the formula Gray = 0.289R + 0.587G + 0.114B and then de-normalised it. The values of the original 2D matrix and the matrix from above formula didn't match – Bharath Ram Sep 13 '19 at 17:19
  • Can you tell us what image processing you are trying to do? I cannot think of a reason why you would need to convert it to 3 channels in the first place according to any normal colour standards, since you don't have an image to start with. Why not just keep it 2D and do your processing on the temperature matrix? – Mozglubov Sep 13 '19 at 20:22
  • I have to perform image segmentation task using a neural network to identify certain distributions of temperatures. So, I am not sure if color matters or simply grayscale is good enough. If at all grayscale is good enough, can you explain me which commands in pythons can I use to convert a 2d matrix (M,N) to a grayscale image(M,N)? Thanks – Bharath Ram Sep 16 '19 at 06:06

1 Answers1

1

As people told in comments, it's likely the best to to operate on a gray scale image, because the temperature field is exactly that...

The typical format for gray scale images is a floating point array with values comprised between 0 and 1, so you want to input your temperatures,

t0 = np.loadtxt(...) # → t.shape is 25x25

and you want to normalize them (saving the values used in normalization)

m_in = t0.min()
m_ax = t0.max()
t1 = (t0-m_in)/(m_ax-m_in) # → 0 ≤ t1 ≤ 1

process your image

t1 = imageproc(t1)

and convert back from a gray scale image to a temperature field reversing the linear transformation we have applied.

t1 = t1*(m_ax-m_in)+m_in

Of course you can apply a non-linear transform to convert from temperatures to gray levels as long as you can revert the forward transformation.

gboffi
  • 22,939
  • 8
  • 54
  • 85
  • Can you tell me which library ( & what command in that library) can I use to convert the t1 (normalised temperature matrix) to a .png file while preserving the resolution? For example, if my t1 matrix is of size 25x25, then my image should be saved as a 25x25 image. Nothing else (like borders, colorbars, etc..) should be there. Thanks. – Bharath Ram Sep 17 '19 at 12:11
  • in the above link they are saving the image using PIL where I have to convert my float values to uint8. Since I am working with Temperatures, it's important that I have lossless conversions back & forth. (Which is why I started out with matplotlib's imsave(). But plt.imsave() can save float numpy arrays only as 3 channel image, not as a grayscale image). Is there a way to save float numpy arrays losslessly into a grayscale image while preserving resolution? – Bharath Ram Sep 30 '19 at 13:54