Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to calculate histogram width at the half-height?

Asked by Alex on 26 May 2013

What is the easiest way to calculate histogram width at the half-height? I was unable to find any information about that in the built in data statistics. Thank you.

0 Comments

Alex

1 Answer

Answer by Image Analyst on 26 May 2013
Accepted answer

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;
	end
end
% 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;
	end
end
grayImage = 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));

0 Comments

Image Analyst

Contact us