#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace std;
using namespace cv;
//完成减少颜色的工作
//用指针访问像素
void colorReduce1(Mat &inputimage, Mat& outputimage, int div);
//用迭代器
void colorReduce2(Mat &inputimage, Mat& outputimage, int div);
int main()
{
Mat srcImage = imread("F:\\C++project\\picturetest\\2.jpg");
imshow("src", srcImage);
Mat dstImage;
dstImage.create(srcImage.rows, srcImage.cols, srcImage.type());
double time0 = static_cast<double>(getTickCount());
colorReduce1(srcImage, dstImage, 32);
time0 = ((double)getTickCount() - time0) / getTickFrequency();
cout << "The last time is :" << time0 << endl;
imshow("the result ", dstImage);
waitKey(0);
return 0;
}
void colorReduce1(Mat &inputimage, Mat& outputimage, int div)
{
outputimage = inputimage.clone();
int rowNumber = outputimage.rows;
int colNumber = outputimage.cols*outputimage.channels();
for (int i = 0; i < rowNumber; i++)
{
uchar*data = outputimage.ptr<uchar>(i);
for (int j = 0; j < colNumber; j++)
{
data[j] = data[j] / div * div+div/2;
}
}
}
void colorReduce2(Mat &inputimage, Mat& outputimage, int div)
{
outputimage = inputimage.clone();
Mat_<Vec3b>::iterator it = outputimage.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = outputimage.end<Vec3b>();
for (; it != itend; ++it)
{
(*it)[0] = (*it)[0] / div * div + div / 2;
(*it)[1] = (*it)[1] / div * div + div / 2;
(*it)[2] = (*it)[2] / div * div + div / 2;
}
}
知识点:
1.颜色空间缩减
2.计时函数:
double time0 = static_cast<double>(getTickCount());
.....
time0 = ((double)getTickCount() - time0) / getTickFrequency();
3.访问图像像素的两个方法对比:
访问地址:0.011s 迭代器:0.3952s