How to achieve better edge enhancement?
Show older comments
Any suggestion how to enhance the image edges clearly?
I = imread('317.jpg');
% A = rgb2gray(I2);
figure;
imshow(I)
title('Original Image')
%%
% Gaussian LPF
F = fspecial('gaussian');
G = imfilter(I,F);
figure('Name', 'Gaussian Blur'), imshow(G); title('G Image');
%% Threshold
bw = im2bw(G,0.35);
figure('Name', 'Binary Image')
imshow(bw)
% Enhance binary image
kernel = -1*ones(1);
kernel(2,2) = 4;
enhancedImage = imfilter(bw, kernel);
figure('Name', 'Enhanced'), imshow(enhancedImage); title('enhancedImage Image');
% Crop
D = im2uint8(enhancedImage);
I2 = imcrop(D,[50 68 130 112]);
figure('Name', 'Cropped');
imshow(I2)
% User define ROI
r = drawrectangle;
mask = createMask(r);
bw2 = activecontour(I2,mask,30,'Chan-Vese');
figure('Name', 'Active Contour')
imshow(bw2);
hold on;
visboundaries(bw2,'Color','r');
figure('Name', 'labelOverlay')
imshow(labeloverlay(I2,bw2));

Answers (1)
Vidhi Agarwal
on 26 Aug 2024
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!.
Categories
Find more on Image Filtering and Enhancement in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!