[英]How to merge multiple images into one image using opencv python
I am trying to merge images while iterating through a function, the function iterates through the folder get each image, now I am trying to merge all the images while iterating and merge them into one single image.我试图在遍历 function 时合并图像,function 遍历文件夹获取每个图像,现在我试图在迭代时合并所有图像并将它们合并为一个图像。
directories = [ x for x in os.listdir('.') if os.path.isdir(x) ]
image_list=[]
images = []
def randomFile(directory):
files_list = os.listdir(directory)
random_num = random.choice(files_list)
print(random_num,directory)
img_1=cv2.imread((os.path.join(directory,random_num)))
if img_1 is not None:
images.append(img_1)
files_list.remove(random_num)
img1_g_noise = cv2.merge(images)
return img1_g_noise
for x in directories[1:]:
randomFile(x)
cv2.imshow("img1_g_noise",randomFile(x))
cv2.waitKey(0)
I am able to access all the images but not been able to merge into one single image, the list images=[] contains all the pixels of all three images我能够访问所有图像但无法合并为一个图像,列表 images=[] 包含所有三个图像的所有像素
In Python/OpenCV/Numpy, if all 4 images are the same dimensions, then, if input is img1, img2, img3, img4, the following will make an image that is a 2x2 collage of the input images.在 Python/OpenCV/Numpy 中,如果所有 4 个图像的尺寸相同,那么,如果输入是 img1、img2、img3、img4,则以下将生成一个图像,它是输入图像的 2x2 拼贴。
img12 = np.hstack((img1, img2))
img34 = np.hstack((img3, img4))
result = np.vstack((img12, img34))
If the images are not the same size, then either crop or resize them to the same size.如果图像大小不同,则将它们裁剪或调整为相同大小。
You can try replacing img1_g_noise = cv2.merge(images) with:您可以尝试将 img1_g_noise = cv2.merge(images) 替换为:
img1_g_noise = cv2.cvtColor(images[0], cv2.COLOR_RGB2RGBA)
images.pop(0)
for img in images:
img1_g_noise = cv2.addWeighted(img1_g_noise,0.5,cv2.cvtColor(img, cv2.COLOR_RGB2RGBA),0.5,0)
return cv2.cvtColor(img1_g_noise, cv2.COLOR_RGBA2RGB)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.