I noticed that when downsampling matrices in openCV using the bicubic interpolation I get negative values even though the original matrix was all positive.
I attach the following code as an example:
// Declaration of variables
cv::Mat M, MLinear, MCubic;
double minVal, maxVal;
cv::Point minLoc, maxLoc;
// Create random values in M matrix
M = cv::Mat::ones(1000, 1000, CV_64F);
cv::randu(M, cv::Scalar(0), cv::Scalar(1));
minMaxLoc(M, &minVal, &maxVal, &minLoc, &maxLoc);
// Printout smallest value in M
std::cout << "smallest value in M = "<< minVal << std::endl;
// Resize M to quarter area with bicubic interpolation and store in MCubic
cv::resize(M, MCubic, cv::Size(0, 0), 0.5, 0.5, cv::INTER_CUBIC);
// Printout smallest value in MCubic
minMaxLoc(MCubic, &minVal, &maxVal, &minLoc, &maxLoc);
std::cout << "smallest value in MCubic = " << minVal << std::endl;
// Resize M to quarter area with linear interpolation and store in MLinear
cv::resize(M, MLinear, cv::Size(0, 0), 0.5, 0.5, cv::INTER_LINEAR);
// Printout smallest value in MLinear
minMaxLoc(MLinear, &minVal, &maxVal, &minLoc, &maxLoc);
std::cout << "smallest value in MLinear = " << minVal << std::endl;
I don't understand why this happens. I noticed that if I choose random values between [0,100] the smallest value is after resizing is typically ~-24 vs. -0.24 for the range of [0,1] as in the code above.
As a comparison, in Matlab this doesn't occur (I am aware of a slight difference in weighting schemes as appears here: imresize comparison - Matlab/openCV).
Here's a short Matlab code snippet that saves the smallest value in any of 1000 random downsized matrices (original dimensions of eahc matrix 1000x1000):
currentMinVal = 1e6;
for k=1:1000
x = rand(1000);
x = imresize(x,0.5);
minVal = min(currentMinVal,min(x(:)));
end