OpenCV图像像素读取及效率分析

来源:互联网 发布:大数据开发工程师招聘 编辑:程序博客网 时间:2024/05/19 07:07

在计算机视觉应用中,对于图像内容的读取分析是第一步,所以学习高效的处理图像是很有用的。一个图像有可能包含数以万计的像素,从根本上说图像就是一系列像素值,所以OpenCV使用数据结构cv::Mat来表示图像。矩阵中每一个元素都代表一个像素,对于灰度图像,像素用8位无符号数,0表示黑色,255表示白色。对于彩色像素而言,每个像素需要三位这样的8位无符号数来表示,即三个通道(R,G,B),矩阵则依次存储一个像素的三个通道的值,然后再存储下一个像素点。

       cv::Mat中,成员变量cols代表图像的宽度(图像的列数),成员变量rows代表图像的高度(图像的行数),step代表以字节为单位的图像的有效宽度,elemSize返回像素的大小,像素的大小 = 颜色大小(字节)*通道数,比如三通道short型矩阵(CV_16SC3)的大小为2*3 = 6,像素的channels方法返回图像的通道数,total函数返回图像的像素数。

       闲话少说直接介绍几种读取方式:

       RGB图像的颜色数目是256*256*256,本文对图像进行量化,缩减颜色数目到256的1/8(即32*32*32)为目标,分别利用一下几种方法实现,比较几种方法的安全和效率。

       1.ptr遍历图像

       cv::Mat中提供ptr函数访问任意一行像素的首地址,特别方便图像的一行一行的横向访问,如果需要一列一列的纵向访问图像,就稍微麻烦一点。但是ptr访问效率比较高,程序也比较安全,有越界判断。

[cpp] view plain copy
  1. int nl= image.rows; //行数  
  2. int nc= image.cols * image.channels(); // 每行的元素个数,每行的像素数*颜色通道数(RGB = 3)  
  3.           
  4. for (int j=0; j<nl; j++) {  
  5.     uchar* data= image.ptr<uchar>(j);  
  6.     for (int i=0; i<nc; i++) {   
  7.       // process each pixel ---------------------                
  8.          data[i]= data[i]/div*div + div/2;  
  9.       // end of pixel processing ----------------  
  10.     } // end of line                     
  11. }  
    也可以使用:

[cpp] view plain copy
  1.     for (int j=0; j<nl; j++) {  
  2.  uchar* data= image.ptr<uchar>(j);  
  3.         for (int i=0; i<nc; i++) {  
  4.           // process each pixel ---------------------                 
  5. *data++= *data/div*div + div/2;  
  6.           // end of pixel processing ----------------  
  7.           } // end of line                     
  8.     }  
   

2.使用迭代器遍历图像

     cv::Mat 同样有标准模板库(STL),可以使用迭代器访问数据。

     用迭代器来遍历图像像素,可简化过程降低出错的机会,比较安全,不过效率较低;如果想避免修改输入图像实例cv::Mat,可采用const_iterator。iterator有两种调用方法,cv::MatIterator_<cv::Vec3b> it;cv::Mat_<cv::Vec3b>::iterator it;中间cv::Vec3b是因为图像是彩色图像,3通道,cv::Vec3b可以代表一个像素。

[cpp] view plain copy
  1. // get iterators  
  2.       cv::Mat_<cv::Vec3b>::iterator it= image.begin<cv::Vec3b>();  
  3.       cv::Mat_<cv::Vec3b>::iterator itend= image.end<cv::Vec3b>();  
  4.   
  5.       for ( ; it!= itend; ++it) {  
  6.           
  7.         // process each pixel ---------------------  
  8.   
  9.         (*it)[0]= (*it)[0]/div*div + div/2;  
  10.         (*it)[1]= (*it)[1]/div*div + div/2;  
  11.         (*it)[2]= (*it)[2]/div*div + div/2;  
  12.   
  13.         // end of pixel processing ----------------  
  14.       }  

 3.at方法遍历

      cv::Mat也是向量,可以使at方法取值,使用调用方法image.at<cv::Vec3b>(j,i),at方法方便,直接给i,j赋值就可以随意访问图像中任何一个像素,其中j表示第j行,i表示该行第i个像素。但是at方法效率是这3中访问方法中最慢的一个,所以如果遍历图像或者访问像素比较多时,建议不要使用这个方法,毕竟程序的效率还是比程序的可读性要重要的。下面是完整的调用方法,其运行时间在下面会介绍。

