python - 如何使用 python 和 opencv 连接不同形状的图像?

标签 python numpy opencv matplotlib opencv3.0

我有一些图片(比如 5 张),每张图片都有不同的形状。我想为我的项目报告连接成一张图片。您能否提供一种使用 opencv 和 python 的简单方法?

生成的图像类似于下图。

在 numpy 中我试过这样的东西,它有效但只适用于两个图像。

r = np.concatenate((images[1][:, :, 1], images[1][:, :, 3]), axis=1)

enter image description here

最佳答案

获得您在屏幕截图中显示的结果可能需要更多的修改,但只需将图像堆叠在彼此之上就可以像这样完成:

import cv2
import numpy as np

image_names = ['original_field_1_0.PNG','original_field_1_1.PNG','original_field_1_3.PNG','original_field_1_4.PNG','original_field_1_5.PNG']
images = []
max_width = 0 # find the max width of all the images
total_height = 0 # the total height of the images (vertical stacking)

for name in image_names:
    # open all images and find their sizes
    images.append(cv2.imread(name))
    if images[-1].shape[1] > max_width:
        max_width = images[-1].shape[1]
    total_height += images[-1].shape[0]

# create a new array with a size large enough to contain all the images
final_image = np.zeros((total_height,max_width,3),dtype=np.uint8)

current_y = 0 # keep track of where your current image was last placed in the y coordinate
for image in images:
    # add an image to the final array and increment the y coordinate
    final_image[current_y:image.shape[0]+current_y,:image.shape[1],:] = image
    current_y += image.shape[0]

cv2.imwrite('fin.PNG',final_image)

基本思想是首先找到图像的总大小,然后创建一个该大小的数组,最后将这些范围内的像素设置为每个单独图像的像素,同时向下(或侧向,取决于你想要什么) ).

您还可以为何时开始另一行或另一列实现阈值。

关于python - 如何使用 python 和 opencv 连接不同形状的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47254294/

相关文章:

python - 如何在 ipython 启动时将目录添加到 sys.path

python - Pandas如何打开里面有表情符号的csv

python - Python中如何根据属性选择子类

python - numpy.random.choice 如何与替换一起使用?

javascript - 如何得到N个和等于M的随机整数

windows - 如何将 cv.imwrite 修复为我没有收到错误,并且图像的副本保存在我想要的文件夹中

python - 添加一个计数器到 Django 管理主页

python - Numba、Jitclass w/nopython 模式和字典

windows - FFMPEG 命令合并 WAV 文件和视频文件?

python - 在视频中每秒绘制一个圆圈