# Untitled

unknown
plain_text
8 days ago
4.2 kB
2
Indexable
Never
```WEEK 1

1. Question: Load an image from a file and display it using MATLAB.
Solution:
% Load and display an image
imshow(image);

2. Question: Convert a colour image to a grayscale image.
Solution:
% Convert image to grayscale
file
grayImage = rgb2gray(colorImage);
imshow(grayImage);
title('Grayscale Image');

3. Question: Convert a colour image to a grayscale image without using built-in function.
Solution: Gray=0.2989×R+0.5870×G+0.1140×B
file

% Extract the RGB components
R = colorImage(:,:,1); % Red channel
G = colorImage(:,:,2); % Green channel
B = colorImage(:,:,3); % Blue channel

% Compute the grayscale image using the formula
grayImage = 0.2989 * double(R) + 0.5870 * double(G) + 0.1140 * double(B);

% Convert the result to uint8
grayImage = uint8(grayImage);

% Display the grayscale image
imshow(grayImage);
title('Grayscale Image (Manual Conversion)');

4. Question: Resize an Image to half its original dimensions.
Solution:

% Resize the image to half the original dimensions
smallImg = imresize(img, 0.5);

% Display the resized image
imshow(smallImg);
title('Resized Image');

WEEK 2
1. Question: Image Rotation
Task: Rotate an image by 45 degrees.
Solution:

% Rotate the image by 45 degrees
rotatedImg = imrotate(img, 45);

% Display the rotated image
imshow(rotatedImg);
title('Rotated Image by 45 Degrees');
2. Question: Image Cropping
Task: Crop a region of interest from an image.
Solution:

% Define the cropping rectangle [x, y, width, height]
rect = [100, 100, 200, 200];

% Crop the image
croppedImg = imcrop(img, rect);

% Display the cropped image
imshow(croppedImg);
title('Cropped Image');
3. Question 5: Perform Edge Detection
Task: Detect edges in a grayscale image using the Sobel operator.
Solution:
matlab
Copy code

% Convert to grayscale
grayImg = rgb2gray(img);

% Detect edges using the Sobel operator
edges = edge(grayImg, 'Sobel');

% Display the edges
imshow(edges);
title('Edge Detection with Sobel Operator');

4. Write a script to generate negative of an image.
file

% Convert to grayscale if the image is in color
if size(img, 3) == 3
img = rgb2gray(img);
end

% Calculate the negative of the image
negativeImg = 255 - img;

% Display the original image
subplot(1, 2, 1);
imshow(img);
title('Original Image');

% Display the negative image
subplot(1, 2, 2);
imshow(negativeImg);
title('Negative Image');

5. write a script to generate negative of an image without using in-built function.

% Convert to grayscale if the image is in color
if size(img, 3) == 3
% Extract the R, G, and B channels
R = img(:, :, 1);
G = img(:, :, 2);
B = img(:, :, 3);

% Convert to grayscale using the luminance method
grayImg = 0.2989 * R + 0.5870 * G + 0.1140 * B;
else
grayImg = img;
end

% Initialize the negative image
[m, n] = size(grayImg);
negativeImg = zeros(m, n, 'uint8'); % Create an empty matrix of the same size

% Calculate the negative of the grayscale image
for i = 1:m
for j = 1:n
negativeImg(i, j) = 255 - grayImg(i, j);
end
end

% Display the original grayscale image

subplot(1, 2, 1);
imshow(grayImg);
title('Original Grayscale Image');

% Display the negative image
subplot(1, 2, 2);
imshow(negativeImg);
title('Negative Image');```