On Jan 22, 9:58 pm, "Learner " <farhan7...@gmail.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message
> > What do you mean by xaxis? What xaxis? The xaxis 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 1D 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 01 first.
numberOfBins = 512; % The range 01 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(k1) + PixelCounts(k);
end
maxY = ylim;
percentToLookFor = 0.01;
pctIndex = 1;
for k = 1:numberOfBins
% Now normalize it to 01.
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;
