i'm trying to implement bicubic interpolation;
this is a follow-up question to my question:MATLAB vs C++ vs OpenCV - imresize
about imresize, so i know openCV doesn't do cubic interpolation like matlab.
and i need to get to the same results like matlab. for imresize.
so for an example I want to get just the first pixel of my image
int* Utilities::MatlabImresize(int* channel,int width, int height, double scale)
{
double test[4][4] = {{channel[0],channel[1], channel[2], channel[3]},
{channel[width],channel[width+1], channel[width+2], channel[width+3]},
{channel[2*width],channel[2*width+1], channel[2*width+2], channel[2*width+3]},
{channel[3*width],channel[3*width+1], channel[3*width+2], channel[3*width+3]}};
double x = bicubicInterpolate(test,0.5,0.5);
return NULL;
}
double Utilities::cubicInterpolate (double p[4], double x)
{
return p[1] + 0.5 * x*(p[2] - p[0] + x*(2.0*p[0] - 5.0*p[1] + 4.0*p[2] - p[3] + x*(3.0*(p[1] - p[2]) + p[3] - p[0])));
}
double Utilities::bicubicInterpolate (double p[4][4], double x, double y)
{
double arr[4];
arr[0] = cubicInterpolate(p[0], y);
arr[1] = cubicInterpolate(p[1], y);
arr[2] = cubicInterpolate(p[2], y);
arr[3] = cubicInterpolate(p[3], y);
return cubicInterpolate(arr, x);
}
I still don't get the same results so I'm guessing my kernel isn't the same as the one matlab are using. how can i get the same results?
**my original 4x4<**br>
155 306 155 306
293 213 293 213
172 325 172 324
291 198 290 198
the results i'm getting from matlab:
151.196136474609 155.925476074219 155.555526733398 145.714401245117
151.044921875000 155.254089355469 157.459579467773 154.982849121094
149.490341186523 151.587142944336 150.641662597656 155.392364501953
153.666915893555 156.283508300781 156.848739624023 155.557098388672
147.997482299805 154.688049316406 157.798034667969 152.912796020508
my result for pixel 0,0 is 155.3437500000000
update:
using @andrey's advice i went into the imresize code
this is what was written there:
Cubic Convolution Interpolation for Digital Image % Processing," IEEE Transactions on Acoustics, Speech, and Signal % Processing, Vol. ASSP-29, No. 6, December 1981, p. 1155.
http://www.academia.edu/2266812/Cubic_convolution_interpolation_for_digital_image_processing
absx = abs(x);
absx2 = absx.^2;
absx3 = absx.^3;
f = (1.5*absx3 - 2.5*absx2 + 1) .* (absx <= 1) + ...
(-0.5*absx3 + 2.5*absx2 - 4*absx + 2) .* ...
((1 < absx) & (absx <= 2));
I do not understand how this function:
float Utilities::Cubic(const float& x, const float& scale)
{
/*
% See Keys, "Cubic Convolution Interpolation for Digital Image
% Processing," IEEE Transactions on Acoustics, Speech, and Signal
% Processing, Vol. ASSP-29, No. 6, December 1981, p. 1155.
*/
float absx = fabs(x*scale);
float absx2 = pow(absx, 2);
float absx3 = pow(absx, 3);
float f = (1.5*absx3 - 2.5*absx2 + 1) * (absx <= 1) +
(-0.5*absx3 + 2.5*absx2 - 4*absx + 2) *
((1 < absx) & (absx <= 2));
return f*scale;
}
x is the distance between the point to be interpolated and the grid point being considered
can handle 4X4 matrix like the other cubic interpolation I have?