I have 3 (512×512) numpy arrays representing the Hue, Saturation and Value channels of my desired HSV image, containing float values.
How do I construct a single 512×512 image from these 3 numpy arrays?
To create an HSV image from the 3 channels, you can put them in a numpy array and transpose it to convert the shape from (3, 512, 512) to (512, 512, 3):
hsv = np.transpose([h, s, v], (1, 2, 0))
With OpenCV you can also use cv2.merge.
Related
So I am trying to write a function that converts RGB to HSI on Python.
I have an image that is saved in np.ndarray (tensor I suppose?) with dimensions (1080, 1920, 3), that is - 1080x1920 pixels in RGB. How can I extract matrix of R/G/B; after I get H/S/I, how do I concatenate the matrices to get back the tensor (1080, 1920, 3)?
Assuming your_image contains the RGB channels, you can extract each channel using the corresponding index:
r = your_image[..., 0]
g = your_image[..., 1]
b = your_image[..., 2]
Note: You may need to normalize the values to the interval [0.0, 1.0]. If so, divide them by 255.0.
Conversely, you can stack the three channels together as follows:
import numpy as np
your_image = np.dstack((r, g, b))
I'm trying to convert an RGB image to a greyscale image, then to a numpy array using the following code snippet:
img = Image.open("image1.png")
img = img.convert('L')
img = np.array(img, dtype='f')
print(img.shape)
The result is a numpy array of shape (128, 128). Is there anyway that I could convert a greyscale image to a numpy array so that it would have the color channel as well, i.e. the shape would be (128, 128, 1)?
Like #Mark mentioned in comments, add a dimension to the end if your array using newaxis:
img=img[...,None]
None will do similar as np.newaxis. It does not create a color, but adds a dimension similar to a single channel image.
I have a numpy array A of shape (512, 512, 4)
Each element is a tuple: (r, g, b, a). It represents a 512x512 RGBA image.
I have a numpy array B of shape (512, 512, 3)
Each element is a tuple: (r, g, b). It represents a similar, RGB image.
I want to fast copy all the 'a' (alpha) values from each element of A into corresponding elements in B. (basically transferring the alpha channel).
resulting B shape would be (512, 512, 4).
How can I achieve this? The algorithm is based on fast pixel manipulation technique laid out here.
Code:
## . input_image is loaded using PIL/pillow
rgb_image = input_image
print(f"Image: {rgb_image}")
rgb_image_array = np.asarray(rgb_image) # convert to numpy array
print(f"Image Array Shape: {rgb_image_array.shape}")
gray_image = rgb_image.convert("L") # convert to grayscale
print(f"Gray image: {gray_image}")
gray_image_array = np.asarray(gray_image)
print(f"Gray image shape: {gray_image_array.shape}")
out_image_array = np.zeros(rgb_image_array.shape, rgb_image_array.dtype)
print(f"Gray image array shape: {out_image_array.shape}")
rows, cols, items = out_image_array.shape
# create lookup table for each gray value to new rgb value
LUT = []
for i in range(256):
color = gray_to_rgb(i / 256.0, positions, colors)
LUT.append(color)
LUT = np.array(LUT, dtype=np.uint8)
print(f"LUT shape: {LUT.shape}")
# get final output that uses lookup table technique.
# notice that at this point, we don't have the alpha channel
out_image_array = LUT[gray_image_array]
print(f"output image shape: {out_image_array.shape}")
# How do I get the alpha channel back from rgb_image_array into out_image_array
Output:
Image: <PIL.Image.Image image mode=RGBA size=512x512 at 0x7FDEF5F2F438>
Image Array Shape: (512, 512, 4)
Gray image: <PIL.Image.Image image mode=L size=512x512 at 0x7FDEF5C25CF8>
Gray image shape: (512, 512)
Gray image array shape: (512, 512, 4)
LUT shape: (256, 3)
output image shape: (512, 512, 3)
Using numpy slices:
import numpy as np
A = [[(1,1,1,4)], [(1,1,1,5)]]
B = [[(2,2,2)], [(3,3,3)]]
# A and B are tensors of order 3
A = np.array(A)
B = np.array(B)
print("A=")
print(A)
print("B=")
print(B)
C = np.copy(A)
# assign along all 1st and 2nd dimensions, but only the first three elements of the third dimension
C[:,:,0:3] = B
print("C=")
print(C)
Output:
A=
[[[1 1 1 4]]
[[1 1 1 5]]]
B=
[[[2 2 2]]
[[3 3 3]]]
C=
[[[2 2 2 4]]
[[3 3 3 5]]]
Let's be careful about terminology
I have a numpy array A of shape (512, 512, 4) Each element is a tuple: (r, g, b, a). It represents a 512x512 RGBA image.
If A has that shape, and has a numeric dtype (e.g. np.int32), then it has 512*512*4 elements. The only way it can have a tuple element is if the dtype was object. I suspect rather that you have a 512x512 image where each pixel is represented by 4 values.
A[0,0,:]
will be a (4,) shape array representing those 4 values (sometimes called channels) of one pixel.
A[:,:,0]
is the r value for the whole image.
If they really are 3d arrays, then #mocav's solution of copying columns (indexing on the last dimension) to a new array is the right one.
Another possibility is that they are structured 2d arrays with 4 and 3 fields respectively. That would print (str) as tuples, though the repr print will make the compound dtype explicit. But the solution will be similar - make a new array of the right shape and dtype (like A), and copy values by field name from B and A. (I'll wait with details until you clarify the situation).
I have a numpy array with a shape like this
x.shape
(100, 1, 300, 300)
Think of this as 100 observations of grayscale images of size 300x300.
Grayscale images have only 1 channel, hence the second 1 in the shape.
I want to convert this to an array of RGB images, with 3 channels.
I want to just copy the grayscale image to the two other channels.
So the final shape would be (100, 3, 300, 300)
How can I do that?
Use np.repeat -
np.repeat(x,3,axis=1)
Sample run -
In [8]: x = np.random.randint(11,99,(2,1,3,4))
In [9]: np.repeat(x,3,axis=1).shape
Out[9]: (2, 3, 3, 4)
I have an array of pixels
np.shape(pred2)
Out[35]: (3000, 3, 32, 32)
It has 3000 images, 3 values rgb and is 32*32 in size for each image. I want to create an image from this.
Here is what I have so far:
img = Image.new( 'RGB', (32,32), "black") # create a new black image
pixels = img.putdata(pred2[1,:])
Can anyone give me a hand here as to what I am doing wrong?
Images are shape (h, w, 3), not (3, h, w). You need to permute your axes accordingly. Depending on whether you care about width vs height, it appears you can just do:
im = pred2[1].T
scipy.misc.imsave('the_image_file.png', im)