【问题标题】:The most efficient way to resize cv::Mat调整 cv::Mat 大小的最有效方法
【发布时间】:2015-08-01 11:16:35
【问题描述】:

全部

我正在编写一个图像处理程序,它在一帧中使用 cv::resize(INTER_LINEAR) 数百次。但是,我发现 cv::resize() 是一个 CPU 杀手,它是我程序中的热点。有没有更好的方法来使用更少的 CPU 来调整图像大小?

代码是这样的:

void process(const cv::Mat& frame) {
    for(int i = 0; i < COUNTS; ++i) {
        int new_rows = CalculateHeight();
        int new_cols = CalculateWidth();
        cv::Mat new_img;
        cv::resize(frame, new_mg, cv::Size(new_cols, new_rows));
        // ...
    }
    // ...
}

谢谢!

【问题讨论】:

  • 看看herepyramids
  • 但是pyrUp() & pyrDown() 对dst尺寸有要求,在我的程序中,不知道图片要缩放到什么尺寸。
  • 只需将行和列乘以调整大小。如果您向我们展示一些代码,您可能会得到更好的答案。
  • 我已经发布了代码。我采用了一些方法来获取新的行和列。
  • 这里的尺寸似乎在 for 循环中总是相同的......你能更好地解释一下每张图片需要哪些(以及多少个)不同的尺寸吗?

标签: c++ opencv optimization cpu-usage


【解决方案1】:

这是我使用 OpenCV 函数调整随机图像大小 10,000 次所做的一些测试的结果。最好的解决方案似乎是在调整大小之前转换为灰度(如果可能),使用 ROI 或滚动您自己的 ASM AVX 函数以使用每 1/3(或您需要的任何缩放因子)行和列调整大小。调整大小功能相当优化。

Colour
INTER_LINEAR 7953.89ms
INTER_LINEAR GPU 2252.72ms
INTER_LINEAR GPU MEMIO 23303.7ms
INTER_NEAREST 7297.58ms
INTER_NEAREST GPU 906.336ms
INTER_NEAREST GPU MEMIO 22374.1ms
BORDER_DEFAULT 47488.8ms
BORDER_REFLECT 47515.4ms
BORDER_REPLICATE 47516ms
BORDER_WRAP 47980.7ms
PYR GPU 4126.93ms

Grayscale
INTER_LINEAR 413.789ms
INTER_LINEAR GPU 1027.85ms
INTER_LINEAR GPU MEMIO 9568.99ms
INTER_NEAREST 978.89ms
INTER_NEAREST GPU 747.621ms
INTER_NEAREST GPU MEMIO 9346.28ms
BORDER_DEFAULT 19266.7ms
BORDER_REFLECT 19274.1ms
BORDER_REPLICATE 19300.8ms
BORDER_WRAP 19386.3ms
PYR GPU 2272.7ms


#include "opencv2/opencv.hpp"
#include "opencv2/cudaimgproc.hpp"
#include "opencv2/cudawarping.hpp"

#include <iostream>
#include <string>
#include <chrono>

using namespace std;
using namespace cv;

template <typename T>
double resizePerfEval(const Mat& frame, unsigned int n, T resizeFlag) {

    auto start = chrono::steady_clock::now();

    for (auto i = 0; i < n; i++) {
        Mat temp;
        resize(frame, temp, Size(), 0.5, 0.5, resizeFlag); 
    }

    return chrono::duration <double, milli>(chrono::steady_clock::now() - start).count();
}

template <typename T>
double pyramidPerfEval(const Mat& frame, unsigned int n, T border) {

    auto start = chrono::steady_clock::now();
    Size s(frame.cols / 2, frame.rows / 2);

    for (auto i = 0; i < n; i++) {
        Mat tmp;
        pyrDown(frame, tmp, s, border); 
    }

    return chrono::duration <double, milli>(chrono::steady_clock::now() - start).count();
}

