0% found this document useful (0 votes)
11 views4 pages

Image Reistration

Uploaded by

shilpa
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)
11 views4 pages

Image Reistration

Uploaded by

shilpa
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/ 4

% Load built-in MATLAB images

fixedImage = imread('cameraman.tif');
movingImage = imread('peppers.png');

% Convert images to grayscale if they are RGB


if size(fixedImage, 3) == 3
fixedImage = rgb2gray(fixedImage);
end
if size(movingImage, 3) == 3
movingImage = rgb2gray(movingImage);
end

% Normalize images
fixedImage = im2double(fixedImage);
movingImage = im2double(movingImage);

% Define the fitness function for GA and ACO


fitnessFunction = @(params) immse(fixedImage, imwarp(movingImage, ...
affine2d([cosd(params(3)) -sind(params(3)) 0; sind(params(3)) cosd(params(3)) 0;
params(1) params(2) 1]), ...
'OutputView', imref2d(size(fixedImage))));

% Set bounds for the parameters [tx, ty, theta]


lb = [-size(fixedImage, 2) -size(fixedImage, 1) -180];
ub = [size(fixedImage, 2) size(fixedImage, 1) 180];

% Custom Genetic Algorithm implementation


function optimalParamsGA = customGA(fitnessFunction, lb, ub, numGenerations, popSize)
% Initialize population
numParams = length(lb);
population = repmat(lb, popSize, 1) + rand(popSize, numParams) .* repmat((ub -
lb), popSize, 1);
fitness = zeros(popSize, 1);

for gen = 1:numGenerations


% Evaluate fitness
for i = 1:popSize
fitness(i) = fitnessFunction(population(i, :));
end

% Selection
[~, sortedIdx] = sort(fitness);
population = population(sortedIdx, :);

% Crossover
for i = 1:2:popSize
if i + 1 <= popSize
alpha = rand;
population(i, :) = alpha * population(i, :) + (1 - alpha) *
population(i + 1, :);
population(i + 1, :) = (1 - alpha) * population(i, :) + alpha *
population(i + 1, :);
end
end
% Mutation
mutationRate = 0.1;
mutationMask = rand(popSize, numParams) < mutationRate;
population = population + mutationMask .* (rand(popSize, numParams) .*
repmat((ub - lb), popSize, 1) - repmat((ub - lb)/2, popSize, 1));

% Enforce bounds
population = max(min(population, ub), lb);
end

% Get the best solution


optimalParamsGA = population(1, :);
end

% Parameters for GA
numGenerations = 50;
popSize = 20;

% Run the optimization using custom GA and measure time


tic;
optimalParamsGA = customGA(fitnessFunction, lb, ub, numGenerations, popSize);
gaTime = toc;

% Apply the optimal transformation to the moving image


tformOptimalGA = affine2d([cosd(optimalParamsGA(3)) -sind(optimalParamsGA(3)) 0;
sind(optimalParamsGA(3)) cosd(optimalParamsGA(3)) 0; optimalParamsGA(1)
optimalParamsGA(2) 1]);
movingRegisteredOptimalGA = imwarp(movingImage, tformOptimalGA, 'OutputView',
imref2d(size(fixedImage)));

% Calculate performance parameters for GA


mseErrorGA = immse(fixedImage, movingRegisteredOptimalGA);
psnrValueGA = psnr(fixedImage, movingRegisteredOptimalGA);
ssimValueGA = ssim(fixedImage, movingRegisteredOptimalGA);

% Display results for GA


disp('GA Optimal Parameters:');
disp(['Translation in x: ', num2str(optimalParamsGA(1))]);
disp(['Translation in y: ', num2str(optimalParamsGA(2))]);
disp(['Rotation angle: ', num2str(optimalParamsGA(3))]);
disp('GA Performance Parameters:');
disp(['Mean Squared Error (MSE): ', num2str(mseErrorGA)]);
disp(['Peak Signal-to-Noise Ratio (PSNR): ', num2str(psnrValueGA)]);
disp(['Structural Similarity Index (SSIM): ', num2str(ssimValueGA)]);
disp(['Elapsed Time (seconds): ', num2str(gaTime)]);

