目的

  • 如何遍历图像中的每一个像素?
  • OpenCV的矩阵值是如何存储的?
  • 如何测试我们所实现算法的性能?
  • 查找表是什么?为什么要用它?

测试用例

颜色空间缩减。具体做法就是:将现有颜色空间值除以某个输入值,以获得较少的颜色数。例如,颜色0到9可取为新值0,10到19可取为10。

计算公式:

Lnew = (Lold / 10) * 10

如果对图像矩阵的每一个像素进行这个操作的话,是比较费时的,因为有大量的乘除操作。 这个时候我们的查找表就派上用场了,提前把值计算好,然后要用的时候,直接赋值即可。

  • 创建查找表
int divideWith; // convert our input string to number - C++ style
stringstream s;
s << argv[2];
s >> divideWith;
if (!s) {
cout << "Invalid number entered for dividing. " << endl;
return -1;
} uchar table[256];
for (int i = 0; i < 256; ++i)
table[i] = divideWith* (i/divideWith);
  • 计时

具体用的是getTickCount()和getTickFrequency()两个函数。第一个函数返回的是CPU自某个事件以来走过的时钟周期数,第二个函数返回你的CPU一秒钟所走的时钟周期数。

double t = (double)getTickCount();
// 做点什么 ...
t = ((double)getTickCount() - t)/getTickFrequency();
cout << "Times passed in seconds: " << t << endl;

1. 高效的方法 Efficient Way

因为图像中的每个像素是可以顺序存储的,所以可以使用下标进行访问,访问前使用isContinuous()来判断矩阵是否连续存储的。

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 * channels;
int nCols = I.cols; 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;
}

另外一种方法来实现遍历功能,就是使用data,data会从Mat中返回指向矩阵第一行第一列的指针。注意如果该指针为NULL则表明对象里面无输入,所以这是一种简单的检查图像是否被成功读入的方法。当矩阵是连续存储时,我们就可以通过遍历data来扫描整个图像。

uchar* p = I.data;

for( unsigned int i =0; i < ncol*nrows; ++i)
*p++ = table[*p];

2. 迭代法

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;
}

3. 通过相关返回值的On-the-fly地址计算

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;
}

4. 核心函数LUT (The Core Function)

operationsOnArrays:LUT() 包含于core module的函数,首先我们建立一个mat型用于查表:

 Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.data;
for( int i = 0; i < 256; ++i)
p[i] = table[i];

然后我们调用函数(I是输入J是输出)

LUT(I, lookUpTable, J);

性能表现

Efficient Way 79.4717 milliseconds

Iterator 83.7201 milliseconds

On-The-Fly RA 93.7878 milliseconds

LUT function 32.5759 milliseconds

结论:尽量使用OpenCV内置函数。调用LUT函数可以获得最快的速度。这是因为OpenCV库可以通过英特尔线程架构启用多线程。如果你喜欢使用指针的方法来扫描图像,迭代法是一个不错的选择,不过速度上较慢。

四种方法完整的代码:

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <sstream> using namespace std;
using namespace cv; 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
<< "./howToScanImages 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], CV_LOAD_IMAGE_GRAYSCALE);
else
I = imread(argv[1], CV_LOAD_IMAGE_COLOR); if (!I.data)
{
cout << "The image" << argv[1] << " could not be loaded." << endl;
return -1;
} int divideWith; // convert our input string to number - C++ style
stringstream s;
s << argv[2];
s >> divideWith;
if (!s)
{
cout << "Invalid number entered for dividing. " << endl;
return -1;
} uchar table[256];
for (int i = 0; i < 256; ++i)
table[i] = divideWith* (i/divideWith); const int times = 100;
double t; t = (double)getTickCount(); for (int i = 0; i < times; ++i)
J = ScanImageAndReduceC(I.clone(), 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)
J = ScanImageAndReduceIterator(I.clone(), 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)
ScanImageAndReduceRandomAccess(I.clone(), 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; Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.data;
for( int i = 0; i < 256; ++i)
p[i] = table[i]; t = (double)getTickCount(); for (int i = 0; i < times; ++i)
LUT(I, lookUpTable, J); 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;
} 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 * channels;
int nCols = I.cols; 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;
} 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;
}

