I'm trying to use OpenCV 2.1 to combine two images into one, with the two images placed adjacent to each other. In Python, I'm doing:
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()
The two input images are:
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
The resulting image is:
It may be hard to distinguish from the rest of the site, but most of the image is white, corresponding to where the individual images should be. The black area is where no image data was written.
Why is all my image data being converted to white?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…