简体   繁体   中英

How to make pixels arrays from RGB image without losing its spatial information in python?

I am wondering is there any workaround to convert RGB images to pixel vectors without losing its spatial information in python. As far as I know, I can read the images and do transformation for images to pixel vectors. I am not sure doing this way still preserve images' spatial information in pixel vectors. How can I make this happen for making pixel vectors from RGB image?

my attempt :

I tried as follow but I am not sure how to make

import matplotlib.pyplot as pl

image = plt.imread('dog.jpg')
im = image/255.0
print(im.shape) #(32, 32, 3)
pixels = im.reshape(im.shape[0]*im.shape[1], im.shape[2])

but I want to make sure how to make pixel vectors from RGB images without losing pixel order and its spatial information. How to make this happen? any thoughts?

I think maybe numpy might have functions to do this. Can anyone point me how to do this with numpy ?

graphic illustration :

here is simple graphic illustration of making pixel vectors from RGB images:

从 RGB 图像制作像素向量

as this diagram shows, we have RGB images with shape of (4,4,3) which needs to make pixel vectors without losing its spatial information and pixel orders then combine pixel vectors from each channel (Red, Green, Blue) as pixel matrix or dataframe. I am curious how to get this done in python?

goal :

I want to make pixel vectors from RGB images so resulted pixel vectors needs to be expanded with taylor expansion. Can anyone point me out how to make this happen?

Are You just trying to reshape each channel to a vector and then joining them horizontally? That's what I understood from the graphic illustration and the way i would do it is something like this:

import matplotlib.pyplot as plt
import numpy as np

image = plt.imread('monkey.png')
image = image / 255.0
red = image[:,:,0]
green = image[:,:,1]
blue = image[:,:,2]

def to_vector(matrix):
    result = []
    for i in range(matrix.shape[1]):
        result = np.vstack(matrix[:,i])
    return result

red = to_vector(red)
green = to_vector(green)
blue = to_vector(blue)

vector = np.hstack((red,green,blue))

Your original attempt was almost a full solution - maybe actually a full solution, depending on what the idea is.

print(im.shape) #(32, 32, 3)
pixels = im.reshape(im.shape[0]*im.shape[1], im.shape[2]) # this is exactly correct
print(pixels.shape) #(1024,3)
reds = pixels[:, 0] #just as an example for where things end up in the result
pixels_channelfirst = np.moveaxis(pixels, 1, 0) # if you want the first axis to be channels
print(pixels.shape) #(3, 1024)
reds = pixels[0, :]

"I want to preserve its pixel order and spatial information" - this does that already, Add one non-zero pixel to a zero image and plot where it goes. if you have doubts. np.hstack in the other answer does as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM