我注意到,在使用双三次插值对openCV中的矩阵进行下采样时,即使原始矩阵都是正的,我也会得到负值。
我附上以下代码作为示例:
// 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个随机缩小矩阵中的最小值( eahc矩阵的原始尺寸为1000x1000):
currentMinVal = 1e6;
for k=1:1000
x = rand(1000);
x = imresize(x,0.5);
minVal = min(currentMinVal,min(x(:)));
end
发布于 2015-05-10 17:07:56
正如您在this answer中看到的,双三次核不是非负的,因此,在某些情况下,负系数可能占主导地位并产生负输出。
您还应该注意到,Matlab默认为使用'Antialiasing'
,这对结果有影响:
I = zeros(9);I(5,5)=1;
imresize(I,[5 5],'bicubic') %// with antialiasing
ans =
0 0 0 0 0
0 0.0000 -0.0000 -0.0000 0
0 -0.0000 0.3055 0.0000 0
0 -0.0000 0.0000 0.0000 0
0 0 0 0 0
imresize(I,[5 5],'bicubic','Antialiasing',false) %// without antialiasing
ans =
0 0 0 0 0
0 0.0003 -0.0160 0.0003 0
0 -0.0160 1.0000 -0.0160 0
0 0.0003 -0.0160 0.0003 0
0 0 0 0 0
https://stackoverflow.com/questions/30149026
复制相似问题