转载自:http://blog.youkuaiyun.com/xiaowei_cqu/article/details/7771760
以下例子源自《The OpenCV Tutorials --Release 2.4.2》2.2 How to scan images, lookup tables and time measurement with OpenCV
图像容器Mat


访问图像中的像素
高效的方法:C操作符[ ]
- Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
- {
- // accept only char type matrices
- CV_Assert(I.depth() != sizeof(uchar));
- int channels = I.channels();
- int nRows = I.rows ;
- int nCols = I.cols* channels;
- if (I.isContinuous())
- {
- nCols *= nRows;
- nRows = 1;
- }
- int i,j;
- uchar* p;
- for( i = 0; i < nRows; ++i)
- {
- p = I.ptr<uchar>(i);
- for ( j = 0; j < nCols; ++j)
- {
- p[j] = table[p[j]];
- }
- }
- return I;
- }
- int nRows = I.rows * channels;
- int nCols = I.cols;
说到数据的存储,这一直就是一个值得关注的问题,Mat_<uchar>对应的是CV_8U,Mat_<char>对应的是CV_8S,Mat_<int>对应的是CV_32S,Mat_<float>对应的是CV_32F,Mat_<double>对应的是CV_64F,对应的数据深度如下:
• CV_8U - 8-bit unsigned integers ( 0..255 )
• CV_8S - 8-bit signed integers ( -128..127 )
• CV_16U - 16-bit unsigned integers ( 0..65535 )
• CV_16S - 16-bit signed integers ( -32768..32767 )
• CV_32S - 32-bit signed integers ( -2147483648..2147483647 )
• CV_32F - 32-bit floating-point numbers ( -FLT_MAX..FLT_MAX, INF, NAN )
• CV_64F - 64-bit floating-point numbers ( -DBL_MAX..DBL_MAX, INF, NAN )
- uchar* p = I.data;
- for( unsigned int i =0; i < ncol*nrows; ++i)
- *p++ = table[*p];
安全的方法:迭代器iterator
相比用指针直接访问可能出现越界问题,迭代器绝对是非常安全的方法:- Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
- {
- // accept only char type matrices
- CV_Assert(I.depth() != sizeof(uchar));
- const int channels = I.channels();
- switch(channels)
- {
- case 1:
- {
- MatIterator_<uchar> it, end;
- for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)
- *it = table[*it];
- break;
- }
- case 3:
- {
- MatIterator_<Vec3b> it, end;
- for( it = I.begin<Vec3b>(), end = I.end<Vec3b>(); it != end; ++it)
- {
- (*it)[0] = table[(*it)[0]];
- (*it)[1] = table[(*it)[1]];
- (*it)[2] = table[(*it)[2]];
- }
- }
- }
- return I;
- }
这种方式虽然安全,但是挺慢的,一会儿就知道了。
更慢的方法:动态地址计算
- Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
- {
- // accept only char type matrices
- CV_Assert(I.depth() != sizeof(uchar));
- const int channels = I.channels();
- switch(channels)
- {
- case 1:
- {
- for( int i = 0; i < I.rows; ++i)
- for( int j = 0; j < I.cols; ++j )
- I.at<uchar>(i,j) = table[I.at<uchar>(i,j)];
- break;
- }
- case 3:
- {
- Mat_<Vec3b> _I = I;
- for( int i = 0; i < I.rows; ++i)
- for( int j = 0; j < I.cols; ++j )
- {
- _I(i,j)[0] = table[_I(i,j)[0]];
- _I(i,j)[1] = table[_I(i,j)[1]];
- _I(i,j)[2] = table[_I(i,j)[2]];
- }
- I = _I;
- break;
- }
- }
- return I;
- }
减小颜色空间 color space reduction
0~9 范围的像素值为 0
10~19 范围的像素值 为 10
20~29 范围的像素值为 20
。。。。。。
着这样的操作将颜色取值降低为 26*26*26 种情况。这个操作可以用一个简单的公式:

在处理图像像素时,每个像素需要进行一遍上述计算也需要一定的时间花销。但我们注意到其实只有 0~255 种像素,即只有256种情况。进一步可以把256种计算好的结果提前存在表中 table 中,这样每种情况不需计算直接从 table 中取结果即可。
- int divideWith=10;
- uchar table[256];
- for (int i = 0; i < 256; ++i)
- table[i] = divideWith* (i/divideWith);
- p[j] = table[p[j]];
LUT : Look up table
OpenCV 很聪明的有个 LUT 函数就是针对这种 Look up talbe 的操作:- Mat lookUpTable(1, 256, CV_8U);
- uchar* p = lookUpTable.data;
- for( int i = 0; i < 256; ++i)
- p[i] = table[i];
- for (int i = 0; i < times; ++i)
- LUT(I, lookUpTable, J);
算法计时
- double t;
- t = (double)getTickCount();
- t = 1000*((double)getTickCount() - t)/getTickFrequency();
- t /= times;
实验结果



