Matlab遙感數字圖像處理函數彙總

Matlab遙感數字圖像處理函數彙總

遙感數字圖像處理
是通過計算機圖像處理系統
對遙感圖像中的像素進行的系列操作過程。

(以下函數均針對每一個波段分開進行處理,如需綜合處理,用reshape函數將圖像矩陣轉換成 向量(行×列×波段數) 即可)

溫馨提示:‘文章有點長,可按目錄定位!’

1 圖像讀取

1.1 讀取遙感影像hdr格式頭文件

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

1.2 讀取img格式或dat格式的遙感圖像

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;  

2 圖像統計與描述

2.1 圖像均值函數

%函數名稱爲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

2.2 圖像中值函數

%函數名稱爲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

2.3 圖像累計直方圖函數

%函數名稱爲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

2.4 圖像衆數函數

%函數名稱爲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

2.5 圖像的協方差矩陣

%函數名稱爲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

2.6 圖像的相關係數矩陣

%函數名稱爲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

3 圖像增強處理

3.1 直方圖線性拉伸

%函數名稱爲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

3.2 直方圖均衡化

%函數名稱爲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

3.3 直方圖匹配

%函數名稱爲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

3.4 中值濾波

%函數名稱爲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

3.5 均值濾波

%函數名稱爲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

3.6 Sobel算子銳化

%函數名稱爲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

3.7 Prewitt算子銳化

%函數名稱爲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

3.8 RGB to HSI

%函數名稱爲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

3.9 HSI to RGB

%函數名稱爲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

3.10 PCA變換

%函數名稱爲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

4 影像融合處理

4.1 基於Brovey變換的影像融合

%函數名稱爲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

4.2 基於乘積變換的影像融合

%函數名稱爲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

4.3基於PCA的影像融合

%函數名稱爲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

4.4 基於HSI變換的影像融合

%函數名稱爲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

5 影像特徵提取

5.1 迭代閾值影像分割算法

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

5.2 區域生長影像分割算法

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

6 影像分類

6.1 K-Means聚類分割

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

6.2 最短距離法分類

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

6.3 最大似然法分類

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

後續有時間再補充~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章