简体   繁体   English

如何在 Python OpenCV Cuda 中连接(合并)图像?

[英]How to concat (merge) images in Python OpenCV Cuda?

I am very newbie on this topic and I need same simplified answers.我是这个话题的新手,我需要相同的简化答案。 I am trying to concat/merge images together in GPU by using cv2.cuda.我正在尝试使用 cv2.cuda 在 GPU 中连接/合并图像。 I have understood that space needs to be allocate for new, merged image and then tiles can be placed by using CopyTo-function.我知道需要为新的合并图像分配空间,然后可以使用 CopyTo 函数放置图块。 I am not sure about the syntax how to do this correctly.我不确定如何正确执行此操作的语法。 Example with source 4 pics to be merged to 2 rows and two columns below.源 4 图片要合并到下面的 2 行和 2 列的示例。 But this do not work like this.但这不是这样工作的。 Can someone help with syntax or propose alternative method?有人可以帮助语法或提出替代方法吗?

import cv2

gpu_image01 = cv2.cuda_GpuMat()
gpu_image02 = cv2.cuda_GpuMat()
gpu_image03 = cv2.cuda_GpuMat()
gpu_image04 = cv2.cuda_GpuMat()

image01 = cv2.imread('img1.png')
image02 = cv2.imread('img2.png')
image03 = cv2.imread('img3.png')
image04 = cv2.imread('img4.png')

gpu_image01.upload(image01)
gpu_image02.upload(image02)
gpu_image03.upload(image03)
gpu_image04.upload(image04)

new_image = cv2.cuda_GpuMat(2, 2)

new_image.copyTo(gpu_image01,(0,0))
new_image.copyTo(gpu_image02,(0,1))
new_image.copyTo(gpu_image03,(1,0))
new_image.copyTo(gpu_image04,(1,1))

result = new_image.download()

Error code from the provided example above:上面提供的示例中的错误代码:

TypeError: Expected Ptr<cv::cuda::GpuMat::Allocator> for argument 'allocator'

I have tried different ways without success.我尝试了不同的方法但没有成功。 For example, if not try to specify matrix size, I get:例如,如果不尝试指定矩阵大小,我会得到:

TypeError: Expected Ptr<cv::UMat> for argument 'mask'

As suggested in comments, here is the answer to my question reflecting the example parameters.正如评论中所建议的,这里是我的问题的答案,反映了示例参数。

import cv2

gpu_image01 = cv2.cuda_GpuMat()
gpu_image02 = cv2.cuda_GpuMat()
gpu_image03 = cv2.cuda_GpuMat()
gpu_image04 = cv2.cuda_GpuMat()

image01 = cv2.imread('img1.png')
image02 = cv2.imread('img2.png')
image03 = cv2.imread('img3.png')
image04 = cv2.imread('img4.png')

gpu_image01.upload(image01)
gpu_image02.upload(image02)
gpu_image03.upload(image03)
gpu_image04.upload(image04)

new_image = cv2.cuda_GpuMat((200,100),gpu_image_resized_01.type())

gpu_image01.copyTo(new_image.rowRange(0, 50).colRange(0, 100))
gpu_image02.copyTo(new_image.rowRange(0, 50).colRange(100, 200))
gpu_image03.copyTo(new_image.rowRange(50, 100).colRange(0, 100))
gpu_image04.copyTo(new_image.rowRange(50, 100).colRange(100, 200))

result = new_image.download()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM