MATLAB: Interp3 RGB – temperature

interp3?rgb

Hello,
I would like to write a piece of code which takes the RGB values from a picture and change them in temperature.
I have a calibration file which has for each RGB a temperature. I wanted to use the interp3 to start from the calibration file and go to the new picture.
THe function interp3 gives me an error when I use it, since it says that V (in my case temperature) should be a 3D matrix.
PLease, would you mind helping me or addressing in a different direction.
Thank you very much
Antonio
I attacch my piece of code
%calRGB.txt contains 4 columns: R,G,B,temperature (calibration file)
load calRGB.txt
%RGB_f.dat contains 5 columns, x, y, R, G, B of the picture (2d picture)
load RGB_f.dat
RGB=RGB_f;
x=RGB(:,1);
y=RGB(:,2);
v=RGB(:,3);
w=RGB(:,4);
z=RGB(:,5);
%initialize temp
temp=x;
temp(:)=0;
%interpolation
for ii = 1:length(x(:,1))
temp(ii)=interp3(calRGB(:,1), calRGB(:,2),calRGB(:,3),calRGB(:,4), v(ii),w(ii),z(ii));
end
%write the temperature for each point on a variable
output=RGB_f;
output(:,:,:)=0;
for ii = 1:length(x(:,1))
output(ii,:)=[x(ii), y(ii) ,temp(ii)];
end

Best Answer

Although you can probably get interp3 to work in this case, I think it is preferable to to use the function TriScatteredInterp for this. It is a two-step process, in which you first create the interpolant from the calibration file, then use that interpolant on the data. You also don't need to use the for loops, because the whole thing can be vectorized. I believe that the code below does what you want, but of course you should check:
% calRGB.txt contains 4 columns: R,G,B,temperature (calibration file)
load calRGB.txt
% calRGB = [0 0 0 0; ...
% 0 0 1 1; ...
% 0 1 0 2; ...
% 0 1 1 3; ...
% 1 0 0 4; ...
% 1 0 1 5; ...
% 1 1 0 6; ...
% 1 1 1 7];
% RGB_f.dat contains 5 columns, x, y, R, G, B of the picture (2d picture)
load RGB_f.dat
% RGB_f = [1 1 0.5 0.5 0.5; ...
% 2 2 0.7 0.7 0.7];
RGB=RGB_f;
x=RGB(:,1);
y=RGB(:,2);
v=RGB(:,3);
w=RGB(:,4);
z=RGB(:,5);
temperatureInterpolant = TriScatteredInterp(calRGB(:,1), calRGB(:,2),calRGB(:,3),calRGB(:,4));
output = [x,y,temperatureInterpolant(v,w,z)]
Note that there is also a syntax for calling TriScatteredInterp that does not require you to split up your RGB data into three separate vectors, but I did not want to stray too far from the syntax of your original code.
Hope that helps. [Note that I included (commented out) a very simple example of RGB arrays that worked for me.]