如何使用python和opencv连接不同形状的图像?
我有一些图片(比如5张),每一张都有不同的形状。我想为我的项目报告连接成一个图像。您能提供一个使用opencv和python的简单方法吗 生成的图像与下图类似 在numpy中,我尝试了类似的方法,但只适用于两幅图像如何使用python和opencv连接不同形状的图像?,python,numpy,opencv,matplotlib,opencv3.0,Python,Numpy,Opencv,Matplotlib,Opencv3.0,我有一些图片(比如5张),每一张都有不同的形状。我想为我的项目报告连接成一个图像。您能提供一个使用opencv和python的简单方法吗 生成的图像与下图类似 在numpy中,我尝试了类似的方法,但只适用于两幅图像 r = np.concatenate((images[1][:, :, 1], images[1][:, :, 3]), axis=1) 获取屏幕截图中显示的结果可能需要更多的修改,但只需将图像堆叠在一起即可完成以下操作: import cv2 import numpy as np
r = np.concatenate((images[1][:, :, 1], images[1][:, :, 3]), axis=1)
获取屏幕截图中显示的结果可能需要更多的修改,但只需将图像堆叠在一起即可完成以下操作:
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)
基本思想是首先找到图像的总大小,然后创建一个该大小的数组,最后在向下迭代(或横向迭代,取决于您想要的)时将这些范围内的像素设置为每个单独图像的像素
您还可以在希望开始另一行或列时实现阈值。获得屏幕截图中显示的结果可能需要更多的修改,但只需将图像堆叠在彼此的顶部即可完成如下操作:
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)
基本思想是首先找到图像的总大小,然后创建一个该大小的数组,最后在向下迭代(或横向迭代,取决于您想要的)时将这些范围内的像素设置为每个单独图像的像素
您还可以在需要启动另一行或列时实现阈值。我修改了代码,使其成为一个简单的函数,可能对其他人有用
def get_one_image(images):
img_list = []
padding = 200
for img in images:
img_list.append(cv2.imread(img))
max_width = []
max_height = 0
for img in img_list:
max_width.append(img.shape[0])
max_height += img.shape[1]
w = np.max(max_width)
h = max_height + padding
# create a new array with a size large enough to contain all the images
final_image = np.zeros((h, w, 3), dtype=np.uint8)
current_y = 0 # keep track of where your current image was last placed in the y coordinate
for image in img_list:
# 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('out.png', final_image)
我修改了代码,使之成为一个简单的函数,可能对其他人有用
def get_one_image(images):
img_list = []
padding = 200
for img in images:
img_list.append(cv2.imread(img))
max_width = []
max_height = 0
for img in img_list:
max_width.append(img.shape[0])
max_height += img.shape[1]
w = np.max(max_width)
h = max_height + padding
# create a new array with a size large enough to contain all the images
final_image = np.zeros((h, w, 3), dtype=np.uint8)
current_y = 0 # keep track of where your current image was last placed in the y coordinate
for image in img_list:
# 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('out.png', final_image)
对的解决方案的这一修改对我起了作用。此函数接收图像列表并输出单个图像,其中所有输入图像垂直堆叠:
def get_one_image(img_list):
max_width = 0
total_height = 200 # padding
for img in img_list:
if img.shape[1] > max_width:
max_width = img.shape[1]
total_height += img.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 img_list:
# add an image to the final array and increment the y coordinate
image = np.hstack((image, np.zeros((image.shape[0], max_width - image.shape[1], 3))))
final_image[current_y:current_y + image.shape[0], :, :] = image
current_y += image.shape[0]
return final_image
对的解决方案的这一修改对我起了作用。此函数接收图像列表并输出单个图像,其中所有输入图像垂直堆叠:
def get_one_image(img_list):
max_width = 0
total_height = 200 # padding
for img in img_list:
if img.shape[1] > max_width:
max_width = img.shape[1]
total_height += img.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 img_list:
# add an image to the final array and increment the y coordinate
image = np.hstack((image, np.zeros((image.shape[0], max_width - image.shape[1], 3))))
final_image[current_y:current_y + image.shape[0], :, :] = image
current_y += image.shape[0]
return final_image