I have a stack of RGB images in a 4d numpy array, so the shape is
(n_images, height, width, n_channels) where n_channels is 3.
How can I get a list of unique RGB values across all the images.
I found this question numpy: unique list of colors in the image for a single image but want to apply it to my stack of images without a for-loop.
You can use np.unique and set the axis, to look across the images. so basically it will look for unique pixel values.
i am reusing the answer from link you shared for your scenario.
np.unique(img.reshape(-1, img.shape[3]), axis=0)
The above code will result in an array of shape (unique_pixels_len,3)
The original answer had img.shape[2] i.e. the channels and in your case it is img.shape[3] which represents the channels.
Related
I have a dataset which comprises of the binary data of pixelated 50x50 images. The array shape is (50, 50, 90245). I want to reach 50x50 pixels of each of the 90245 images. How can I slice the array?
If data is the variable storing the image data, and i is the index of the image you want to access, then you can do:
data[:,:,i]
to get the desired image data.
If data is the variable storing the image data, and i is the index of the image you want to access, then you can do as #BrokenBenchmark suggested. In case you want a (50,50,1) 3D array as the output, you could do:
data[:,:,i:i+1]
to get the image as a 3D array.
Edit1: If you reshaped your data matrix to be of shape (90245,50,50), you can get the ith image by doing data[i,:,:] or just data[i] to get a (50,50) image. Similarly, to get a (1,50,50) image, you could do data[i:i+1,:,:] or just data[i:i+1].
Edit2: To reshape the array, you could use the swapaxes() function in numpy.
I have a 480x640 image and I am cropping to 195x195.
The original 480x640 has a corresponding (68,2) array which can be used to further process the image. Since I cropped the image, how am can I rescale the (68,2) array values to correspond to the new cropped image?
I tried to do something along the line:
shape = shape*((195*195)/(480*640))
but it was no use. N.B. shape is the (68,2) array
I have a 3d numpy array. Each of the 1000 rows is an 150x150 image.
Meaning that the input array has a shape (1000,150,150)
Some of these 1000 images are duplicated.
The task is to find the unique images (based on the 150x150 pixel values) and all the indices where each unique image is present in the original dataframe.
I tried:
u, indices = np.unique(img, return_inverse=True)
but this is not what I am looking for: this would give me 256 different values from 0 to 255 which are the possible pixels level.
What I rather need is to split the 1000 indexes into groups, each one having the same sequence of the 48x48 pixels.
Any suggestion?
I am trying to convert an image into an array of pixels.
Here is my current code.
im = Image.open("beeleg.png")
pixels = im.load()
im.getdata() # doesn't work
print(pixels # doesn't work
Ideally, my end goal is to convert the image into a vector of just pixels, so for instance if I have an image of dimensions 100x100, then I want a vector of dimensions 1x10000, where each value is between [0, 255]. Then, divide each of the values in the array by 256 and add a bias of 1 in the front of the vector. However, I am not able to proceed with all this without being able to obtain an array. How to proceed?
Scipy's ndimage library is generally the go-to library for working with pixels as data (arrays). You can load an image from file (most common formats supported) using scipy.ndimage.imread into a numpy array which can be easily reshaped and mathematically operated on. The mode keyword can be used to specify a colorspace transformation upon load (convert an RGB image to black and white). In your case you asked for single color pixels from 0-255 (8bit grayscale) so you would use mode='L'. See The Documentation for usage / more useful functions.
If use OpenCV, gray=cv2.imread(image,0) will return a grayscale image with n rows x m cols single channel numpy array. rows, cols = gray.shape will return the height and width of the image.
I'm would like to go from an image filename to a list of coordinates of the white pixels in the image.
I know it involves PIL. I have tried using Image.load() but this doesn't help because the output is not indexable (to use in a for loop).
You can dump an image as a numpy array and manipulate the pixel values that way.
from PIL import Image
import numpy as np
im=Image.open("someimage.png")
pixels=np.asarray(im.getdata())
npixels,bpp=pixels.shape
This will give you an array whose dimensions will depend on how many bands you have per pixel (bpp above) and the number of rows times the number of columns in the image -- shape will give you the size of the resulting array. Once you have the pixel values, it ought to be straightforward to filter out those whose values are 255
To convert a numpy array back to an image use:
im=Image.fromarray(pixels)