当灰度图像宽度为2048,4096时,旋转90图像的运行时间比附近

时间:2018-04-18 05:45:49

标签: image opencv rotation

现在我要使用openCV Mat结构将图像旋转90度,代码在下面。 而且我发现了一种现象,当图像宽度为4096时,运行时间约为图像宽度运行时间的两倍,为4097.以下是运行时间输出。有人知道为什么吗?

#include<string>
#include<fstream>
#include<iostream>
#include "opencv.hpp"
using namespace cv;
void main()
{
std::string ImageFileName0 = "F:\\temp\\4096-3000.bmp";
std::string ImageFileName1 = "F:\\temp\\4097-3000.bmp";

Mat image4096 = imread(ImageFileName0);
Mat image4097 = imread(ImageFileName1);

int lWidth4096 = image4096.size().width;
int lHeight4096 = image4096.size().height;
Mat image4096Res(lWidth4096, lHeight4096, CV_8UC1);
for (int i=0; i<lHeight4096; i++)
{
    for (int j=0; j<lWidth4096;j++)
    {
        image4096Res.at<uchar>(j, i) = 0;
    }
}

int lWidth4097 = image4097.size().width;
int lHeight4097 = image4097.size().height;
Mat image4097Res(lWidth4097, lHeight4097, CV_8UC1);
for (int i = 0; i < lHeight4097; i++)
{
    for (int j = 0; j < lWidth4097; j++)
    {
        image4097Res.at<uchar>(j, i) = 0;
    }
}

for (int i = 0; i < 10; i++)
{
    double time0 = static_cast<double>(getTickCount());
    for (int j = 0; j < lWidth4096; ++j)
    {
        for (int i = 0; i<lHeight4096; ++i)
        {
            image4096Res.at<uchar>(j, i) = image4096.at<uchar>(i, j);
        }
    }
    double time4096 = (static_cast<double>(getTickCount()) - time0) / getTickFrequency();

    double time1 = static_cast<double>(getTickCount());
    for (int j = 0; j < lWidth4097; ++j)
    {
        for (int i = 0; i<lHeight4097; ++i)
        {
            //srcTmp = srcimage.PointToRow(i);
            //*(dstTmp + i) = srcimage.GetPixelValue(j, i);
            image4097Res.at<uchar>(j, i) = image4097.at<uchar>(i, j);

        }
    }
    double time4097 = (static_cast<double>(getTickCount()) - time1) / getTickFrequency();
    std::cout << "4096 time:" << time4096*1000 << std::endl;
    std::cout << "4097 time:" << time4097*1000 << std::endl;
    std::cout << std::endl;
}
namedWindow("aa",CV_WINDOW_NORMAL);
imshow("aa", image4096Res);

waitKey();
}

运行时间结果:

4096时间:149.337

4097时间:56.8092

4096时间:143.556

4097时间:67.4758

4096时间:142.07

4097时间:58.2825

4096时间:153.973

4097时间:57.1894

4096时间:145.086

4097时间:58.7944

4096时间:156.33

4097时间:87.9404

4096时间:140.224

4097时间:56.9525

4096时间:144.413

4097时间:57.133

4096时间:141.672

4097时间:54.916

4096时间:148.443

4097时间:55.8449

Time consuming varies with the width of the image is here

很明显,只有当图像宽度为1024,2048,4096时,执行时间才会异常,而整体趋势会线性增加。

1 个答案:

答案 0 :(得分:0)

您是否可以尝试使用此版本来查看是否存在相同的差异 还添加了测试以查看加载的图像是否为CV_8UC1类型。 请注意,您和我的解决方案的结果是镜像图像

int lWidth4096 = image4096.size().width;
int lHeight4096 = image4096.size().height;
Mat image4096Res = cv::Mat::zeros(lWidth4096, lHeight4096, CV_8UC1);

int lWidth4097 = image4097.size().width;
int lHeight4097 = image4097.size().height;
Mat image4097Res = cv::Mat::zeros(lWidth4097, lHeight4097, CV_8UC1);

if (image4096.type() != CV_8UC1)
    throw "need 8bit image as input";

if (image4097.type() != CV_8UC1)
    throw "need 8bit image as input";

for (int i = 0; i < 10; i++)
{
    double time0 = static_cast<double>(getTickCount());
    for (int j = 0; j < lWidth4096; ++j)
    {
        uint8_t *buf = image4096Res.ptr(j);
        for (int i = 0; i<lHeight4096; ++i)
        {
            //image4096Res.at<uchar>(j, i) = image4096.at<uchar>(i, j);
            //buf[i] = image4096.at<uchar>(i, j);
            buf[i] = (image4096.data + image4096.step[0] * i)[j];
        }
    }
    double time4096 = (static_cast<double>(getTickCount()) - time0) / getTickFrequency();

    double time1 = static_cast<double>(getTickCount());
    for (int j = 0; j < lWidth4097; ++j)
    {
        uint8_t *buf = image4097Res.ptr(j);
        for (int i = 0; i<lHeight4097; ++i)
        {
            //image4097Res.at<uchar>(j, i) = image4097.at<uchar>(i, j);
            //buf[i] = image4097.at<uchar>(i, j);
            buf[i] = (image4097.data + image4097.step[0] * i)[j];
        }
    }
    double time4097 = (static_cast<double>(getTickCount()) - time1) / getTickFrequency();
    std::cout << "4096 time:" << time4096 * 1000 << std::endl;
    std::cout << "4097 time:" << time4097 * 1000 << std::endl;
    std::cout << std::endl;
}