python - 如何查看用OpenCV生成的图像的金字塔?

标签 python numpy opencv

我需要使用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)

结果是:
wrong picture

但是我需要图像看起来像这样:

Right picture

我怎样才能做到这一点?

最佳答案

这是在Python / OpenCV中执行此操作的一种方法。

输入:

enter image description here

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)

结果:

enter image description here

关于python - 如何查看用OpenCV生成的图像的金字塔?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60593425/

相关文章:

python - 如何在 Databricks 中检索实验中的 model.pkl

python - 尝试使用 ElasticSearch 后端更新索引时出错

math - Opencv 中的线性回归?

winapi - 错误 D8016 : '/ZI' and '/clr' command-line options are incompatible

python - 使用 OpenCV 和 ROS 时卡住黑色图像和轨迹栏

python - 如何过滤复合键?

javascript - Python:发送 JSON 数据时使用 POST 请求的 FastAPI 错误 422

python - numpy 将数组元素与另一个数组相乘

python - 使用 PCA 计算原始数据集和转换后的数据集之间丢失的数据

python - 如何计算 pandas 中 n 列而不是行之间的差异