Untitled

mail@pastecode.io avatar
unknown
plain_text
6 days ago
4.2 kB
2
Indexable
Never
Question: Load an image from a file and display it using MATLAB.
Solution:
% Load and display an image
image = imread('example.jpg'); % Replace 'example.jpg' with your image file
imshow(image);
title('Loaded Image');
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');
Question: Image Rotation
Task: Rotate an image by 45 degrees.
Solution:
% Load the image
img = imread('peppers.png');
% 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:
% Load the image
img = imread('peppers.png');
% 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
% Load the image
img = imread('peppers.png');
% 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');
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');


Img= imread("cameraman.tif");
[m,n]= size(img);
img_128= zeros(m/2, n/2);
For i=1:m/2
      For j=1:n/2
            img_128(I,j) = img(2*i,2*j);
      end
end
Figure, imshow(img);
Figure, imshow(img_128,[]);



Filename =  'mandrill.jpg';
image1 = imread(filename);
gray = im2gray(image1);

subplot(1,2,1), imshow(image1), title('Original Image');
subplot(1,2,2), imshow(gray), title('Grayscale image');
Leave a Comment