简体   繁体   English

将 3 个单独的 numpy 数组组合成 Python 中的 RGB 图像

[英]Combine 3 separate numpy arrays to an RGB image in Python

So I have a set of data which I am able to convert to form separate numpy arrays of R, G, B bands.所以我有一组数据,我可以将其转换为单独的 R、G、B 波段的 numpy 数组。 Now I need to combine them to form an RGB image.现在我需要将它们组合起来形成一个 RGB 图像。

I tried 'Image' to do the job but it requires 'mode' to be attributed.我试过“图像”来完成这项工作,但它需要归因于“模式”。

I tried to do a trick.我试着做一个把戏。 I would use Image.fromarray() to take the array to image but it attains 'F' mode by default when Image.merge requires 'L' mode images to merge.我会使用 Image.fromarray() 将数组转换为图像,但当 Image.merge 需要“L”模式图像合并时,它默认达到“F”模式。 If I would declare the attribute of array in fromarray() to 'L' at first place, all the RGB images become distorted.如果我首先将 fromarray() 中的数组属性声明为“L”,则所有 RGB 图像都会失真。

But, if I save the images and then open them and then merge, it works fine.但是,如果我保存图像然后打开它们然后合并,它工作正常。 Image reads the image with 'L' mode.图像以“L”模式读取图像。

Now I have two issues.现在我有两个问题。

First, I dont think it is an elegant way of doing the work.首先,我认为这不是一种优雅的工作方式。 So if anyone knows the better way of doing it, please tell所以如果有人知道更好的方法,请告诉

Secondly, Image.SAVE is not working properly.其次,Image.SAVE 不能正常工作。 Following are the errors I face:以下是我面临的错误:

In [7]: Image.SAVE(imagefile, 'JPEG')
----------------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

/media/New Volume/Documents/My own works/ISAC/SAMPLES/<ipython console> in <module>()

TypeError: 'dict' object is not callable

Please suggest solutions.请提出解决方案。

And please mind that the image is around 4000x4000 size array.请注意图像大约是 4000x4000 大小的数组。

rgb = np.dstack((r,g,b))  # stacks 3 h x w arrays -> h x w x 3

还要将浮点数 0 .. 1 转换为 uint8 s,

rgb_uint8 = (np.dstack((r,g,b)) * 255.999) .astype(np.uint8)  # right, Janna, not 256

I don't really understand your question but here is an example of something similar I've done recently that seems like it might help:我不太明白你的问题,但这里有一个我最近做过的类似事情的例子,看起来可能会有所帮助:

# r, g, and b are 512x512 float arrays with values >= 0 and < 1.
from PIL import Image
import numpy as np
rgbArray = np.zeros((512,512,3), 'uint8')
rgbArray[..., 0] = r*256
rgbArray[..., 1] = g*256
rgbArray[..., 2] = b*256
img = Image.fromarray(rgbArray)
img.save('myimg.jpeg')

I hope that helps我希望有帮助

rgb = np.dstack((r,g,b))  # stacks 3 h x w arrays -> h x w x 3

This code doesnt create 3d array if you pass 3 channels.如果您传递 3 个通道,此代码不会创建 3d 数组。 2 channels remain.保留 2 个通道。

Convert the numpy arrays to uint8 before passing them to Image.fromarray在将 numpy 数组传递给Image.fromarray之前将它们转换为uint8

Eg.例如。 if you have floats in the range [0..1]:如果您在 [0..1] 范围内有浮点数:

r = Image.fromarray(numpy.uint8(r_array*255.999))

Your distortion i believe is caused by the way you are splitting your original image into its individual bands and then resizing it again before putting it into merge;我认为您的失真是由于您将原始图像分成单独的波段,然后在将其合并之前再次调整大小的方式造成的;

`
image=Image.open("your image")

print(image.size) #size is inverted i.e columns first rows second eg: 500,250

#convert to array
li_r=list(image.getdata(band=0))
arr_r=np.array(li_r,dtype="uint8")
li_g=list(image.getdata(band=1))
arr_g=np.array(li_g,dtype="uint8")
li_b=list(image.getdata(band=2))
arr_b=np.array(li_b,dtype="uint8")

# reshape 
reshaper=arr_r.reshape(250,500) #size flipped so it reshapes correctly
reshapeb=arr_b.reshape(250,500)
reshapeg=arr_g.reshape(250,500)

imr=Image.fromarray(reshaper,mode=None) # mode I
imb=Image.fromarray(reshapeb,mode=None)
img=Image.fromarray(reshapeg,mode=None)

#merge
merged=Image.merge("RGB",(imr,img,imb))
merged.show()
`

this works well !这很好用!

If using PIL Image convert it to array and then proceed with the below, else using matplotlib or cv2 perform directly.如果使用 PIL Image 将其转换为数组,然后继续下面的操作,否则使用 matplotlib 或 cv2 直接执行。

image = cv2.imread('')[:,:,::-1]
image_2 = image[10:150,10:100]
print(image_2.shape)

img_r = image_2[:,:,0]
img_g = image_2[:,:,1]
img_b = image_2[:,:,2]

image_2 = img_r*0.2989 + 0.587*img_g + 0.114*img_b 

image[10:150,10:100,0] = image_2
image[10:150,10:100,1] = image_2
image[10:150,10:100,2] = image_2

plt.imshow(image,cmap='gray')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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