内容简介:#数字图像经数字化的图像,一般用一个矩阵来表示。如一张800x600的图像,在程序中是由一个800x600大小的矩阵表示,一张图像就是由这800x600个点组成,在彩色图像和灰度图像上面有所区别,彩色图像是三通道的,即每一个点有RGB三个元素,灰度图像则是一个点只有一个元素。另外特殊的是还有4通道的图像,除了RGB三原色外,4通道图像还有一个alpha通道,用来表示图像的透明度。
#数字图像
经数字化的图像,一般用一个矩阵来表示。如一张800x600的图像,在程序中是由一个800x600大小的矩阵表示,一张图像就是由这800x600个点组成,在彩色图像和灰度图像上面有所区别,彩色图像是三通道的,即每一个点有RGB三个元素,灰度图像则是一个点只有一个元素。另外特殊的是还有4通道的图像,除了RGB三原色外,4通道图像还有一个alpha通道,用来表示图像的透明度。
索引图像
索引图像也称为调色版图像,索引图像提供了一个方法减少表达一幅图像所需的数据量。原理是颜色值构成一个查找表(look up table),也称调色板,如8位灰度级的彩色图像理论上拥有256x256x256总颜色,但是通过查找表的方式,一个像素点可以以记录(126,24,44)的方式去查找调色版中的图像,无需每个像素点直接记录256x256x256这么多中颜色里面的其中一种颜色。数字图像里图像一般以这种形式储存图像颜色信息,那如何去扫描访问图像的像素信息,opencv中的示例how_to_scan_image.cpp给出了答案
示例程序
下面是示例源码
//头文件
#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);//通过c指针的方式扫描
Mat& ScanImageAndReduceIterator(Mat& I, const uchar* table);//通过迭代器的方式扫描
Mat& ScanImageAndReduceRandomAccess(Mat& I, const uchar * table);//通过at函数访问
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();//记录晶振起点
//c语言指针风格访问测试
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]
//创建初始化查找表lookuptable
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);//使用LUT函数将I数据处理后存入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;//像素点的列数 = 列数x通道数
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i,j;
uchar* p;//c语义指针形式
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)];//使用at函数
break;
}
case 3:
{
Mat_<Vec3b> _I = I;//使用<cv::Vec3b>
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]
程序知识点
三种访问方式
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;//像素点的列数 = 列数x通道数
if (I.isContinuous())
{
nCols *= nRows;
nRows = 1;
}
int i,j;
uchar* p;//c语义指针形式
for( i = 0; i < nRows; ++i)
{
p = I.ptr<uchar>(i);//指针记录行地址
for ( j = 0; j < nCols; ++j)
{
p[j] = table[p[j]];//从查找表取值
}
}
return I;
}
可以看到,以指针的方式访问,首先是声明一个uchar类型的指针记录行地址,然后遍历一行中所有的像素点。下一行继续如此。
###迭代器方式
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;
}
迭代器的访问方式也简单,首先是记录迭代的开始begin,一直迭代到结束end,不包含end。关键语句
MatIterator_<uchar> it, end;
for( it = I.begin<uchar>(), end = I.end<uchar>(); it != end; ++it)//迭代器访问
*it = table[*it];
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]];
}
//使用c++新标准上述代码还可以简化为
for( auto it = I.begin<uchar>(), end = I.end<uhar>(); it != end; ++it)
*it = table[*it];
for(auto 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]];
}
###at函数方式
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)];//使用at函数
at的方式是比较常用的方法了,我一般也是使用这个方法,这个方法访问有一个优点,就是相比指针的访问形式,这个方法可检查数组是否越界,如果数组越界就会报out_of_range错误,这样对调试就可以避免一些不必要的错误。这个方式缺点就是访问速度是稍慢的,在debug调试模式之下尤为明显,但是release之下速度与其他方式倒是差距不大。
统计程序运行耗费时间
t = (double)getTickCount();//记录晶振起点
//c语言指针风格访问测试
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;
原理大概是这样的,计算机里有个叫晶振的东西,一直在按照固定的频率去"震动",这个震动形成的就是为计算机计算提供动力的电信号,一般是方波,也称时钟信号。这个震动频率可以理解为计算机处理器的主频,即计算速度。所以计算耗时的原理可以解释为
//1.记录震动的起始点 t = (double)getTickCount(); //2.程序运行 ... //3.记录震动的结束点,减去起始点,就得到程序运行过程中震动的次数 (double)getTickCount() - t //4.结果处以震动的频率(也就是震动的速度),就得到整个过程的时间,时间单位时s 1000*((double)getTickCount() - t)/getTickFrequency();//*1000后即ms
LUT函数
//! [table-init]
//创建初始化查找表lookuptable
Mat lookUpTable(1, 256, CV_8U);
uchar* p = lookUpTable.ptr();
for( int i = 0; i < 256; ++i)
p[i] = table[i];
//! [table-init]
函数原型
void cv::LUT ( InputArray src, InputArray lut, OutputArray dst ) Python: dst = cv.LUT( src, lut[, dst] )
函数执行的操作
由图可知,LUT函数执行的操作也很简单,即对输入图像每个像素点加上一个d,然后输出。这个d有条件限制的,输入图像是8位无符号时d为0,8位有符号时为128。
回顾总结
数字图像由一个矩阵表示,不同图像一个像素点有不一样的通道数,灰度图一个通道,彩色图3个通道,有些图像多一个alpha的透明的通道。本例学习了 c语言 指针方式、迭代器和at函数方式访问像素,at访问方式是安全的。计算程序耗时和LUT函数。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- laravel自带用户认证
- Mac自带apache配置
- opencv自带例子学习-图像混合
- Golang中自带的强大命令工具
- Android调用系统自带的分享功能
- 使用oracle自带的命令进行导入导出
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
白话大数据与机器学习
高扬、卫峥、尹会生 / 机械工业出版社 / 2016-6 / 69
本书通俗易懂,有高中数学基础即可看懂,同时结合大量案例与漫画,将高度抽象的数学、算法与应用,与现实生活中的案例和事件一一做了关联,将源自生活的抽象还原出来,帮助读者理解后,又带领大家将这些抽象的规律与算法应用于实践,贴合读者需求。同时,本书不是割裂讲解大数据与机器学习的算法和应用,还讲解了其生态环境与关联内容,让读者更全面地知晓渊源与未来,是系统学习大数据与机器学习的不二之选: ·大数据产业......一起来看看 《白话大数据与机器学习》 这本书的介绍吧!