Best way to copy part of three ofImage in another ofImage

I have three images, extracted from three cameras.

What I’m trying to do is copy different slices of them (one slice for each image) in another ofImage containing the three slices.
For each slice I would need to specify both the coordinates of the source image from where to start copying and the coord of the target image where this slice should be placed.

The cropFrom() method doesn’t look like what I’m looking for, and the drawSubSection look promising but I can’t specify the source image.

What is the best approach to do it?
N00b here, sorry :blush:

Are you working at the pixel level (i.e. do you need to save these slices to disk) or are you drawing them to the screen?

Just drawing them on screen

'm not sure what you mean by you can’t specify the source image, but couldn’t you do something like this:

ofImage img0;
ofImage img1;
ofImage img2;
int w = 30; // slice width
int h = 60; // slice height

img0.drawSubsection(0, 0, w, h, 0, 0);
img1.drawSubsection(w, h, w, h, w, 0);
img2.drawSubsection(2 * w, h, w, h, 2 * w, 0);

Something like that should give you three vertical slices sourced from three images …

Yes you are right, but how do I create an ofImage from them?
I will later need to pass this image composed by slices to openCV (as a single image)

oh, I solved, that was easy :frowning:
Once I sliced the three images, I can create an ofImage from an area of the screen with the grabScreen method

grabScreen can be kind of slow, another way to do this if you need to access the final pixels is by using cropTo to crop to an ofPixels and then paste those in the correct position in the final image:

ofPixels crop;





thx arturo, this is exactly what I was looking for!
Thx a lot

1 Like

Hello Arturo,

I am really new to this, and I don’t know where to write the code you provided… is it in the setup(), draw(), update()? which part is in .h, which is in .cpp?

Thank you.

Welcome to OF @TitusHora :slight_smile:

At you can find video tutorials and the ofBook, both should clarify the questions you are having :slight_smile:

@TitusHora, it depends if what you want to draw changes or not, if it doesn’t then in setup would be the best since you will only do it once and use it multiple times. if the images change then in update.

The ofPixels and ofImages should be declared in ofApp.h so you can access them from all the methods though.

Also if you just want this for drawing take a look at image.drawSubsection which allows to draw only a part of an image and it’s way faster than this method

In any case as @hamoid says if you have doubts about this kind of issues then you probably need to understand better what each of those functions do

Thank you, @arturo, much appreciated.

@hamoid, thank you.

I am learning, slowly… however, this one I needed fast :slight_smile: