I am trying to implement bicubic interpolation,
this is the next question to my question: MATLAB vs C ++ vs OpenCV - imresize
about imresize, so I know that openCV does not perform cubic interpolation like matlab. and I need to get the same results as matlab. for increase.
so for example, I want to get only 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 do not get the same results, so I assume that my kernel is not the same as the one used by Matlab. 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 get 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 imresize code
here is what it says:
Cubic Convolution Interpolation for Digital Image Processing% ", IEEE Transactions in 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) { 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 is considered
can handle 4X4 matrix, like other cubic interpolation?