% Custom Ant Colony Optimization (ACO) implementation


function optimalParamsACO = customACO(fitnessFunction, lb, ub, numAnts,
numIterations)
% Initialize parameters
numParams = length(lb);
pheromone = ones(numAnts, numParams);
bestParams = [];
bestFitness = inf;
% ACO main loop
for iter = 1:numIterations
antParams = zeros(numAnts, numParams);
for i = 1:numAnts
for j = 1:numParams
% Generate solutions based on pheromone
antParams(i, j) = lb(j) + (ub(j) - lb(j)) * rand * pheromone(i, j);
end
% Evaluate fitness
fitness = fitnessFunction(antParams(i, :));
if fitness < bestFitness
bestFitness = fitness;
bestParams = antParams(i, :);
end
% Update pheromone
pheromone(i, :) = pheromone(i, :) * (1 - 0.1) + 0.1 * (1 / (1 +
fitness));
end
end
optimalParamsACO = bestParams;
end

% Parameters for ACO


numAnts = 20;
numIterations = 50;

% Run the optimization using custom ACO and measure time


tic;
optimalParamsACO = customACO(fitnessFunction, lb, ub, numAnts, numIterations);
acoTime = toc;

% Apply the optimal transformation to the moving image


tformOptimalACO = affine2d([cosd(optimalParamsACO(3)) -sind(optimalParamsACO(3)) 0;
sind(optimalParamsACO(3)) cosd(optimalParamsACO(3)) 0; optimalParamsACO(1)
optimalParamsACO(2) 1]);
movingRegisteredOptimalACO = imwarp(movingImage, tformOptimalACO, 'OutputView',
imref2d(size(fixedImage)));

% Calculate performance parameters for ACO


mseErrorACO = immse(fixedImage, movingRegisteredOptimalACO);
psnrValueACO = psnr(fixedImage, movingRegisteredOptimalACO);
ssimValueACO = ssim(fixedImage, movingRegisteredOptimalACO);

% Display results for ACO


disp('ACO Optimal Parameters:');
disp(['Translation in x: ', num2str(optimalParamsACO(1))]);
disp(['Translation in y: ', num2str(optimalParamsACO(2))]);
disp(['Rotation angle: ', num2str(optimalParamsACO(3))]);
disp('ACO Performance Parameters:');
disp(['Mean Squared Error (MSE): ', num2str(mseErrorACO)]);
disp(['Peak Signal-to-Noise Ratio (PSNR): ', num2str(psnrValueACO)]);
disp(['Structural Similarity Index (SSIM): ', num2str(ssimValueACO)]);
disp(['Elapsed Time (seconds): ', num2str(acoTime)]);

% Display the registered images


figure;
subplot(2,2,1);
imshow(fixedImage);
title('Fixed Image');

subplot(2,2,2);
imshow(movingImage);
title('Moving Image');

subplot(2,2,3);
imshow(movingRegisteredOptimalGA);
title('Registered Image using GA');

subplot(2,2,4);
imshow(movingRegisteredOptimalACO);
title('Registered Image using ACO');

% Display the error images (absolute difference)


figure;
subplot(1,2,1);
errorImageGA = abs(fixedImage - movingRegisteredOptimalGA);
imshow(errorImageGA, []);
title('Error Image using GA');
colorbar;

subplot(1,2,2);
errorImageACO = abs(fixedImage - movingRegisteredOptimalACO);
imshow(errorImageACO, []);
title('Error Image using ACO');
colorbar;

% Comparison of performance parameters


figure;
c = categorical({'MSE', 'PSNR', 'SSIM'});
gaValues = [mseErrorGA, psnrValueGA, ssimValueGA];
acoValues = [mseErrorACO, psnrValueACO, ssimValueACO];
bar(c, [gaValues; acoValues]);
legend('GA', 'ACO');
title('Performance Comparison');

You might also like