opencv(C++)四种不同访问方式及速度对比

2021-09-02  本文已影响0人  1037号森林里一段干木头

摘要:

比较一下opencv四种不同的访问方式的效率,多次测试的结论就是用指针的方式是最快的,方法2、3、4都是指针的方式,在release模式下,方法2、方法3、方法4很接近,没多少差别,在不同尺度下稍微各有一点点优劣,我个人常用方法3,因为它简洁高效,方法2看起来似乎也不错,方法4需要图像数据是连续的才能用。

假设:图像image为3通道8bit的图像,现在要访问它的第row行,第col列的r,g,b值

1. 方法一:Mat.at<vec>(row,col)

//Mat.at<vec>(i,j)方式访问
cv::Vec3b bgr = image.at<cv::Vec3b>(row, col);
blue  = bgr[0];
green = bgr[1];
red   = bgr[2];

2. 方法二:Mat.ptr<vec>(row)

//Mat.at<vec>(i,j)方式访问
cv::Vec3b *bgr = image.ptr<cv::Vec3b>(row);
blue  = bgr[col][0];
green = bgr[col][1];
red   = bgr[col][2];

3. 方法三:Mat.ptr<uchar>(row)

uchar *ptr = image.ptr<uchar>(row);//获得图像第row行的首地址
blue  = *(ptr + col*3 );
green = *(ptr + col*3 + 1);
red   = *(ptr + col*3 + 2);

4. 方法四:Mat.data + 偏移量

//image.data是图像矩阵的首地址
uchar *ptr = image.data + row * image.cols * 3 +  col*3;
blue  = *(ptr);
green = *(ptr + 1);
red   = *(ptr + 2);

注:其他数据类型的访问可参考https://www.jianshu.com/p/cfe373dc8c95

测试

图片尺寸427 * 640 * 3,重复颜色反转操作99次,显示平均时间在图片上

完整源码

#include "opencv.hpp"
#include "imageProcess.h"

#define TIMES 1

//Mat.at<vec>(i,j)方式访问
void method_1(cv::Mat image)
{
    //cv::Mat image = image_.clone();
    double startTime = cv::getTickCount();
    int w = image.cols;
    int h = image.rows;
    for (int times = 0; times < TIMES; times++)
    {
        for (int row = 0; row < h; row++)
        {
            for (int col = 0; col < w; col++)
            {
                image.at<cv::Vec3b>(row, col)[0] = 255 - image.at<cv::Vec3b>(row, col)[0];
                image.at<cv::Vec3b>(row, col)[1] = 255 - image.at<cv::Vec3b>(row, col)[1];
                image.at<cv::Vec3b>(row, col)[2] = 255 - image.at<cv::Vec3b>(row, col)[2];
            }
        }
    }
    double endTime = cv::getTickCount();
    double t = ((endTime - startTime) / cv::getTickFrequency()) * 1000 / TIMES;
    std::ostringstream ss;
    ss << "Execute time : " << std::fixed << std::setprecision(2) << t << " ms ";
    putText(image, ss.str(), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(0, 0, 255), 2, 8);
    imshow("method1", image);
}

//Mat.ptr<vec3b>(row);
void method_2(cv::Mat image)
{
    //cv::Mat image = image_.clone();
    //cv::imshow("2", image);
    double startTime = cv::getTickCount();
    int w = image.cols;
    int h = image.rows;
    cv::Vec3b* curr=NULL;
    for (int times = 0; times < TIMES; times++)
    {
        for (int row = 0; row < h; row++) 
        {
            curr = image.ptr<cv::Vec3b>(row);
            for (int col = 0; col < w; col++) 
            {
                curr[col][0] = 255 - curr[col][0];
                curr[col][1] = 255 - curr[col][1];
                curr[col][2] = 255 - curr[col][2];
            }
        }
    }
    
    double endTime = cv::getTickCount();
    double t = ((endTime - startTime) / cv::getTickFrequency()) * 1000 / TIMES;
    std::ostringstream ss;
    ss << "Execute time : " << std::fixed << std::setprecision(2) << t << " ms ";
    putText(image, ss.str(), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(0, 0, 255), 2, 8);
    imshow("method2", image);
}


void method_3(cv::Mat image)
{
    //cv::Mat image = image_.clone();
    double startTime = cv::getTickCount();
    int w = image.cols;
    int h = image.rows;
    uchar * ptr=NULL;//指针定义应该放在这里,如果放在for循环中定义会消耗一部分变量分配时间;
    for (int times = 0; times < TIMES; times++)
    {
        for (int row = 0; row < h; row++) 
        {
            ptr = image.ptr<uchar>(row);
            for (int col = 0; col < w; col++)
            {
                *(ptr + 3 * col) = 255 - *(ptr + 3 * col);
                *(ptr + 3 * col + 1) = 255 - *(ptr + 3 * col + 1);
                *(ptr + 3 * col + 2) = 255 - *(ptr + 3 * col + 2);
            }
        }
    }
    
    double endTime = cv::getTickCount();
    double t = ((endTime - startTime) / cv::getTickFrequency()) * 1000 / TIMES;
    std::ostringstream ss;
    ss << "Execute time : " << std::fixed << std::setprecision(2) << t << " ms ";
    putText(image, ss.str(), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(0, 0, 255), 2, 8);
    imshow("method3", image);
}

//当图像数据是连续的,就可以从矩阵的第一个元素开始
//按相对位置访问整个矩阵
void method_4(cv::Mat image)
{
    //assert(image.isContinuous());
    //std::cout <<"image.isContinuous():"<< image.isContinuous()<< std::endl;

    double startTime = cv::getTickCount();
    int w = image.cols;
    int h = image.rows;
    uchar * imgPtr = NULL;
    for (int times = 0; times < TIMES; times++)
    {
        //image.data与image.ptr<uchar>(0)等价;
        for (int row = 0; row < h; row++) {
            imgPtr = image.data + row * image.step;
            for (int col = 0; col < w; col++) {
                //以下三种方式都是等价的,
                *(imgPtr + col*3) = 255 - *(imgPtr + col * 3);
                *(imgPtr + col * 3 + 1) = 255 - *(imgPtr + col * 3 + 1);
                *(imgPtr + col * 3 + 2) = 255 - *(imgPtr + col * 3 + 2);

                /*imgPtr[col * 3] = 255 - imgPtr[col * 3];
                imgPtr[col * 3 + 1] = 255 - imgPtr[col * 3 + 1];
                imgPtr[col * 3 + 2] = 255 - imgPtr[col * 3 + 2];*/

                /**(imgPtr++) = 255 - *(imgPtr);
                *(imgPtr++) = 255 - *(imgPtr);
                *(imgPtr++) = 255 - *(imgPtr);*/
            }
        }
    }
    double endTime = cv::getTickCount();
    double t = ((endTime - startTime) / cv::getTickFrequency()) * 1000 / TIMES;
    std::ostringstream ss;
    ss << "Execute time : " << std::fixed << std::setprecision(2) << t << " ms ";
    putText(image, ss.str(), cv::Point(20, 20), cv::FONT_HERSHEY_SIMPLEX, 0.75, cv::Scalar(0, 0, 255), 2, 8);
    imshow("method4", image);
}


int main()
{
    std::string imagePath = "K:\\deepImage\\building.jpg";
    cv::Mat m1 = cv::imread(imagePath);
    //cv::resize(m1, m1, cv::Size(1000, 1000));
    method_1(m1.clone());
    method_2(m1.clone());
    method_3(m1.clone());
    method_4(m1.clone());
    cv::waitKey(0);

    return 0;
}
上一篇下一篇

猜你喜欢

热点阅读