//--------------------------------------【程序说明】-------------------------------------------
// 程序说明:《OpenCV3编程入门》OpenCV2版书本配套示例程序24
// 程序描述:来自一本国外OpenCV2书籍的示例-遍历图像像素的14种方法
// 测试所用IDE版本:Visual Studio 2010
// 测试所用OpenCV版本: 2.4.9
// 2014年11月 Revised by @浅墨_毛星云
//------------------------------------------------------------------------------------------------
/*------------------------------------------------------------------------------------------*\
This file contains material supporting chapter 2 of the cookbook:
Computer Vision Programming using the OpenCV Library.
by Robert Laganiere, Packt Publishing, 2011.
This program is free software; permission is hereby granted to use, copy, modify,
and distribute this source code, or portions thereof, for any purpose, without fee,
subject to the restriction that the copyright notice may not be removed
or altered from any source or altered source distribution.
The software is released on an as-is basis and without any warranties of any kind.
In particular, the software is not guaranteed to be fault-tolerant or free from failure.
The author disclaims all warranties with regard to this software, any use,
and any consequent failure, is purely the responsibility of the user.
Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name
\*------------------------------------------------------------------------------------------*/
//---------------------------------【头文件、命名空间包含部分】-----------------------------
// 描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
//---------------------------------【宏定义部分】---------------------------------------------
// 描述:包含程序所使用宏定义
//-------------------------------------------------------------------------------------------------
#define NTESTS 14
#define NITERATIONS 20
//----------------------------------------- 【方法一】-------------------------------------------
// 说明:利用.ptr 和 []
//-------------------------------------------------------------------------------------------------
void colorReduce0(Mat &image, int div=64) {
int nl= image.rows; //行数
int nc= image.cols * image.channels(); //每行元素的总元素数量
for (int j=0; j<nl; j++)
{
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++)
{
//-------------开始处理每个像素-------------------
data[i]= data[i]/div*div + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//-----------------------------------【方法二】-------------------------------------------------
// 说明:利用 .ptr 和 * ++
//-------------------------------------------------------------------------------------------------
void colorReduce1(Mat &image, int div=64) {
int nl= image.rows; //行数
int nc= image.cols * image.channels(); //每行元素的总元素数量
for (int j=0; j<nl; j++)
{
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++)
{
//-------------开始处理每个像素-------------------
*data++= *data/div*div + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//-----------------------------------------【方法三】-------------------------------------------
// 说明:利用.ptr 和 * ++ 以及模操作
//-------------------------------------------------------------------------------------------------
void colorReduce2(Mat &image, int div=64) {
int nl= image.rows; //行数
int nc= image.cols * image.channels(); //每行元素的总元素数量
for (int j=0; j<nl; j++)
{
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++)
{
//-------------开始处理每个像素-------------------
int v= *data;
*data++= v - v%div + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//----------------------------------------【方法四】---------------------------------------------
// 说明:利用.ptr 和 * ++ 以及位操作
//----------------------------------------------------------------------------------------------------
void colorReduce3(Mat &image, int div=64) {
int nl= image.rows; //行数
int nc= image.cols * image.channels(); //每行元素的总元素数量
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
//掩码值
uchar mask= 0xFF<<n; // e.g. 对于 div=16, mask= 0xF0
for (int j=0; j<nl; j++) {
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++) {
//------------开始处理每个像素-------------------
*data++= *data&mask + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//----------------------------------------【方法五】----------------------------------------------
// 说明:利用指针算术运算
//---------------------------------------------------------------------------------------------------
void colorReduce4(Mat &image, int div=64) {
int nl= image.rows; //行数
int nc= image.cols * image.channels(); //每行元素的总元素数量
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
int step= image.step; //有效宽度
//掩码值
uchar mask= 0xFF<<n; // e.g. 对于 div=16, mask= 0xF0
//获取指向图像缓冲区的指针
uchar *data= image.data;
for (int j=0; j<nl; j++)
{
for (int i=0; i<nc; i++)
{
//-------------开始处理每个像素-------------------
*(data+i)= *data&mask + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
data+= step; // next line
}
}
//---------------------------------------【方法六】----------------------------------------------
// 说明:利用 .ptr 和 * ++以及位运算、image.cols * image.channels()
//-------------------------------------------------------------------------------------------------
void colorReduce5(Mat &image, int div=64) {
int nl= image.rows; //行数
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
//掩码值
uchar mask= 0xFF<<n; // e.g. 例如div=16, mask= 0xF0
for (int j=0; j<nl; j++)
{
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<image.cols * image.channels(); i++)
{
//-------------开始处理每个像素-------------------
*data++= *data&mask + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
// -------------------------------------【方法七】----------------------------------------------
// 说明:利用.ptr 和 * ++ 以及位运算(continuous)
//-------------------------------------------------------------------------------------------------
void colorReduce6(Mat &image, int div=64) {
int nl= image.rows; //行数
int nc= image.cols * image.channels(); //每行元素的总元素数量
if (image.isContinuous())
{
//无填充像素
nc= nc*nl;
nl= 1; // 为一维数列
}
int n= static_cast<int>(log(static_cast<double>(div))/log(2.0));
//掩码值
uchar mask= 0xFF<<n; // e.g. 比如div=16, mask= 0xF0
for (int j=0; j<nl; j++) {
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++) {
//-------------开始处理每个像素-------------------
*data++= *data&mask + div/2;
//-------------结束像素处理------------------------
} //单行处理结束
}
}
//------------------------------------【方法八】------------------------------------------------
// 说明:利用 .ptr 和 * ++ 以及位运算 (continuous+channels)
//-------------------------------------------------------------------------------------------------
void colorReduce7(Mat &image, int div=64) {