I understand that you have a query about enhancing the edges of an image. Here are a few suggestions for the same:
- Ensure that the image is converted to grayscale before applying any edge detection techniques. This simplifies the processing, and it is a standard practice for edge detection.
- Instead of using a simple Laplacian kernel, you can use methods like the Canny edge detector for better results.
- The kernel you defined seems incorrect. If you want to use a Laplacian kernel, it should be a 3x3 matrix. Consider using “fspecial('laplacian')” for a standard Laplacian filter.
- Consider using unsharp masking for edge enhancement, which combines the original image with a high-pass filtered version.
Here’s is the revised code for the same:
I = imread('317.jpg');
% Convert to grayscale
I_gray = rgb2gray(I);
% Display original grayscale image
figure;
imshow(I_gray);
title('Original Grayscale Image');
% Apply Gaussian Low Pass Filter
F = fspecial('gaussian', [5 5], 1); % Adjust size and sigma as needed
G = imfilter(I_gray, F, 'same');
figure;
imshow(G);
title('Gaussian Blurred Image');
% Edge Detection using Canny
edges = edge(G, 'canny');
figure;
imshow(edges);
title('Canny Edge Detection');
% Optional: Enhance edges using Unsharp Masking
H = fspecial('unsharp');
enhancedImage = imfilter(I_gray, H);
figure;
imshow(enhancedImage);
title('Enhanced Image with Unsharp Masking');
% Convert to binary image for segmentation
bw = imbinarize(enhancedImage, 'adaptive', 'Sensitivity', 0.35);
figure;
imshow(bw);
title('Binary Image');
% Crop and apply active contour model
D = im2uint8(bw);
I2 = imcrop(D, [50 68 130 112]);
figure;
imshow(I2);
title('Cropped Image');
% Draw rectangle for initial mask
r = drawrectangle;
mask = createMask(r);
bw2 = activecontour(I2, mask, 30, 'Chan-Vese');
hold on;
visboundaries(bw2, 'Color', 'r');
% Display the final result
figure;
imshow(labeloverlay(I2, bw2));
title('Segmented Image with Boundaries');
The output of the following code will look like:
For better understanding of grayscale images and conversion to it, Canny edge detection and “fspecial” function, refer to the following documentation:
- https://www.mathworks.com/help/matlab/ref/rgb2gray.html
- https://www.mathworks.com/help/images/edge-detection.html
- https://www.mathworks.com/help/images/ref/fspecial.html
Hope that Helps!.