MATLAB code for extracting temperature value from BMP format image followed by background removal
11 views (last 30 days)
Show older comments
I have a thermal image folder and I want to calculate the temperature value for each image followed by background separation. Kindly suggest code.
5 Comments
Image Analyst
on 8 Nov 2023
OK, fine. But you say that like that makes the above comments irrelevant. They are not. Have you checked them out and tried anything? You didn't even attach any images for us to look at for some reason. 🤔
I've attached the latest version of my RGB-to-temperature demo below in the Answers section (scroll down).
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
Answers (2)
Image Analyst
on 8 Nov 2023
See my thermal demo.
1 Comment
Image Analyst
on 9 Nov 2023
Edited: Image Analyst
on 9 Nov 2023
Yes, my demo will work for the images you uploaded. Did you try it?
If the colorbars are all in the same location, then you should hard code that location instead of asking the user to d raw it every time.
To process a sequence of images, see the FAQ:
Inside the for loop call a function that will analyze just one image - the one with the current file name on each iteration.
Write back with any problems you had adapting the code.
You can save the floating point termperature results to a .mat file with save. You could also save the temperature image as a PNG image but of course the values would then be in gray levels instead of degrees.
DGM
on 10 Nov 2023
Edited: DGM
on 20 Nov 2023
An estimate of the colormap can be derived from the image.
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1534612/20231109_143249_IR.BMP');
% extract the sample CT from the colorbar
cbar = imcrop(inpict,[40.51 255.51 159.98 9.98]);
CT0 = permute(mean(im2double(cbar),1),[2 3 1]);
That only needs to be done once. If all the images come from the same camera and use the same map, you can reuse it. Just package it in a function like the one attached. The colormap should probably be expanded, as the map applied by the camera is likely not the same length the colorbar width in pixels. In this case, the photo region contains 244 unique colors, so we can be sure that the original map was longer than the colorbar. It's hard to guess how long the original map would be, but 256 is as good a guess as any. If you have many images, you might be able to get a better estimate based on the number of unique colors that appear across all images. In lieu of that, it's best to err on the side of making the map too long.
Also, be wary when the ends of the colorbar are constant-valued. That creates ambiguity. In this case, the first 4 columns of the colorbar are the exact same color. All other columns are unique. This raises the question as to whether this flat spot is a defect in the original colormap, or in the way the camera's firmware draws the colorbar. The distinction would be a few degrees difference in the estimated temperature values. I choose to assume that the defect is in the representation of the colorbar, so I'm omitting the nonunique parts of the extracted color table. Again, it's hard to be certain based only on a single image from one camera model. The manufacturer isn't going to document any of these things.
At that point, estimating the data values requires only the T values associated with the ends of the colorbar.
% load an image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1534612/20231109_143249_IR.BMP');
% crop out the photo region if desired
% obviously, temp data in the non-photo region is invalid
inpict = imcrop(inpict,[0.51 14.51 239.98 239.98]);
% generate an expanded CT
nlevels = 256; % this is just a guess
CTf = chauvinarnoux(nlevels); % attached
% apply the CT to estimate the data values
Tlim = [26.2 55.7]; % this will differ with each image
T = rgb2ind(inpict,CTf);
T = rescale(T,Tlim(1),Tlim(2),'inputmin',0,'inputmax',nlevels-1);
% display the data
hi = imagesc(T);
colorbar
colormap(jet(256))
% generate a mask based on the T data
maxt = 35; % pick some threshold temperature
mask = T<maxt; % apply the threshold
mask = bwareaopen(mask,100); % get rid of specks
% do something with the mask??
% in this example, i'm just hiding non-ROI content
% the actual T data is not altered
hi.AlphaData = mask;
caxis([Tlim(1) maxt]) % adjust the colorbar limits to match
% for demonstration, show the temp value at some point
dt = datatip(hi,138,177);
The application of the mask in this demo is purely visual. The T data is not altered. The mask can be used for logical addressing, allowing value replacement or extraction.
ROImeanT = mean(T(mask)) % average temperature in the mask region
2 Comments
DGM
on 20 Nov 2023
I don't know what your studies require, and I have only seen a single characteristic image.
Once the T data is estimated, you can binarize it like any other 2D array. I just picked a level that mostly isolated the leaves. You don't have to do it that way. See graythresh() and imbinarize(). You could play around with the Image Segmenter app that comes with IPT.
Instead of
maxt = 35; % pick some threshold temperature
mask = T<maxt; % apply the threshold
... maybe you could let graythresh() just guess at a good value
maxt = graythresh(mat2gray(T)); % T needs to be normalized
maxt = Tlim(1) + maxt*diff(Tlim) % threshold needs to be denormalized
mask = T<maxt; % apply the threshold
... but in my experience it's going to be junk in all but the most distinctly bimodal images. It won't do well for the given image.
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!