最終更新: mikk_ni3_92 2007年10月09日(火) 10:13:04履歴
現在地 >> メニュー >> サンプルコード::OpenCV >> 減色処理
#include <cv.h>
#include <highgui.h>
#include <iostream>
#define MAX_CLUSTERS (8)
using namespace std;
int main( int argc, char **argv)
{
IplImage *imgA = cvLoadImage( "test2.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(imgA ==NULL)
{
cout<<"Can't Load Image ." << endl;
exit(0);
}
unsigned long int size;
size = imgA->width * imgA->height;
CvMat *clusters;
clusters = cvCreateMat (size, 1, CV_32SC1);//32 ビット 1チャンネル
CvMat *points;
points = cvCreateMat (size, 1, CV_32FC3); //32 ビット 3チャンネル
unsigned long int i;
for (i = 0; i < size; i++)
{
points->data.fl[i*3] = (unsigned char) imgA->imageData[i*3];
points->data.fl[i*3 + 1] = (unsigned char) imgA->imageData[i*3 + 1];
points->data.fl[i*3 + 2] = (unsigned char) imgA->imageData[i*3 + 2];
}
cvKMeans2 (points, MAX_CLUSTERS, clusters, cvTermCriteria (CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0));
CvMat *color = cvCreateMat (MAX_CLUSTERS, 1, CV_32FC3);
CvMat *count = cvCreateMat (MAX_CLUSTERS, 1, CV_32SC1);
cvSetZero (color);
cvSetZero (count);
for (i = 0; i < size; i++)
{
int idx = clusters->data.i[i];
int j = ++count->data.i[idx];
color->data.fl[idx * 3 ] = color->data.fl[idx * 3 ] * (j - 1) / j + points->data.fl[i * 3 ] / j;
color->data.fl[idx * 3 + 1] = color->data.fl[idx * 3 + 1] * (j - 1) / j + points->data.fl[i * 3 + 1] / j;
color->data.fl[idx * 3 + 2] = color->data.fl[idx * 3 + 2] * (j - 1) / j + points->data.fl[i * 3 + 2] / j;
}
for (i = 0; i < size; i++)
{
int idx = clusters->data.i[i];
imgA->imageData[i * 3 ] = (char) color->data.fl[idx * 3 ];
imgA->imageData[i * 3 + 1] = (char) color->data.fl[idx * 3 + 1];
imgA->imageData[i * 3 + 2] = (char) color->data.fl[idx * 3 + 2];
}
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvWaitKey(0);
cvReleaseImage( &imgA );
cvDestroyWindow("window");
return 0;
}
#include <cv.h>
#include <highgui.h>
#include <iostream>
#define MAX_CLUSTERS (8)
using namespace std;
int main( int argc, char **argv)
{
IplImage *imgA = cvLoadImage( "test2.jpg", CV_LOAD_IMAGE_ANYDEPTH | CV_LOAD_IMAGE_ANYCOLOR);
if(imgA ==NULL)
{
cout<<"Can't Load Image ." << endl;
exit(0);
}
unsigned long int size;
size = imgA->width * imgA->height;
CvMat *clusters;
clusters = cvCreateMat (size, 1, CV_32SC1);//32 ビット 1チャンネル
CvMat *points;
points = cvCreateMat (size, 1, CV_32FC3); //32 ビット 3チャンネル
unsigned long int i;
for (i = 0; i < size; i++)
{
points->data.fl[i*3] = (unsigned char) imgA->imageData[i*3];
points->data.fl[i*3 + 1] = (unsigned char) imgA->imageData[i*3 + 1];
points->data.fl[i*3 + 2] = (unsigned char) imgA->imageData[i*3 + 2];
}
cvKMeans2 (points, MAX_CLUSTERS, clusters, cvTermCriteria (CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0));
CvMat *color = cvCreateMat (MAX_CLUSTERS, 1, CV_32FC3);
CvMat *count = cvCreateMat (MAX_CLUSTERS, 1, CV_32SC1);
cvSetZero (color);
cvSetZero (count);
for (i = 0; i < size; i++)
{
int idx = clusters->data.i[i];
int j = ++count->data.i[idx];
color->data.fl[idx * 3 ] = color->data.fl[idx * 3 ] * (j - 1) / j + points->data.fl[i * 3 ] / j;
color->data.fl[idx * 3 + 1] = color->data.fl[idx * 3 + 1] * (j - 1) / j + points->data.fl[i * 3 + 1] / j;
color->data.fl[idx * 3 + 2] = color->data.fl[idx * 3 + 2] * (j - 1) / j + points->data.fl[i * 3 + 2] / j;
}
for (i = 0; i < size; i++)
{
int idx = clusters->data.i[i];
imgA->imageData[i * 3 ] = (char) color->data.fl[idx * 3 ];
imgA->imageData[i * 3 + 1] = (char) color->data.fl[idx * 3 + 1];
imgA->imageData[i * 3 + 2] = (char) color->data.fl[idx * 3 + 2];
}
cvNamedWindow("window",CV_WINDOW_AUTOSIZE);
cvShowImage("window",imgA);
cvWaitKey(0);
cvReleaseImage( &imgA );
cvDestroyWindow("window");
return 0;
}