Flimm
Flimm

Reputation: 151203

How do I concatenate two matrices in 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

Upvotes: 10

Views: 14467

Answers (4)

Abid Rahman K
Abid Rahman K

Reputation: 52646

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

eg :

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

Upvotes: 13

Froyo
Froyo

Reputation: 18487

You should use OpenCV. Legacy uses cvmat. But numpy arrays are really easy to work with.

As suggested by @abid-rahman-k, you can use hstack(which I didn't know about) so I had used this.

h1, w1 = img.shape[:2]
h2, w2 = img1.shape[:2]
nWidth = w1+w2
nHeight = max(h1, h2)
hdif = (h1-h2)/2
newimg = np.zeros((nHeight, nWidth, 3), np.uint8)
newimg[hdif:hdif+h2, :w2] = img1
newimg[:h1, w2:w1+w2] = img

But if you want to work with Legacy code, this should help

Let's assume that height of img0 is greater than height of image

nW = img0.width+image.width
nH = img0.height
newCanvas = cv.CreateImage((nW,nH), cv.IPL_DEPTH_8U, 3)
cv.SetZero(newCanvas)
yc = (img0.height-image.height)/2
cv.SetImageROI(newCanvas,(0,yc,image.width,image.height))
cv.Copy(image, newCanvas)
cv.ResetImageROI(newCanvas)
cv.SetImageROI(newCanvas,(image.width,0,img0.width,img0.height))
cv.Copy(img0,newCanvas)
cv.ResetImageROI(newCanvas)

Upvotes: 3

Jeru Luke
Jeru Luke

Reputation: 21223

OpenCV has in-built functions for concatenating images vertically/horizontally:

  • cv2.vconcat()
  • cv2.hconcat()

Note: While concatenating, images must be of the same dimensions or else you will come across an error message similar to: error: (-215:Assertion failed)....

Code:

img = cv2.imread('flower.jpg', 1)

# concatenate images vertically    
vertical_concat = cv2.vconcat([img, img])

enter image description here

# concatenate images horizontally
horizontal_concat = cv2.hconcat([img, img])

enter image description here

Upvotes: 2

don_q
don_q

Reputation: 415

I know this question is old, but I stumbled across it because I was looking to concatenate arrays that are two dimensions ( not just concatenate in 1 dimension ).

np.hstack will not do this.

Assuming you have two 640x480 images that are simply two dimensions use dstack.

a = cv2.imread('imgA.jpg')
b = cv2.imread('imgB.jpg')

a.shape            # prints (480,640)
b.shape            # prints (480,640)

imgBoth = np.dstack((a,b))
imgBoth.shape      # prints (480,640,2)

imgBothH = np.hstack((a,b))
imgBothH.shape     # prints (480,1280)  
                   # = not what I wanted, first dimension not preserverd

Upvotes: 1

Related Questions