遥感数字图像处理
是通过计算机图像处理系统
对遥感图像中的像素进行的系列操作过程。
(以下函数均针对每一个波段分开进行处理,如需综合处理,用reshape函数将图像矩阵转换成 向量(行×列×波段数) 即可)
温馨提示:‘文章有点长,可按目录定位!’
function parameter =HDR_Read(HDRfilename)
%设置函数HDR_Read,返回值为parameter,参数为HDRfilename
fileID = fopen(HDRfilename, 'r'); %以只读方式打开.hdr头文件
%一直读取头文件的内容直至文件末尾
while ~feof(fileID)
fileline=fgetl(fileID);
C = cellstr(strsplit(fileline,'= ')); %按"="分割每一行
if(C(1)=="samples ")
samples=str2double(cell2mat(C(2))); %存储列数samples
end
if(C(1)=="lines ")
lines=str2double(cell2mat(C(2))); %存储行数lines
end
if(C(1)=="bands ") %存储波段数bands
bands=str2double(cell2mat(C(2)));
end
if(C(1)=="interleave ") %存储envi中数据存储方式
interleave=cell2mat(C(2));
end
if(C(1)=="data type ") %存储数据类型
datatype=cell2mat(C(2));
end
end
fclose(fileID); %关闭文件的读取
%定义ENVI中的数据类型格式
data_type = {'uint8' 'int16' 'int32' 'float32' 'float64' 'uint16' 'uint32' 'uint64'};
%将envi中不同的数据格式转换成matlab中的特定格式
switch datatype
case '1'
datatype = cell2mat(data_type(1)); %灰度范围0-255
case '2'
datatype = cell2mat(data_type(2)); %16位整数
case '3'
datatype = cell2mat(data_type(3)); %32位整数
case '4'
datatype = cell2mat(data_type(4)); %32位浮点数
case '5'
datatype = cell2mat(data_type(5)); %64位浮点数
case '6'
datatype = cell2mat(data_type(6)); %灰度范围0-2^16
case '7'
datatype = cell2mat(data_type(7)); %灰度范围0-2^32
case '8'
datatype = cell2mat(data_type(8)); %灰度范围0-2^64
end
%将各数据存入返回值参数parameter中
parameter = {samples,lines,bands,interleave,datatype};
end
function Re=IMG_Read(IMGfilename)
HDRfilename =strcat(strtok(IMGfilename,'.'),'.hdr'); %得到相同名字的头文件
parameter = HDR_Read(HDRfilename); %读取头文件中的参数信息
samples = cell2mat(parameter(1)); %得到列数
lines = cell2mat(parameter(2)); %得到行数
bands = cell2mat(parameter(3)); %获得波段数
interleave = cell2mat(parameter(4)); %获得图像存储格式
datatype = cell2mat(parameter(5)); %获取数据格式
fid = fopen(IMGfilename, 'r'); %以只读方式打开img文件
Image=fread(fid,datatype); %按指定格式读取img图像文件
%针对不同的图像存储格式,处理的到图像矩阵
switch interleave
case 'bsq' %当存储格式为bsq时,按波段存储
Image=reshape(Image,[samples,lines,bands]);%将图像读入三维矩阵
for i=1:bands
IMG(:,:,i)=Image(:,:,i)';
end
Image=IMG;
case 'bil' %当存储格式为bil时,按行存储
IMG=zeros(lines,samples,bands);
count=1;
for row =1:lines
for i=1:bands
IMG(row,:,i) =Image((count-1)*samples+1:count*samples);
count=count+1;
end
end
Image=IMG;
case 'bip' %当存储格式为bip时,按像元存储
IMG=zeros(lines,samples,bands);
count=1;
for row=1:lines
for col=1:samples
IMG(row,col,:)=Image((count-1)*bands+1:count*bands);
count=count+1;
end
end
Image=IMG;
otherwise
error('格式错误,无法读取图像!');
end
Re = {samples,lines,bands,datatype,Image};
fclose(fid);
subplot(2,2,1);
imgmul=cat(3,Image(:,:,3),Image(:,:,2),Image(:,:,1));%合成3维矩阵
colormap(colorcube);
imshow(uint8(imgmul));
colorbar;
title('前三个波段组合图像');
subplot(2,2,2);
img1=Image(:,:,1);
colormap(gray);
imshow(uint8(img1));
colorbar;
title('第一个波段灰度图');
subplot(2,2,3);
img2=Image(:,:,2);
colormap(gray);
imshow(uint8(img2))
colorbar;
title('第二个波段灰度图');
subplot(2,2,4);
img3=Image(:,:,3);
colormap(gray);
imshow(uint8(img3))
colorbar;
title('第三个波段灰度图');
% axis off;
%函数名称为Image_Mean,输入参数Image,输出参数Mean
function [Mean] = Image_Mean(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%将三维矩阵转换成二维矩阵,方便计算
Image1 = reshape(Image,[m*n,bands]);
%初始化求和矩阵
Sum = zeros(bands,1);
%计算每个波段所有像元的灰度之和
for k = 1:bands
for i = 1:m*n
Sum(k) = Sum(k) + Image1(i,k);
end
end
%计算每个波段的灰度平均值
Mean = Sum /(m*n);
end
%函数名称为Image_Median,输入参数Image,输出参数Median
function [Median] = Image_Median(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%将三维矩阵转换成二维矩阵,方便计算
Image1 = reshape(Image,[m*n,bands]);
%对每一个波段进行冒泡排序
for k = 1:bands
for i = 1:m*n
for j = 1:m*n-i
if(Image1(j,k)>Image1(j+1,k))
temp = Image1(j,k);
Image1(j,k) = Image1(j+1,k);
Image1(j+1,k) = temp;
end
end
end
end
%去排序后每个波段中间值作为中值
for k = 1:bands
if(mod(m*n,2) == 0)
Median(k) = Image1(m*n/2,k)/2 + Image1(m*n/2+1,k)/2;
else
Median(k) = Image1(m*n/2+1,k);
end
end
end
%函数名称为Image_Hist,输入参数Image,输出参数Hist
function [Hist] = Image_Hist(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%将三维矩阵转换成二维矩阵,方便计算
Image1 = reshape(Image,[m*n,bands]);
%初始化三维矩阵,行表示256种灰度,列表示灰度值、个数、累计个数
Hist = zeros(256,2,bands);
%求每个波段中每个灰度值的个数
for k = 1:bands
for i = 1:256
for j = 1:m*n
if(Image1(j,k) == i-1)
Hist(i,1,k) = Hist(i,1,k) + 1;
end
end
end
end
%转换为频率直方图
Hist = Hist./(m*n);
%求每个波段每个灰度的累计个数
Hist(1,2,:) = Hist(1,1,:);
for k = 1:bands
for i = 2:256
Hist(i,2,k) = Hist(i-1,2,k) + Hist(i,1,k);
end
end
end
%函数名称为Image_Mode,输入参数Image,输出参数Mode
function [Mode] = Image_Mode(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%将三维矩阵转换成二维矩阵,方便计算
Image1 = reshape(Image,[m*n,bands]);
%计算直方图矩阵
Hist = Image_Hist(Image);
%另初始最大个数为0
Max = 0 ;
%初始众数矩阵,每一行是一个波段的众数
Mode = zeros(bands,1);
%计算每个波段灰度值的众数
for k = 1:bands
for i = 1:256
if(Hist(i,1,k)>Max)
Max = Hist(i,1,k);
Mode(k) = i-1;
end
end
end
end
%函数名称为Image_Cov,输入参数Image,输出参数Cov
function [Cov] = Image_Cov(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%将三维矩阵转换成二维矩阵,方便计算
%只有三个波段的情况下
Image1 = reshape(Image(:,:,1),[m*n,1]);
Image2 = reshape(Image(:,:,2),[m*n,1]);
Image3 = reshape(Image(:,:,3),[m*n,1]);
IM = [Image1;Image2;Image3];
%求每一个波段灰度值的均值
Mean = Image_Mean(Image);
%初始化协方差矩阵
Cov = zeros(bands);
%计算协方差矩阵
for i = 1:bands
for j = 1:bands
for k = 1:m*n
Cov(i,j) = Cov(i,j)+(IM(i,k)-Mean(i))*(IM(j,k)-Mean(k)) ;
end
Cov(i,j) = Cov(i,j);
end
end
end
%函数名称为Image_R,输入参数Image,输出参数R
function [R] = Image_R(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%计算协方差矩阵
Cov = Image_Cov(Image);
%初始化相关系数矩阵
R = zeros(bands);
%计算相关系数
for i = 1:bands
for j = 1:bands
R = Cov(i,j)/(sqrt(Cov(i,i))*sqrt(Cov(j,j)));
end
end
end
%函数名称为Image_LinearStretch,输入参数Image,输出参数IMAGE
function [IMAGE] = Image_LinearStretch(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%初始化计算矩阵
Image1 = reshape(Image,[m*n,bands]);
%计算每个波段灰度的最大最小值
for k = 1:bands
Max(k,1) = max(max(Image(:,:,k)));
Min(k,1) = min(min(Image(:,:,k)));
end
%线性拉伸
for k = 1:bands
for i = 1:m*n
Image1(i,k) = (Image1(i,k)-Min(k))*255/(Max(k)-Min(k)) + 0;
end
end
%将二维矩阵转换为三维矩阵
IMAGE = reshape(Image1,[m,n,bands]);
%画图,左右分别表示原图和处理后的图像
figure(1);
subplot(1,2,1);
imshow(uint8(Image));
title('原始图像');
subplot(1,2,2);
imshow(uint8(IMAGE));
title('线性拉伸后的图像');
end
%函数名称为Image_HistogramEqualization,输入参数Image,输出参数IMAGE
function [IMAGE] = Image_HistogramEqualization(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%初始化计算矩阵
IMAGE = Image;
%计算图像的直方图矩阵
Hist = Image_Hist(Image);
%建立灰度映射函数
for k = 1:bands
for i = 1:256
P(i,k) = round(255* Hist(i,2,k));
end
end
%计算直方图均衡化后的矩阵
for k = 1:bands
for i = 1:m
for j = 1:n
IMAGE(i,j,k) = P(IMAGE(i,j,k)+1,k);
end
end
end
%画图,左右分别表示原图和处理后的图像
figure(1);
subplot(1,2,1);
imshow(uint8(Image));
title('原始图像');
subplot(1,2,2);
imshow(uint8(IMAGE));
title('直方图均衡化后的图像')
end
%函数名称为Image_HistogramMatching,输入参数Image1,Image2,输出参数IMAGE
function [IMAGE] = Image_HistogramMatching(Image1,Image2)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image1);
%初始化计算矩阵
IMAGE = Image1;
%计算图像的直方图矩阵
Hist1 = Image_Hist(Image1);
Hist2 = Image_Hist(Image2);
%建立灰度映射函数
for k = 1:bands
for i = 1:256
[vv,index] = min(abs(Hist1(i,2,k)-Hist2(:,2,k)));
P(i,k) = index -1;
end
end
%计算直方图均衡化后的矩阵
for k = 1:bands
for i = 1:m
for j = 1:n
IMAGE(i,j,k) = P(IMAGE(i,j,k)+1,k);
end
end
end
%画图
figure(1);
subplot(1,3,1);
imshow(uint8(Image1));
title('原始图像');
subplot(1,3,2);
imshow(uint8(Image2));
title('参照图像');
subplot(1,3,3);
imshow(uint8(IMAGE));
title('直方图匹配化后的图像')
end
%函数名称为Image_MedianFilter,输入参数Image,输出参数IMAGE
function [IMAGE] = Image_MedianFilter(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%加高斯噪声
Image1 = imnoise(uint8(Image),'gaussian');
%初始化矩阵
IMAGE = Image1;
%定义模板大小,假设模板大小3×3
A = 1;
%中值滤波
for k = 1:bands
for i = 1+A:m-A
for j = 1+A:n-A
temp = Image1(i-A:i+A,j-A:j+A,k);
IMAGE(i,j,k) = round(median(median(temp)));
end
end
end
%画图
figure(1);
subplot(1,3,1);
imshow(uint8(Image));
title('原始图像');
subplot(1,3,2);
imshow(uint8(Image1));
title('加噪声以后的图像');
subplot(1,3,3);
imshow(uint8(IMAGE));
title('中值滤波后的图像')
end
%函数名称为Image_Mean,输入参数Image,输出参数IMAGE
function [IMAGE] = Image_Mean(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%加高斯噪声
Image1 = imnoise(uint8(Image),'gaussian');
%定义模板大小,假设模板大小3×3
A = 1;
%初始化矩阵
IMAGE = Image1;
%均值滤波
for k = 1:bands
for i = 1+A:m-A
for j = 1+A:n-A
temp = Image1(i-A:i+A,j-A:j+A,k);
IMAGE(i,j,k) = round(mean(mean(temp)));
end
end
end
%画图
figure(1);
subplot(1,3,1);
imshow(uint8(Image));
title('原始图像');
subplot(1,3,2);
imshow(uint8(Image1));
title('加噪声以后的图像');
subplot(1,3,3);
imshow(uint8(IMAGE));
title('均值滤波后的图像')
end
%函数名称为Image_Sobel,输入参数Image,输出参数IMAGE
function [IMAGE] = Image_Sobel(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%定义模板大小,假设模板大小3×3
A = 1;
%定义Sobel算子x,y方向矩阵
Sobelx = [-1 -2 -1;0 0 0;1 2 1];
Sobely = [-1 0 1;-2 0 2;-1 0 1];
%初始化矩阵
Image1 = zeros(m,n,bands);
IMAGE = Image;
%Sobel算子
for k = 1:bands
for i = 1+A:m-A
for j = 1+A:n-A
temp = Image(i-A:i+A,j-A:j+A,k);
Image1(i,j,k) = abs(sum(sum(Sobelx.*temp)))+abs(sum(sum(Sobely.*temp)));
end
end
end
IMAGE = Image + Image1;
%画图,左右分别表示原图和两幅处理后的图像
figure(1);
subplot(1,3,1);
imshow(uint8(Image));
title('原始图像');
subplot(1,3,2);
imshow(uint8(Image1));
title('边缘提取图像');
subplot(1,3,3);
imshow(uint8(IMAGE));
title('Sobel算子锐化后的图像')
end
%函数名称为Image_Prewitt,输入参数Image,输出参数IMAGE
function [IMAGE] = Image_Prewitt(Image)
%获取矩阵的行、列、波段数
[m,n,bands] = size(Image);
%定义模板大小,假设模板大小3×3
A = 1;
%定义Prewitt算子x,y方向矩阵
Prewittx = [-1 -2 -1;0 0 0;1 2 1];
Prewitty = [-1 0 1;-2 0 2;-1 0 1];
%初始化矩阵
Image1 = zeros(m,n,bands);
IMAGE = Image;
%Sobel算子
for k = 1:bands
for i = 1+A:m-A
for j = 1+A:n-A
temp = Image(i-A:i+A,j-A:j+A,k);
Image1(i,j,k) = abs(sum(sum(Prewittx.*temp)))+abs(sum(sum(Prewitty.*temp)));
end
end
end
IMAGE = Image + Image1;
%画图,左中右分别表示原图和两幅处理后的图像
figure(1);
subplot(1,3,1);
imshow(uint8(Image));
title('原始图像');
subplot(1,3,2);
imshow(uint8(Image1));
title('边缘提取图像');
subplot(1,3,3);
imshow(uint8(IMAGE));
title('Prewitt锐化后的图像')
end
%函数名称为Image_RGB2HSI,输入参数Image,输出参数HSI
function HSI = Image_RGB2HSI(Image)
%RGB->HSI变换
[lines,samples,bands] = size(Image);
HSI = zeros(lines,samples,3); %用三维向量分别存储HSI(色度,饱和度,亮度)
for i = 1:lines
for j = 1:samples
if( Image(i,j,1)== Image(i,j,2) && Image(i,j,2) ==Image(i,j,3))
JD = 0;
else
JD = 180*acos((2*Image(i,j,1)-Image(i,j,2)-Image(i,j,3))/(2*((Image(i,j,1)-Image(i,j,2))^2+(Image(i,j,1)-Image(i,j,3))*(Image(i,j,2)-Image(i,j,3)))^0.5))/pi;
end
%求H色度
if(Image(i,j,2)>=Image(i,j,3))
HSI(i,j,1) = JD;
else
HSI(i,j,1) = 360 - JD;
end
%求S饱和度
HSI(i,j,2) = 1-3*min(min(Image(i,j,1),Image(i,j,2)),Image(i,j,3))/(Image(i,j,1)+Image(i,j,2)+Image(i,j,3));
%求I亮度
HSI(i,j,3) = (Image(i,j,1)+Image(i,j,2)+Image(i,j,3))/3;
end
end
%画图
figure(1);
subplot(1,2,1);
imshow(uint8(Image));
title('RGB图像')
subplot(1,2,2);
imshow(uint8(HSI));
title('HSI图像');
end
%函数名称为Image_HSI2RGB,输入参数Image,输出参数HSI
function Image = Image_HSI2RGB(HSI)
%HSI->RGB变换
[lines,samples] = size(HSI(:,:,1));
Image = zeros(lines,samples,3);
for i = 1:lines
for j = 1:samples
if(HSI(i,j,1)>=0 && HSI(i,j,1)<120)
Image(i,j,3) = round(HSI(i,j,3)*(1-HSI(i,j,2)));
Image(i,j,1) = round(HSI(i,j,3)*(1+HSI(i,j,2)*cos(pi*HSI(i,j,1)/180)/cos(pi*(60-HSI(i,j,1))/180)));
Image(i,j,2) = round(3*HSI(i,j,3)- Image(i,j,1)-Image(i,j,3));
end
if(HSI(i,j,1)>=120 && HSI(i,j,1)<240)
Image(i,j,1) = round(HSI(i,j,3)*(1-HSI(i,j,2)));
Image(i,j,2) = round(HSI(i,j,3)*(1+HSI(i,j,2)*cos(pi*(HSI(i,j,1)-120)/180)/cos(pi*(180-HSI(i,j,1))/180)));
Image(i,j,3) = round(3*HSI(i,j,3)- Image(i,j,1)- Image(i,j,2));
end
if(HSI(i,j,1)>=240 && HSI(i,j,1)<360)
Image(i,j,2) = round(HSI(i,j,3)*(1-HSI(i,j,2)));
Image(i,j,3) = round(HSI(i,j,3)*(1+HSI(i,j,2)*cos(pi*(HSI(i,j,1)-240)/180)/cos(pi*(360-HSI(i,j,1))/180)));
Image(i,j,1) = round(3*HSI(i,j,3)- Image(i,j,2)- Image(i,j,3));
end
end
end
%画图
figure(1);
subplot(1,2,1);
imshow(uint8(HSI));
title('HSI图像');
subplot(1,2,2);
imshow(uint8(Image));
title('RGB图像')
end
%函数名称为Image_HSI2RGB,输入参数Image,输出参数HSI
function Y = Image_PCA(Image)
[lines,samples,bands] = size(Image);
%PCA(K->L)变换
JZ = zeros(bands,lines*samples);
for i = 1:bands
JZ(i,:) = reshape(Image(:,:,i),[1,lines*samples]);
end
COV = cov(JZ');
[TZXL,TZZ] = eigs(COV); %得到特征值和特征向量
% [TZZ,index] = sort(diag(TZZ),'descend'); %将特征值按降序排列
% TZXL = TZXL(:,index); %特征值为bands列,特征向量矩阵的每一列代表对应特征值的特征向量
A = TZXL';
Y = A * JZ; %Y的每一行代表第n各主成分
end
%函数名称为Image_BroveyChange,输入参数Image1,Image2,输出参数Image3
function Image3 = Image_BroveyChange(Image1,Image2);
%Image1表示多光谱图像
[lines1,samples1,bands1] = size(Image1);
%Image2表示全色影像
[lines2,samples2,bands2] = size(Image2);
Image3 = zeros(lines2,samples2,bands1);
for k = 1:bands1
for i = 1:lines2
for j = 1:samples2
Image3(i,j,k) = Image2(i,j,k)*Image1(i,j,k)/sum(Image1(i,j,:));
end
end
end
figure(1)
subplot(1,3,1);
imshow(uint8(Image1));
title('多光谱影像');
subplot(1,3,2);
imshow(uint8(Image2));
title('全色影像');
subplot(1,3,3);
imshow(uint8(Image3));
title('Brovey变换融合后的影像');
end
%函数名称为Image_MultiplicativeChange,输入参数Image1,Image2,输出参数Image3
function Image3 = Image_MultiplicativeChange(Image1,Image2);
%Image1表示多光谱图像
[lines1,samples1,bands1] = size(Image1);
%Image2表示全色影像
[lines2,samples2,bands2] = size(Image2);
Image3 = zeros(lines2,samples2,bands1);
for k = 1:bands1
for i = 1:lines2
for j = 1:samples2
Image3(i,j,k) = Image2(i,j,k)*Image1(i,j,k);
end
end
end
Image3 = round((Image3 - min(Image3))*255./(max(Image3) - min(Image3)));
%画图
figure(1)
subplot(1,3,1);
imshow(uint8(Image1));
title('多光谱影像');
subplot(1,3,2);
imshow(uint8(Image2));
title('全色影像');
subplot(1,3,3);
imshow(uint8(Image3));
title('乘积变换融合后的影像');
end
%函数名称为Image_PCAFusion,输入参数Image1,Image2,输出参数Image3
function Image3 = Image_PCAFusion(Image1,Image2)
%Image1表示多光谱图像
[lines1,samples1,bands1] = size(Image1);
%Image2表示全色影像
[lines2,samples2,bands2] = size(Image2);
Image3 = zeros(lines2,samples2,bands1);
%PCA影像融合算法
AY = PCA(Image1);
%得到PCA系数
A = cell2mat(AY(1));
%得到第一主成分
Y = round(cell2mat(AY(2)));
%把高分辨率影像与第一主成分图像做直方图匹配
YY = matching(Image2(:,:,1),reshape(Y(1,:),[lines1,samples1]));
%将第一主成分转换成一行
Y(1,:) = reshape(YY,[1,lines2*samples2]);
%PCA逆变换
X = double(uint8(inv(A)* Y));
%将矩阵转换成三维矩阵
for i = 1:bands1
Image3(:,:,i) = reshape(X(i,:),[lines1,samples1]);
end
%画图
figure(1);
subplot(1,3,1);
imshow(uint8(Image1));
title('高光谱影像');
subplot(1,3,2);
imshow(uint8(Image2));
title('全色影像');
subplot(1,3,3);
imshow(uint8(Image3));
title('PCA融合后的影像');
end
%函数名称为Image_HSIChange,输入参数Image1,Image2,输出参数Image3
function Image = Image_HSIChange(Image1,Image2)
%把多光谱图像转换为HSI
HSI = RGB2HSI(Image1);
%提取出I向量
HSI(:,:,3) = round(HSI(:,:,3));
%把全色影像与I向量做直方图匹配
I = matching(Image2(:,:,1),HSI(:,:,3));
% I = Image2(:,:,1);
%把I向量用全色波段替换
HSI(:,:,3) = I;
%将替换后的HSI转换到RGB空间
Image3 =HSI2RGB(HSI);
figure(1);
subplot(1,3,1);
imshow(uint8(Image1));
title('高光谱影像');
subplot(1,3,2);
imshow(uint8(Image2));
title('全色影像');
subplot(1,3,3);
imshow(uint8(Image3));
title('HSI变换融合后的影像');
end
function G1 = Image_IterativeThresholdSegmentation(Image)
%迭代阈值影像分割算法
Image = Image(:,:,1);
[lines,samples] = size(Image);
T = median( reshape(Image,[1,lines*samples]));
T0 = inf;
while( abs(T0 - T) > 1e-2)
T0 = T;
G1 = Image>=T;
K1 = G1.*Image;
G2 = Image<T;
K2 = G2.*Image;
miu1 = sum(sum(K1))/sum(sum(G1));
miu2 = sum(sum(K2))/sum(sum(G2));
T = (miu1+miu2)/2;
end
figure(1);
subplot(1,2,1);
imshow(uint8(Image));
title('原图像');
subplot(1,2,2);
imshow(uint8(255*G1));
title('迭代分割后的图像');
end
function Image1 = Image_RegionGrowing(Image)
%区域生长影像分割算法
Image = Image(:,:,1);
[lines,samples] = size(Image);
figure(2);
subplot(1,2,1);
imshow(uint8(Image));
title('原图像');
Image1 = zeros(lines,samples);
[y0,x0]=getpts; %获得区域生长起始点
x0 = round(x0);
y0 = round(y0);
% seed=I(x0,y0);
T = 5;
num = 1;
index = 1;
point = cell(1,1000);
point{1,1}= [x0,y0];
while(index ~= num || index ==1)
x = point{1,index}(1);
y = point{1,index}(2);
for i = -1:1
for j = -1:1
xx = x + i;
yy = y + j;
if( xx>0 && xx<=lines && yy>0 && yy<=samples && abs(Image(xx,yy)-Image(x,y))<=T && Image1(xx,yy)==0)
Image1(xx,yy) = 255;
num = num + 1;
point{1,num} = [xx,yy];
end
end
end
index = index + 1;
end
subplot(1,2,2);
imshow(uint8(Image1));
title('区域生长影像分割算法处理后图像');
end
function Image1 = Image_KMeans(Image)
%k-means
[lines,samples,bands] = size(Image);
K = 5;
A = reshape(Image(:,:,:),lines*samples,3);
Center = zeros(K,3);
center = zeros(K,3);
%初始化聚类中心
for k = 1:K
Center(k,:)= round(255*k/K);
end
Result = zeros(lines*samples,1);
while(Center~=center)
center = Center;
%按最短距离聚类
for i = 1:lines*samples
for k = 1:K
Data(k) = sqrt((A(i,1)-Center(k,1))^2+(A(i,2)-Center(k,2))^2+(A(i,3)-Center(k,3))^2);
end
[m,index] = min(Data);
Result(i,1) = index;
end
%重新计算聚类中心
for k = 1:K
indexx = find(Result == K);
for j = 1:3
Center(K,3) = mean(A(indexx,j));
end
end
end
figure(1);
subplot(1,2,1);
imshow(uint8(Image));
title('原始图像');
Image1 = reshape(Result,lines,samples);
subplot(1,2,2);
imshow(label2rgb(Image1));
title('K-means聚类分割后的影像');
end
function Image2 = Image_MinDistance(Image)
%最短距离分类
[lines,samples,bands] = size(Image);
K = 3;
class1 = double(imread('class21.jpg'));
class2 = double(imread('class22.jpg'));
class3 = double(imread('class23.jpg'));
Class1 = round([mean(mean(class1(:,:,1))),mean(mean(class1(:,:,2))),mean(mean(class1(:,:,3)))]);
Class2 = round([mean(mean(class2(:,:,1))),mean(mean(class2(:,:,2))),mean(mean(class2(:,:,3)))]);
Class3 = round([mean(mean(class3(:,:,1))),mean(mean(class3(:,:,2))),mean(mean(class3(:,:,3)))]);
Class = reshape([Class1,Class2,Class3],[K,3]);
Image= double(imread('监督分类2.jpg'));
[lines,samples,bands] = size(Image);
Image1 = reshape(Image,[lines*samples,3]);
for i = 1:lines*samples
for k = 1:K
Data1(k) = sqrt((Image1(i,1)-Class(k,1))^2+(Image1(i,2)-Class(k,2))^2+(Image1(i,3)-Class(k,3))^2);
end
[m,index] = min(Data1);
Result(i,1) = index;
end
figure(3);
subplot(1,2,1);
imshow(uint8(Image));
title('原始图像');
Image2 = reshape(Result,lines,samples);
subplot(1,2,2);
imshow(label2rgb(Image2));
title('最短距离法聚类分割后的影像');
end
function pattern = Image_MaxLike(x,Theta)
% 功能:运用最大似然算法对图像进行分割
%输入参数:
% x: x为n*m的特征样本矩阵,每行为一个样本,每列为样本的特征
% Theta:即θ,可用试探法取一固定分数,如:1/2
%输出参数:pattern:输出聚类分析后的样本类别
%clear all
%clc
%x=[0,0; 3,8; 2,2;1,1; 5,3; 4,8; 6,3; 5,4; 6,4; 7,5]
%Theta=0.5;
%[pattern,centerIndex]=MaxMinDisFun(x,0.5)%
%参考https://blog.csdn.net/guyuealian/article/details/53708042
maxDistance=0;
index=1;%相当于指针指示新中心点的位置
k=1; %中心点计数,也即是类别
center=zeros(size(x)); %保存中心点
patternNum=size(x,1); %输入的数据数(样本数)
%distance=zeros(patternNum,3);%distance每列表示所有样本到每个聚类中心的距离
minDistance=zeros(patternNum,1);%取较小距离
pattern=(patternNum);%表示类别
center(1,:)=x(1,:);%第一个聚类中心
pattern(1)=1;
for i=2:patternNum
distance(i,1)=sqrt((x(i,:)-center(1,:))*(x(i,:)-center(1,:))');%欧氏距离,与第1个聚类中心的距离
minDistance(i,1)=distance(i,1);
pattern(i)=1; %第一类
if(maxDistance<distance(i,1))
maxDistance=distance(i,1);%与第一个聚类中心的最大距离
index=i;%与第一个聚类中心距离最大的样本
end
end
k=k+1;
center(k,:)=x(index,:);%把与第一个聚类中心距离最大的样本作为第二 个聚类中心
pattern(index)=2; %第二类
minDistance(index,1)=0;
while 1
for i=2:patternNum
if(minDistance(i,1)~=0)
distance(i,k)=sqrt((x(i,:)-center(k,:))*(x(i,:)-center(k,:))');%与第k个聚类中心的距离
if(minDistance(i,1)>distance(i,k))
minDistance(i,1)=distance(i,k);
pattern(i)=k;
end
end
end
%%查找minDistance中最大值
max=0;
for i=2:patternNum
if((max<minDistance(i,1))&minDistance(i,1)~=0) % (x(i,:)~=center(k,:))
max=minDistance(i,1);
index=i;
end
end
if(max>(maxDistance*Theta))
k=k+1;
center(k,:)=x(index,:);
pattern(index)=k;
minDistance(index,1)=0;
else
break;
end
end
后续有时间再补充~