Binary image and edge images are not displayed
4 views (last 30 days)
Show older comments
Surabhi A S
on 18 Jun 2023
Commented: Surabhi A S
on 20 Jun 2023
I have a code for displaying images but the output is not as expected. From the binary image I want to extract the edged image. The code for binary image is as follows:
clc;
clear all;
close all;
img = imread('MoS20027imageenhanced.jpg');
% Convert the image to grayscale
gray_img = rgb2gray(img);
% Convert the grayscale image to a binary image
binary_img = imbinarize(gray_img);
% Clean it up.
% Fill holes.
binary_img = imfill(binary_img, 'holes');
% Get rid of small blobs.
binary_img = bwareaopen(binary_img, 1000);
% Smooth border
binary_img = imclose(binary_img, true(5));
% Display the original image and the binary image side-by-side
figure;
subplot(1,2,1);
imshow(img);
title('Original Image');
figure;
imshow(binary_img);
title('Binary Image');
0 Comments
Accepted Answer
Image Analyst
on 18 Jun 2023
To get the edges from your segmented image, you can use bwperim (to get an image) or bwboundaries (to get a list of (x,y) coordinates.
% Plot the borders of all the blobs in the overlay above the original grayscale image
% using the coordinates returned by bwboundaries().
% bwboundaries() returns a cell array, where each cell contains the row/column coordinates for an object in the image.
imshow(gray_img); % Optional : show the original image again. Or you can leave the binary image showing if you want.
% Here is where we actually get the boundaries for each blob.
boundaries = bwboundaries(binary_img);
% boundaries is a cell array - one cell for each blob.
% In each cell is an N-by-2 list of coordinates in a (row, column) format. Note: NOT (x,y).
% Column 1 is rows, or y. Column 2 is columns, or x.
numberOfBoundaries = size(boundaries, 1); % Count the boundaries so we can use it in our for loop
% Here is where we actually plot the boundaries of each blob in the overlay.
hold on; % Don't let boundaries blow away the displayed image.
for k = 1 : numberOfBoundaries
thisBoundary = boundaries{k}; % Get boundary for this specific blob.
x = thisBoundary(:,2); % Column 2 is the columns, which is x.
y = thisBoundary(:,1); % Column 1 is the rows, which is y.
plot(x, y, 'r-', 'LineWidth', 2); % Plot boundary in red.
end
hold off;
caption = sprintf('%d Outlines, from bwboundaries()', numberOfBoundaries);
fontSize = 15;
title(caption, 'FontSize', fontSize);
axis('on', 'image'); % Make sure image is not artificially stretched because of screen's aspect ratio.
9 Comments
Image Analyst
on 20 Jun 2023
No. It's already been many days. If you continue to wait until someone offers you a turnkey robust system, you'll have waited longer than if you just processed all your images manually, even though it's tedious. With the range of images I've seen from you, and hearing of the measurements you insist on, I feel I cannot donate enough time to you to do the job. In other words, it's going to take longer than 5 or 10 minutes. I hope you understand. I actually have a full time day job that I do in addition to making short contributions to this forum. If you really want it robust and automated and are willing to wait a few months, then I'm sure a university or image processing consultant or even the Mathworks could be hired to do the job. It won't be cheap/free or quick though. Good luck.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!