我正在尝试使用OpenCV 2.1将两个图像合并为一个,两个图像彼此相邻放置。在Python中,我正在做:
import numpy as np, cv
img1 = cv.LoadImage(fn1, 0)
img2 = cv.LoadImage(fn2, 0)
h1, w1 = img1.height,img1.width
h2, w2 = img2.height,img2.width
# Create an array big enough to hold both images next to each other.
vis = np.zeros((max(h1, h2), w1+w2), np.float32)
mat1 = cv.CreateMat(img1.height,img1.width, cv.CV_32FC1)
cv.Convert( img1, mat1 )
mat2 = cv.CreateMat(img2.height, img2.width, cv.CV_32FC1)
cv.Convert( img2, mat2 )
# Copy both images into the composite image.
vis[:h1, :w1] = mat1
vis[:h2, w1:w1+w2] = mat2
h,w = vis.shape
vis2 = cv.CreateMat(h, w, cv.CV_32FC3)
vis0 = cv.fromarray(vis)
cv.CvtColor(vis0, vis2, cv.CV_GRAY2BGR)
cv.ShowImage('test', vis2)
cv.WaitKey()
两个输入图像是:
https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/box.png?rev=2270
https://code.ros.org/trac/opencv/browser/trunk/opencv/samples/c/box_in_scene.png?rev=2270
生成的图像是:
可能很难与网站的其他部分区分开来,但大部分图像都是白色的,与单个图像的位置相对应。黑色区域没有写入图像数据。
为什么我的所有图像数据都会转换为白色?
答案 0 :(得分:83)
对于图像大小相同的情况(这是显示图像处理结果的常见情况),您可以使用numpy的连接来简化代码。
垂直堆叠(img1 over img2):
vis = np.concatenate((img1, img2), axis=0)
水平堆叠(img2左侧的img1):
vis = np.concatenate((img1, img2), axis=1)
验证:
import cv2
import numpy as np
img = cv2.imread('img.png')
vis = np.concatenate((img1, img2), axis=1)
cv2.imwrite('out.png', vis)
答案 1 :(得分:15)
对于那些希望将2张彩色图片合并为一张的人, 对安德烈的答案来说,这是一个微不足道的模式,对我有用:
img1 = cv2.imread(imageFile1)
img2 = cv2.imread(imageFile2)
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
#create empty matrix
vis = np.zeros((max(h1, h2), w1+w2,3), np.uint8)
#combine 2 images
vis[:h1, :w1,:3] = img1
vis[:h2, w1:w1+w2,:3] = img2
答案 2 :(得分:14)
import numpy as np, cv2
img1 = cv2.imread(fn1, 0)
img2 = cv2.imread(fn2, 0)
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
vis = np.zeros((max(h1, h2), w1+w2), np.uint8)
vis[:h1, :w1] = img1
vis[:h2, w1:w1+w2] = img2
vis = cv2.cvtColor(vis, cv2.COLOR_GRAY2BGR)
cv2.imshow("test", vis)
cv2.waitKey()
或者您更喜欢传统方式:
import numpy as np, cv
img1 = cv.LoadImage(fn1, 0)
img2 = cv.LoadImage(fn2, 0)
h1, w1 = img1.height,img1.width
h2, w2 = img2.height,img2.width
vis = np.zeros((max(h1, h2), w1+w2), np.uint8)
vis[:h1, :w1] = cv.GetMat(img1)
vis[:h2, w1:w1+w2] = cv.GetMat(img2)
vis2 = cv.CreateMat(vis.shape[0], vis.shape[1], cv.CV_8UC3)
cv.CvtColor(cv.fromarray(vis), vis2, cv.CV_GRAY2BGR)
cv.ShowImage("test", vis2)
cv.WaitKey()
答案 3 :(得分:3)
您还可以使用OpenCV的内置函数cv2.hconcat
和cv2.vconcat
,就像它们的名称一样,它们分别用于水平和垂直连接图像。
import cv2
img1 = cv2.imread('opencv/lena.jpg')
img2 = cv2.imread('opencv/baboon.jpg')
v_img = cv2.vconcat([img1, img2])
h_img = cv2.hconcat([img1, img2])
cv2.imshow('Horizontal', h_img)
cv2.imshow('Vertical', v_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
水平串联
垂直串联
答案 4 :(得分:0)
在OpenCV 3.0中,您可以轻松地按以下方式使用它:
#combine 2 images same as to concatenate images with two different sizes
h1, w1 = img1.shape[:2]
h2, w2 = img2.shape[:2]
#create empty martrix (Mat)
res = np.zeros(shape=(max(h1, h2), w1 + w2, 3), dtype=np.uint8)
# assign BGR values to concatenate images
for i in range(res.shape[2]):
# assign img1 colors
res[:h1, :w1, i] = np.ones([img1.shape[0], img1.shape[1]]) * img1[:, :, i]
# assign img2 colors
res[:h2, w1:w1 + w2, i] = np.ones([img2.shape[0], img2.shape[1]]) * img2[:, :, i]
output_img = res.astype('uint8')
答案 5 :(得分:0)
使用单行代码完成此操作的三种最佳方法
import cv2
import numpy as np
img = cv2.imread('Imgs/Saint_Roch_new/data/Point_4_Face.jpg')
dim = (256, 256)
resizedLena = cv2.resize(img, dim, interpolation = cv2.INTER_LINEAR)
X, Y = resizedLena, resizedLena
# Methode 1: Using Numpy (hstack, vstack)
Fusion_Horizontal = np.hstack((resizedLena, Y, X))
Fusion_Vertical = np.vstack((newIMG, X))
cv2.imshow('Fusion_Vertical using vstack', Fusion_Vertical)
cv2.waitKey(0)
# Methode 2: Using Numpy (contanate)
Fusion_Vertical = np.concatenate((resizedLena, X, Y), axis=0)
Fusion_Horizontal = np.concatenate((resizedLena, X, Y), axis=1)
cv2.imshow("Fusion_Horizontal usung concatenate", Fusion_Horizontal)
cv2.waitKey(0)
# Methode 3: Using OpenCV (vconcat, hconcat)
Fusion_Vertical = cv2.vconcat([resizedLena, X, Y])
Fusion_Horizontal = cv2.hconcat([resizedLena, X, Y])
cv2.imshow("Fusion_Horizontal Using hconcat", Fusion_Horizontal)
cv2.waitKey(0)
答案 6 :(得分:0)
为了水平堆叠:
imgHor = np.hstack((img, img))
为了垂直堆叠:
imgVer = np.vstack((img, img))
为了显示:
cv2.imshow("Horizontal", imgHor) # horizontal stack
cv2.imshow("Vertical", imgVer) # vertical stack