template <typename T>
double resizePerfEvalGPU(const Mat& frame, unsigned int n, T resizeFlag, bool uploadDownload=false) {

    auto start = chrono::steady_clock::now();

    Mat tmp;
    cuda::GpuMat frame_d, temp;
    frame_d.upload(frame);

    for (auto i = 0; i < n; i++) {          

        cuda::resize(frame_d, temp, Size(), 0.5, 0.5, resizeFlag);
        if (uploadDownload) {
            temp.download(tmp);
            frame_d.upload(frame);
        }
    }

    return chrono::duration <double, milli>(chrono::steady_clock::now() - start).count();
}

double pyramidPerfEvalGPU(const Mat& frame, unsigned int n, bool uploadDownload = false) {

    auto start = chrono::steady_clock::now();

    Mat tmp;
    cuda::GpuMat frame_d, temp;
    frame_d.upload(frame);

    for (auto i = 0; i < n; i++) {      

        cuda::pyrDown(frame_d, temp);
        if (uploadDownload) {
            temp.download(tmp);
            frame_d.upload(frame);
        }

    }

    return chrono::duration <double, milli>(chrono::steady_clock::now() - start).count();
}


void runTest(const Mat& frame, unsigned int n) {

    cout << "INTER_LINEAR "     << resizePerfEval(frame, n, INTER_LINEAR) << "ms" << endl;
    cout << "INTER_LINEAR GPU " << resizePerfEvalGPU(frame, n, INTER_LINEAR) << "ms" << endl;
    cout << "INTER_LINEAR GPU MEMIO " << resizePerfEvalGPU(frame, n, INTER_LINEAR, true) << "ms" << endl;

    cout << "INTER_NEAREST "    << resizePerfEval(frame, n, INTER_NEAREST) << "ms" << endl;
    cout << "INTER_NEAREST GPU "    << resizePerfEvalGPU(frame, n, INTER_NEAREST) << "ms" << endl;
    cout << "INTER_NEAREST GPU MEMIO " << resizePerfEvalGPU(frame, n, INTER_NEAREST, true) << "ms" << endl;

    cout << "BORDER_DEFAULT "   << pyramidPerfEval(frame, n, BORDER_DEFAULT) << "ms" << endl;
    cout << "BORDER_REFLECT "   << pyramidPerfEval(frame, n, BORDER_REFLECT) << "ms" << endl;
    cout << "BORDER_REPLICATE " << pyramidPerfEval(frame, n, BORDER_REPLICATE) << "ms" << endl;
    cout << "BORDER_WRAP "      << pyramidPerfEval(frame, n, BORDER_WRAP) << "ms" << endl;
    cout << "PYR GPU "          << pyramidPerfEvalGPU(frame, n) << "ms" << endl;

}

int main(int argc, char* argv[])
{

    Mat gsframe, frame = Mat::ones(Size(1920, 1080), CV_8UC3);
    randu(frame, Scalar::all(0), Scalar::all(255));
    cvtColor(frame, gsframe, CV_BGR2GRAY);
    auto n = 10000;

    cout << "Colour" << endl;
    runTest(frame, n);

    cout << endl << "Grayscale" << endl;
    runTest(gsframe, n);    

    return 0;
}

如果算法在 PC 上运行,另一种方法是在启用 CUDA 的 GPU 上调整大小。但是,您在选择卡时必须小心,因为您需要足够高的内存带宽以适应从 GPU 内存上传和下载图像所花费的时间。

请注意,当图像在 GPU 内存上不可用时,CPU 在灰度上优于 GPU。如果图像在 GPU 内存上可用,那么对于 Color,它使用 GPU 的速度提高了 3.5 倍(特别是对于非常大的图像尺寸)。对于高端应用,可以使用带有 GPUDirect 的 NVIDIA 采集卡来实现这一点。

在 Xeon E5 v2 @ 3.0Ghz 680GTX 上进行了基准测试

【讨论】:

    猜你喜欢
    • 2016-12-01
    • 1970-01-01
    • 2023-04-05
    • 2014-06-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    相关资源
    最近更新 更多