DIP LAB 3
Task 1: Apply RGB image directly for addition and subtraction.
clc;clear all; close all;
%img1=rgb2gray(imread('MATLAB Drive/lena.png'));
%img2=rgb2gray(imread('MATLAB Drive/peppers.png'));
img1=imread('C:\Users\dsplab\Downloads\images 2.jpeg');
img2=imread('C:\Users\dsplab\Downloads\ipad-2048x2048-thumbnail_00396-
256x256.jpg');
[m,n]=size(img1);
%out1=zeros(m,n);
for i=1:m
for j=1:n
out1(i,j)=img1(i,j)+img2(i,j);
out2(i,j)=img1(i,j)-img2(i,j);
out3(i,j)=img1(i,j).*img2(i,j);
end
end
imshow(img1);
figure,imshow(img2);
figure,imshow(out1);
figure,imshow(out2);
figure,imshow(out3);
Task 2
clc;clear all; close all;
%img1=rgb2gray(imread('MATLAB Drive/lena.png'));
%img2=rgb2gray(imread('MATLAB Drive/peppers.png'));
img1=imread('C:\Users\dsplab\Downloads\mri1.jpg');
img2=imread('C:\Users\dsplab\Downloads\mri2.jpg');
[m,n]=size(img1);
%out1=zeros(m,n);
for i=1:m
for j=1:n
out1(i,j)=img1(i,j)+img2(i,j);
out2(i,j)=img1(i,j)-img2(i,j);
out3(i,j)=img1(i,j).*img2(i,j);
end
end
imshow(img1);
figure,imshow(img2);
figure,imshow(out1);
figure,imshow(out2);
figure,imshow(out3);
TASK 3
positiveImage = imread('C:\Users\dsplab\Downloads\Screenshot_20240722-
122616.png');
negativeImage = 255 - positiveImage;
imshow(negativeImage)
Task 4
a1 = imread('C:\Users\dsplab\Downloads\images 2.jpeg'); % Read the
image
a = double(a1)/255; % Normalized Image
%c = 2; % Constant
imshow(a1),title("Original image");
c = [0.25 0.5 0.75 1 1.25 1.5 1.75 2];
for r=1:length(c)
f = c(r).*log(1 + (a)); % Log Transform
figure;
imshow(f)
title("log transformation image",i)
end
Task 5
C = [0.2, 1.2, 3.1, 4.4];
gamma = 0.2:0.4:4;
gray_img=imread('C:\Users\dsplab\Downloads\images 2.jpeg')
figure('Name', 'Task 5: Power-law Transformation');
count = 1;
for i = 1:length(C)
for j = 1:length(gamma)
power_img = C(i) * (double(gray_img) / 255).^gamma(j);
imshow(power_img)
end
end
Task 6
% Task 6: Contrast stretching with different thresholds on RGB image
rgb_img = imread('peppers.png'); % Use an RGB image
thresholds = [50, 100, 150, 200];
figure('Name', 'Task 6: Contrast Stretching on RGB Image');
for i = 1:length(thresholds)
stretched_img = double(rgb_img); % Convert to double for
calculations
for channel = 1:3
channel_img = stretched_img(:,:,channel);
low_values = channel_img < thresholds(i);
high_values = channel_img >= thresholds(i);
stretched_img(:,:,channel) = low_values .* channel_img + ...
high_values .* (255 * (channel_img - thresholds(i)) / (255
- thresholds(i)));
end
subplot(2,2,i), imshow(uint8(stretched_img)), title(['Threshold =
', num2str(thresholds(i))]);
end