how to smooth the edge of a binary image
18 次查看(过去 30 天)
显示 更早的评论
Hi,
I've got the bellow zoomed image which does have a not very good resolution. The image itslef is attached.
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:
Thanks
Navid
3 个评论
DGM
2024-2-27
I went ahead and posted a comment on the other thread which might help solve a big chunk of your problem.
回答(1 个)
Vidhi Agarwal
2024-3-19
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Segmentation and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!