How to convert a gray scale image of thermal camera to colored one?

49 views (last 30 days)
Dear All,
I have a thermal infrared camera that produces colored quick look images every 30 min. The images stored in jp2 grayscale image. I would like to convert back the gray scaled images to colored one. The size of colored image is 480x640x3 unit8I and the size of gray scaled one is 480x640x3 unit16. I tried the previously recommended methods on https://www.mathworks.com/matlabcentral/answers/86854-how-to-convert-a-gray-scale-image-to-color-image-using-matlab or on https://www.mathworks.com/matlabcentral/answers/67137-convert-gray-image-back-to-rgb but noone help me. I attached an image of quicke look and its gray scale jp2 mage as saved as png in Matlab.
Could someone suggest me a method for converting back the gray scaled jp2 image to same colored image as the quicke look one?

Accepted Answer

DGM
DGM on 31 Oct 2022
Edited: DGM on 31 Oct 2022
Working around the fact that the grayscale image is a screenshot, this is basically just a matter of finding the colormap.
% read/collapse grayscale image
Agrayfull = imread('graytherm.png');
Agrayfull = Agrayfull(:,:,1);
% vectorize image
Agray = Agrayfull(:);
% read RGB image, vectorize
Argb = imread('colortherm.jpg');
Argb = im2double(reshape(Argb,[],3));
% find average colors in Argb which correspond
% to the pixel values in Agray
uc = unique(Agray);
numcolors = numel(uc);
CTestimate = zeros(numcolors,3);
for ucidx = 1:numcolors
mask = Agray == uc(ucidx); % find pixels with this gray level
CTestimate(ucidx,:) = mean(Argb(mask,:),1); % find average color
end
CTestimate; % this is a short color table that's only as long as unique(Agray)
% display the image with the estimated CT
imagesc(Agrayfull)
colormap(CTestimate)
colorbar
% interpolate to make a longer CT
ncout = 256;
ucl = linspace(double(uc(1)),double(uc(end)),ncout);
CT = interp1(double(uc),CTestimate,ucl);
% display the image with the interpolated CT
imagesc(Agrayfull)
colormap(CT)
colorbar
If you want to save the image like that, you can just use imwrite. You can save it as indexed or RGB:
Aind = im2uint8(mat2gray(Agrayfull)); % rescale
imwrite(Aind,CT,'myindexedimage.png') % save as indexed
imwrite(ind2rgb(Aind,CT),'myrgbimage.png') % or save as RGB
You might be wondering why the result doesn't look as smooth as the original color image. That's probably because of the fact that you're dealing with a screenshot of something that probably used to be uint16. While the image appears to have quite low contrast, in uint16, there's still roughly 6400 gray levels available to represent that narrow range of gray. Once the image gets crushed into uint8, there's only 25 gray levels left to represent the same content. Save images using imwrite, not by saving the figure.

More Answers (2)

Walter Roberson
Walter Roberson on 31 Oct 2022
Edited: Walter Roberson on 31 Oct 2022
You would use
YourMatrix = rgb2gray(YourRGBMatrix);
Then
image(YourMatrix)
colormap(AppropriateMap)
Or you would use
RGB = ind2rgb(YourMatrix, AppropriateMap);
if you needed to create it as an array.
There is also
imwrite(YourMatrix, AppropriateMap, filename)
  1 Comment
Walter Roberson
Walter Roberson on 31 Oct 2022
Edited: Walter Roberson on 31 Oct 2022
The difficult part is finding a colormap that you are willing to accept.
The FLIR Quick Look apparently uses a nonlinear colormap, in which it is difficult to find any logic for the choice of color. In the past people have posted color pictures from those cameras, and when we examine the colorbar increase of temperature does not map to increase of brightness or to continuous change in hsv coordinates. Because of that, the only way we have found to convert the color image to temperature is to grab the color bar out of the image and match image color to the color bar to get a relative position. The Quick Look often does not even bother to label the colorbar so only relative temperatures be deduced.
You want the opposite, to go from gray to color, but the challenge is not having a reference colorbar anywhere for us to be able to tell how to colorize back.

Sign in to comment.


Beata Szabo-Takacs
Beata Szabo-Takacs on 1 Dec 2022
Dear All,
DGM's answer works correctly but I found a more simple method as well. I just share it to help to anybody who has a same issue. The method is the following:
I = imread('MyImage.jp2'); %% it is the grayscaled image
>> colormap(jet);
>> J = imadjust(I,stretchlim(I),[]);
>> imagesc(J);
>> colrbar;
  1 Comment
DGM
DGM on 1 Dec 2022
Edited: DGM on 1 Dec 2022
Bear in mind what Walter mentioned about the colormap. With that in mind, consider the question as asked.
"Could someone suggest me a method for converting back the gray scaled jp2 image to same colored image as the quicke look one?"
If scale and the particulars of appearance don't matter for visualization purposes, it might suffice, but you can see why I'm inclined to back-calculate the original mapping instead of using jet().

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!