[Digital image processing matlab] (HSI transform fusion algorithm)

[Digital image processing matlab] (HSI transform fusion algorithm)

Input a high-resolution panchromatic image HR and a low-resolution multi-spectral image MS, and use the HSI transform fusion algorithm to achieve image fusion. The conversion between RGB and HSI images calls the custom function RGB2HSI(), HSI2RGB( ), the specific code of the function is.

RGB2HSI()

function Image1=RGB2HSI(image)
%调用代码-------------------------------------------
%imageR=imread('animal.jpg');  
%RGB2HSI(image);
%--------------------------------------------------

image=im2double(image);
%从三维数组中提取三幅分量图像
R1=image(:,:,1);
G1=image(:,:,2);
B1=image(:,:,3);
I=(R1+G1+B1)/3;        %亮度分量,范围[0,1]
m=min(min(R1,G1),B1);
S=1-3*m./(R1+G1+B1);    %饱和度分量,范围[0,1]
theta=acos(((R1-G1)+(R1-B1))./(2*((R1-G1).^2+((R1-B1).*(G1-B1))).^(1/2))); %弧度 
H=theta;   %色度分量,以角度表示,范围是[0,1](弧度除以2*pi后)
if B1>G1
    H=2*pi-theta;       
end     
if S==0
    H=0;
end
H=H/(2*pi);
Image1=cat(3,H,S,I);
subplot(1,2,1),imshow(image);
title('RGB原图')
subplot(1,2,2),imshow(Image1);
title('转换后的HSI图像')
end

HSI2RGB()

function rgb= HSI2RGB(hsi) 
H = hsi(:, :, 1) * 2 * pi; 
S = hsi(:, :, 2); 
I = hsi(:, :, 3); 

R = zeros(size(hsi, 1), size(hsi, 2)); 
G = zeros(size(hsi, 1), size(hsi, 2)); 
B = zeros(size(hsi, 1), size(hsi, 2)); 

idx = find( (0 <= H) & (H < 2*pi/3)); 
B(idx) = I(idx) .* (1 - S(idx)); 
R(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx)) ./ cos(pi/3 - H(idx))); 
G(idx) = 3*I(idx) - (R(idx) + B(idx)); 

idx = find( (2*pi/3 <= H) & (H < 4*pi/3) ); 
R(idx) = I(idx) .* (1 - S(idx)); 
G(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 2*pi/3) ./ cos(pi - H(idx))); 
B(idx) = 3*I(idx) - (R(idx) + G(idx)); 

idx = find( (4*pi/3 <= H) & (H <= 2*pi)); 
G(idx) = I(idx) .* (1 - S(idx)); 
B(idx) = I(idx) .* (1 + S(idx) .* cos(H(idx) - 4*pi/3) ./cos(5*pi/3 - H(idx))); 
R(idx) = 3*I(idx) - (G(idx) + B(idx)); 

rgb = cat(3, R, G, B); 
rgb = max(min(rgb, 1), 0); 
figure,
%subplot(1,2,1),
imshow(rgb);title('转换回的RGB图像') 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
H1 = hsi(:, :, 1) * 2 * pi; 
S1 = hsi(:, :, 2); 
I1 = hsi(:, :, 3);
R1 = zeros(size(hsi, 1), size(hsi, 2)); 
G1= zeros(size(hsi, 1), size(hsi, 2)); 
B1 = zeros(size(hsi, 1), size(hsi, 2));
if 0<=H1<2*pi/3;
    B1=I1.*(1-S1);
    R1=I1.*(1+(S1.*cos(H1)./cos(pi/3-H1)));
    G1=3.*I1-(B1+R1);   
elseif((2*pi/3<=H1)&&(H1<4*pi/3));
       R1=I1.*(1-S1);
        G1=I1.*(1+(S1.*cos(H1-2*pi/3)./cos(pi-H1)));
        B1=3.*I1-(R1+G1); 
else((4*pi/3<=H1)&(H1<2*pi));
    G1=I1.*(1-S1);
    B1=I1.*(1+(S1.*cos(H1-4*pi/3)./cos(10*pi/6-H1)));
    R1=3.*I1-(G1+B1);
end
r=cat(3,R1,G1,B1);
r=max(min(r,1),0);
subplot(1,2,2),imshow(r);
title('转换回的RGB图像') 

end

Fusion algorithm based on HSI transformation:

function F=HSI_melt(HR,MS)
%调用代码------------------------------------------------------
%Image1=imread('HR.jpg');Image2=imread('MS.jpg');
%HSI_melt(Image1,Image2);
%--------------------------------------------------------------
HR=im2double(HR); [row,column,~]=size(HR);
MS=im2double(MS);
MS1=RGB2HSI(MS);     %多光谱影像转换至HSI空间
H=MS1(:,:,1); S=MS1(:,:,2); I=MS1(:,:,3);
MAX2=max(max(I));MIN2=min(min(I));
MAX1=max(max(HR));MIN1=min(min(HR));
%高分辨率影像拉与亮度分量一致,假设变换前[MIN1,MAX2]后的亮度值为[MIN2,MAX2]
A=(MAX2-MIN2)/(MAX1-MIN1);B=(MAX1*MIN2-MIN1*MAX2)/(MAX1-MIN1);
HR1=zeros(row,column);
for i=1:row
    for j=1:column
        HR1(i,j)=HR(i,j)*A+B;
    end
end

MS2=cat(3,H,S,HR1);
Image=HSI2RGB(MS2);
figure,imshow(Image);
title('基于HSI变换的影像融合结果');
end

test:

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/gsgs1234/article/details/124144602