Atharva Kulkarni Lab 1 3
Atharva Kulkarni Lab 1 3
Q.1 Write a program to perform basic arithme c and logical opera ons on images
x = imread("/MATLAB Drive/naruto.jpeg");
y = imread("/MATLAB Drive/naruto2.jpeg");
image1 = imresize(x, [size(y,1) size(y,2)]);
result_add = image1 + y;
result_subtract = image1 - y;
result_multiply = image1 .* y;
result_divide = image1 ./ y;
result_and = bitand(image1, y);
result_or = bitor(image1, y);
result_not = imcomplement(image1);
figure; subplot(3, 3, 1); imshow(image1);
title('Input Image 1');
subplot(3, 3, 2); imshow(y);
title('Input Image 2');
subplot(3, 3, 3); imshow(result_add);
title('Addition');
subplot(3, 3, 4); imshow(result_subtract);
title('Subtraction');
subplot(3, 3, 5); imshow(result_multiply);
title('Multiplication');
subplot(3, 3, 6); imshow(result_divide);
title('Division');
subplot(3, 3, 7); imshow(result_and);
title('AND');
subplot(3, 3, 8);
imshow(result_or);
title('OR');
subplot(3, 3, 9);
imshow(result_not);
title('NOT on Image 1');
OUTPUT:
Q.2 Divide the image into N equal parts and perform the Above opera on randomly on
each part.
input_image = imread("/MATLAB Drive/minato.jpeg");
[rows, cols, ~] = size(input_image);
N=5
part_rows= floor(rows / N);
part_cols = floor(cols / N);
starting_points = randperm(N^2, N);
figure;
for i = 1:N
row_start = 1 + part_rows * floor((starting_points(i) - 1) / N);
row_end = row_start + part_rows - 1;
OUTPUT:
ASSIGNMENT 3
Q.1 Select P & Q points randomly in an image and calculate the distance
imagePath = "/MATLAB Drive/naruto.jpeg";
image = imread(imagePath);
imshow(image);
title('Image with Two Random Points');
[height, width, ~] = size(image);
randomPoint1 = randi([1, width], 1, 2);
randomPoint2 = randi([1, width], 1, 2);
hold on;
scatter(randomPoint1(1), randomPoint1(2), 50, 'r', 'filled');
scatter(randomPoint2(1), randomPoint2(2), 50, 'g', 'filled');
hold off;
euclideanDist = norm(randomPoint2 - randomPoint1);
manhattanDist = sum(abs(randomPoint2 - randomPoint1));
chebyshevDist = max(abs(randomPoint2 - randomPoint1));
minkowskiDist = norm(randomPoint2 - randomPoint1, 3);
cosineDist = 1 - dot(randomPoint1, randomPoint2) / (norm(randomPoint1) *
norm(randomPoint2));
correlationDist = 1 - corr2(randomPoint1, randomPoint2);
hammingDist = sum(randomPoint1 ~= randomPoint2) / numel(randomPoint1);
fprintf('Random Point 1: (%d, %d)\n', randomPoint1(1), randomPoint1(2));
fprintf('Random Point 2: (%d, %d)\n', randomPoint2(1), randomPoint2(2));
fprintf('Euclidean Distance: %.2f\n', euclideanDist);
fprintf('Manhattan Distance: %.2f\n', manhattanDist);
fprintf('Chebyshev Distance: %.2f\n', chebyshevDist);
fprintf('Minkowski Distance: %.2f\n', minkowskiDist);
fprintf('Cosine Distance: %.2f\n', cosineDist);
fprintf('Correlation Distance: %.2f\n', correlationDist);
fprintf('Hamming Distance: %.2f\n', hammingDist);
OUTPUT:
Q.2 Select P & Q in an Image where pixel intensities are same and calculate the distance
imagePath = '/MATLAB Drive/gojo.jpeg';
image = imread(imagePath);
imshow(image);
title('Image with two random points of same intensity');
% Get image dimensions
[height, width, ~] = size(image);
% Step 2: Find two random points with the same intensity
intensity = randi([0, 255]); % Random intensity value between 0 and 255
matchingPoints = find(image == intensity); % Find all points with the same intensity
numMatchingPoints = numel(matchingPoints);
% Check if there are enough matching points
if numMatchingPoints < 2
error('Not enough points with the same intensity.');
end
% Randomly select two points from the matching points
randIndices = randperm(numMatchingPoints, 2);
point1Index = matchingPoints(randIndices(1));
point2Index = matchingPoints(randIndices(2));
% Convert linear index to (y, x) coordinates
[point1Y, point1X] = ind2sub([height, width], point1Index);
[point2Y, point2X] = ind2sub([height, width], point2Index);
% Display the random points with the same intensity on the image
hold on;
scatter(point1X, point1Y, 50, 'r', 'filled');
scatter(point2X, point2Y, 50, 'g', 'filled');
hold off;
% Step 3: Calculate distances
euclideanDistance = norm([point2X, point2Y] - [point1X, point1Y]);
manhattanDistance = abs(point2X - point1X) + abs(point2Y - point1Y);
chebyshevDistance = max(abs(point2X - point1X), abs(point2Y - point1Y));
minkowskiDistance = norm([point2X, point2Y] - [point1X, point1Y], 3);
cosineDistance = 1 - dot([point1X, point1Y], [point2X, point2Y]) / ...
(norm([point1X, point1Y]) * norm([point2X, point2Y]));
correlationDistance = 1 - corr2([point1X, point1Y], [point2X, point2Y]);
hammingDistance = sum([point1X, point1Y] ~= [point2X, point2Y]) / 2;
fprintf('Point 1: (%d, %d), Intensity: %d\n', point1X, point1Y, intensity);
fprintf('Point 2: (%d, %d), Intensity: %d\n', point2X, point2Y, intensity);
fprintf('Euclidean Distance: %.2f\n', euclideanDistance);
fprintf('Manhattan Distance: %.2f\n', manhattanDistance);
fprintf('Chebyshev Distance: %.2f\n', chebyshevDistance);
fprintf('Minkowski Distance: %.2f\n', minkowskiDistance);
fprintf('Cosine Distance: %.2f\n', cosineDistance);
fprintf('Correlation Distance: %.2f\n', correlationDistance);
fprintf('Hamming Distance: %.2f\n', hammingDistance);
OUTPUT: