필터 지우기
필터 지우기

how to smooth the edge of a binary image

조회 수: 26 (최근 30일)
NaviD
NaviD 2024년 2월 27일
답변: Vidhi Agarwal 2024년 3월 19일
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
NaviD
NaviD 2024년 2월 27일
Thanks. Attached is the updated image.
I don't want to change the image size as I need to use this image as a mask to find the width of the artery model. I was hoping to be able to smooth the transition between black and white zones to make the calcualtion of width with less sudden variations.
Somehow this question is linked to my previous question in the forum: https://au.mathworks.com/matlabcentral/answers/2037096-find-diameter-based-on-the-centreline-and-edge#comment_3074888
Thanks
Navid
DGM
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
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:
  1. Extract the boundary of an object in a binary image.
  2. 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.)
  3. 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.
  4. 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!

Community Treasure Hunt

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

Start Hunting!

Translated by