[cpp] view plain copy
  1. int nl= image.rows; // number of lines  
  2. int nc= image.cols; // number of columns  
  3.              
  4.    for (int j=0; j<nl; j++) {  
  5.        for (int i=0; i<nc; i++) {  
  6.   
  7.          // process each pixel ---------------------  
  8.                 
  9.                image.at<cv::Vec3b>(j,i)[0]=    image.at<cv::Vec3b>(j,i)[0]/div*div + div/2;  
  10.                image.at<cv::Vec3b>(j,i)[1]=    image.at<cv::Vec3b>(j,i)[1]/div*div + div/2;  
  11.                image.at<cv::Vec3b>(j,i)[2]=    image.at<cv::Vec3b>(j,i)[2]/div*div + div/2;  
  12.   
  13.          // end of pixel processing ----------------  
  14.   
  15.          } // end of line                     
  16.    }  
[cpp] view plain copy
  1.   

  4.row(i),col(i)

       cv::Mat提供image.row(i),和image.col(j)对图像整行和整列进行处理,处理比较方便,因为很少遇到,所以就没有进行效率比较

  程序代码如下(三通道):

[cpp] view plain copy
  1. result.row(0).setTo(cv::Scalar(0,0,0));//将第一行数据设为零  
  2.     result.row(result.rows-1).setTo(cv::Scalar(0,0,0));//将最后一行数据设置为零  
  3.     result.col(0).setTo(cv::Scalar(0,0,0));//将第一列数据设为零  
  4.     result.col(result.cols-1).setTo(cv::Scalar(0,0,0));//将最后一列数据设为零  

5.高效率图像遍历循环

       对于迭代的for循环,外面一层的循环次数越少速度越快,同样是cols*rows*channels()次循环,使用nc = cols,nl = rows*channels(),与使用nc = cols*rows*channels,nl = 1,以及nc = cols,nl = rows,for函数最里层运行三次颜色的处理。这三种方法最快的是第二种,第三种其次,最慢的是第一种. 

[cpp] view plain copy
  1. int nl= image.rows; // number of lines  
  2. int nc= image.cols * image.channels(); // total number of elements per line  
  3.   
  4. if (image.isContinuous())  {  
  5.  // then no padded pixels  
  6.  nc= nc*nl;   
  7.  nl= 1;  // it is now a 1D array  
  8.  }  
  9.   
  10. int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));  
  11. // mask used to round the pixel value  
  12. uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0  
  13.              
  14.    for (int j=0; j<nl; j++) {  
  15.   
  16.  uchar* data= image.ptr<uchar>(j);  
  17.   
  18.        for (int i=0; i<nc; i++) {  
  19.   
  20.          // process each pixel ---------------------  
  21.                 
  22.          *data++= *data&mask + div/2;  
  23.   
  24.          // end of pixel processing ----------------  
  25.   
  26.          } // end of line                     
  27.    }  

   6.位运算代替乘法和除法

[cpp] view plain copy
  1.        int nl= image.rows; // number of lines  
  2. int nc= image.cols * image.channels(); // total number of elements per line  
  3. int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));  
  4. // mask used to round the pixel value  
  5. uchar mask= 0xFF<<n; // e.g. for div=16, mask= 0xF0  
  6.              
  7.    for (int j=0; j<nl; j++) {  
  8.   
  9.  uchar* data= image.ptr<uchar>(j);  
  10.   
  11.        for (int i=0; i<nc; i++) {  
  12.   
  13.          // process each pixel ---------------------  
  14.                 
  15.          *data++= *data&mask + div/2;  
  16.   
  17.          // end of pixel processing ----------------  
  18.   
  19.          } // end of line                     
  20.    }  


运行时间比较,以测试图片为例,(Debug模式下的时间)

   ptr函数的两种方法的时间:
                using .ptr and [] =3.50202ms
                using .ptr and * ++ =3.26124ms
   迭代器方法:
              using Mat_ iterator =143.06ms
   at方法的运行时间:
             using at =252.779ms
   高效率迭代方法:
            using .ptr and * ++ and bitwise (continuous) =2.68335ms
   位运算方法:

            using .ptr and * ++ and bitwise =2.59823ms


还有一些比较的函数方法,现在只给出运行时间:
         using .ptr and * ++ and modulo =3.78029ms
         using .ptr and * ++ and bitwise =2.59823ms
         using direct pointer arithmetic =2.57317ms
         using .ptr and * ++ and bitwise with image.cols * image.channels() =22.864ms
         using Mat_ iterator and bitwise =139.92ms
         using MatIterator_ =185.996ms
        using .ptr and * ++ and bitwise (continuous+channels) =2.11271ms
         using input/output images =2.97717ms
         using overloaded operators =2.7237ms


源图像:


量化处理结果 image1:


量化处理后的结果 image2:

原创粉丝点击