컬러 감축
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
// .ptr와 [] 사용하기(.at보다 빠름)
void colorReduce(Mat &image, int div=64) {
int nl= image.rows; // 행 개수
int nc= image.cols * image.channels(); // 각 행에 있는 원소의 총 개수
for (int j=0; j<nl; j++) {
uchar* data= image.ptr<uchar>(j);
for (int i=0; i<nc; i++) {
data[i]= data[i]/div*div + div/2;
}
}
}
void main()
{
Mat image;
int64 t, tinit;
image= imread("image.jpg");
if (!image.data) {
cerr<<"Error: 이미지 파일을 읽을 수 없습니다!!"<<endl;
exit(1);
}
imshow("original", image);
tinit = getTickCount();
colorReduce(image);
t = getTickCount() - tinit;
cout << "using .ptr and [] =" << 1000.*t/getTickFrequency() << "ms" << endl;
imshow("color reduce", image);
waitKey(0);
}