More pixel troubles

Sorry for filling the forums with all of my problems, but I have one more. I’ve been trying to average the pixel values of forty images by adding all the values together in an array and then dividing each totaled pixel by 40.

I am not 100% sure if I am adding them correctly, but I am definately doing something psychedelic to the image. In terms of dividing, no matter what I try, I can only at best get the code to evenly dim the image and at worst make it totally black (code featured below!). I figure that I probably need to make something a float in order to divide correctly, but I can’t figure out the syntax. I’ve been shuffling things around for hours with little luck.

  
processedPixels = new unsigned char[imageIn.width * imageIn.height * 4];  
	  
	int imageup = 16;  
	int num = imageIn.width * imageIn.height * 3;  
	  
	for(int b = 0; b < num; b++){   
		processedPixels[b] = 0;  
	}	  
	  
	for(int a = 12; a < imageup; a++){   
	  imageIn.loadImage("P20600"+ofToString(a)+".jpg");  
	  unsigned char * pix = imageIn.getPixels();  
  
		for(int i = 0; i < num; i++){   
			processedPixels[i] = pix[i] + processedPixels[i];	  
		}  
	}  
  
	for(int c = 0; c < num; c++){   
	  processedPixels[c] = processedPixels[c] / 40;  
	}	  

(This is pre-morning-coffe talk, but ) I think you need to make:

  
processedPixels = new unsigned char[imageIn.width * imageIn.height * 4];   

into:

  
processedPixels = new unsigned int[imageIn.width * imageIn.height * 4];   

Otherwise as an unsigned char it can only hold values up to 255, which as you add stuff will simply overflow, i.e 255+5 = 5 instead of 260

/A

also you are using ( * 3):

  
num = imageIn.width * imageIn.height * 3;   

to loop through an image of length ( * 4 ):

  
processedPixels = new unsigned int[imageIn.width * imageIn.height * 4];  

Sorry for filling the forums with all of my problems, but I have one more.

that’s what the forums are for :slight_smile:

hahakid is spot on - you will need to sum into something bigger. unsigned chars only 8 bits, so you are overflowing, float or double could also work. some info about data types is here:

http://www.space.unibe.ch/comp-doc/c-ma-…-types.html

good luck !!
-z

Alright, I figured out to change the char array to an int:

  
int processedPixels [imageIn.width * imageIn.height * 3];  

And I compiled it, but now got a new error with:

  
	imageOut.setFromPixels(processedPixels, imageIn.width, imageIn.height, OF_IMAGE_COLOR);   

It says… “note: candidates are: void ofImage::setFromPixels(unsigned char*, int, int, int)”

Clearly I need “processedPixels” to be a char in order to output it as an image.

I’ve been trying to make “processedPixels” a char with itoa, but I can’t seem to get it work.

  
 char bee[5760000]; //height * width * 3  
	char* itoa(processedPixels[c], bee, 10);   
  

And I’ve read elsewhere that itoa won’t even really do what I need it to do (if I do get it to work). In short, I am very confused again.

Hey Randy -

It is actually much simpler than that.
You can just put (unsigned char *) in front of processed pixels to ‘cast’ it to a unsigned char array. This tells the compiler that you want to treat your int array as unsigned char for just this operation. Just make sure you don’t have any values in your array over 255 or less than 0. I imagine you have divided all the processed pixels at this point by the number of times you have added into the array - so you should be good - but values outside of that range will wrap over and look quite strange.

Anyway

  
imageOut.setFromPixels((unsigned char *)processedPixels, imageIn.width, imageIn.height, OF_IMAGE_COLOR);  

Should do the trick.

You can read more about type casting in section 2.7 of The C Programming Language
http://guiguan.googlepages.com/TheCProg-…-dition.pdf

Theo

I did that. I now get this:

GDB: Program received signal: “EXC_BAD_ACCESS”

Someone I spoke with about this says my that my typing seems fine, but I must be doing something screwy with memory allocation. I don’t think I am. Then again, I wouldn’t know if I was…

