how to get horizontal projection and vertical projection of histogram of an binary image ? can any tell me how to get these things ?
9 views (last 30 days)
Show older comments
- how to get these type of histograms can any one tell me and thanks advance >>>!
how to get histogram projection for below image ?can any one tell me how to do it this type of histograms and see my example outputs



- clear all; close all; a=imread('prewitresult.jpg'); bw = 1-im2bw(a); h = sum(bw,2); figure,plot(sum(bw,2),1:size(bw,1));
0 Comments
Answers (3)
Image Analyst
on 14 Feb 2017
There is no histogram there. Those are not histograms. Those are projections using the sum() operation. Do it like this:
verticalProfile = sum(binaryImage, 2);
horizontalProfile = sum(binaryImage, 1);
4 Comments
Image Analyst
on 6 Oct 2018
If you want "solid" plots, use bar() and barh() and reverse the y axis for the vertical profile:
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;
%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = 'robertresult.jpg';
% 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
rgbImage = 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(rgbImage)
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(rgbImage);
% 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 = rgbImage(:, :, 1); % Take red channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
hp = impixelinfo();
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
drawnow;
% Get the horizontal profile:
horizontalProfile = sum(grayImage, 1);
% Display the profile.
subplot(2, 2, 2);
bar(1:columns, horizontalProfile, 'FaceColor', 'k');
grid on;
xlabel('Column', 'FontSize', fontSize);
ylabel('Count', 'FontSize', fontSize);
title('Horizontal Profile', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Get the vertical profile:
verticalProfile = sum(grayImage, 2);
% Display the profile.
subplot(2, 2, 3:4);
barh(1:rows, verticalProfile, 'FaceColor', 'k');
xlabel('Count', 'FontSize', fontSize);
ylabel('Row', 'FontSize', fontSize);
% Flip the y axis
ax = gca
ax.YDir = 'reverse';
grid on;
title('Vertical Profile', 'FontSize', fontSize, 'Interpreter', 'None');

Jimmy Hasugian
on 14 Mar 2018
You can try this..
verticalProfile = sum(binaryImage, 2);
horizontalProfile = sum(binaryImage, 1);
figure; bar(horizontalProfile);
figure; barh(verticalProfile);
1 Comment
Eduardo Martins
on 4 Oct 2018
Nice, this works for me. Thank you very much.
im = imread('kart.jpeg'); gray=rgb2gray(im);
imshow(im); imshow(gray);
bw=imbinarize(gray, 0.4);
imshow(bw);
verticalProfile = sum(bw, 2); horizontalProfile = sum(bw, 1); figure; bar(horizontalProfile); figure; barh(verticalProfile);
See Also
Categories
Find more on Convert Image Type in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

