【数字图像处理matlab】 (统计与描述)
一些数字图像处理,统计与描述相关代码,matlab编写
1.均值
读入一张RGB影像,计算得到像素均值。
matlab函数为mean(),直接实现代码为:
ave=mean(mean(mean(A)))
%均值
A=imread('flower.jpg');
[row,column,band]=size(A);
n=row*column;
z=1;sum=0;
for i=1:row
for j=1:column
for k=1:band
x(z)=double(A(i,j,k));
sum=sum+x(z);
z=z+1;
end
end
end
ave=sum/(row*column*band)
2.排序
一维数组排序,
matlab自带函数实现:
sort(x)
%排序[a,b]=sort(x)
[row,column]=size(x);
n=row*column;
for i=1:n
for j=1:n-i
if x(j)>x(j+1)
t=x(j);
x(j)=x(j+1);
x(j+1)=t;
end
end
end
x
3.中值
一维数组中值,先调用2.排序函数,再寻找中值。
matlab自带函数实现:
median(x)
%中值<排好序的数组>
if rem(n,2) %求余函数,判断n为奇数还是偶数
i=fix(n/2)+1;
mid=x(i)
else
mid=(x(n/2)+x(n/2+1))/2 %n为偶数时
end
4.众数
图像和一维数组众数实现
matlab自带函数实现:
mode(x)
function F=zhong(image)
%调用代码-----------------------
%image=rgb2gray(imread('eye.jpg'));
%zhong(image);
%调用代码-----------------------
%求图像众数
[row,column]=size(image);
X=imhist(image);
X=X';
[~,index]=max(X);
mode=index-1
time=X(1);Mode=0;
for i=2:256
if X(i)>time
time=X(i);
Mode=i-1;
end
end
Mode
%一维数组众数
Y=[7,2,3,6,8,3,5,3,1,3];
Z=zeros(1,max(Y)+1);
[r,c]=size(Y);
for i=1:c
Z(Y(i)+1)=Z(Y(i)+1)+1;
end
[time2,index2]=max(Z);
Mode1=index2-1
end
5+6.方差/协方差 相关系数
RGB图像的方差/协方差,相关系数计算
matlab自带函数实现:
var(x)
corrcoef(X)
%方差,协方差,var(x),...相关系数corrcoed(X),X为二维数组
A=
sum1=0;sum2=0;
for i=1:row
for j=1:column
x(z)=A(i,j,1); %第一波段和第二波段的方差,相关系数
y(z)=A(i,j,2);
sum1=sum1+x(z);
sum2=sum2+y(z);
z=z+1;
end
end
ave1=sum1/(row*column);
ave2=sum2/(row*column);
s1=0;s2=0;cov=0;
for i=1:n
s1=s1+(x(i)-ave1)*(x(i)-ave1)/(n-1); %方差
s2=s2+(y(i)-ave2)*(y(i)-ave2)/(n-1);
cov=cov+(x(i)-ave1)*(y(i)-ave2)/(n-1); %协方差
end
s1
s2
r=cov/sqrt(s1*s2) %相关系数
6.直方图/累计直方图
读入一个RGB图像,输出其灰度直方图和累计直方图。
当然,也可以读入的时候将图像转化为灰度图:image=rgb2gray(imread(‘xxx.jpg’));
function Result=Histogram(imageR)
%统计R波段的灰度直方图
%输入为r波段(一个波段)
%输出为灰度直方图
%调用代码-------------------------------
% imageR=imread('animal.jpg');
% Histogram(imageR);
[row,column]=size(imageR);
Result=zeros(1,256);
for i=1:row
for j=1:column
k=imageR(i,j);
Result(k+1)=Result(k+1)+1;%记录每个灰度值的像素数
end
end
figure;
z=zeros(1,256);
for i=1:256
for j=1:i
z(i)=z(i)+Result(j); %累计直方图
end
end
subplot(1,3,1);imshow(imageR); %将多个图画画在一个平面上
subplot(1,3,2);bar(Result); %axis tight;
subplot(1,3,3);bar(z);
end
测试: