# Revision history [back]

### Option 1

Use cv::absdiff.

### Option 2

When performing the subtraction, specify an "intermediate data depth" capable of storing all possible intermediate values - from the positive limit to the negative limit.

Example:

cv::Mat matDiff; // will be filled with result.
cv::Mat matTemp; // temporarily holds the intermediate result.
cv::subtract(matOne, matTwo, matTemp, cv::noArray(), CV_32S);
matTemp = cv::abs(matTemp);
matTemp.convertTo(matDiff, CV_8U); // Or other precisions (depths).


Both options should work. However, if you find that one of the options is not working as documented, be sure to file a bug report or share your findings here. Thanks.

### Option 1

Use cv::absdiff.

### Option 2

When performing the subtraction, specify an "intermediate data depth" capable of storing all possible intermediate values - from the positive limit to the negative limit.

Example:

cv::Mat matDiff; // will be filled with result.
cv::Mat matTemp; // temporarily holds the intermediate result.
cv::subtract(matOne, matTwo, matTemp, cv::noArray(), CV_32S);
matTemp = cv::abs(matTemp);
matTemp.convertTo(matDiff, CV_8U); // Or other precisions (depths).


Both options should work. However, if you find that one of the options is not working as documented, be sure to file a bug report or share your findings here. Thanks.

I posted both options because my use of an earlier version of OpenCV had a bug in absdiff that requires the workaround. I haven't checked ever since. Anyone using the advice above should manually verify that the code works on their version of OpenCV before further use.