Compose and image and then save it

Hi,

I want to take a series of snapshots of a webcam and compose those snapshots into a new image and then save that image to disk. At the moment I’m using ofFbo to compose the image and then a series of commands that just seem off to me and I’m wondering if I’m taking a less-than-handy route.

Here’s what’s happening:

I have a vector<ofImage*> of ofImage instances, they’re all 200x200 px. I compose the FBO object (which is 400x400 px):

  
  
    fbo.begin();  
    ofBackground(255, 0, 255);  
    imgs[0]->draw(0,0);  
    imgs[1]->draw(200,0);  
    imgs[2]->draw(0,200);  
    imgs[3]->draw(200,200);  
  fbo.end();  
  

This next part is what my question’s about, it’s where I go from FBO to saved image on disk:

  
  
    ofTexture resultTex;  
    resultTex = fbo.getTextureReference();  
      
    ofPixels pix;  
    resultTex.readToPixels(pix);  
  
    ofImage img;  
    img.setFromPixels(pix);  
  
    img.saveImage("snapshot" + ofToString(imagesSaved) + ".png");  
    imagesSaved++; // an int  
  

Is this simply how this goes? What other methods are there of composing images which can be saved to disk?

I don’t know if this would be of any help, but the code below is what i have used to save my images onto my HD with a time/date stamp on the name.

  
  
currentTime = time(0);  
tmStruct = *localtime(&currentTime);  
strftime(image, sizeof(image), "images/image-%d-%m-%Y__%H-%M-%S.jpg", &tmStruct);  
ofSaveImage(gFGcolorImg.getPixelsRef(), image, OF_IMAGE_QUALITY_BEST);  
//cout << image << endl;  
return image;  
  
  

Thanks for the snippet but it’s not quite what I meant. The flow I was referring to, going from FBO to ofTexture to ofPixels to ofImage, seems to have some unnecessary steps. I’m using FBO since it’s the only way I’ve found so far, which allows me to compose multiple images (and/or shapes for that matter) into one image, and then write that result to disk.

I did some speedtests to seperate the time the [FBO > ofTexture > ofPixels > ofImage] cycle takes from the time it takes to save the result, also checked out some different file formats. The FBO is 470x470 px.

[FBO > ofTexture > ofPixels > ofImage] : about 20ms
saving to JPG : about 18ms (BEST or WORST quality doesn’t make much difference)
saving to PNG : 83 ms
saving to TIF : about 18ms (surprising to me, thought the lack of compression would speed things up faster than JPG)

So I probably want to save to JPG instead of PNG, and that will help. Still I’m curious if my goal (compose image and save to disk) can be achieved in any different way than what I’m doing right now…

Take a look at ofxMsAThreadedImagerSaver if you need to save an image without a performance hit to your app. Sometimes depending on the size of the image can cause an app to slow down.

Hey, there’s progress already! Thanks for that tip. If there’s any helpers on the first part, I’m all ears too.

you can just do:

  
  
ofFbo fbo;  
ofPixels pix;  
  
fbo.readToPixels(pix);  
ofSaveImage(pix,path);  
  

1 Like

No way to get this right for me… ( i have tested all methods )

ofImage src;
src.loadImage( path );
src.getTextureReference().readToPixels( pxs );
ofSaveImage( pxs, ss.str() );

The error is hard to explain, an image will be clearer > http://frankiezafe.org/fileadmin/sites/frankiezafe/labo/trashy-image-saved.jpg

Entry image on the left, output image on the right.
I’m doing the operation in update(), i’m running on mint 17.2 with nouveau drivers
Note: copying pixels from an fbo is even worse! > the best i had is an image of the correct size, filled with random pixels…

// a bit later

problem seems to be related to “jpg” export, png are doing well!
related to : https://github.com/openframeworks/openFrameworks/issues/4324

sorry for the noise…