MATLAB Answers


Detect the hottest spot of a wire in thermal image

Asked by George Anton on 14 May 2018
Latest activity Commented on by Image Analyst
on 19 May 2018

Hi , i am currently working on a project that i have to detect the hottest spot of the wire in the image i attached.I detected the wire as you see in the second image using the Hough Transform. What i have to do to detect the hottest spot of the wire (as you see an the center of the wire the spot is whiter). I assume that i have to take all the pixels of the wire and compute the average temperature and then compare that temperature again with all the pixels and find where the temperature is higher than the average? On the other hand i don't have the exact temperature of every pixel. Should i find the spot using the color (the whiter the spot the hottest?). And how can i do this?

Thank you


Sign in to comment.

2 Answers

Answer by Image Analyst
on 14 May 2018

Use the max function. One way:

maxTemp = max(tempImage(:));
[rows, columns] = find(tempImage == maxTemp);

To find the wire, threshold and call mean.

mask = tempImage > someValue;
meanTemp = mean(tempImage(mask));

  1 Comment

Hi and thanks for the reply! I am a little bit confused and i don't know if i explained exactly what i want to do. So the first step is to detect only the wire and i have done it using the Hough Transform.The second thing is to detect the hottest spot only of the wire and mark it as i marked in the image.The logic behind this project is that the wire has a certain temperature and at the hottest there is probably a malfunction.So i don't know if your code does exactly what i want.Can you help me with the code and explain a little bit more?

Thank you!

Sign in to comment.

Answer by Image Analyst
on 15 May 2018

Try this:

clc;    % Clear the command window.
close all;  % Close all figures (except those of imtool.)
clear;  % Erase all existing variables. Or clearvars if you want.
workspace;  % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = pwd
if ~exist(startingFolder, 'dir')
	% If that folder doesn't exist, just start in the current folder.
	startingFolder = pwd;
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.png');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
	% User clicked the Cancel button.
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
	% The file doesn't exist -- didn't find it there in that folder.
	% Check the entire search path (other folders) for the file by stripping off the folder.
	fullFileNameOnSearchPath = baseFileName; % No path this time.
	if ~exist(fullFileNameOnSearchPath, 'file')
		% Still didn't find it.  Alert user.
		errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage);
if numberOfColorChannels > 1
	% It's not really gray scale like we expected - it's color.
	% Use weighted sum of ALL channels to create a gray scale image.
	grayImage = rgb2gray(grayImage);
	% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
	% which in a typical snapshot will be the least noisy channel.
	% grayImage = grayImage(:, :, 2); % Take green channel.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
hp = impixelinfo;
axis on;
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Crop the image to get rid of feet and legs.
grayImage = grayImage(1:350, :);
% Filter the image with a top hat filter.
filteredImage = imtophat(grayImage, true(25));
% Display the image.
subplot(2, 2, 2);
imshow(filteredImage, []);
title('Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Display the image.
subplot(2, 2, 3);
title('Histogram of Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
grid on;
% Threshold the image.
% threshold(filteredImage)
binaryImage = filteredImage > 15;  % Hgh enough to get rid of bad JPEG artifacts.  Never use JPEG for image analysis!
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Extract largest blob only
binaryImage = bwareafilt(binaryImage, 1);
% Display the image.
subplot(2, 2, 4);
imshow(binaryImage, []);
title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Find mean intensity
props = regionprops(binaryImage, grayImage, 'MeanIntensity', 'MaxIntensity');
meanIntensity = props.MeanIntensity
MaxIntensity = props.MaxIntensity
% Find hottest spot.
% Get coordinates of pixels in the mask.
[rows, columns] = find(grayImage == MaxIntensity);
% Put a cross at every such intensity on the original image
subplot(2, 2, 1);
hold on;
for k = 1 : length(rows)
	xHottestSpot = columns(k);
	yHottestSpot = rows(k);
	plot(xHottestSpot, yHottestSpot, 'r+', 'MarkerSize', 20, 'LineWidth', 2);
message = sprintf('Number of pixels with max intensity of %f = %d.\n',...
	MaxIntensity, length(rows))


Thank you for the answer i really appreciate it!But i have one question.You cropped the image to get rid of the legs.But this image is only to simulate the outdoor wires.I will use o drone with a thermal camera to detect the hottest spot of the wires and in that case there will be lots of 'distract and hot' points and maybe the hottest point isn't on the wire(it could be the ground,a tree ,etc).This is the reason that i detected only the wire with the Hough Transform.So is it possible to change somehow the code and take advantage of the Hough Transform so we don't need to crop the image? This is my code to detect the wire:

rotI = imread('FLIR_520.png');

BW = edge(rotI,'canny');


[H,theta,rho] = hough(BW);

P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));

x = theta(P(:,2));

y = rho(P(:,1));


lines = houghlines(BW,theta,rho,P,'FillGap',70,'MinLength',631);

figure, imshow(rotI), hold on

max_len = 0;

for k = 1:length(lines)

   xy = [lines(k).point1; lines(k).point2];
   % Plot beginnings and ends of lines
   % Determine the endpoints of the longest line segment
   len = norm(lines(k).point1 - lines(k).point2);
   if ( len > max_len)
      max_len = len;
      xy_long = xy;
% highlight the longest line segment

Thank you very much

Your code seems to find the line. Can we assume that the line will be straight (unlike the one in the image)? If so, just use imline() to burn that line into a mask (see attached demo). Then use the mask and widen it a bit, say 5 or 9 pixels or whatever. Then use the mask to inspect all the values in the mask:

mask = imdilate(mask, true(9));
meanGrayLevelInMask = mean(grayImage(mask))
maxGrayLevelInMask = max(grayImage(mask))

Sign in to comment.