How to do it...
To apply the iterators to the color reduction example, the following steps are performed:
- We are going to loop over the pixels using the usual begin and end iterator methods, except that these ones are, again, template methods. Then, we have to get the begin position:
// obtain iterator at initial position cv::Mat_<cv::Vec3b>::iterator it= image.begin<cv::Vec3b>();
- Then, we must obtain the end position of the iterator:
// obtain end position cv::Mat_<cv::Vec3b>::iterator itend= image.end<cv::Vec3b>();
- Next, we must loop over the iterator until the end position:
// loop over all pixels for ( ; it!= itend; ++it) {
- Finally, apply the color reduction function to the pixel:
// 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 ---------------- } }
Remember that the iterator here returns a cv::Vec3b instance because we are processing a color image. Each color channel element is accessed using the dereferencing operator [].