Binary image and edge images are not displayed

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');

 Risposta accettata

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 Commenti

I am not getting the binary image of the attached image itself
I ran your code and you do a get a binary image with a blob in it.
Surabhi A S
Surabhi A S il 18 Giu 2023
Modificato: Surabhi A S il 18 Giu 2023
Can you pls attach an image of the output? And also if i don't want the boundary but I just need the edges of triangles marked using any edge detection algorithm.
You can see the one blob you segmented out
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
rgbImage = imread('MoS20027imageenhanced.jpg');
figure;
subplot(2,2,1);
imshow(rgbImage);
title('Original RGB Image');
% Convert the image to grayscale
grayImage = rgb2gray(rgbImage);
subplot(2,2,2);
imshow(imadjust(grayImage), []);
title('Gray Scale Image');
subplot(2, 2, 3);
imhist(grayImage);
grid on;
title('Histogram of Gray Scale Image');
% Convert the grayscale image to a binary image
binary_img = imbinarize(grayImage);
% Clean it up.
% Fill holes.
binary_img = imfill(binary_img, 'holes');
% Get rid of small blobs, smaller than 1000 pixels.
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
subplot(2,2,4);
imshow(binary_img);
axis('on', 'image')
title('Binary Image');
% Find the centroid(s).
props = regionprops(binary_img, 'Centroid', 'Area');
allAreas = [props.Area]
xyCentroids = vertcat(props.Centroid)
allAreas =
14245 73432 1364 2685
xyCentroids =
2263.90881010881 2.11295191295191
2639.22315884083 3248.53923357664
187.619501466276 2106.80498533724
4890.16014897579 736.441713221602
So you can see it found 4 blobs that you told it to find. You are not telling it to find edges of triangles, and anyway, those triangles are so dim and noisy you'd have a hard time automating that to any degree of accuracy.
Surabhi A S
Surabhi A S il 19 Giu 2023
Modificato: Surabhi A S il 19 Giu 2023
I agree that those are dim. Is there any way to find the triangles after enhancing the contrast of the image atleast? As I want to detect the edges of the triangle using any (canny,sobel) algorithm.
Is there anyway from which I can detect the edges?
We went through this before in your other questions. Sine you have many triangles that are very low contrast and overlapped by other triangles your best bet would be to use drawline or drawpolyline or drawpolygon to hand-specify the vertices.
Yes the problem is I can't keep that doing for every image as I have many images. I need a generalised code for an image or a general code to alter few parameters for each image. Any update on this?
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.
Ok. Thank you for your valuable time spent.

Accedi per commentare.

Più risposte (0)

Prodotti

Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by