MATLAB: How to rescale an histogram

histogramMATLABnormalizerescale

Hello,
I am using a code which allows to plot an histogram of H (Hue parameter) values of each pixel from a picture. I would like to compare histograms from two different pictures. I know how to overlay them but one is smmaller than the other one so it's difficult to read the whole figure. I don't know how to normalize or rescale (I am not sure about the right word) them. I want to set the value of the higher bin to 1 for each histogram.
Please find below the code I am using :
% ** 00 ** ***Define path to folder and pick movie file***
[file,fold] = uigetfile('*.tif','Select the tif movie file');
% ** 1 ** ***Extract all tif data
info=imfinfo([fold,file]); %Yields error but give length of movie
name=file(1:length(file)-4);
s=struct;
for i=1:length(info)
im=imread([fold,file],i);
s(i).ima=im; %put images in an array structure
end
clear im
% ** 2 ** ***Pick several images and plot all points in colorspace***
list=[1];
sl=struct;
n=0;
for i=list
n=n+1;
sl(n).ima=s(i).ima;
end
for i=1:length(list)
im=sl(i).ima; %image in rgb
%Plot RGB
figure('Color',[1 1 1])
imagesc(im)
axis equal tight
set(gca,'XTick',[],'YTick',[])
title(['Frame ' num2str(list(i))],'Fontsize',16)
saveas(gcf,[fold,'Frame_' num2str(list(i),'%.3i') '.png'],'png')
%Convert RBG to HSV and keep only H
imH=rgb2hsv(im);
imH=imH(:,:,1);
%Convert RGB to XYZ
imX=rgb2xyz(im);
%Convert to xyz
x=imX(:,:,1)./(imX(:,:,1)+imX(:,:,2)+imX(:,:,3));
y=imX(:,:,2)./(imX(:,:,1)+imX(:,:,2)+imX(:,:,3));
%Reshape to vector
xx=reshape(x,[1 size(x,1)*size(x,2)]);
yy=reshape(y,[1 size(y,1)*size(y,2)]);
%Remove the background based on criterion on RBG sum
Sc=10000; %ENTER VALUE
S=im(:,:,1)+im(:,:,2)+im(:,:,3);
S(S<Sc)=0;
S=double(reshape(S,[1 size(x,1)*size(x,2)]));
H=reshape(imH,[1 size(x,1)*size(x,2)]);
xx(S==0)=[];
yy(S==0)=[];
H(S==0)=[];
figure('Color',[1 1 1],'units','normalized','outerposition',[0 0 1 1])
subplot(2,2,1)
imagesc(im)
axis equal tight
set(gca,'XTick',[],'YTick',[])
title(['Frame ' num2str(list(i))],'Fontsize',16)
subplot(2,2,[2,4])
plotChromaticity
hold all
hd=histogram2(xx,yy,300,'DisplayStyle','tile');
colormap gray
title('CIE plot','Fontsize',16)
subplot(2,2,3)
histogram(H,300)
title('Histo H layer','Fontsize',16)
saveas(gcf,[fold,'Frame_' num2str(list(i),'%.3i') '_CIE.png'],'png')
end

Best Answer

Your code would be so much easier to understand if you'd used meaningful variable names instead of useless xx, yy, S, etc. As it is, I've not really tried to understand what you're plotting.
Rather than setting the highest bin to 1, which I don't think is a good way of comparing histograms, I'd recommend that you normalise your histograms so that the sum of the bar is 1. This is easily achieved by setting the 'Normalization' option of histogram to 'probability' (or 'pdf' if your bins are not equal size)
histogram(H, 300, 'Normalization', 'probability')
Related Question