如何在Python OpenCV中连接两个矩阵?

Fli*_*imm 8 python opencv

如何将两个矩阵连接成一个矩阵?得到的矩阵应该具有与两个输入矩阵相同的高度,并且其宽度将等于两个输入矩阵的宽度之和.

我正在寻找一个预先存在的方法,将执行相当于此代码:

def concatenate(mat0, mat1):
    # Assume that mat0 and mat1 have the same height
    res = cv.CreateMat(mat0.height, mat0.width + mat1.width, mat0.type)
    for x in xrange(res.height):
        for y in xrange(mat0.width):
            cv.Set2D(res, x, y, mat0[x, y])
        for y in xrange(mat1.width):
            cv.Set2D(res, x, y + mat0.width, mat1[x, y])
    return res
Run Code Online (Sandbox Code Playgroud)

Abi*_*n K 10

如果您正在使用cv2,(那么您将获得Numpy支持),您可以使用Numpy函数np.hstack((img1,img2))来执行此操作.

例如:

import cv2
import numpy as np

# Load two images of same size
img1 = cv2.imread('img1.jpg')
img2 = cv2.imread('img2.jpg')

both = np.hstack((img1,img2))
Run Code Online (Sandbox Code Playgroud)