Problem with ofPixels.h and getColor

Hi, im trying to make this in OF: https://www.youtube.com/watch?v=NbX3RnlAyGU (min 10:22)
but after 5 or so seconds i get this exception in the ofPixels.h file.

ofColor_<PixelType> ofPixels_<PixelType>::Pixel::getColor() const{
	ofColor_<PixelType> c;
	switch(pixelFormat){
		case OF_PIXELS_RGB:
			c.set( pixel[0], pixel[1], pixel[2] );
			break;

this is my setup function:


	
	imagen.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR);
	imagen.load("osaka.jpg");
	imagen.resize(ofGetWidth(), ofGetHeight());
	pixelesImagen.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR);
	pixelesImagen = imagen.getPixels();


	for (int i = 0;i < ofGetWidth()*ofGetHeight()+1;i += 1) {
		Sines newSines;
		s.push_back(newSines);
	}
	

and this is my Draw function:

for (int i = 0;i < ofGetWidth();i += 11) {
		for (int j = 0;j < ofGetHeight();j += 11) {
			int index = 0;
			ofColor c;
			if (s[(i + ofGetWidth() * j)].posx < imagen.getWidth() && s[(i + ofGetWidth() * j)].posy < imagen.getHeight()) {
				index = pixelesImagen.getPixelIndex(s[(i + ofGetWidth() * j)].posx, s[(i + ofGetWidth() * j)].posy);
			}
			else {
				c.set(0, 0, 0);
			}
			c = imagen.getColor(index);
			ofSetColor(c);
			
			s[(i + ofGetWidth() * j)].SineUpdate(floatSlider);
			s[(i + ofGetWidth() * j)].SineDraw();
		
		}
	}

i made a screen recording: https://photos.app.goo.gl/Q9iwjECsqybgGi7W7

I believe the error is caused because the position of the bubbles goes further away than the image size(i hope that made sense) and so the getColor function cant get the parameters it needs, that’s why i tried putting that if statement, but it didn’t work. What should i try?

Hello @Alejo_RGB,
this looks really hard to read.


if (s[(i + ofGetWidth() * j)].posx < imagen.getWidth() && s[(i + ofGetWidth() * j)].posy < imagen.getHeight()) {
   			index = pixelesImagen.getPixelIndex(s[(i + ofGetWidth() * j)].posx, s[(i + ofGetWidth() * j)].posy);
   		}

Did you know that the getColor() method also takes X and Y inputs? Instead of just getColor(index), you can also use getColor(x,y). This will make it easier to debug the math.

Another way to make debugging easier is to change your window size to your image dimension in main.cpp.

Note:, you don’t need to call imagen.allocate() if you are loading it from disk. You also don’t need to call pixelesImagen.allocate() if you use the getPixels() method.

1 Like

i figured it out, the problem was that the blobs went out of the picture so they didn’t have any image data to get the color from.

	if (posx > ofGetWidth() - 20) {
		velocityx = -velocityx;
	}
	if (posx < 0 + 10) {
		velocityx = -velocityx;
	}



	if (posy > ofGetHeight() - 20) {
		velocityy = -velocityy;
	}
	if (posy < 0 + 10) {
		velocityy = -velocityy;
	}