# طلب كود يقوم بعمل الدالة imhist

(star programmer) #1

محتاجه كود لعمل histogram في ال matlab بحيث يقوم بنفس عمل الدالة imhist

ضرووري ارجوكم

(mr.volt) #2

[SIZE=3]اليك ما تريد اخي الكريم

[/size][LEFT][SIZE=3]

``````
[/size][SIZE=3] function [yout,x] = imhist(varargin)[/size]
[SIZE=3][a, n, isScaled, top, map] = parse_inputs(varargin{:});

if islogical(a)
if (n ~= 2)
messageId = 'Images:imhist:invalidParameterForLogical';
message1 = 'N must be set to two for a logical image.';
error(messageId, '%s', message1);
return;
end
y(2) = sum(a(:));
y(1) = numel(a) - y(2);
y = y';
elseif isa(a,'int16')
y = imhistc(int16touint16(a), n, isScaled, top); % Call MEX file to do work.
else
y = imhistc(a, n, isScaled, top); % Call MEX file to do work.
end

classin = class(a);
switch classin
case 'uint8'
x = linspace(0,255,n)';
case 'uint16'
x = linspace(0,65535,n)';
case {'double','single'}
x = linspace(0,1,n)';
case 'logical'
x = [0,1]';
case 'int16'
x = linspace(-32768,32767,n)';
otherwise
eid = 'Images:imhist:internalError';
msg = 'Internal error: I or X has invalid data type.';
error(eid,'%s', msg);
end

if (nargout == 0)
plot_result(x, y, map, isScaled, classin);
else
yout = y;
end

%%%
%%% Function plot_result
%%%
function plot_result(x, y, cm, isScaled, classin)

n = length(x);
stem(x,y, 'Marker', 'none')
hs = gca;

limits = axis;
if n ~= 1
limits(1) = min(x);
else
limits(1) = 0;
end
limits(2) = max(x);
var = sqrt(y'*y/length(y));
limits(4) = 2.5*var;
axis(limits);

% Get axis position and make room for color stripe.
pos = get(hs,'pos');
stripe = 0.075;
set(hs,'pos',[pos(1) pos(2)+stripe*pos(4) pos(3) (1-stripe)*pos(4)])
set(hs,'xticklabel','')

% Create axis for stripe
axes('Position', [pos(1) pos(2) pos(3) stripe*pos(4)]);
limits = axis;

% Create color stripe
if isScaled,
binInterval = 1/n;
xdata = [binInterval/2 1-(binInterval/2)];
switch classin
case 'uint8'
xdata = 255*xdata;
limits(1) = 0;
limits(2) = 255;
C = (1:n)/n;
case 'uint16'
xdata = 65535*xdata;
limits(1) = 0;
limits(2) = 65535;
C = (1:n)/n;
case 'int16'
xdata = 65535*xdata - 32768;
limits(1) = -32768;
limits(2) = 32767;
C = (1:n)/n;
case {'double','single'}
limits(1) = 0;
limits(2) = 1;
C = (1:n)/n;
case 'logical'
limits(1) = 0;
limits(2) = 1;
C = [0 1];
otherwise
messageId = sprintf('Images:%s:invalidImageClass', mfilename);
message = ['The input image must be uint8, uint16, double, or' ...
' logical.'];
error(messageId, '%s', message);
end

% image(X,Y,C) where C is the RGB color you specify.
image(xdata,[0 1],repmat(C, [1 1 3]));
else
if length(cm)<=256
image([1 n],[0 1],1:n); colormap(cm)
limits(1) = 0.5;
limits(2) = n + 0.5;
else
image([1 n],[0 1],permute(cm, [3 1 2]));
limits(1) = 0.5;
limits(2) = n + 0.5;
end
end

set(gca,'yticklabel','')
axis(limits);
j=line(limits([1 2 2 1 1]),limits([3 3 4 4 3]));set(j,'linestyle','-')
set(j,'color',get(gca,'xcolor'))

% Put a border around the stripe.
j=line(limits([1 2 2 1 1]),limits([3 3 4 4 3]));set(j,'linestyle','-')
set(j,'color',get(gca,'xcolor'))

% Special code for a binary image
if strcmp(classin,'logical')
% make sure that the stripe's X axis has 0 and 1 as tick marks.
set(gca,'XTick',[0 1]);

% remove unnecessary tick marks from axis showing the histogram
axes(hs)
set(gca,'XTick',0);

% make the histogram lines thicker
h = get(hs,'children');
obj = findobj(h,'flat','Color','b');
lineWidth = 10;
set(obj,'LineWidth',lineWidth);
else
axes(hs)
end

set(gcf,'Nextplot','replace')

%%%
%%% Function parse_inputs
%%%
function [a, n, isScaled, top, map] = parse_inputs(varargin)

iptchecknargin(1,2,nargin,mfilename);
a = varargin{1};
iptcheckinput(a, {'double','uint8','logical','uint16','int16','single'}, ...
{'2d','nonsparse'}, mfilename, ['I or ' 'X'], 1);
n = 256;
isScaled = [];
top = [];
map = [];

if isa(a,'double') || isa(a,'single')
isScaled = 1;
top = 1;
map = [];

elseif isa(a,'uint8')
isScaled = 1;
top = 255;
map = [];

elseif islogical(a)
n = 2;
isScaled = 1;
top = 1;
map = [];

else % int16 or uint16
isScaled = 1;
top = 65535;
map = [];
end

if (nargin ==2)
if (numel(varargin{2}) == 1)
% IMHIST(I, N)
n = varargin{2};
iptcheckinput(n, {'numeric'}, {'real','positive','integer'}, mfilename, ...
'N', 2);

elseif (size(varargin{2},2) == 3)
if isa(a,'int16')
eid = sprintf('Images:%s:invalidIndexedImage',mfilename);
msg1 = 'An indexed image can be uint8, uint16, double, single, or ';
msg2 = 'logical.';
error(eid,'%s %s',msg1, msg2);
end

% IMHIST(X,MAP) or invalid second argument
n = size(varargin{2},1);
isScaled = 0;
top = n;
map = varargin{2};

else
messageId = sprintf('Images:%s:invalidSecondArgument', mfilename);
message4 = 'Second argument must be a colormap or a positive integer.';
error(messageId, '%s', message4);
end
end

``````

[/size][/left]

(star programmer) #3

متشكرة جدا على الرد … بس طلعلي خطأ مش عارفة اصححه اللى هو ده

??? Error: File: C:\MATLAB6p1\work\imhist.m Line: 168 Column: 21
Expected a variable, function, or constant, found “|”.

بس على العموم انا توصلت نتيجة البحث إلى الكود ده

``````I=imread('sunset.jpg');
I2=rgb2gray(I);
val=[];
num=[];
counter=1;
for i=0:1:255
[x,y]=find(I2==i); %finds where I2==i
val(counter)=i;
num(counter)=length(x);  %find how many pixels of I2 have value of i
counter=counter+1;
end
plot(val,num);

``````