CPlusPlus TOP Example with input image

Hi All,

I am trying to write a CPlusPlus TOP that takes an input image, does some processing and then outputs the resulting image. I looked at both of the examples available and they don’t take any input images. Also I found nothing in the documentation. I haven’t used openGL before, so it’s not easy to just know that I can get an FBO index as I don’t know what to do with it. I would really appreciate an example.

My ultimate goal is to get an openGL memory location that I can use in OpenCV as a GpuMat.

Thank you,

–alberto

Your best bet might be to check out the latest iteration of the openFrameworks C++ TOP examples as I believe the latest examples take inputs and perform oF processing on them then spit them out, so you could grab what you need from that and use openCV native.

On a side note, we did something similar but ended up using ofxCV to use openCV through oF so we could still use some oF helper functions. So you may be tempted to go down the same route once you have the oF examples compiled, but if you’re comfortable with vanilla openCV and C++, I’d actually recommend trying native openCV first as I think it might be quite a bit faster than dealing with oF.

Thanks for your reply. I haven’t been able to figure out how to get a handle to the image given the FBO number. I suppose if I didn’t want to use oF I could just install it and reverse engineer what it’s doing but it seems to me that an example of this should be included somewhere or the documentation should give a hint as to how to do it. It just seems weird to me and it’s the only think keeping me so far from committing to use TouchDesigner for my project.
An example of a CPlusPlus TOP that takes an input image and outputs the same image should exist somewhere.
In the meantime I will try oF and see if I can find the answer.

Thanks again
–alberto

Elburz meant the TouchDesigner Cplusplus TOP examples for Openframeworks which are available as a zip on this page:
derivative.ca/wiki088/index … Frameworks
(ignore the sentence on that page about the openframeworks examples only for 32bit, that’s not the case anymore since Of9.x)

Also see bottom paragraph of that page for some example code where the input of the Cplusplus TOP is used.

Sorry, I wasnt clear. The link Idzard sent will have the examples I mentioned, which are C++ TOPs that take an input image and process them with oF, so you can just ignore the oF part, and take the parts of the code that reference the input image. I agree the C++ TOP could use some love, but for now that would be your best bet. You wouldn’t need to even download/install oF if you’re comfortable enough with C++ to just grab the necessary code from that example.

I was able to use oF to solve my problem. That solves my immediate problem. Thanks for you help. As soon as I have some spare time I will try to solve it without oF. I will post my solution when I do.

Thanks again.

The methods used to fetch the incoming texture aren’t oF specific, only the extra processing that happens to it uses oF, so you could use those in a vanilla C++ TOP.

Actually, this is how I’m getting the pixels in the image according to the example (seems pretty oF specific):

const OP_TOPInput *topInput = inputs->getInputTOP(0);

ofTexture texture;
ofPixels pixels;

texture.setUseExternalTextureID(topInput->textureIndex);
texture.texData.width = topInput->width;
texture.texData.height = topInput->height;
texture.texData.tex_w = topInput->width;
texture.texData.tex_h = topInput->height;
texture.texData.tex_t = 1.0f;
texture.texData.tex_u = 1.0f;
texture.texData.textureTarget = topInput->textureType;

pixels.allocate(width, height, 3);
texture.readToPixels(pixels);

texture.allocate(pixels);
renderer->draw(texture, 0, 0, 0, width, height, 0, 0, width, height);

The “readToPixels” line is the one that takes very long to execute. What I think I need is just a pointer to the pixels so I can process them where they are without copying them or downloading them to RAM.

Thanks for all your help,

–alberto

I find myself using the VS peek definition function basically all the time when working on C++ OPs. So that’s the line I was referencing getting from the example:

const OP_TOPInput *topInput = inputs->getInputTOP(0);

Then you can see this is how it’s read into the ofTexture:

topInput->textureIndex

To work without the oF, you can just grab the OpenGL texture using that texture index and feed it to an openCV mat (stackoverflow.com/questions/1215 … opencv-mat). If you peek definition on the OP_TOPInput you can see some inline documentation

// The OpenGL Texture index for this TOP.
// This is only valid when accessed from C++ TOPs.
// Other C++ OPs will have this value set to 0 (invalid).
GLuint textureIndex;

Thanks for your reply Elburz. I have another question: If the texture is already in the GPU, why not just use a GpuMat instead of a Mat? I’m trying to avoid copying the pixel data from GPU to CPU and then copying it back to GPU. Is this possible?

I’m no OpenCV expert, so you very well may be able to go straight to a GPU mat since you have the OpenGL texture index. Should be straight forward to test it if you solved your previous issue of finding the texture.