Extracting edges from a shape

14 ビュー (過去 30 日間)
Usama Hussain
Usama Hussain 2022 年 12 月 19 日
コメント済み: Usama Hussain 2022 年 12 月 22 日
Hello all.
I have an irregular form shape like in the image(s) below (created using inpolygon).
I only want to extract the outer edges of this shape, so that I can only have an idea of what the shape is (rather than the complete internal points).
I used contours (imcontour) module which highlights the regions I desire to attain (as shown in the image below).
So I only want to attain these yellow regions.
Help will be much appreciated. Thanks in advance :)
  1 件のコメント
DGM
DGM 2022 年 12 月 19 日
編集済み: DGM 2022 年 12 月 19 日
If the hatched pattern was created using inpolygon(), doesn't that imply that you already have the polygon that defines the boundary?
It would help to have an example that describes the form of the data and the steps taken to get it into that form.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2022 年 12 月 20 日
Alright, I created the binary image in Photoshop from your red crosshairs screenshot. It's attached. Then I wrote code for it to get the perimeter image, and also a list of (x,y) coordinates in a round-the-clock sorted fashion using bwboundaries
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%--------------------------------------------------------------------------------------------------------
% READ IN IMAGE
folder = [];
baseFileName = 'binaryImage.png';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
if numberOfColorChannels > 1
% It's not really gray scale like we expected - it's color.
fprintf('It is not really gray scale like we expected - it is color\n');
% Extract the blue channel.
grayImage = rgb2gray(grayImage);
% Update the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(grayImage)
end
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 1);
imshow(grayImage);
impixelinfo;
axis('on', 'image');
title('Original Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Maximize window.
g = gcf;
g.WindowState = 'maximized';
drawnow;
% Binarize
binaryImage = grayImage > 128;
% Call bwperim
perimImage = bwperim(binaryImage);
%--------------------------------------------------------------------------------------------------------
% Display the image.
subplot(2, 2, 2);
imshow(perimImage);
impixelinfo;
axis('on', 'image');
title('Perimeter Image', 'FontSize', fontSize, 'Interpreter', 'None');
% Find xp and yp (unsorted)
[yp, xp] = find(perimImage);
% Find boundaries (which will be sorted)
% 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.
subplot(2, 2, 3);
imshow(grayImage); % 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(binaryImage);
% 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', 3); % 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.
  6 件のコメント
Usama Hussain
Usama Hussain 2022 年 12 月 21 日
I tried it before but did not give adequate result. Anyways I'll try it again.
Usama Hussain
Usama Hussain 2022 年 12 月 22 日
Thanks so much Image Analyst.
Boundary function works fine.
Thanks all along :)

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2022 年 12 月 19 日
If you have a digital image, you can use bwperim or bwboundaries
Snippet for you to modify:
% 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(originalImage); % 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(mask);
% 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.
  6 件のコメント
Usama Hussain
Usama Hussain 2022 年 12 月 20 日
What I inserted as an image is exactly in png format and I used that to create a grayscale image and then converted to binary (which I used as mask).
Then ran the code you provided.
After that, I attained an image. But the outer edges were highlighted (like the contour image) and I still got the inner regions, which is what I want to remove.
Image Analyst
Image Analyst 2022 年 12 月 20 日
And you forgot to attach that code or image. Alright, I guess I'll have to create an image from the screenshots you supplied and then continue on with a demo after that. I'll need several minutes to do that.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by