How to concatenate three or more images vertically?

元气小坏坏 提交于 2019-12-24 11:24:02

问题


I try to concatenate three images vertically and I need some assistance with the code/function. So far I imported one image and cropped 3 smaller images with the same size. Now I want to concatenate them in one image that will be long, but narrow. However, I can't find an appropriate function or even if I find one I get an error message when I apply it to my code.

I already tried to make a collection from my three pictures and then use the function skimage.io.concatenate_images(sf_collection), but this results in a 4-dimensional picture that cannot be visualized.

sf_collection = (img1,img2,img3)
concat_page = skimage.io.concatenate_images(sf_collection)

My expected output is the three images to be concatenated vertically in one image (very long and narrow).


回答1:


Ive never used skimage.io.concatenate, but I think you are looking for np.concatenate. It defaults to axis=0, but you can specify axis=1 for a horizontal stack. This also assumes you have already loaded the images into their array from.

from scipy.misc import face
import numpy as np
import matplotlib.pyplot as plt

face1 = face()
face2 = face()
face3 = face()

merge = np.concatenate((face1,face2,face3))

plt.gray()
plt.imshow(merge)

which returns:

If you look at the skimage.io.concatenate_images docs, it's using np.concatenate too. It seems like that function provides a data structure to hold collections of images, but not merge into a single image.




回答2:


Like this:

import numpy as np

h, w = 100, 400

yellow = np.zeros((h,w,3),dtype=np.uint8) + np.array([255,255,0],dtype=np.uint8)
red    = np.zeros((h,w,3),dtype=np.uint8) + np.array([255,0,0],dtype=np.uint8)
blue   = np.zeros((h,w,3),dtype=np.uint8) + np.array([0,0,255],dtype=np.uint8)

# Stack vertically
result = np.vstack((yellow,red,blue))

Use the following to stack side-by-side (horizontally):

result = np.hstack((yellow,red,blue))


来源:https://stackoverflow.com/questions/56777297/how-to-concatenate-three-or-more-images-vertically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!