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;
}
Sorry for filling the forums with all of my problems, but I have one more.
that’s what the forums are for
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:
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.
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?
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.
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];
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) :
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…