原理是根据C++中的“/” 运算自动取余特点。
1 //--------------------------------------------------------------------------------
2 //#include <opencv2/core/core.hpp>
3 //#include <opencv2/highgui/highgui.hpp>
4 #include <opencv.hpp>
5 #include <iostream>
6 using namespace std;
7 using namespace cv;
8
9 //--------------------------------------------------------------------------------
10 // 全局函数声明
11 //--------------------------------------------------------------------------------
12 void colorReduce(Mat& inputImg, Mat& outputImg, int div);
13 //--------------------------------------------------------------------------------
14 // main()控制台入口函数
15 //--------------------------------------------------------------------------------
16 int main()
17 {
18 //创建原始图像并显示
19 Mat srcImage = imread("1.jpg");
20 namedWindow("原始图像");
21 imshow("原始图像", srcImage);
22 //按照原始图像的参数规格创建效果图像
23 Mat dstImage;
24 dstImage.create(srcImage.rows, srcImage.cols, srcImage.type());
25 //count times;
26 double time0 = static_cast<double>(getTickCount());
27 //use colorReduce function;
28 colorReduce(srcImage, dstImage, 16);
29 //统计时间
30 time0 = ((double)getTickCount() - time0) / getTickFrequency();
31 cout<<endl<<"此方法的时间是"<<time0<<"s"<<endl;
32 //imshow dstimage
33 namedWindow("效果图");
34 imshow("效果图",dstImage);
35 waitKey(0);
36 }
3 //#include <opencv2/highgui/highgui.hpp>
4 #include <opencv.hpp>
5 #include <iostream>
6 using namespace std;
7 using namespace cv;
8
9 //--------------------------------------------------------------------------------
10 // 全局函数声明
11 //--------------------------------------------------------------------------------
12 void colorReduce(Mat& inputImg, Mat& outputImg, int div);
13 //--------------------------------------------------------------------------------
14 // main()控制台入口函数
15 //--------------------------------------------------------------------------------
16 int main()
17 {
18 //创建原始图像并显示
19 Mat srcImage = imread("1.jpg");
20 namedWindow("原始图像");
21 imshow("原始图像", srcImage);
22 //按照原始图像的参数规格创建效果图像
23 Mat dstImage;
24 dstImage.create(srcImage.rows, srcImage.cols, srcImage.type());
25 //count times;
26 double time0 = static_cast<double>(getTickCount());
27 //use colorReduce function;
28 colorReduce(srcImage, dstImage, 16);
29 //统计时间
30 time0 = ((double)getTickCount() - time0) / getTickFrequency();
31 cout<<endl<<"此方法的时间是"<<time0<<"s"<<endl;
32 //imshow dstimage
33 namedWindow("效果图");
34 imshow("效果图",dstImage);
35 waitKey(0);
36 }
1 void colorReduce(Mat& inputImage, Mat& outputImage, int div)
2 {
3 //参数准备
4 outputImage = inputImage.clone();
5 int rowNum = outputImage.rows;
6 int colNum = outputImage.cols;
7 //存彩色图像
8 for (int i = 0; i < rowNum; i++)
9 {
10 for (int j = 0; j < colNum; j++)
11 {
12 //------------------处理每个像素-----------------
13 outputImage.at<Vec3b>(i,j)[0] = outputImage.at<Vec3b>(i,j)[0]/div*div + div/2; //---B
14 outputImage.at<Vec3b>(i,j)[1] = outputImage.at<Vec3b>(i,j)[1]/div*div + div/2; //G
15 outputImage.at<Vec3b>(i,j)[2] = outputImage.at<Vec3b>(i,j)[2]/div*div + div/2; //R
16 }
17
18 }
19 }
3 //参数准备
4 outputImage = inputImage.clone();
5 int rowNum = outputImage.rows;
6 int colNum = outputImage.cols;
7 //存彩色图像
8 for (int i = 0; i < rowNum; i++)
9 {
10 for (int j = 0; j < colNum; j++)
11 {
12 //------------------处理每个像素-----------------
13 outputImage.at<Vec3b>(i,j)[0] = outputImage.at<Vec3b>(i,j)[0]/div*div + div/2; //---B
14 outputImage.at<Vec3b>(i,j)[1] = outputImage.at<Vec3b>(i,j)[1]/div*div + div/2; //G
15 outputImage.at<Vec3b>(i,j)[2] = outputImage.at<Vec3b>(i,j)[2]/div*div + div/2; //R
16 }
17
18 }
19 }
学习来源于opencv3入门biancheng。 感谢作者