interp3 RGB - temperature

3 次查看(过去 30 天)
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

采纳的回答

the cyclist
the cyclist 2011-3-31
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.]
  1 个评论
mortain Antonio
mortain Antonio 2011-4-5
Dear the cyclist,
however, may you tell me how I can make working interp3 for this case, please?
Thank you

请先登录,再进行评论。

更多回答(2 个)

mortain Antonio
mortain Antonio 2011-4-4
I am sorry if it results a bit big....
Let me know what you think about this
Dear the cyclist,
first of all, sorry if I reply quite late, I was not able to reply earlier. Thank you very much for your work and your exhaustive explanation, you really got my question. However, I tried to make working your code and it gives to me lots of NaN. It's a pity that I cannot add here the files I'm using, however, I'll try to put what I can... calRGB
3.3128203e+01 3.7530547e+01 3.7283281e+01 4.5000000e+01
3.2997578e+01 3.7545391e+01 3.7246562e+01 4.3860000e+01
3.3141016e+01 3.7380781e+01 3.7118984e+01 4.3430000e+01
3.3153516e+01 3.7557109e+01 3.7240000e+01 4.3150000e+01
3.3099766e+01 3.7484609e+01 3.7140156e+01 4.2720000e+01
3.3246016e+01 3.7328828e+01 3.6969375e+01 4.2400000e+01
3.3193594e+01 3.7447891e+01 3.7087656e+01 4.2140000e+01
3.3137422e+01 3.7337969e+01 3.7199453e+01 4.1750000e+01
3.3322969e+01 3.7625625e+01 3.7409922e+01 4.1460000e+01
3.3314219e+01 3.7642578e+01 3.7359063e+01 4.1150000e+01
3.3411641e+01 3.7439688e+01 3.7097734e+01 4.0880000e+01
5.0838125e+01 5.4981016e+01 5.8657422e+01 3.9900000e+01
5.2608594e+01 5.5843125e+01 6.1243906e+01 3.9750000e+01
5.3269297e+01 5.7343828e+01 6.3986719e+01 3.9500000e+01
5.3031953e+01 5.7514297e+01 6.5498750e+01 3.9000000e+01
5.2722187e+01 5.7567578e+01 6.6899609e+01 3.8800000e+01
5.2381797e+01 5.7205156e+01 6.7599922e+01 3.8600000e+01
5.1823281e+01 5.7159609e+01 7.0421719e+01 3.8440000e+01
5.1283516e+01 5.6775391e+01 7.1306406e+01 3.8250000e+01
5.0887422e+01 5.6915000e+01 7.2822422e+01 3.8150000e+01
5.0251172e+01 5.6550703e+01 7.4417656e+01 3.8000000e+01
4.8685859e+01 5.6264219e+01 7.8484297e+01 3.7850000e+01
4.7006562e+01 5.6163125e+01 8.2691797e+01 3.7550000e+01
4.4842656e+01 5.5794375e+01 8.7005859e+01 3.7300000e+01
4.1267109e+01 5.5336172e+01 9.3183125e+01 3.7010000e+01
3.9713906e+01 5.5516250e+01 9.6253984e+01 3.6850000e+01
3.7998047e+01 5.5524219e+01 9.9238750e+01 3.6750000e+01
3.7581875e+01 5.5256563e+01 9.9213594e+01 3.6450000e+01
3.6644375e+01 5.5386719e+01 1.0078375e+02 3.6250000e+01
3.6512656e+01 5.5705547e+01 1.0157664e+02 3.6120000e+01
3.6330156e+01 5.6167109e+01 1.0269156e+02 3.6000000e+01
3.6358359e+01 5.7191563e+01 1.0412586e+02 3.5800000e+01
3.6332734e+01 5.7965859e+01 1.0539773e+02 3.5500000e+01
3.6524844e+01 5.8514453e+01 1.0597375e+02 3.5350000e+01
3.6603438e+01 5.9457734e+01 1.0709766e+02 3.5100000e+01
3.6721562e+01 6.0058125e+01 1.0766813e+02 3.4850000e+01
3.6620937e+01 6.0420547e+01 1.0786492e+02 3.4750000e+01
3.6774297e+01 6.2394687e+01 1.0891781e+02 3.4630000e+01
3.6975391e+01 6.2988359e+01 1.0998594e+02 3.4500000e+01
3.6893437e+01 6.3211641e+01 1.0994758e+02 3.4380000e+01
3.6923750e+01 6.3640234e+01 1.0969352e+02 3.4250000e+01
3.7311953e+01 6.5263281e+01 1.1080906e+02 3.4000000e+01
3.7341641e+01 6.5609922e+01 1.1101641e+02 3.3850000e+01
3.7429844e+01 6.7125078e+01 1.1089172e+02 3.3480000e+01
3.7571250e+01 6.8247500e+01 1.1094383e+02 3.3300000e+01
3.7621250e+01 6.8676719e+01 1.1072594e+02 3.3150000e+01
3.7638047e+01 6.9665781e+01 1.1126609e+02 3.3000000e+01
3.7738125e+01 7.0512969e+01 1.1083180e+02 3.2800000e+01
3.7929609e+01 7.1173281e+01 1.1098344e+02 3.2620000e+01
3.8256563e+01 7.3488281e+01 1.1004078e+02 3.2400000e+01
3.8210000e+01 7.3070391e+01 1.1099758e+02 3.2500000e+01
3.8348203e+01 7.4424219e+01 1.1007125e+02 3.2250000e+01
3.8558281e+01 7.5726953e+01 1.1036258e+02 3.2000000e+01
3.8462422e+01 7.6946641e+01 1.0917344e+02 3.1750000e+01
3.8791016e+01 7.8916719e+01 1.0868867e+02 3.1500000e+01
3.9224531e+01 8.1745859e+01 1.0751453e+02 3.1250000e+01
3.9756875e+01 8.5175156e+01 1.0684477e+02 3.1000000e+01
4.0156406e+01 8.7390469e+01 1.0612383e+02 3.0750000e+01
4.0488516e+01 8.9925547e+01 1.0426992e+02 3.0500000e+01
4.1186328e+01 9.2837266e+01 1.0342875e+02 3.0250000e+01
4.1717500e+01 9.5760938e+01 1.0268797e+02 3.0000000e+01
4.2436797e+01 9.8547578e+01 1.0135094e+02 2.9750000e+01
4.3261328e+01 1.0123945e+02 1.0063070e+02 2.9500000e+01
4.3653437e+01 1.0286656e+02 9.9072500e+01 2.9250000e+01
4.4786328e+01 1.0596602e+02 9.7762109e+01 2.9000000e+01
4.7503906e+01 1.1060344e+02 9.4646563e+01 2.8500000e+01
4.9853125e+01 1.1287641e+02 9.3267422e+01 2.8250000e+01
5.3080469e+01 1.1476039e+02 9.1479297e+01 2.8000000e+01
5.5945313e+01 1.1528133e+02 9.0206563e+01 2.7750000e+01
5.8494844e+01 1.1565938e+02 8.9493359e+01 2.7600000e+01
6.7080234e+01 1.1296352e+02 8.6403984e+01 2.7200000e+01
7.0883359e+01 1.1137656e+02 8.5334531e+01 2.7000000e+01
7.9821484e+01 1.0702750e+02 8.2538750e+01 2.6750000e+01
9.0080781e+01 1.0320547e+02 8.0716797e+01 2.6500000e+01
9.7965156e+01 9.7789844e+01 7.8095703e+01 2.6250000e+01
1.0408312e+02 9.0591719e+01 7.5464375e+01 2.6000000e+01
1.0119023e+02 8.5446641e+01 7.3583281e+01 2.5750000e+01
8.7053906e+01 7.5529375e+01 6.8587656e+01 2.5500000e+01
Thank you very much for your time and patience.
As you understood, I make an interpolation for getting the calRGB.
Are all the NaN out of the range or what?
Thank you very much
Antonio
  2 个评论
