2008/06/04

OpenCV: Rebuild the library with Windows, GTK+ 2.x

To fix: Install latest gtk+2.x ( 2.12.9 as of now ).

Remove all the *Windows functions,


#include 
#include

#define MAX_CLUSTERS (32) /* NX^ */

#include
#include

static struct rusage rusage_before;
static struct rusage rusage_after;

void
startTimer(void)
{
getrusage(RUSAGE_SELF, &rusage_before);
}


float
stopTimer(void)
{
long tsec, tusec;

getrusage(RUSAGE_SELF, &rusage_after);

tsec = rusage_after.ru_utime.tv_sec - rusage_before.ru_utime.tv_sec;
tusec = rusage_after.ru_utime.tv_usec - rusage_before.ru_utime.tv_usec;

return (tsec + ((float)tusec) / 1000000.0);
}

int
main (int argc, char **argv)
{
int i, size;
IplImage *src_img = 0, *dst_img = 0;
CvMat *clusters;
CvMat *points;
CvMat *color = cvCreateMat (MAX_CLUSTERS, 1, CV_32FC3);
CvMat *count = cvCreateMat (MAX_CLUSTERS, 1, CV_32SC1);

// (1)
if (argc != 2 || (src_img = cvLoadImage (argv[1], CV_LOAD_IMAGE_COLOR)) == 0)
return -1;

size = src_img->width * src_img->height;
dst_img = cvCloneImage (src_img);
clusters = cvCreateMat (size, 1, CV_32SC1);
points = cvCreateMat (size, 1, CV_32FC3);

// (2)sNZls
for (i = 0; i < size; i++) {
points->data.fl[i * 3 + 0] = (uchar) src_img->imageData[i * 3 + 0];
points->data.fl[i * 3 + 1] = (uchar) src_img->imageData[i * 3 + 1];
points->data.fl[i * 3 + 2] = (uchar) src_img->imageData[i * 3 + 2];
}

// (3)NX^O
startTimer();
cvKMeans2 (points, MAX_CLUSTERS, clusters, cvTermCriteria (CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 10, 1.0));
printf( "finished making cmapped pix: %7.3f secs\n", stopTimer());

// (4)eNX^lvZ
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 + 0] = color->data.fl[idx * 3 + 0] * (j - 1) / j + points->data.fl[i * 3 + 0] / 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;
}

// (5)NX^F`
for (i = 0; i < size; i++) {
int idx = clusters->data.i[i];
dst_img->imageData[i * 3 + 0] = (char) color->data.fl[idx * 3 + 0];
dst_img->imageData[i * 3 + 1] = (char) color->data.fl[idx * 3 + 1];
dst_img->imageData[i * 3 + 2] = (char) color->data.fl[idx * 3 + 2];
}

#if 0
// (6)\CL[I
cvNamedWindow ("src", CV_WINDOW_AUTOSIZE);
cvShowImage ("src", src_img);
cvNamedWindow ("low-color", CV_WINDOW_AUTOSIZE);
cvShowImage ("low-color", dst_img);
cvWaitKey (0);

cvDestroyWindow ("src");
cvDestroyWindow ("low-color");
#endif

cvReleaseImage (&src_img);
cvReleaseImage (&dst_img);
cvReleaseMat (&clusters);
cvReleaseMat (&points);
cvReleaseMat (&color);
cvReleaseMat (&count);

return 0;
}



[vuhung@teriyaki c]$./kmeans
OpenCV ERROR: Unspecified error (The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support)
in function cvNamedWindow, window.cpp(71)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)
Terminating the application...
called from cvUnregisterType, cxpersistence.cpp(4933)