0% found this document useful (0 votes)
21 views10 pages

function pca code

The document describes a MATLAB GUI for PCA-based image fusion, allowing users to select visible and infrared images for processing. It includes functionalities for image selection, preprocessing, anisotropic diffusion, PCA fusion, and displaying the fused image along with performance metrics like PSNR, SSIM, and entropy. The GUI is designed to facilitate the fusion of thermal and visible light images for enhanced analysis.

Uploaded by

nikhil032k4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views10 pages

function pca code

The document describes a MATLAB GUI for PCA-based image fusion, allowing users to select visible and infrared images for processing. It includes functionalities for image selection, preprocessing, anisotropic diffusion, PCA fusion, and displaying the fused image along with performance metrics like PSNR, SSIM, and entropy. The GUI is designed to facilitate the fusion of thermal and visible light images for enhanced analysis.

Uploaded by

nikhil032k4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

function pca_fusion_gui()

% Create GUI Figure

fig = figure('Name', 'PCA-Based Image Fusion', 'NumberTitle', 'off', 'Position', [100, 100, 600, 400]);

% UI Components

uicontrol('Style', 'text', 'Position', [200, 350, 200, 25], 'String', 'Infrared & Visible Image Fusion',
'FontSize', 12, 'FontWeight', 'bold');

btn1 = uicontrol('Style', 'pushbutton', 'Position', [50, 300, 200, 40], 'String', 'Select Visible Image',
'Callback', @select_vi);

