将两个图像与OpenCV组合 [英] Combining Two Images with OpenCV

查看:460
本文介绍了将两个图像与OpenCV组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用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

#创建一个足够大的数组,以保持两个图像彼此相邻。
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)

#将两个图像复制到合成图像中。
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测试',vis2)
cv.WaitKey()

两个输入图像是: / p>

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



生成的图片是:





这可能很难与网站的其他部分区分开来,但是大部分图片都是白色的,对应于单个图片应该在哪里。



为什么我所有的图像数据都转换为白色?

解决方案

对于图像大小相同(这是显示图像处理结果的常见情况)的情况,可以使用numpy的concatenate来简化代码。



垂直堆叠(img1 over img2):

  vis = np.concatenate (img1,img2),axis = 0)

水平堆叠:

  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)


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?

解决方案

For cases where your images happen to be the same size (which is a common case for displaying image processing results), you can use numpy's concatenate to simplify your code.

To stack vertically (img1 over img2):

vis = np.concatenate((img1, img2), axis=0)

To stack horizontally (img1 to the left of img2):

vis = np.concatenate((img1, img2), axis=1)

To verify:

import cv2
import numpy as np
img = cv2.imread('img.png')
vis = np.concatenate((img1, img2), axis=1)
cv2.imwrite('out.png', vis)

这篇关于将两个图像与OpenCV组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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