目标
如何通过图像的每一个像素?
OpenCV矩阵值如何存储?
如何衡量我们的算法的性能?
什么是查找表(LUT),为什么使用它们?
测试用例
考虑一个简单的颜色空间减少方法。通过对矩阵项存储使用unsigned char C和C ++类型,像素通道最多可能有256个不同的值。对于三通道图像,这可以允许形成太多的颜色(1600万)。使用这么多色调可能会对我们的算法性能造成沉重打击。然而,有时候,只要少一些工作能够获得相同的最终结果就足够了。
在这种情况下,我们常常会减少色彩空间。这意味着我们将颜色空间当前值与新的输入值分开,以减少颜色。例如,零和九之间的每个值都将变为零,十到十九值之间的值变为10等等。
当您使用int值将uchar(unsigned char-aka值在0和255之间)值分隔时,结果也将是char。这些值只能是char值。因此,任何分数将被向下舍入。利用这一事实,uchar域中的上层操作可以表示为:
![](https://img.haomeiwen.com/i7647943/9cdcc51ee666ce57.png)
简单的色彩空间缩小算法将仅包含通过图像矩阵的每个像素并应用该公式。值得注意的是,我们做一个除法和乘法运算。这些操作对于系统来说是昂贵的。如果可能,通过使用更便宜的操作(如少量减法,加法或赋值运算)来避免这种情况。此外,请注意,我们只有上限操作的输入值有限。在uchar系统的情况下,这是256。
因此,对于较大的图像,通过使用查找表,预先计算所有可能的值,并且在分配期间仅仅进行分配是明智的。查找表是简单的数组(具有一个或多个维),对于给定的输入值变量保存最终的输出值。它的优点在于我们不需要进行计算,只需要读取结果。
我们的测试用例程序将执行以下操作:读取控制台图像(可以是彩色或灰度图像),并使用给定的控制台行参数减少整数值。在OpenCV中,目前有三种主要的逐个扫描图像像素的方法。为了使事情更有趣,将使用所有这些方法对每个图像进行扫描,并打印出花费多长时间。
Note:可以在这里下载完整cpp教程代码。
如何衡量时间?
那么OpenCV提供了两个简单的函数来实现这个cv :: getTickCount()和cv :: getTickFrequency()。
第一个从某个事件返回系统CPU的时间clock(就像您启动系统一样)。
第二次返回您的CPU在一秒钟内发出多少次时间clock。
所以为了测量秒数,两次操作之间的时间如下:
double t = (double)getTickCount();
// do something ...
t = ((double)getTickCount() - t) / getTickFrequency();
cout << "Times passed in seconds: " << t << endl;
图像矩阵如何存储在内存中?
正如上一节的Mat - 基本图像容器教程,矩阵的大小取决于使用的颜色系统。更准确地说,它取决于所使用的通道数量。
在灰度图像的情况下:
![](https://img.haomeiwen.com/i7647943/4edaa23e99ea0f68.png)
对于多通道图像,列包含与通道数一样多的子列。例如在BGR颜色系统的情况下:
![](https://img.haomeiwen.com/i7647943/b21da0389fc6b804.png)
注意,通道的顺序是反向的:BGR而不是RGB。因为在许多情况下,存储器足够大以便以连续的方式存储行,所以行可以一个接一个地跟随,创建一个长行。因为一切都在一个接一个的地方,这可能有助于加快扫描过程。
可以使用cv :: Mat :: isContinuous()函数来询问矩阵是否是这种情况。
完整代码
#include <opencv2/core.hpp>
#include <opencv2/core/utility.hpp>
#include "opencv2/imgcodecs.hpp"
#include <opencv2/highgui.hpp>
#include <iostream>
#include <sstream>
using namespace std;
using namespace cv;
static void help()
{
cout
<< "\n--------------------------------------------------------------------------" << endl
<< "This program shows how to scan image objects in OpenCV (cv::Mat). As use case"
<< " we take an input image and divide the native color palette (255) with the " << endl
<< "input. Shows C operator[] method, iterators and at function for on-the-fly item address calculation." << endl
<< "Usage:" << endl
<< "./how_to_scan_images <imageNameToUse> <divideWith> [G]" << endl
<< "if you add a G parameter the image is processed in gray scale" << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
}
Mat& ScanImageAndReduceC(Mat& I, const uchar* table);
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table);
int main(int argc, char* argv[])
{
help();
if (argc < 3)
{
cout << "Not enough parameters" << endl;
return -1;
}
Mat I, J;
if (argc == 4 && !strcmp(argv[3], "G"))
I = imread(argv[1], IMREAD_GRAYSCALE);
else
I = imread(argv[1], IMREAD_COLOR);
if (I.empty())
{
cout << "The image" << argv[1] << " could not be loaded." << endl;
return -1;
}
//! [dividewith]
int divideWith = 0; // convert our input string to number - C++ style
stringstream s;
s << argv[2];
s >> divideWith;
if (!s || !divideWith)
{
cout << "Invalid number entered for dividing. " << endl;
return -1;
}
uchar table[256];
for (int i = 0; i < 256; ++i)
table[i] = (uchar)(divideWith * (i / divideWith));
//! [dividewith]
const int times = 100;
double t;
t = (double)getTickCount();
for (int i = 0; i < times; ++i)
{
cv::Mat clone_i = I.clone();
J = ScanImageAndReduceC(clone_i, table);
}
t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
t /= times;
cout << "Time of reducing with the C operator [] (averaged for "
<< times << " runs): " << t << " milliseconds." << endl;
t = (double)getTickCount();
for (int i = 0; i < times; ++i)
{
cv::Mat clone_i = I.clone();
J = ScanImageAndReduceIterator(clone_i, table);
}
t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
t /= times;
cout << "Time of reducing with the iterator (averaged for "
<< times << " runs): " << t << " milliseconds." << endl;
t = (double)getTickCount();
for (int i = 0; i < times; ++i)
{
cv::Mat clone_i = I.clone();
ScanImageAndReduceRandomAccess(clone_i, table);
}
t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
t /= times;
cout << "Time of reducing with the on-the-fly address generation - at function (averaged for "
<< times << " runs): " << t << " milliseconds." << endl;
//! [table-init]
Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for (int i = 0; i < 256; ++i)
p[i] = table[i];
//! [table-init]
t = (double)getTickCount();
for (int i = 0; i < times; ++i)
//! [table-use]
LUT(I, lookUpTable, J);
//! [table-use]
t = 1000 * ((double)getTickCount() - t) / getTickFrequency();
t /= times;
cout << "Time of reducing with the LUT function (averaged for "
<< times << " runs): " << t << " milliseconds." << endl;
return 0;
}
//! [scan-c]
Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
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;
}
//! [scan-c]
//! [scan-iterator]
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
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;
}
//! [scan-iterator]
//! [scan-random]
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar* const table)
{
// accept only char type matrices
CV_Assert(I.depth() == CV_8U);
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;
}
//! [scan-random]
用法
![](https://img.haomeiwen.com/i7647943/1e0f25d3f5be111d.png)
网友评论