Hi I a trying to write a simple program that for a given x,y position in an image , loops through all the pixels in x, y’s vicinity (i.e. for a given radius) and swaps them with the identical pixels in another image.
When I run this program with a 20x20 image and the x,p positions as 10,10 and a radius of 5 I should have a square in the center of the destination image - but instead I get lines swapped out. I’m think I’m not understanding how to process the pixels correctly. Help!
ofPixels &srcPixels = img2.getPixels();
ofPixels &destPixels = img1.getPixels();
int h = img1.getHeight();
int w = img1.getWidth();
//set the radius for the area we want to swap pixelsaround eye gaze position
int radius = 5;
int diameter = (radius * 2) ; //diameter to change is double radius * 3 indices per pixel
posX = 10;
posY = 10;
posX = posX - radius;
posY = posY - radius;
//make sure we don't go off edge of image
if (posX < 0) {
// cout << "posx < 0";
posX = 0;
}
if (posY < 0) {
posY = 0;
// cout << "posx < 0";
}
// using the new adjusted PosX and PosY loop through to swap pixels out row by row
int index = (posY * w) + posX; // calculate first insertion point in array of pixels
for (int i = 0; i < diameter; i++) { // loop on Y coords
int temp = posX;
for (int j = 0; j < diameter; j++ ) { // loop on x coords
if (posX < w) {
// cout << "i " << i << "posX " << posX << "posY " << posY << "index " << index << ".\n";
destPixels[index] = srcPixels[index];
destPixels[index+1] = srcPixels[index+1];
destPixels[index+2] = srcPixels[index+2];
index = index + 2;
posX++;
}
}
if (posY < h) {
posY++;
posX = temp; //set x back to starting value
int index = (posY * w) + posX; // calculate first insertion point in array of pixels
}
}