总结一下OpenCV遍历图像的几种方法

电子说

1.3w人已加入

描述

在图形处理中,遍历每个像素点是最基本的功能,是做算法的基础,这篇文章来总结一下OpenCV遍历图像的几种方法。

本文章参考文档OpenCV tutorials的how_to_scan_images.cpp例子。

最有效率--指针

用c语言直接访问是最有效率的,最快的,下面是简单的示例。

int scan_image_c(Mat &I)
{
    int channels = I.channels();
    if (channels != 3)
    {
        printf("test support only three channel.\\n");
        return -1;
    }


    for (int i = 0; i < I.rows; i++)
    {
        Vec3b *ptr = I.ptr

最安全--迭代器

迭代器是C++中的一个概念,因为迭代器从用户手中接管了一些工作,它会保证访问的安全,所以必然会导致一些性能上的降低,简单例子如下。

int scan_image_iterator(Mat &I)
{
    int channels = I.channels();
    if (channels != 3)
    {
        printf("test support only three channel.\\n");
        return -1;
    }


    MatIterator_

最便捷--at方法

OpenCV的Mat类中有一个at方法,它可以直接返回某个像素点,示例如下。

int scan_image_random(Mat &I)
{
    int channels = I.channels();
    if (channels != 3)
    {
        printf("test support only three channel.\\n");
        return -1;
    }


    for( int i = 0; i < I.rows; ++i)
    {
        for( int j = 0; j < I.cols; ++j 
        {
            I.at

完整例子

#include 
#include 


using namespace std;
using namespace cv;


int scan_image_c(Mat &I);
int scan_image_iterator(Mat &I);
int scan_image_random(Mat &I);


int main( int argc, char* argv[])
{
    if (argc != 2)
    {
        cout << "input parameters failed!" << endl;
        return -1;
    }


    Mat I;
    I = imread(argv[1], IMREAD_COLOR);


    if (I.empty())
    {
        cout << "The image" << argv[1] << " could not be loaded." << endl;
        return -1;
    }


    const int times = 100;
    double t = 0;


    t = (double)getTickCount();


    for (int i = 0; i < times; ++i)
    {
        cv::Mat clone_i = I.clone();
        scan_image_c(clone_i);
    }


    t = 1000*((double)getTickCount() - t)/getTickFrequency();
    t /= times;


    cout << "Time of scan_image_c        (averaged for "
         << times << " runs): " << t << " ms."<< endl;


    t = (double)getTickCount();


    for (int i = 0; i < times; ++i)
    {
        cv::Mat clone_i = I.clone();
        scan_image_iterator(clone_i);
    }


    t = 1000*((double)getTickCount() - t)/getTickFrequency();
    t /= times;


    cout << "Time of scan_image_iterator (averaged for "
        << times << " runs): " << t << " ms."<< endl;


    t = (double)getTickCount();


    for (int i = 0; i < times; ++i)
    {
        cv::Mat clone_i = I.clone();
        scan_image_random(clone_i);
    }


    t = 1000*((double)getTickCount() - t)/getTickFrequency();
    t /= times;


    cout << "Time of scan_image_random   (averaged for "
        << times << " runs): " << t << " ms."<< endl;


    return 0;
}


int scan_image_c(Mat &I)
{
    int channels = I.channels();
    if (channels != 3)
    {
        printf("test support only three channel.\\n");
        return -1;
    }


    for (int i = 0; i < I.rows; i++)
    {
        Vec3b *ptr = I.ptr

运行结果如下:

Time of scan_image_c        (averaged for 100 runs): 2.04884 ms.
Time of scan_image_iterator (averaged for 100 runs): 4.77701 ms.
Time of scan_image_random   (averaged for 100 runs): 3.64237 ms.

从数据上看,c语言的方法确实是最快的,和其他两种方式拉开了一定的差距。而at遍历比迭代器遍历快了不少。

在平常使用中,我们可以根据每个方法的优点去选择不同的方法。

审核编辑:刘清

打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分