using namespace cv;
void colorReduce0(cv::Mat image, int div=64) {
int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line
for (int j=0; j<nl; j++) {
// get the address of row j
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++) {
// process each pixel ---------------------
data[i]= data[i]/div*div + div/2;
// end of pixel processing ----------------
} // end of line
}
}
void colorReduce1(const cv::Mat &image, cv::Mat &imageOut,int div=64) {
int nl= image.rows; // number of lines
int nc= image.cols * image.channels(); // total number of elements per line
for (int j=0; j<nl; j++) {
// get the address of row j
const uchar* data= image.ptr<uchar>(j);
uchar* dataOut= imageOut.ptr<uchar>(j);
for (int i=0; i<nc; i++) {
// process each pixel ---------------------
dataOut[i]= data[i]/div*div + div/2;
// end of pixel processing ----------------
} // end of line
}
}
int main(int argc, char** argv)
{
Mat img1 = imread("lena.jpg");
Mat img2=img1.clone();
//Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
if(img1.empty() )
{
printf("Can't read one of the images\n");
return -1;
}
imshow("org", img1);
waitKey(0);
cv::Mat imageOut(img1.rows,img1.cols,img1.type());
colorReduce1(img1,imageOut ,64);
imshow("imageOut", imageOut);
waitKey(0);
colorReduce0(img1, 128);
// drawing the results
imshow("reduce128", img1);
colorReduce0(img2, 64);
// drawing the results
imshow("reduce64", img2);
waitKey(0);
return 0;
}