OpenCV에는 C++ STL 호환 'Mat iterator' 클래스가 있습니다. 이 'Mat iterator' 클래스를 사용하면 픽셀에 매우 쉽게 액세스할 수 있습니다. 'Mat iterator' 클래스의 객체를 생성해야 합니다. 'Mat_
다음 프로그램은 OpenCV에서 Iterator 메서드를 사용하여 색상을 줄이는 방법을 보여줍니다.
예
#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; }
출력