Untitled

mail@pastecode.io avatar
unknown
plain_text
13 days ago
2.6 kB
1
Indexable
Never
2. Question: Convert a colour image to a grayscale image.
Solution:
% Convert image to grayscale
colorImage = imread('example.jpg'); % Replace 'example.jpg' with your image 
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
% Read the color image
colorImage = imread('example.jpg'); % Replace 'example.jpg' with your image 
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:
% Load the image
img = imread('peppers.png');
% Resize the image to half the original dimensions
smallImg = imresize(img, 0.5);
% Display the resized image
imshow(smallImg);
title('Resized Image');

4. Write a script to generate negative of an image.
% Load the image
img = imread('peppers.png'); % Replace 'peppers.png' with your 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.
% Load the image
img = imread('peppers.png'); % Replace 'peppers.png' with your image file
% 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
 % Image is already grayscale
 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');
Leave a Comment