如何将numpy.zeros数组可视化为RGB图像?

时间:2022-05-26 21:27:30

I am working on Ubuntu using python 2.7 with OpenCV I am trying to display 4 images in the same window. Because opencv does not have any function for this I will try to make one. I have search in Stack Overflow and I found some complicated solutions but generated in C/C++ which I do not know.

我正在使用带有OpenCV的python 2.7来处理Ubuntu我试图在同一个窗口中显示4个图像。因为opencv没有任何功能,我会尝试制作一个。我在Stack Overflow中搜索,我发现了一些复杂的解决方案,但是用C / C ++生成,我不知道。

So, my strategy is: I have 4 color BGR images. All 4 same size: (x,y) I will generate a numpy array of zeros 4 times the size of the original images (2x,2y) I will copy each images in the numpy zero array, but in different position, so they will look each image next to the other:

所以,我的策略是:我有4种颜色的BGR图像。所有4个相同的大小:(x,y)我将生成一个numpy零的原始图像大小的4倍(2x,2y)我将复制每个图像在numpy零阵列,但在不同的位置,所以他们将看看另一个旁边的每个图像:

import cv2
import numpy as np

def ShowManyImages():




    img1 = cv2.imread('img1.png',1)
    img2 = cv2.imread('img2.png',1)
    img3 = cv2.imread('img3.png',1)
    img4 = cv2.imread('img4.png',1)

    x,y,_ = img1.shape
    print x, y   # show: 500,500
    img_final = np.zeros((x*2, y*2, 3), np.uint8)
    print img_final.shape   # show: 1000,1000,3
    img_final[0:500,0:500,:] = img1[:,:,:]

    cv2.imshow('final', img_final)


    cv2.waitKey()

    cv2.destroyAllWindows()



ShowManyImages()

However, the code does not show any image. I can't figure out why. Any ideas?

但是,代码不显示任何图像。我无法弄清楚为什么。有任何想法吗?

Note: to make the code shorter, I only show the code for the first img (img1)

注意:为了缩短代码,我只显示第一个img的代码(img1)

1 个解决方案

#1


1  

Here's an approach with few stack operations assuming a0,a1,a2,a3 as the four 3D (RGB) image arrays -

这是一种几乎没有堆栈操作的方法,假设a0,a1,a2,a3为四个3D(RGB)图像阵列 -

a01 = np.hstack((a0,a1))
a23 = np.hstack((a2,a3))
out = np.vstack((a01, a23))

Sample run -

样品运行 -

In [245]: a0 = np.random.randint(0,9,(2,3,3))
     ...: a1 = np.random.randint(0,9,(2,3,3))
     ...: a2 = np.random.randint(0,9,(2,3,3))
     ...: a3 = np.random.randint(0,9,(2,3,3))
     ...: 

In [246]: a01 = np.hstack((a0,a1))
     ...: a23 = np.hstack((a2,a3))
     ...: out = np.vstack((a01, a23))
     ...: 

In [247]: out.shape
Out[247]: (4, 6, 3)

Thus, we would have them stacked like so -

因此,我们会像往常一样堆积它们 -

a0 | a1
-------
a2 | a3

#1


1  

Here's an approach with few stack operations assuming a0,a1,a2,a3 as the four 3D (RGB) image arrays -

这是一种几乎没有堆栈操作的方法,假设a0,a1,a2,a3为四个3D(RGB)图像阵列 -

a01 = np.hstack((a0,a1))
a23 = np.hstack((a2,a3))
out = np.vstack((a01, a23))

Sample run -

样品运行 -

In [245]: a0 = np.random.randint(0,9,(2,3,3))
     ...: a1 = np.random.randint(0,9,(2,3,3))
     ...: a2 = np.random.randint(0,9,(2,3,3))
     ...: a3 = np.random.randint(0,9,(2,3,3))
     ...: 

In [246]: a01 = np.hstack((a0,a1))
     ...: a23 = np.hstack((a2,a3))
     ...: out = np.vstack((a01, a23))
     ...: 

In [247]: out.shape
Out[247]: (4, 6, 3)

Thus, we would have them stacked like so -

因此,我们会像往常一样堆积它们 -

a0 | a1
-------
a2 | a3