Sampling an area from an image


I want to do a what seems like a simple task, but I’m getting a bit confused with all the possibilities in OF. Basically I want to sample a 5x5 pixel area from an image to begin with and draw it.

I load up a 100x100 image, get a random (center) position for my 5x5 bucket from that image. The question then becomes, how do I copy/crop that 5x5 pixel data from ofImageto an ofPixels? ofFbo? ofTexture? object. Which one should I use and why? Is it a case-specific decision and can’t be case-agnostic?

Let’s say I just want to draw that random sampled 5x5 pixel area on screen in the end to simplify things.


Possibly someone else may have a better solution, but this is what I would do:

To draw:

ofImage img;
img.getTexture().drawSubsection(0, 0, 5, 5, 0, 0); // Upper left 5*5

To get the pixel-data:

ofPixels fiveByFive;
fiveByFive.crop(0, 0, 5, 5); // Upper left 5*5
1 Like

yes this depends a lot on the case but if all you want to do is to draw that area then what @Jildert proposes, using drawSubsection, should be the easiest and the fastest

I was looking at the ofPixel class and all the “setFromPixels” methods.
As I’ve loaded the pixel data from an ofImage using

ofLoadImage(pixData, exampleImage);

I’d love something that would allow me to create an array of vector of ofPixels ‘buckets’ from the pixData object, i.e. setFromPixels(x_pos, y_pos, w, h, channels); Perhaps there’s something that would let me do that manually knowing the width and height of the original sampled image and the index NR in the pixData object?

I’d loop through the pixData object creating a bunch of smaller pixel data objects.

My aim is to later compare a randomly given 5x5 ofPixel object to any of those sampled buckets.