save a opengl Image with opencv
Ok, ich hab hier mal eine Funktion geschrieben mit der man opengl Bilder mit opencv speichern kann. Generell kann man auch alles andere außer opencv nehmen, aber es bietet sich hier gerade an.
Es ist bestimmt nicht die feine Art Pointer zu verbiegen, aber in der BV kann man es sich nicht leisten unnötig Daten zu kopieren.
Code:
int saveImage()
{
// buffer for the opengl image
unsigned char *image;
//Allocate our buffer for the image
if ((image = (unsigned char*)malloc(3* windowswidth * windowsheight * sizeof(char))) == NULL)
{
cerr << "Failed to allocate memory for image\n";
return -1;
}
char fname[32];
static int counter = 0;
// create a filename
snprintf(fname, sizeof(fname), "jpg/%05d.jpg", counter);
// Copy the image into our buffer
glReadBuffer(GL_BACK_LEFT);
glReadPixels(0,0,windowswidth, windowsheight,GL_RGB,GL_UNSIGNED_BYTE,image);
// create a new opencv image
// windowswidth & windowsheight ist that what you write
// by glutInitWindowSize (windowswidth, windowsheight);
IplImage *cvimage = cvCreateImageHeader(cvSize(windowswidth, windowsheight), IPL_DEPTH_8U, 3);
// Ugly Stuff
cvimage->imageData = (char*)image;
// flip it vertically
cvConvertImage(cvimage, cvimage, CV_CVTIMG_FLIP);
// save it
cvSaveImage(fname , cvimage);
counter++;
free(image);
return 0;
}