btn2 = uicontrol('Style', 'pushbutton', 'Position', [350, 300, 200, 40], 'String', 'Select Infrared
Image', 'Callback', @select_ir);

btn3 = uicontrol('Style', 'pushbutton', 'Position', [225, 200, 150, 40], 'String', 'Run Fusion',
'Callback', @run_fusion);

% Axes for Image Display

ax1 = axes('Position', [0.1, 0.1, 0.35, 0.35]);

ax2 = axes('Position', [0.55, 0.1, 0.35, 0.35]);

% Global Variables for Image Storage

global I1 I2;

% Callback Functions

function select_vi(~, ~)

[file, path] = uigetfile({'*.jpg;*.png;*.bmp'}, 'Select Visible Image');

if file

I1 = imread(fullfile(path, file));

if size(I1, 3) > 1

I1 = rgb2gray(I1);

end

imshow(I1, 'Parent', ax1);

title(ax1, 'Visible Image');


end

end

function select_ir(~, ~)

[file, path] = uigetfile({'*.jpg;*.png;*.bmp'}, 'Select Infrared Image');

if file

I2 = imread(fullfile(path, file));

if size(I2, 3) > 1

I2 = rgb2gray(I2);

end

imshow(I2, 'Parent', ax2);

title(ax2, 'Infrared Image');

end

end

function run_fusion(~, ~)

if isempty(I1) || isempty(I2)

errordlg('Please select both images first!', 'Input Error');

return;

end

% Preprocessing

I1 = imresize(I1, [256, 256]);

I2 = imresize(I2, [256, 256]);

I1 = histeq(I1);

I2 = histeq(I2);

% Anisotropic Diffusion

num_iter = 25; delta_t = 0.15; kappa = 30; option = 1;

A1 = anisodiff2D(double(I1), num_iter, delta_t, kappa, option);

A2 = anisodiff2D(double(I2), num_iter, delta_t, kappa, option);


% PCA Fusion

D1 = double(I1) - A1;

D2 = double(I2) - A2;

C1 = cov([D1(:), D2(:)]);

[V11, D11] = eig(C1);

if D11(1,1) >= D11(2,2)

pca1 = V11(:,1) / sum(V11(:,1));

else

pca1 = V11(:,2) / sum(V11(:,2));

end

imf1 = pca1(1) * D1 + pca1(2) * D2;

imf2 = (0.75 * A1 + 0.75 * A2);

Out = double(imf1) + double(imf2);

% Show Fused Image

figure;

imshow(uint8(Out), []);

title('Fused Image');

imwrite(uint8(Out), 'Fused_Output.png');

end

end

function pca_fusion_gui()

% Create GUI Figure

fig = figure('Name', 'PCA Image Fusion', 'NumberTitle', 'off', 'Position', [100, 100, 600, 450]);

% UI Components
uicontrol('Style', 'text', 'Position', [200, 400, 200, 25], 'String', 'Infrared & Visible Image Fusion',
'FontSize', 12, 'FontWeight', 'bold');

uicontrol('Style', 'pushbutton', 'Position', [50, 350, 200, 40], 'String', 'Select Visible Image',
'Callback', @select_vi, 'TooltipString', 'Choose a visible light image');

uicontrol('Style', 'pushbutton', 'Position', [350, 350, 200, 40], 'String', 'Select Infrared Image',
'Callback', @select_ir, 'TooltipString', 'Choose an infrared image');

uicontrol('Style', 'pushbutton', 'Position', [225, 250, 150, 40], 'String', 'Run Fusion', 'Callback',
@run_fusion, 'TooltipString', 'Fuse the images using PCA');

% Axes for Image Display

ax1 = axes('Position', [0.1, 0.1, 0.35, 0.35]);

ax2 = axes('Position', [0.55, 0.1, 0.35, 0.35]);

% Global Variables for Image Storage

global I1 I2;

% Callback Functions

function select_vi(~, ~)

[file, path] = uigetfile({'*.jpg;*.png;*.bmp'}, 'Select Visible Image');

if file

I1 = imread(fullfile(path, file));

if size(I1, 3) > 1

I1 = rgb2gray(I1);

end

imshow(I1, 'Parent', ax1);

title(ax1, 'Visible Image');

end

end

function select_ir(~, ~)

[file, path] = uigetfile({'*.jpg;*.png;*.bmp'}, 'Select Infrared Image');


if file

I2 = imread(fullfile(path, file));

if size(I2, 3) > 1

I2 = rgb2gray(I2);

end

imshow(I2, 'Parent', ax2);

title(ax2, 'Infrared Image');

end

end

function run_fusion(~, ~)

if isempty(I1) || isempty(I2)

errordlg('Please select both images first!', 'Input Error');

return;

end

% Preprocessing

I1 = imresize(I1, [256, 256]);

I2 = imresize(I2, [256, 256]);

I1 = histeq(I1);

I2 = histeq(I2);

% Anisotropic Diffusion

num_iter = 25; delta_t = 0.15; kappa = 30; option = 1;

A1 = anisodiff2D(double(I1), num_iter, delta_t, kappa, option);

A2 = anisodiff2D(double(I2), num_iter, delta_t, kappa, option);

% PCA Fusion

D1 = double(I1) - A1;

D2 = double(I2) - A2;

C1 = cov([D1(:), D2(:)]);
[V11, D11] = eig(C1);

if D11(1,1) >= D11(2,2)

pca1 = V11(:,1) / sum(V11(:,1));

else

pca1 = V11(:,2) / sum(V11(:,2));

end

imf1 = pca1(1) * D1 + pca1(2) * D2;

imf2 = (0.75 * A1 + 0.75 * A2);

Out = double(imf1) + double(imf2);

% Display Fused Image

figure;

imshow(uint8(Out), []);

title('Fused Image');

imwrite(uint8(Out), 'Fused_Output.png');

% Compute Performance Metrics

psnr_value = psnr(uint8(Out), uint8(I1));

ssim_value = ssim(uint8(Out), uint8(I1));

entropy_value = entropy(uint8(Out));

% Show Metrics in Command Window

fprintf('Performance Metrics:\n');

fprintf('PSNR: %.2f dB\n', psnr_value);

fprintf('SSIM: %.4f\n', ssim_value);

fprintf('Entropy: %.2f\n', entropy_value);

end

end
function pca_fusion_gui()

% Create GUI Figure

fig = figure('Name', 'PCA Image Fusion', 'NumberTitle', 'off', 'Position', [100, 100, 600, 450]);

% UI Components

uicontrol('Style', 'text', 'Position', [200, 400, 200, 25], 'String', 'Infrared & Visible Image Fusion',
'FontSize', 12, 'FontWeight', 'bold');

uicontrol('Style', 'pushbutton', 'Position', [50, 350, 200, 40], 'String', 'Select Visible Image',
'Callback', @select_vi, 'TooltipString', 'Choose a visible light image');

uicontrol('Style', 'pushbutton', 'Position', [350, 350, 200, 40], 'String', 'Select Infrared Image',
'Callback', @select_ir, 'TooltipString', 'Choose an infrared image');

uicontrol('Style', 'pushbutton', 'Position', [225, 250, 150, 40], 'String', 'Run Fusion', 'Callback',
@run_fusion, 'TooltipString', 'Fuse the images using PCA');

% Axes for Image Display

ax1 = axes('Position', [0.1, 0.1, 0.35, 0.35]);

ax2 = axes('Position', [0.55, 0.1, 0.35, 0.35]);

% Global Variables for Image Storage

global I1 I2;

% Callback Functions

function select_vi(~, ~)

[file, path] = uigetfile({'*.jpg;*.png;*.bmp'}, 'Select Visible Image');

if file

I1 = imread(fullfile(path, file));

if size(I1, 3) > 1

I1 = rgb2gray(I1);

end
imshow(I1, 'Parent', ax1);

title(ax1, 'Visible Image');

end

end

function select_ir(~, ~)

[file, path] = uigetfile({'*.jpg;*.png;*.bmp'}, 'Select Infrared Image');

if file

I2 = imread(fullfile(path, file));

if size(I2, 3) > 1

I2 = rgb2gray(I2);

end

imshow(I2, 'Parent', ax2);

title(ax2, 'Infrared Image');

end

end

function run_fusion(~, ~)

if isempty(I1) || isempty(I2)

errordlg('Please select both images first!', 'Input Error');

return;

end

% Preprocessing

I1 = imresize(I1, [256, 256]);

I2 = imresize(I2, [256, 256]);

I1 = histeq(I1);

I2 = histeq(I2);

% Anisotropic Diffusion

num_iter = 25; delta_t = 0.15; kappa = 30; option = 1;


A1 = anisodiff2D(double(I1), num_iter, delta_t, kappa, option);

A2 = anisodiff2D(double(I2), num_iter, delta_t, kappa, option);

% PCA Fusion

D1 = double(I1) - A1;

D2 = double(I2) - A2;

C1 = cov([D1(:), D2(:)]);

[V11, D11] = eig(C1);

if D11(1,1) >= D11(2,2)

pca1 = V11(:,1) / sum(V11(:,1));

else

pca1 = V11(:,2) / sum(V11(:,2));

end

imf1 = pca1(1) * D1 + pca1(2) * D2;

imf2 = (0.75 * A1 + 0.75 * A2);

Out = double(imf1) + double(imf2);

% Display Fused Image

figure;

imshow(uint8(Out), []);

title('Fused Image');

imwrite(uint8(Out), 'Fused_Output.png');

% Compute Performance Metrics

psnr_value = psnr(uint8(Out), uint8(I1));

ssim_value = ssim(uint8(Out), uint8(I1));

entropy_value = entropy(uint8(Out));

% Show Metrics in Command Window

fprintf('Performance Metrics:\n');
fprintf('PSNR: %.2f dB\n', psnr_value);

fprintf('SSIM: %.4f\n', ssim_value);

fprintf('Entropy: %.2f\n', entropy_value);

end

end

You might also like