the cyclist
the cyclist 2011-4-5
Almost certainly the NaNs are value that lie outside the "convex hull" (the outer boundary) of your known values. You can test this by temporarily choosing "nearest" as the interpolation method for TriScatteredInterp, and seeing if the NaNs go away. My guess is they will.
You can use the CONVHULL command to plot out what your convex hull looks like.
Are you able to extend your grid of known points, to capture those points? If not, you can't really interpolate between.
[Also, you should accept this answer if it was helpful, so that others with a similar question can get the benefit of the answer in the future.]
mortain Antonio
mortain Antonio 2011-4-5
Dear Cyclist,
thank you very much for your fast and accurate responses.
Your points have been really useful for me.
Tonight I will try and I will let you know.
Cheers,
Antonio

请先登录,再进行评论。


mortain Antonio
mortain Antonio 2011-4-5
Dear Cyclist,
I was quite curious to try with the method nearest as you suggested. It is now giving this error:
??? Input argument "x" is undefined.
Error in ==> nearest at 24
if isinteger(x)
Error in ==> TriScatterdInterp at 88
temperatureInterpolant =
TriScatteredInterp(calRGB(:,1),
calRGB(:,2),calRGB(:,3),calRGB(:,4),nearest);
However, did you forget to put a ; at the last row of your code at the first message?
What's the matter now with this message, please?
I tried to change the calibration file, where it seemed a bit weird (which means I cut the temperatures above 38.4 and I set 24.7 to RGB=(0,0,0). However, I still have NaN...
Another question, shall I write:
K=CONVHULL(output(:,1),output(:,2),output(:,3));
in order to have the surface? Because it tells me that:
??? Undefined function or method 'CONVHULL' for input
arguments of type 'double'.
Error in ==> TriScatterdInterp at 90
K=CONVHULL(output(:,1),output(:,2),output(:,3));
Thank you very much for your help.
Antonio
  1 个评论
the cyclist
the cyclist 2011-4-5
When you use the nearest method, you need to enclose that in single quotes: 'nearest'.
The command is lower-case: convhull. "help convhull" for details. (I was using the unfortunate convention from the documentation, in which commands are written in all caps even though that is not how you type them.)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by