首页 > 解决方案 > Umat 很慢(OpenCV、Python)

问题描述

我一直在修补 OpenCV 以调整大量 (100k+) 图像 (16-24MP) 的大小。不知何故,使用 CPU 似乎总是快 30-50% 左右。当我运行 ryzen 1700x 和 1080ti 时,我期望它会反过来。如果有人能给我提示我做错了什么,那就太好了。我正在运行 OpenCV 4.0.0.pre 和 OpenCL 1.2

#!/usr/bin/python
import numpy as np
import cv2 as cv
import glob
from multiprocessing import Pool
import time
path =''
dic=[]
def resizer(file):
    img = cv.imread(file)
    height, width = img.shape[:2]

    dim = float(width)/float(height)
    if dim > 1:
        width=4000
        height= 4000/dim
        start_time = time.time()
        res = cv.resize(cv.UMat(img), (int(width), int(height)), interpolation=cv.INTER_CUBIC)
        print("--- %s seconds ---" % (time.time() - start_time))
    else:
        width=4000*dim
        height= 4000
        start_time = time.time()
        res = cv.resize(cv.UMat(img), (int(width), int(height)), interpolation=cv.INTER_CUBIC)
        print("--- %s seconds ---" % (time.time() - start_time))
    name=file.split('/')[-1]
    cv.imwrite('/small/{}'.format(name), res)

for file in glob.glob(path+'*.JPG'):
    dic.append(file)

if __name__ == "__main__":
    pool=Pool(16)
    pool.map(resizer, dic)
    pool.terminate()

标签: pythonopencvgpucpuimage-resizing

解决方案


对于像调整大小这样的计算简单的任务,将数据传送到 GPU 内存并再次返回所需的时间比通过更快的计算保存的时间要长。

特别是因为 openCV 将在 CPU 上使用并行 CPU 内核和长指令字 SIMD 优化汇编程序来调整大小。


推荐阅读