Here is my code:

  
void testApp::setup(){	   
	ofBackground(255,255,255);	  
	  
	imageIn.loadImage("signature140.png");  
  
	int processedPixels [imageIn.width * imageIn.height * 4];  
	int imageup = 16;  
	int num = imageIn.width * imageIn.height * 3;  
	  
	for(int b = 0; b < num; b++){   
		processedPixels[b] = 0;  
	}	  
	for(int a = 12; a < imageup; a++){   
  
	  imageIn.loadImage("P20600"+ofToString(a)+".jpg");  
  
	  unsigned char * pix = imageIn.getPixels();  
  
		for(int i = 0; i < num; i++){   
			processedPixels[i] = pix[i] + processedPixels[i];	  
		}  
       printf("Characters: %c \n", 'a');   
		  
	}  
	  
	for(int c = 0; c < num; c++){   
	processedPixels[c] = processedPixels[c] / 40;  
	}	  
  
	imageOut.setFromPixels((unsigned char *)processedPixels, imageIn.width, imageIn.height, OF_IMAGE_COLOR);   
  
	imageOut.saveImage("averageImage.png");	  
}  
  

At one point, I’m adding a char to an int. Could this be it? I don’t see why it should be a problem?

  
processedPixels[i] = pix[i] + processedPixels[i];	  
  

prolly you are accessing an array out of bounds but I can’t see offhand.

fyi a good debugging technique, comment your code out backwards from the end of the function call that crashes and see what for loop is causing the crash. Work from the bottom to the top.

ie,

  
  
// imageOut.saveImage("averageImage.png");      
  

then

  
  
/*  
 imageOut.setFromPixels((unsigned char *)processedPixels, imageIn.width, imageIn.height, OF_IMAGE_COLOR);  
*/  
  

etc…

if you move backwards, you can often pinpoint the exact call that is causing the crash and see your error.

hope that helps –
zach

Alright… my problem is:

  
int processedPixels [imageIn.width * imageIn.height * 3];  

I have tried every variation of making this an int, long, float, etc. that I can think of. I have rewritten the statement in all kinds of valid configurations. I have tried hard coding the width and height. I have “cleared all” countless times. I’ve also been playing around with trying to reallocate a memory block with malloc and calloc… but somehow I think there has to be an easy answer to this. Why would this keep throwing me an error?

The only thing that will even let the program run is:

  
processedPixels = new unsigned char[imageIn.width * imageIn.height * 3];  
  

Why does C++ hate me?

c++ doesn’t hate you !!

you might be over-running the stack

the stack is for local variables, the heap is for dynamically allocated variables. you can set the stack stize somewhere in xcode, but it’s fixed, so you are likely overflowing it when you type it the way that isn’t working (as a local variable) :

http://www.cs.jcu.edu.au/Subjects/cp200-…-Stack.html
http://www.devx.com/tips/Tip/14276

if you need a temporary variable, (ie, not new) then you might need to poke around xcode, or your compiler about where to set that. I know how to do this for codewarrior.

google “stack size”, or search in the help of your compiler for that term.

if you need to do it on the heap, you can do what you are doing. If you need that memory repetitively, you can even do this:

  
  
float * aFrigginLotOfMemory = new float[1000000000];  // can't stop me !!  
// do something   
delete aFrigginLotOfMemory;  /// clean up !   
  

of course new-ing and deleting often is a bit taxing – there is time involved with large memory allocation calls, you would want to new in setup if you need that memory often for example…

hope that helps!!
zach

also could be helpful
http://developer.apple.com/qa/qa2005/qa1419.html

yeah that looks like it is it.
crazy I have never hit that before but:

  
int something[1024 * 768 * 4];  
  

in testApp::setup() brings up the debugger every time.

Looking at: http://developer.apple.com/qa/qa2005/qa1419.html
There are a couple of ways to increase your stack size, but it will always be limited by the max stack size of the system.

Zach’s suggestion of using a pointer is probably the better solution.

ps: setting the options listed in the apple doc didn’t allow the above code to run without crashing.