I am not sure how i can get histogram both the fig which i looked in are the same.. Anybody help me in completing the code.

clc;
close all;
clear all;
img=imread('C:\UsersDesktop\dipproj2\lena.bmp');
[p,q]=size(img)
k=1:0:256;
x= ones(1,256);
for k=1:0:256
for i=1:size(p,1)
for j=1:size(q,1)
k=value(256,i)
% if (img(i,j)==k)
% x(k+1)=x(k+1)+1;
x(k+1)=x(k+1)+1;
end
end
end
d=zeros(1,256);
d=x*(1/(p*q));
s=sum(k+1)
s(1)=d(1,1);
for i=2:256
s(i)=s(i-1)+d(1,i);
end
y=ones(1,256);
u=y*256
%sk=255*s;
z=floor(u);
final=img;
for k=0:256
for i=1:size(256,1)
for j=1:size(256,1)
if(img(i,j)==k)
final(i,j)=z(1,k+1)+1;
end
end
end
end
figure(1),
subplot(2,1,1),
title('histogram'),
imagesc(final),
colormap(gray);
subplot(2,1,2),
imhist(final);
figure(2),
subplot(2,1,1),
title('histogram1'),
imagesc(img),
colormap(gray);
subplot(2,1,2),
imhist(img);

 Accepted Answer

Why are you computing the histogram yourself??? You know, there is a function for that called imhist() in the Image Processing Toolbox. See this demo:
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.
format longg;
format compact;
fontSize = 22;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% 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
% Read in the image from disk.
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Get histograms for each color channel.
[pixelCountsR grayLevelsR] = imhist(redChannel, 256);
[pixelCountsG grayLevelsG] = imhist(greenChannel, 256);
[pixelCountsB grayLevelsB] = imhist(blueChannel, 256);
% Plot them
subplot(2, 2, 2);
bar(grayLevelsR, pixelCountsR, 'BarWidth', 1, 'FaceColor', 'r');
grid on;
title('Red Histogram', 'FontSize', fontSize);
subplot(2, 2, 3);
bar(grayLevelsG, pixelCountsG, 'BarWidth', 1, 'FaceColor', 'g');
grid on;
title('Green Histogram', 'FontSize', fontSize);
subplot(2, 2, 4);
bar(grayLevelsB, pixelCountsB, 'BarWidth', 1, 'FaceColor', 'b');
grid on;
title('Blue Histogram', 'FontSize', fontSize);
Regarding your code...What do you think this line will do:
for k=1:0:256
You have a step size of 0!!!! And in this line:
for k=0:256
what do you think will happen when k = 256 in this line:
final(i,j)=z(1,k+1)+1;
Normally you run from 0 to 255 (but not as indexes) or from 1 to 256, not from 0 to 256.

3 Comments

I need to develop the code without the imhist function. please could you provide me another code for histogram equalization without using imhist function. Let me know if I change the step size and index, does the code works fine?
It's easy enough for you to try. Just change 0 to 1 and run it.
Well, because you at least gave a shot at it, I decided to correct the first part of the code. Hopefully you can get the last part where you apply the transform to the image to equalize it.
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.
format longg;
format compact;
fontSize = 22;
% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'cameraman.tif';
% 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);
% 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]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
% Compute the histogram
theHistogram= ones(1,256);
theHistogram = zeros(1,256);
for i=1:rows
for j=1:columns
% Get the gray level of this pixel
grayLevel = grayImage(256,i);
% Increment the histogram at that gray level.
theHistogram(grayLevel+1) = theHistogram(grayLevel+1) + 1;
end
end
% Plot the histogram as a bar chart.
subplot(2, 2, 2);
bar(theHistogram);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 256]); % Scale x axis manually.
% Compute the CDF of the histogram without using cumsum().
cdf(1)= theHistogram(1);
for k = 2 : 256
cdf(k) = cdf(k - 1) + theHistogram(k);
end
% Plot CDF.
subplot(2, 2, 3);
plot(cdf);
grid on;
title('CDF of original image', 'FontSize', fontSize);
xlim([0 256]); % Scale x axis manually.

Sign in to comment.

More Answers (0)

Categories

Find more on Display Image in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!