/*------------------------------------------------------------------------------------------*\ This file contains material supporting chapter 2 of the cookbook: Computer Vision Programming using the OpenCV Library. by Robert Laganiere, Packt Publishing, 2011. This program is free software; permission is hereby granted to use, copy, modify, and distribute this source code, or portions thereof, for any purpose, without fee, subject to the restriction that the copyright notice may not be removed or altered from any source or altered source distribution. The software is released on an as-is basis and without any warranties of any kind. In particular, the software is not guaranteed to be fault-tolerant or free from failure. The author disclaims all warranties with regard to this software, any use, and any consequent failure, is purely the responsibility of the user. Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name \*------------------------------------------------------------------------------------------*/ #include #include #include // using .ptr and [] 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(j); for (int i=0; i(j); for (int i=0; i(j); for (int i=0; i(log(static_cast(div))/log(2.0)); // mask used to round the pixel value uchar mask= 0xFF<(j); for (int i=0; i(log(static_cast(div))/log(2.0)); int step= image.step; // effective width // mask used to round the pixel value uchar mask= 0xFF<(log(static_cast(div))/log(2.0)); // mask used to round the pixel value uchar mask= 0xFF<(j); for (int i=0; i(log(static_cast(div))/log(2.0)); // mask used to round the pixel value uchar mask= 0xFF<(j); for (int i=0; i(log(static_cast(div))/log(2.0)); // mask used to round the pixel value uchar mask= 0xFF<(j); for (int i=0; i::iterator it= image.begin(); cv::Mat_::iterator itend= image.end(); for ( ; it!= itend; ++it) { // process each pixel --------------------- (*it)[0]= (*it)[0]/div*div + div/2; (*it)[1]= (*it)[1]/div*div + div/2; (*it)[2]= (*it)[2]/div*div + div/2; // end of pixel processing ---------------- } } // using Mat_ iterator and bitwise void colorReduce9(cv::Mat &image, int div=64) { // div must be a power of 2 int n= static_cast(log(static_cast(div))/log(2.0)); // mask used to round the pixel value uchar mask= 0xFF<::iterator it= image.begin(); cv::Mat_::iterator itend= image.end(); // scan all pixels for ( ; it!= itend; ++it) { // process each pixel --------------------- (*it)[0]= (*it)[0]&mask + div/2; (*it)[1]= (*it)[1]&mask + div/2; (*it)[2]= (*it)[2]&mask + div/2; // end of pixel processing ---------------- } } // using MatIterator_ void colorReduce10(cv::Mat &image, int div=64) { // get iterators cv::Mat_ cimage= image; cv::Mat_::iterator it=cimage.begin(); cv::Mat_::iterator itend=cimage.end(); for ( ; it!= itend; it++) { // process each pixel --------------------- (*it)[0]= (*it)[0]/div*div + div/2; (*it)[1]= (*it)[1]/div*div + div/2; (*it)[2]= (*it)[2]/div*div + div/2; // end of pixel processing ---------------- } } void colorReduce11(cv::Mat &image, int div=64) { int nl= image.rows; // number of lines int nc= image.cols; // number of columns for (int j=0; j(j,i)[0]= image.at(j,i)[0]/div*div + div/2; image.at(j,i)[1]= image.at(j,i)[1]/div*div + div/2; image.at(j,i)[2]= image.at(j,i)[2]/div*div + div/2; // end of pixel processing ---------------- } // end of line } } // with input/ouput images void colorReduce12(const cv::Mat &image, // input image cv::Mat &result, // output image int div=64) { int nl= image.rows; // number of lines int nc= image.cols ; // number of columns // allocate output image if necessary result.create(image.rows,image.cols,image.type()); // created images have no padded pixels nc= nc*nl; nl= 1; // it is now a 1D array int n= static_cast(log(static_cast(div))/log(2.0)); // mask used to round the pixel value uchar mask= 0xFF<(j); const uchar* idata= image.ptr(j); for (int i=0; i(log(static_cast(div))/log(2.0)); // mask used to round the pixel value uchar mask= 0xFF<