MATLAB: How to find out full width at half maximum from this graph
fwhm
Related Solutions
Because you could have other peaks that are separated from the main peak, and have a height of more than the half height of the tallest peak, you must start at the tallest peak and slide down it to locate the half height values on the same peak. Try this demo code:
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;% Check that user has the Image Processing Toolbox installed.
hasIPT = license('test', 'image_toolbox');if ~hasIPT % User does not have the toolbox installed.
message = sprintf('Sorry, but you do not seem to have the Image Processing Toolbox.\nDo you want to try to continue anyway?'); reply = questdlg(message, 'Toolbox missing', 'Yes', 'No', 'Yes'); if strcmpi(reply, 'No') % User said No, so exit.
return; endend% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');button = menu('Use which demo image?', 'CameraMan', 'Cell', 'Eight', 'Coins', 'Pout');if button == 1 baseFileName = 'cameraman.tif';elseif button == 2 baseFileName = 'cell.tif';elseif button == 3 baseFileName = 'eight.tif';elseif button == 4 baseFileName = 'coins.png';else baseFileName = 'pout.tif';end% Read in a standard MATLAB gray scale demo image.folder = fullfile(matlabroot, '\toolbox\images\imdemos');% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, '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; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);if numberOfColorBands > 1 % It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = grayImage(:, :, 2); % Take green channel.
end% Display the original gray scale image.
subplot(2, 1, 1);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off') % Let's compute and display the histogram.
[pixelCount, grayLevels] = imhist(grayImage);subplot(2, 1, 2); bar(pixelCount);grid on;title('Histogram of original image', 'FontSize', fontSize);xlim([0 grayLevels(end)]); % Scale x axis manually.
% ======== MAIN CODE RIGHT HERE ===============================
[fullHeight, indexOfMax] = max(pixelCount);halfHeight = fullHeight / 2;% Initialize
index1 = indexOfMax;index2 = indexOfMax;% Search dark side until values fall below half height.
for k = indexOfMax-1 : -1 : grayLevels(1) if pixelCount(k) < halfHeight break; end index1 = k;end% Search bright side until values fall below half height.
for k = indexOfMax+1 : grayLevels(end) if pixelCount(k) < halfHeight break; end index2 = k;end% Place vertical bars at the half height locations
yl = ylim();line([index1, index1], [yl(1), yl(2)], 'Color', 'r');line([index2, index2], [yl(1), yl(2)], 'Color', 'r');% Inform user
message = sprintf('Max index at %d.\nHalf height indexes at %d and %d',... indexOfMax, index1, index2);uiwait(helpdlg(message));
The Signal Processing Toolbox findpeaks function can return the FWHM of every peak. See the documentation sections on Determine Peak Widths and w for the necessary details. To return only the results from the largest or most prominent peaks, other name-value pair arguments will allow selections based on these (and other) criteria.
Related Question
- I want to add dot in image
- How to enter equation into matrix column and refer to adjacent column.
- Peak Width at Half Height
- How can I remove noise by using frequency domain technique?
- How to find a value in x axis in 1d plot with a given y
- How i can remove this central white line from an image
- How to extract the width of the following graph.
Best Answer