Segmentation Fault on beginners image processing problem

Hello, I'm fairly new to C++ and image processing. This seems to be something easy to fix, but I can't figure it out.

Here's my code:

#include <cstdlib>
#include <iostream>
#include "Image.h"
#include "Dip.h"

using namespace std;

#define Usage "Proj1 img1 img2\n"

int main(int argc, char *argv[])
{
Image img1, img2; // the original image
int nr, nc, ntype, nchan, i, j, k, N, r, g, b, max;
float maxr, maxg, maxb;
Image red, green, blue;

// check if the number of arguments on the command line is correct
if (argc < 3) {
cout << Usage;
exit(3);
}

// read the input image file
img1 = readImage(argv[1]);
nr = img1.getRow();
nc = img1.getCol();
ntype = img1.getType();
nchan = img1.getChannel();

img2.createImage(nr, nc, ntype);

red = img1.getRed();
maxr = red.getMaximum();
green = img1.getGreen();
maxg = green.getMaximum();
blue = img1.getBlue();
maxb = blue.getMaximum();
max = int((maxr + maxg + maxb) / 3);

for (i=0; i<nr; i++){
for (j=0; j<nc; j++){
for(k=0; k<nchan; k++){
img2(i, j, k) = img1(i, j, k) + (max/2 - img1(i+2,j+2,k));
}
}
}

writeImage(img2, argv[2]);


system("PAUSE");
return EXIT_SUCCESS;
}

I get a segmentation fault when I run the program like this, but if I change
img2(i, j, k) = img1(i, j, k) + (max/2 - img1(i+2,j+2,k));
to
img2(i, j, k) = img1(i, j, k) + (max/2 - img1(i,j+2,k));
it works. The algorithm for the process contains the i+2.
The header files are given by the professor, so they work fine.

Any help would be awesome. thanks
Last edited on
Topic archived. No new replies allowed.