我注意到在使用双三次插值法(downsampling)缩小openCV中的矩阵时,即使原始矩阵全为正值,仍然会得到负值。
我附上以下代码作为示例:
我不明白为什么会发生这种情况。我注意到,如果我在[0,100]之间选择随机值,则调整大小后的最小值通常为~-24,而范围为[0,1]的最小值为-0.24,如上面的代码所示。
作为比较,在Matlab中不会发生这种情况(我知道这里有一种轻微的加权方案差异,如此处所示:imresize comparison - Matlab/openCV)。
这是一个简短的Matlab代码片段,保存了任何1000个随机缩小矩阵中的最小值(每个矩阵的原始尺寸为1000x1000):
我附上以下代码作为示例:
// 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;
我不明白为什么会发生这种情况。我注意到,如果我在[0,100]之间选择随机值,则调整大小后的最小值通常为~-24,而范围为[0,1]的最小值为-0.24,如上面的代码所示。
作为比较,在Matlab中不会发生这种情况(我知道这里有一种轻微的加权方案差异,如此处所示:imresize comparison - Matlab/openCV)。
这是一个简短的Matlab代码片段,保存了任何1000个随机缩小矩阵中的最小值(每个矩阵的原始尺寸为1000x1000):
currentMinVal = 1e6;
for k=1:1000
x = rand(1000);
x = imresize(x,0.5);
minVal = min(currentMinVal,min(x(:)));
end