多说无益,先贴代码,再看解析:
函数的功能是实现图像颜色的缩减。
<span style="font-size:14px;">#include <opencv2\core\core.hpp> #include <opencv2\highgui\highgui.hpp> using namespace std; using namespace cv; void colorReduce(Mat &src,Mat &dst, int div = 64); //注意:这里的div是个默认参数,其初始化只能在函数声明中进行,函数定义里不能给div初始化 int main(){ Mat source = imread("1.jpg",1); Mat reduceColor(source.size(), source.channels()); colorReduce(source, reduceColor); imshow("source", source); imshow("reducecolor", reduceColor); waitKey(0); } void colorReduce(Mat &src, Mat &dst,int div){ src.copyTo(dst); int nline = dst.rows; int nchannel = dst.cols*dst.channels();//在opencv中颜色是按照BGR的顺序存储,故每行的通道数为前面的式子 for (int j = 0; j < n1ine; j++) { uchar* data = dst.ptr<uchar>(j); //获取每行的头地址 for (int i = 0; i < nchannel; i++) { data[i] = data[i]/div*div + div/2;//颜色缩减方法 } } } </span>
时间: 2024-10-09 22:36:23