如何在Python OpenCV中连接两个矩阵? [英] How do I concatenate two matrices in Python OpenCV?

查看:183
本文介绍了如何在Python OpenCV中连接两个矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I concatenate two matrices into one matrix? The resulting matrix should have the same height as the two input matrices, and its width will equal the sum of the width of the two input matrices.

我正在寻找一种将执行与以下代码等效的方法:

I am looking for a pre-existing method that will perform the equivalent of this code:

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

推荐答案

如果您使用的是cv2(那么您将获得Numpy支持),则可以使用Numpy函数np.hstack((img1,img2))来做到这一点.

If you are using cv2, (you will get Numpy support then), you can use Numpy function np.hstack((img1,img2)) to do this.

例如:

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))

这篇关于如何在Python OpenCV中连接两个矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