MATLAB: 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.

histogramImage Processing Toolbox

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);

Best 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.
Related Question