Simply save ofTexture from videoGrabber

#1

Hey Guys,
i´m stucked in a very basic problem. I want to getTexture() from a videoGrabber. I also tried the way with ofFbo and ofBuffer. But I cant come to a result. I think its a silly mistake i did. But please help. Thank you!

ofApp.h:

#pragma once

#include "ofMain.h"

class ofApp : public ofBaseApp{

	public:
		void setup();
		void update();
		void draw();

		void mousePressed(int x, int y, int button);

		void gotMessage(ofMessage msg);
		
    ofVideoGrabber cam;
    ofTexture texture;
    ofBufferObject buffer;
    ofFbo fbo;
    
};

ofApp.cpp

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
    int w = 320;
    int h = 240;
    cam.setup(w,h);
    texture.allocate(w,h, GL_RGBA);
    buffer.allocate();
    buffer.bind(GL_TEXTURE_BUFFER);
    
    fbo.allocate(200, 200);
    fbo.begin();
    ofClear(255,255,255);
    fbo.end();
}

//--------------------------------------------------------------
void ofApp::update(){
    cam.update();
    
    
    fbo.begin();
    ofClear(255,255,255);
   cam.draw(0,0);
    fbo.end();
  
}

//--------------------------------------------------------------
void ofApp::draw(){
    cam.draw(0,0);
   texture.draw(500,0);
   
    
}



//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
    //cam.getTexture().copyTo(buffer);
    fbo.getTexture().copyTo(buffer);
   
    texture.loadData(buffer, GL_RGBA, GL_UNSIGNED_BYTE);
   
    
}


#2

do you want to save a frame of the video as an image in the disk?

then the easiest is to use the pixels not the texture:

ofSaveImage(cam.getPixels(), "image.png");
#3

no, i want to copy it in a ofTexture (or ofImage would also be fine) and later in a vector
It worked fine in a previous step when I used grabScreen. But I don’t want to do this drawing and copying on stage before.

#4

the only way to copy a texture into another is using an fbo just create an fbo, draw the video into it and get the internal texture with getTexture

you could also use ofImage::setFromPixels(cam.getPixels()) which would be slightly slower but avoids creating an fbo (or many if you want to save several frames

#5

Thank you, arturo! the problem was I think, that I allocated the ofTexture just once and in a vector he replaces all the instances with the same ofTexture.
But now I´m using ofImage::setFromPixels(cam.getPixels()) and it works fine for me.