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

Thread Subject:
how to scale the axis of histogram?

Subject: how to scale the axis of histogram?

From: Learner

Date: 22 Jan, 2012 19:36:34

Message: 1 of 5

hello all,

actually i took an image & applied mat2gray. Therefore the x-axis is scaled from 0-255 to 0-1.

Now i want to rescale the x-axis to 0-255 after some manipulations in 0-1 sclae, so that clear comparison can be made in the scale of 0-255.

How can i do that?

thanks in advance.

Subject: how to scale the axis of histogram?

From: ImageAnalyst

Date: 23 Jan, 2012 02:42:13

Message: 2 of 5

On Jan 22, 11:36 am, "Learner " <farhan7...@gmail.com> wrote:
> hello all,
>
> actually i took an image & applied mat2gray. Therefore the x-axis is scaled from 0-255 to 0-1.
>
> Now i want to rescale the x-axis to 0-255 after some manipulations in 0-1 sclae, so that clear comparison can be made in the scale of 0-255.
>
> How can i do that?
>
> thanks in advance.

-------------------------------------------------------
What do you mean by x-axis? What x-axis? The x-axis has units of
pixels, as does the y axis, regardless of what you did with the pixel
intensities. Did you take a histogram, and maybe you mean the "gray
level" axis that has the bins of the pixel counts????

Subject: how to scale the axis of histogram?

From: Learner

Date: 23 Jan, 2012 05:58:09

Message: 3 of 5

ImageAnalyst <imageanalyst@mailinator.com> wrote in message
> What do you mean by x-axis? What x-axis? The x-axis has units of
> pixels, as does the y axis, regardless of what you did with the pixel
> intensities. Did you take a histogram, and maybe you mean the "gray
> level" axis that has the bins of the pixel counts????

yes, the gray levels. how can i do that?

Subject: how to scale the axis of histogram?

From: ImageAnalyst

Date: 23 Jan, 2012 07:08:12

Message: 4 of 5

On Jan 22, 9:58 pm, "Learner " <farhan7...@gmail.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message
> > What do you mean by x-axis?  What x-axis?  The x-axis has units of
> > pixels, as does the y axis, regardless of what you did with the pixel
> > intensities.  Did you take a histogram, and maybe you mean the "gray
> > level" axis that has the bins of the pixel counts????
>
> yes, the gray levels. how can i do that?

-------------------------------------------------------------------
You need to know amin and amax so that you can recover the original
gray levels. If you don't pass it in to mat2gray, then you won't know
what it picked so you'll have to calculate it anyway. Here, try this
demo:
Copy and paste into an editor window and save it as test1.m and run
it.

% function test1
% by ImageAnalyst
% IMPORTANT: The newsreader may break long lines into multiple lines.
% Be sure to join any long lines that got split into multiple single
lines.
% These can be found by the red lines on the left side of your
% text editor, which indicate syntax errors, or else just run the
% code and it will stop at the split lines with an error.

function test1()
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 20;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
fullFileName = fullfile(folder, baseFileName);
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% 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.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
% Get sample data between gray levels 57 and 255.
grayImage = imread(fullFileName) + 50;
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);
% Display the original gray scale image.
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Grayscale Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize
figure.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')

% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(grayImage);
subplot(2, 2, 2);
bar(pixelCount);
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
grid on;

% Plot histograms.
[PixelCounts, GLs] = PlotHistogram(grayImage(:));


%=====================================================================
% Takes histogram of 1-D image array imageArray, and plots it.
function [PixelCounts, GLs] = PlotHistogram(imageArray)
% To get a histogram of a double image, you need to convert it to the
% range of 0-1 first.
numberOfBins = 512; % The range 0-1 will be divided into 1000 bins.
fontSize = 20;
dblMinImageValue = double(min(imageArray(:)));
dblMaxImageValue = double(max(imageArray(:)));
% normalizedImage = double(imageArray - dblMinImageValue) ./
(dblMaxImageValue - dblMinImageValue);
normalizedImage = mat2gray(imageArray);
[PixelCounts, normalizedGLs] = imhist(normalizedImage,
numberOfBins); % make sure you label the axes after imhist because
imhist will destroy them.
% Un normalize GLs so it will plot right
GLs = normalizedGLs * (dblMaxImageValue - dblMinImageValue) +
dblMinImageValue;

% Plot the histogram.
subplot(2, 2, 3);
    % Plot the histogram as a bar chart.
bar(normalizedGLs, PixelCounts, 'BarWidth', 1);
grid on;
title('Histogram of normalized image', 'FontSize', fontSize);

% Plot it with the rescaled gray levels.
subplot(2, 2, 4);
bar(GLs, PixelCounts, 'BarWidth', 1);
grid on;
lowestValueToPlot = dblMinImageValue;
highestValueToPlot = dblMaxImageValue;
xlim([lowestValueToPlot highestValueToPlot]); % Needs to be after
bar() function because bar() function automatically sets x limits
itself, overriding what you've set.

% Calculate CDF, Cumulative Distribution Function
cdf = zeros(1, numberOfBins);
cdf(1) = PixelCounts(1);
global CDFPercentiles;
CDFPercentiles = zeros(1, 100);
CDFIndexes = zeros(1, 100);
for k = 2:numberOfBins
cdf(k) = cdf(k-1) + PixelCounts(k);
end
maxY = ylim;
percentToLookFor = 0.01;
pctIndex = 1;
for k = 1:numberOfBins
% Now normalize it to 0-1.
cdf(k) = cdf(k) / cdf(numberOfBins);
% Scan the CDF and find where the CDF exceeds each percentage from 1
to
% 100 in steps of 1%.
if cdf(k) >= percentToLookFor
% Records the gray level where it bumps up a percent.
CDFPercentiles(pctIndex) = GLs(k);
CDFIndexes(pctIndex) = k; % Index in CDF where this occurred.
pctIndex = pctIndex + 1;
percentToLookFor = percentToLookFor + 0.01;
end
% Now normalize it to the range for plotting.
cdf(k) = maxY(2) * cdf(k) ;
end
hold on;
plot(GLs, cdf);
title('Histogram of normalized image with rescaled GL axis',
'FontSize', fontSize);

return;

Subject: how to scale the axis of histogram?

From: Learner

Date: 24 Jan, 2012 05:03:11

Message: 5 of 5

sir, very very thanxs, for ur help.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us