我需要使用pydown函数的结果生成单个图像(如上图所示),但是我无法将较小的图像放在第二列中。下面是我的代码:
def comporImagem(piramide):
linhas, colunas, dim = piramide[0].shape
imagem_composta = np.zeros((linhas, colunas + colunas // 2, dim), dtype=np.int)
imagem_composta[:linhas, :colunas, :] = piramide[0]
i_linhas = 0
for i in range(3):
nova_imagem = cv2.pyrDown(piramide[i_linhas])
linhas, colunas, dim = nova_imagem.shape
i_linhas = i_linhas + 1
piramide[i_linhas] = nova_imagem
imagem_composta[:linhas, :colunas, :] = piramide[i_linhas]
return imagem_composta
arquivo="test"
piramide=[]
for i in range(4):
nome=arquivo+str(i)+".jpg"
piramide.append(cv2.imread(nome))
imagem=comporImagem(piramide)
imagem=imagem[:,:,::-1]
plt.imshow(imagem)
结果是:
但是我需要图像看起来像这样:
我怎样才能做到这一点?
最佳答案
这是在Python / OpenCV中执行此操作的一种方法。
输入:
import cv2
import numpy as np
# set number of levels
levels = 4
# read input as img
img = cv2.imread('lena.png')
hh, ww, cc= img.shape
# create first layer of pyramid as copy of original
pyramid = [img.copy()]
# create black image of desired output size as:
# output height = height of input and output width = width + width/2 of input
outimage = np.zeros((hh, ww + ww // 2, 3), dtype=np.uint8)
# put img into outimage at top left corner
outimage[0:hh, 0:ww, :cc] = pyramid[0]
# create next level and add to pyramid and outimage
yoffset = 0
xoffset = ww
for i in range(1, levels):
img_small = cv2.pyrDown(pyramid[i-1])
ht, wd = img_small.shape[:2]
pyramid.append(img_small)
outimage[yoffset:yoffset + ht, xoffset:xoffset + wd ] = img_small
yoffset += ht
# save resulting output
cv2.imwrite('lena_pyramid.png', outimage)
# show results
cv2.imshow('INPUT', img)
cv2.imshow('OUTPUT', outimage)
cv2.waitKey(0)
结果:
关于python - 如何查看用OpenCV生成的图像的金字塔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60593425/