OpenCV从入门到放弃系列之——如何扫描图像、利用查找表和计时的更多相关文章

  1. OpenCV学习笔记:如何扫描图像、利用查找表和计时

    目的 我们将探索以下问题的答案: 如何遍历图像中的每一个像素? OpenCV的矩阵值是如何存储的? 如何测试我们所实现算法的性能? 查找表是什么?为什么要用它? 测试用例 这里我们测试的,是一种简单的 ...

  2. day-15 用opencv怎么扫描图像,利用查找表和计时

    一.本节知识预览 1.  怎样遍历图像的每一个像素点? 2.  opencv图像矩阵怎么被存储的? 3.  怎样衡量我们算法的性能? 4.  什么是查表,为什么要使用它们? 二.什么是查表,为什么要使 ...

  3. OpenCV从入门到放弃系列之——core模块.核心功能(一)

    Mat - 基本图像容器 世间的图像是各种各样的,但是到了计算机的世界里所有的图像都简化为了数值矩以及矩阵信息.作为一个计算视觉库,OpenCV的主要目的就是处理和操作这些信息,来获取更高级的信息,也 ...

  4. OpenCV从入门到放弃系列之——图像的基本操作

    读取.修改.保存图像 图像读取函数imread(); 图像颜色空间的转换cvtColor(); 图像保存至硬盘imwrite(); /********************************* ...

  5. [大数据从入门到放弃系列教程]在IDEA的Java项目里,配置并加入Scala,写出并运行scala的hello world

    [大数据从入门到放弃系列教程]在IDEA的Java项目里,配置并加入Scala,写出并运行scala的hello world 原文链接:http://www.cnblogs.com/blog5277/ ...

  6. [大数据从入门到放弃系列教程]第一个spark分析程序

    [大数据从入门到放弃系列教程]第一个spark分析程序 原文链接:http://www.cnblogs.com/blog5277/p/8580007.html 原文作者:博客园--曲高终和寡 **** ...

  7. php从入门到放弃系列-01.php环境的搭建

    php从入门到放弃系列-01.php环境的搭建 一.为什么要学习php 1.php语言适用于中小型网站的快速开发: 2.并且有非常成熟的开源框架,例如yii,thinkphp等: 3.几乎全部的CMS ...

  8. php从入门到放弃系列-04.php页面间值传递和保持

    php从入门到放弃系列-04.php页面间值传递和保持 一.目录结构 二.两次页面间传递值 在两次页面之间传递少量数据,可以使用get提交,也可以使用post提交,二者的区别恕不赘述. 1.get提交 ...

  9. php从入门到放弃系列-03.php函数和面向对象

    php从入门到放弃系列-03.php函数和面向对象 一.函数 php真正的威力源自它的函数,内置了1000个函数,可以参考PHP 参考手册. 自定义函数: function functionName( ...

随机推荐

  1. 3个著名加密算法(MD5、RSA、DES)的解析

    MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2.MD3和MD4发展而来.    M ...

  2. vs2010项目使用vs2013编译报错 无法打开包括文件:“winapifamily.h”

    我的老项目是vs2010下的项目.最近安装vs2013后,打开sln解决方案,调试运行报错 C:\Program Files (x86)\Windows Kits\8.0\Include\um\win ...

  3. cygwin E437

    这个简单错误居然查到了 报错E437: terminal capability "cm" required 执行:# export TERM=xterm

  4. Contributing to the C++ Core Guidelines

    Contributing to the C++ Core Guidelines "Within C++ is a smaller, simpler, safer language strug ...

  5. C#下没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG))

    C#下没有注册类 (异常来自 HRESULT:0x80040154 (REGDB_E_CLASSNOTREG)) 原因:没有原生支持64位,而是以32位兼容方式运行 解决办法:在项目属性里设置“生成” ...

  6. 搭建Python+Django开发环境

    第一步:安装python. 常见的windows系统,直接python网站下载 最新的版本python3.5. python安装好之后,配置好环境变量.使得python和 pip命令能够正常使用. 第 ...

  7. mongodb群集

    项目目标:故障自动切换和自动修复成员节点,各个数据库之间数据完全一致.项目描述:副本集没有固定主节点,是整个集群选举得出的一个主节点,当其不工作时变    更其他节点.最小的副本集也应该具备一个pri ...

  8. Linux selinux iptables

    关闭SELINUX – 使用getenforce命令检查SELINUX状态,若结果不是”Disabled”,可使用setenforce 0命令临时关闭SELINUX.要永久关闭SELINUX,需修改/ ...

  9. (转)学习使用Jmeter做压力测试(一)--压力测试基本概念

    一.性能测试的概念 性能测试是通过自动化的测试工具模拟多种正常峰值及异常负载条件来对系统的各项性能指标进行测试.负载测试和压力测试都属于性能测试,两者可以结合进行. 通过负载测试,确定在各种工作负载下 ...

  10. docker 介绍

    docker 介绍 安装 sudo apt-get install docker.io sudo docker info 查看是否安装成功 hello world sodu docker run he ...