detection of hottest spot in wire
2 views (last 30 days)
Show older comments
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;
end
% 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.
return;
end
% 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);
uiwait(warndlg(errorMessage));
return;
end
end
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.
end
% 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]);
drawnow;
% Crop the image to get rid of feet and legs.
grayImage = grayImage(50, :);
% Filter the image with a top hat filter.
filteredImage = imtophat(grayImage, true(50));
% Display the image.
subplot(2, 2, 2);
imshow(filteredImage, []);
title('Filtered Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Display the image.
subplot(2, 2, 3);
histogram(filteredImage);
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);
end
message = sprintf('Number of pixels with max intensity of %f = %d.\n',...
MaxIntensity, length(rows))
uiwait(helpdlg(message));
IN THE ABOVE CODE WHAT IS THE MEANING OF LINE filteredImage = imtophat(grayImage, true(50));
1 Comment
dpb
on 7 May 2019
One would presume it does what the preceding comment says...
...
grayImage = grayImage(50, :);
% Filter the image with a top hat filter.
filteredImage = imtophat(grayImage, true(50));
...
Accepted Answer
Guillaume
on 7 May 2019
WHAT IS THE MEANING OF LINE filteredImage = imtophat(grayImage, true(50));
It performs top hat filtering of the image, using a square filtering element of width 50. Using a logical array (true(50)) to construct the filtering element is not the best idea. Matlab will have to convert it to a double array, so it would have been clearer and faster to use ones(50) instead. To be even more explicit, the author could have used strel.
filteredImage = imtophat(grayImage, strel('square', 50));
Now, hopefully you're not asking what is top hat filtering. If you are, read the documentation (link above) and if it's still not enough grab your favorite image processing text book.
0 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!