gpt4 book ai didi

OpenCV : What is the real performance difference between continuous and non-continuous matrices?

转载 作者:太空宇宙 更新时间:2023-11-03 21:58:34 26 4
gpt4 key购买 nike

目前我正在尝试了解 openCV 中连续矩阵和非连续矩阵之间的一些区别。有人向我建议连续矩阵提供更好的性能,因为程序不必在每一列的末尾跳回到下一行的开头。

简而言之,连续和非连续矩阵之间的可比较性能差异是什么?

最佳答案

好吧,这主要取决于您将如何使用矩阵。如果您无论如何都要进行大量“跳跃”——这不会有太大区别,但在“连续”用例中,它会影响百分之几十。

下面的例子(简单地移动了矩阵值)给出了一个输出:

image.isContinuous() = 1
roi.isContinuous() = 0
image: 0.0162504 s
roi: 0.0219723 s
Sanity check: OK

大约有 30% 的差异。您的里程数会因硬件和实际用例而异。

来源(注意第一个循环在那种情况下如何简单得多):

#include <opencv2/core/core.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
int cols = 4096;
int rows = 4096;
int scale = 2;

Mat image(rows, cols, CV_8UC1);
Mat image_big(rows * scale, cols * scale, CV_8UC1);
Mat roi = image_big(Rect(0, 0, cols, rows));

randu(image, 0, 255);
image.copyTo(roi);

cout << "image.isContinuous() = " << image.isContinuous() << "\n" << "roi.isContinuous() = " << roi.isContinuous() << endl;

{
cout << "image: ";
double start = getTickCount();
for (int i = 1; i < image.total(); i++)
{
image.data[i - 1] = image.data[i];
}
cout << (getTickCount() - start)/getTickFrequency() << " s" << endl;
}

{
cout << "roi: ";
double start = getTickCount();
for (int y = 0; y < roi.cols; y++)
{
if (y != 0) {
roi.ptr<char>(y-1)[roi.cols-1] = roi.ptr<char>(y)[0];
}

for (int x = 1; x < roi.rows; x++)
{
roi.ptr<char>(y)[x - 1] = roi.ptr<char>(y)[x];
}
}
cout << (getTickCount() - start)/getTickFrequency() << " s" << endl;
}

cout << "Sanity check: " << (countNonZero(image - roi) ? "FAIL" : "OK") << endl;
}

关于OpenCV : What is the real performance difference between continuous and non-continuous matrices?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18849357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com