Passing a reference of an ofImage vector to a class

Hi, I have some issues with creating ofImage from the pixels of a video. I want to create a vector of ofImage where the images in the vector have been set by pixels from a new video frame. This is so I can draw sections of the image with drawSubsection().

This works fine in ofApp.cpp like so:

void ofApp::setup(){
    video0.load("focal0.mov");    
    video0.play();
}
void ofApp::update(){
    video0.update();
}
void ofApp::draw(){
    video0.getCurrentFrame();
    if (video0.isFrameNew()){  
        ofPixels pixels = video0.getPixels();
        image0.setFromPixels(pixels);
        images.push_back(image0);
    }
    for (int i = 0; i< images.size(); i++) {
        images[0].drawSubsection(50, 50, 100, 100, 50, 50);
        images.clear(); //shows the next new video frame.     
    }
}

But what I want to do now is create a reference of the images vector so I can pass it to a class and then draw multiple videos with multiple drawSubsection(). I have got this working if I use normal .jpg images that I load in but I get issues with trying it with video pixels saved to an ofImage.

These variables change slightly using pointers

ofImage* image0;
vector<ofImage*> images;

I created a getImages function in my ofApp

vector<ofImage*> *ofApp::getImages(){
    
    return &images;
} 

I then call the constructor in the set up of ofApp

for(int x = 0; x < 72; x++){
    for(int y = 0; y < 36; y++){
        imSec.push_back(Section(getImages(), (x%12)*75, (y%6)*100));
    }
}

The program builds and runs successfully. The video starts playing but then the program crashes due to a BAD_ACCESS. (screenshot)

Any help on this would be really appreciated
Thanks

Think I actually solved it.

Before I was doing image0->setFromPixels(pixels); as image0 was a pointer of type ofImage (ofImage* image0;).

So what I did was created a new ordinary ofImage variable imageN0.

I then use imageN0 to set from pixels. I then made image0 equal a reference of imageN0. Then I pushed image0 into my images vector. Like so:

imageN0.setFromPixels(pixels);
image0 = &imageN0;
images.push_back(image0);

Now my reference to the vector of images work. It is a bit slow. But clearly, it looks like you can’t setFromPixels() with a pointer of type ofImage.

If anyone has a better approach that would be good to hear but this is how I got around it.

you can definitely call setFromPixels on an ofImage pointer if it’s properly allocated, you must be moving it somewhere or doing something that invalidates the pointer.

if you can avoid using pointers it makes things much easier and unless you need to share some resource it’s usually not necessary.

you can do the same without pointers and make it as fast. instead of creating the image outside the vector and then pushing it back create it directly inside the vector and then call the methods there:

images.emplace_back();
images.back().setFromPixels(pixels):

the first methods to access the vector of images can also be:

vector<ofImage> & ofApp::getImages(){
    return images;
}

if you need it but it’s usually a better idea to pass things from ofApp to other classes than trying to access things from ofApp from other objects
which returns areference instead of a pointer

Thanks for the advice @arturo

I tried to have ago in implementing this

images.emplace_back();
images.back().setFromPixels(pixels);

But when I run everything now I just get a blank screen. I know it is kind of working because if I put a break point on images.back().setFromPixels(pixels); I see that pixels value is the size of my video frames.

I also tried to push back an empty ofImage and then apply the above methods but that had the same result. Do you know what I could be missing in my implementation? Thanks

void ofApp::draw(){  
    video0.getCurrentFrame();
    if (video0.isFrameNew()){
        ofPixels pixels = video0.getPixels();
        images.emplace_back();
        images.back().setFromPixels(pixels);
    }
    for (int i = 0; i< imSec.size(); i++) {  
        imSec[i].display();
    }
}
vector<ofImage> & ofApp::getImages(){  
    return images;
}

it’s hard to know what might be going on without seeing the code that does the actual drawing. another way to speed it up would be to do:

 if (video0.isFrameNew()){
        images.emplace_back(video0.getPixels());
    }

which creates the image directly from the video pixels avoiding at least one copy per frame

For now all I am doing is simply drawing the image from the vector in my class like so:

Section::Section(vector<ofImage> imSection, int xPos, int yPos){
//...
}
void Section::display(){
    for (int i = 0; i< imagesSection.size(); i++) {  
        imagesSection[0].draw(0,0);
    }
}

But neither approach shows anything on the screen. Which is a bit strange.

mmh, but that’s a different vector than images in ofApp no?

Well it is a reference to it (or trying to be).

With my getImages function, I am trying to pass the images vector to my class as a reference.

This is the function.

vector<ofImage> & ofApp::getImages(){
    
    return images;
}

And this is how I call the constructor in ofApp.cpp setup()

for(int x = 0; x < 72; x++){
    for(int y = 0; y < 36; y++){
        imSec.push_back(Section(getImages(), (x%12)*53, (y%6)*60));
    }
}

Here I pass in getImages which I think is supposed to be a reference of the images vector. So therefore imagesSection vector is referenced to the images vector