

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to reduce the color using Iterator Method in OpenCV?
OpenCV has C++ STL compatible 'Mat iterator' class. Using this 'Mat iterator' class, we can access pixels very easily. We have to create an object of 'Mat iterator' class. We can do it as 'Mat_<Vec3b>: : iterator example'. We have to use an underscore after 'Mat' like 'Mat_' because it is a template method. In this method, the return type must be specified while creating an object of 'iterator' class. That is why we have declared the datatype <Vec3b>.
The following program demonstrates how to reduce the color using Iterator Method in OpenCV.
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> using namespace std;//Declaring std namespace using namespace cv;//Declaring cv namespace void reducing_Color(Mat& image, int div = 64){ //Declaring the function// Mat_<Vec3b>::iterator iterator_start;//Declaring starting iterator// iterator_start = image.begin<Vec3b>();//Obtain iterator at initial position// Mat_<Vec3b>::iterator iterator_end;//Declaring ending iterator// iterator_end = image.end<Vec3b>();//Obtain iterator an end position// for (; iterator_start != iterator_end; iterator_start++){ //Loop for all pixels// (*iterator_start)[0] = (*iterator_start)[0] / div * div + div / 2;//Process pixels of first channel// (*iterator_start)[1] = (*iterator_start)[1] / div * div + div / 2;//Process pixels of second channel// (*iterator_start)[2] = (*iterator_start)[2] / div * div + div / 2;//Process pixels of third channel// } } int main() { Mat image;//taking an image matrix// image = imread("mango.jpg");//loading an image// namedWindow("Image Window");//Declaring another window// reducing_Color(image);//calling the function// imshow("Image Window", image);//showing the image with reduced color// waitKey(0); return 0; }
Output
- Related Questions & Answers
- How to reduce color using Pointer Method in OpenCV?
- How to detect the color using OpenCV in C++?
- How to track the color in OpenCV using C++?
- How to convert color spaces in OpenCV using C++?
- How to create custom color maps in Java using OpenCV?
- How to change the color spaces of an image using Java OpenCV library?
- How to apply the reduce method for objects in JavaScript?
- The iterator() method of CopyOnWriteArrayList in Java
- How to change Pixel values using Direct Access Method in OpenCV?
- Average with the Reduce Method in JavaScript
- IntStream iterator() method in Java
- DoubleStream iterator() method in Java
- LongStream iterator() method in Java
- ArrayBlockingQueue iterator() method in Java
- Return Largest Numbers in Arrays passed using reduce method?
Advertisements