# Revision history [back]

int iVal = inputImage1->at<uchar>(i,j) - inputImage2->at<uchar>(i,j);

This line. Cast them to int before subtracting. Why? The subtraction is being performed, then it's turning to int. So if you do 0-1, you get 255, which is then stored. If you set a breakpoint you'll see that your condition checking for less than 0 is never true.

  int iVal = (int)inputImage1->at<uchar>(i,j) - (int)inputImage2->at<uchar>(i,j);


Also, that is what references are for. Passing things into functions without making copies, that is.

int iVal = inputImage1->at<uchar>(i,j) - inputImage2->at<uchar>(i,j);

This line. Cast them to int before subtracting. Why? The subtraction is being performed, then it's turning to int. So if you do 0-1, you get 255, which is then stored. If you set a breakpoint you'll see that your condition checking for less than 0 is never true.

  int iVal = (int)inputImage1->at<uchar>(i,j) - (int)inputImage2->at<uchar>(i,j);


Also, that is what references are for. Passing things into functions without making copies, that is.

int iVal = inputImage1->at<uchar>(i,j) - inputImage2->at<uchar>(i,j);EDIT: See comments about saturate_cast and the missing else.

This line. Cast them to int before subtracting. Why? The subtraction is being performed, then it's turning to int. So if you do 0-1, you get 255, which is then stored. If you set a breakpoint you'll see that your condition checking for less than 0 is never true.

  int iVal = (int)inputImage1->at<uchar>(i,j) - (int)inputImage2->at<uchar>(i,j);


Also, that is what references are for. Passing things into functions without making copies, that is.