#matlab #imageprocessing #programming #question #intermediate
Write a MATLAB program to perform the following image processing tasks on a grayscale image:
1. Load an image named 'cameraman.tif' from the built-in MATLAB dataset.
2. Convert the image to grayscale if it's not already in that format.
3. Apply Gaussian blur with a 5x5 kernel.
4. Perform edge detection using the Sobel operator.
5. Display the original, blurred, and edge-detected images in a single figure with three subplots.
6. Save the edge-detected image as 'edge_result.png'.
Note: This code assumes that the 'cameraman.tif' image is available in the MATLAB path (it's a built-in dataset). The program processes the image through blurring and edge detection, displays the results in a single figure, and saves the final edge-detected image.
By: @DataScienceQ 🚀
Write a MATLAB program to perform the following image processing tasks on a grayscale image:
1. Load an image named 'cameraman.tif' from the built-in MATLAB dataset.
2. Convert the image to grayscale if it's not already in that format.
3. Apply Gaussian blur with a 5x5 kernel.
4. Perform edge detection using the Sobel operator.
5. Display the original, blurred, and edge-detected images in a single figure with three subplots.
6. Save the edge-detected image as 'edge_result.png'.
% 1. Load the cameraman image
img = imread('cameraman.tif');
% 2. Convert to grayscale if necessary
if size(img, 3) == 3
img = rgb2gray(img);
end
% 3. Apply Gaussian blur with 5x5 kernel
blurred = imgaussfilt(img, 2); % sigma=2 for 5x5 kernel
% 4. Perform edge detection using Sobel operator
edges = edge(blurred, 'sobel');
% 5. Display all images in subplots
figure;
subplot(1,3,1);
imshow(img);
title('Original Image');
axis off;
subplot(1,3,2);
imshow(blurred);
title('Gaussian Blurred');
axis off;
subplot(1,3,3);
imshow(edges);
title('Edge Detected (Sobel)');
axis off;
% 6. Save the edge-detected image
imwrite(edges, 'edge_result.png');
disp('Image processing completed. Results saved.')
Note: This code assumes that the 'cameraman.tif' image is available in the MATLAB path (it's a built-in dataset). The program processes the image through blurring and edge detection, displays the results in a single figure, and saves the final edge-detected image.
By: @DataScienceQ 🚀