#include<cv.h> #include<highgui.h> void colorReduce(cv::Mat &imag, int div = 64) { int n1 = imag.rows; int nc = imag.cols*imag.channels();//每一行的像素数目(多通道的像素需要乘3) for (int j = 0; j < n1; j++)//对每一行像素点进行操作 { uchar *data = imag.ptr<uchar>(j);//定位到每一行像素数组的首地址 for (int i = 0; i < nc; i++) { data[i] = data[i] / div*div + div / 2;//缩小像素点范围可以利用data[i]=0和data[i]=255求出最大像素值范围 } } } int main() { cv::Mat image = cv::imread("C:\Users\dell\Desktop\2.jpg"); colorReduce(image); cv::namedWindow("Image"); cv::imshow("Image", image); cv::waitKey(0); return 0; }