用pycuda和opencv读取并行图像

问题描述

我有一个程序,每次迭代都读取图像:

for h in range(H):
    for w in range(W):
        z = depth_map[h,w]
        val = content_map[h,w]
        img = cv2.imread('h' + str(h) + '_w' + str(w) + '_z' + str(z) + '.png')
        res[h-grid_h:h+grid_h+1,w-grid_w:w+grid_w+1] = val * img

我想并行执行此操作,目前我正在使用cpu多重处理并发送H范围,并最终将它们全部求和。

pool = multiprocessing.Pool(processes=num_of_processes)
x = pool.map(func,hrange)
pool.close()
pool.join()

这非常有用,但是,由于操作符简单(仅读取和乘法),我相信gpu可能会显着加速它,这对我来说非常重要。

我发现pycuda在gpu多重处理中很有帮助: https://documen.tician.de/pycuda/

具体来说,是用C ++编写的基础层,例如从doc:

import pycuda.autoinit
import pycuda.driver as drv
import numpy

from pycuda.compiler import SourceModule
mod = SourceModule("""
__global__ void multiply_them(float *dest,float *a,float *b)
{
  const int i = threadIdx.x;
  dest[i] = a[i] * b[i];
}
""")

multiply_them = mod.get_function("multiply_them")

我想使用该层并对输入h和w进行操作,这可以通过执行以下操作来实现:

Mat img = imread(image_path,IMREAD_COLOR)

但是它需要包括:

#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;

我收到以下错误:

b'/tmp/tmpkj_doq74.cu:3:14: fatal error: opencv2/core.hpp: No such file or directory\n     #include <opencv2/core.hpp>\n              ^~~~~~~~~~~~~~~~~~\ncompilation terminated.\n']

使用pycuda时如何导入该库?

或者,我可以使用python cv2代替C ++进行并行计算吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...