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.
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:
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
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;
}
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