openCV bicubic imresize创建负值

时间:2015-05-10 07:54:37

标签: matlab opencv image-processing antialiasing bicubic

我注意到,当使用双三次插值对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

1 个答案:

答案 0 :(得分:3)

正如你在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