how to smooth the edge of a binary image
23 views (last 30 days)
Show older comments
Hi,
I've got the bellow zoomed image which does have a not very good resolution. The image itslef is attached.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1628948/image.png)
As you can see the edges between the 0 and 1 areas does not have smooth transition. I am wondering if you could advise how to increase the resolution (smooth the intersection), maybe a linear interpolation could help. Somehting like the red line in this photo:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1628953/image.png)
Thanks
Navid
3 Comments
DGM
on 27 Feb 2024
I went ahead and posted a comment on the other thread which might help solve a big chunk of your problem.
Answers (1)
Vidhi Agarwal
on 19 Mar 2024
Hi NaviD,
I understand your query is regarding a method to smooth out the edges of image. Below is an approach using MATLAB that demonstrates how to:
- Extract the boundary of an object in a binary image.
- Identify key points along this boundary. (For example, uses points at regular intervals, but you could implement more sophisticated methods to choose points based on curvature or other criteria.)
- To approximate the boundary with straight lines, we'll use a simple method by selecting key points, we'll either manually choose significant points or select every nth point along the boundary for a straightforward simplification.
- Display the original boundary and the approximated straight-line boundary for comparison.
Below is the code you can try:
% Step 1: Read and process the binary image
binaryImage = imread('text.png');
if size(binaryImage, 3) == 3
binaryImage = rgb2gray(binaryImage);
end
binaryImage = logical(imbinarize(binaryImage));
% Step 2: Find the boundary of the binary object
boundaries = bwboundaries(binaryImage);
largestBoundary = boundaries{1};
% Step 3: Approximate the boundary using straight lines
% For demonstration, let's select every 10th point as a key point
keyPointsIndex = 1:10:length(largestBoundary); % Adjust the interval as needed
keyPoints = largestBoundary(keyPointsIndex, :);
% Step 4: Draw the approximated boundary
approxImage = uint8(zeros(size(binaryImage, 1), size(binaryImage, 2), 3)); % Now it's a 3-channel image
for i = 1:length(keyPoints)-1
approxImage = insertShape(approxImage, 'Line', [keyPoints(i,2), keyPoints(i,1), keyPoints(i+1,2), keyPoints(i+1,1)], 'Color', 'white', 'LineWidth', 1);
end
% Display the original and approximated images
figure;
subplot(1,2,1);
imshow(binaryImage);
title('Original Binary Image');
subplot(1,2,2);
imshow(approxImage); % Directly display approxImage without converting it back
title('Approximated Boundary Image');
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!