首页 > 其他 > 详细

颜色空间缩减,降低运算复杂度,保留代表性颜色

时间:2015-10-10 12:20:43      阅读:744      评论:0      收藏:0      [点我收藏+]

原理是根据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 }

 

  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 }

 

学习来源于opencv3入门biancheng。 感谢作者 

 

颜色空间缩减,降低运算复杂度,保留代表性颜色

原文:http://www.cnblogs.com/gyearth/p/4866291.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!