How to achieve better edge enhancement?

11 ビュー (過去 30 日間)
MOHAMED GILANI
MOHAMED GILANI 2021 年 12 月 10 日
編集済み: Image Analyst 2024 年 8 月 26 日
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));

回答 (1 件)

Vidhi Agarwal
Vidhi Agarwal 2024 年 8 月 26 日
I understand that you have a query about enhancing the edges of an image. Here are a few suggestions for the same:
  1. 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.
  2. Instead of using a simple Laplacian kernel, you can use methods like the Canny edge detector for better results.
  3. 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.
  4. 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:
Hope that Helps!.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by