Scrolling images in vector

So, mirroring this topic

I believe to be encountering a similar issue with my program. Essentially I am mapping the x coordinate of the ofRectangle object to the series of frames. My problem is this jerky, jittering between different elements in the array. What I mean is as I scroll along the x axis to cycle through array, different elements(images) are loaded than whats printed out. so, the result would be a consistent scroll with occasional random elements throughout the entire cycle.

i’ve made sure the datatypes are the same, but I am a bit unsure what I am overseeing here.

cpp below:

#include "testApp.h"
#include "ObjectFinder.h"


using namespace ofxCv;
using namespace cv;

void testApp::setup() {
    
    ofDirectory dir;
    
    int nFiles = dir.listDir("miko");
    if(nFiles) {
        
        for(int i=0; i<dir.numFiles(); i++) {
            
            // add the image to the vector
            string filePath = dir.getPath(i);
            ofImage img;
         images.push_back(ofImage(img));
           images.back().loadImage(filePath);
        
        }
        
    }
    else printf("Could not find folder\n");
    
    
    
    
    
	ofSetVerticalSync(true);
    
    //faces.load("haarcascade_frontalface_default.xml");

	finder.setup("haarcascade_frontalface_default.xml");
	finder.setPreset(ObjectFinder::Fast);
	
    
    //we can now get back a list of devices.
	vector<ofVideoDevice> devices = cam.listDevices();
	
    for(int i = 0; i < devices.size(); i++){
		cout << devices[i].id << ": " << devices[i].deviceName;
        if( devices[i].bAvailable ){
            cout << endl;
        }else{
            cout << " - unavailable " << endl;
        }
	}
    
	//cam.setDeviceID(0);
    cam.setDesiredFrameRate(60);
    cam.initGrabber(320, 240);
}

void testApp::update() {
	cam.update();
	if(cam.isFrameNew()) {
		finder.update(cam);
	}
}

void testApp::draw() {
	
    
    for(int i = 0; i < finder.size(); i++) {
        ofRectangle object = finder.getObjectSmoothed(i);

       // newX = object.x;
        newX = object.getPositionRef().x;

    }
    
    newX = ofClamp(newX, 0, 150);
    scaledX = ofMap(newX, 0,150, 0, 155);

    
    float targetX = scaledX;
    float dx = scaledX - x;
    if(abs(dx) > 1) {
        x += dx * easing;
    }
    cout << "x coordinate = " << x <<endl;
    
    
    images[x].draw(0,0);
    

    
	cam.draw(0, 0);
    finder.draw();
	ofDrawBitmapStringHighlight(ofToString(finder.size()), 10, 20);
    //cout << "x coordinate" << cur.x;

}

and header:

#pragma once

#include "ofMain.h"
#include "ofxCv.h"

class testApp : public ofBaseApp {
public:
	void setup();
	void update();
	void draw();
	
	ofVideoGrabber cam;
	ofxCv::ObjectFinder finder;
    
   vector <ofImage> images;

    float xPicVal;
    
    float easing = 0.06;
   int x;
 int newX;
  int scaledX;
    
};
1 Like

You are using x (which is an int) as the index for your std::vector full of images. Then, for (some reason) you are adding a float (dx * easing) using to an your int index x … which causes your index to be subject to unpredictable rounding errors resulting in image vector indices that will flipflop between some int x and an adjacent int value (causing your images to flicker as it switches between the images in the vector I’d guess). I believe that you may be mixing up your x value (which I think you are trying to use as a position) with the image index that you want to draw …

Hey Chris,

Then, for (some reason) you are adding a float (dx * easing) using to an your int index x .

well, I was trying to use a smoothing algorithm to create momentum a bit between image frames change.

Though, even if I go about this:

images[scaledX].draw(0,0);

I am still faced with similar results.

which causes your index to be subject to unpredictable rounding errors resulting in image vector indices that will flipflop between some int x and an adjacent int value

I am a bit unclear then, especially if these integers are unassigned beforehand, and to my understanding, not arbitrary to the algorithm I am (trying) to implement.

I believe that you may be mixing up your x value (which I think you are trying to use as a position)

correct

hope you had a good semester, btw!