clc;
clear all;
close all;
im=imread('contrast (3).tif');
d=ndims(im);
if(d==2)
%----------------------------------------------------------
% Gray Image
%----------------------------------------------------------
im=double(im);
[M N]=size(im);
MN=M*N;
sum=0;
for i=1:M
for j=1:N
sum = sum + im(i,j);
end
end
B=sum/MN;
disp(['Brightness of an Gray Image is ',num2str(B)]);
sum=0;
for i=1:M
for j=1:N
S=im(i,j)-B;
S=S^2;
sum = sum + S;
end
end
C=sum/MN;
con=double(C)^0.5;
disp(['Contrast of an Gray Image is ',num2str(con)]);
else
%----------------------------------------------------------
% RGB
%----------------------------------------------------------
B_sum=0;
C_sum=0;
for k=1:3
I=im(:,:,k);
I=double(I);
[M N]=size(I);
MN=M*N;
sum=0;
for i=1:M
for j=1:N
sum = sum + I(i,j);
end
end
B=sum/MN;
B_sum=B_sum+B;
sum=0;
for i=1:M
for j=1:N
S=I(i,j)-B;
S=S^2;
sum = sum + S;
end
end
C=sum/MN;
con=double(C)^0.5;
C_sum=C_sum+con;
end
disp(['Brightness of an RGB image is ',num2str(B_sum)]);
disp(['Contrast of an RGB image is ',num2str(C_sum)]);
end
clear all;
close all;
im=imread('contrast (3).tif');
d=ndims(im);
if(d==2)
%----------------------------------------------------------
% Gray Image
%----------------------------------------------------------
im=double(im);
[M N]=size(im);
MN=M*N;
sum=0;
for i=1:M
for j=1:N
sum = sum + im(i,j);
end
end
B=sum/MN;
disp(['Brightness of an Gray Image is ',num2str(B)]);
sum=0;
for i=1:M
for j=1:N
S=im(i,j)-B;
S=S^2;
sum = sum + S;
end
end
C=sum/MN;
con=double(C)^0.5;
disp(['Contrast of an Gray Image is ',num2str(con)]);
else
%----------------------------------------------------------
% RGB
%----------------------------------------------------------
B_sum=0;
C_sum=0;
for k=1:3
I=im(:,:,k);
I=double(I);
[M N]=size(I);
MN=M*N;
sum=0;
for i=1:M
for j=1:N
sum = sum + I(i,j);
end
end
B=sum/MN;
B_sum=B_sum+B;
sum=0;
for i=1:M
for j=1:N
S=I(i,j)-B;
S=S^2;
sum = sum + S;
end
end
C=sum/MN;
con=double(C)^0.5;
C_sum=C_sum+con;
end
disp(['Brightness of an RGB image is ',num2str(B_sum)]);
disp(['Contrast of an RGB image is ',num2str(C_sum)]);
end
No comments:
Post a Comment