Creation of an automatic polyline

My objective is to create an automatic polyline in the attached image, which is a tube with a bubble, so i can aproximate the area of the bubble to any polygon. I don't know which function or command to use. I searched for references on the internet and the only thing i found was the construction of a "manual" polyline (link below). Can anybody help me?
Thanks in advance!

7 Comments

Yea that's very possible. How did you get the green line in the first place?
What is your reason for doing this by the way? I assume you want to manually adjust the perimeter once you setup a polyline or something like that.
Can you upload the binary image by saving the variable as a .mat file and attaching it to the question?
This just looks like a big white blob. Can you attach the original gray scale or RGB image also, along with the code you used to segment it and create that binary image?
And I'm not sure what "I need it to make an approach of the bubble area as a semi ellipse, take this area and make a rotation to calculate it's volume." means. Do you just want to fit an ellipse to the white blob? If so calling regionprops() and asking for MajorAxisLength and MinorAxisLength is enough for you to get a volume (assuming rotation symmetry out of the plane). I don't see why rotation is needed.
Why did you edit away all your questions? That is very rude.
I second that.
This is what I could recover from Google cache:
Creation of an automatic polyline
My objective is to create an automatic polyline in the attached image, which is a tube with a bubble, so i can aproximate the area of the bubble to any polygon. I don't know which function or command to use. I searched for references on the internet and the only thing i found was the construction of a "manual" polyline (link below). Can anybody help me?
Thanks in advance!
(Answers Dev) Restored edit

Sign in to comment.

 Accepted Answer

It looks like you have a binary image but you uploaded it as a .jpeg, I have included 3 lines to "workaround" and get back to an initial binary image, but ideally you would include the image by saving the Image variable from your workspace as a .mat file and attaching it to the question.
To specify the polyline you first need to figure out the position coordinates of the vertices and to do that I used the boundary function. We also only need to input points near the perimeter for the boundary function for it to work, so using bwperim first is much more efficient than inputting all points corresponding the inside of your bubble.
Finally, having the vertices of the perimeter you can input them into the drawpolyline function as I have shown below. You will get roughly one vertex for every pixel on the perimeter and I'm guess you don't want to draw every single one of them, so the value 'skip' can be adjusted to skip more or less pixels per vertex as you require.
clear
close all
clc
fontSize = 16;
skip = 10;
%Right click image provided in question > save as 'Jordan.jpeg' in working
%directory for matlab.
% Load Image
I = imread('Jordan.jpeg');
% Workaround to get back to a binary image
I(:,[1:86 403:end],:) = 0;
I([1:31 500:end],:,:) = 0;
B = imbinarize(rgb2gray(I));
% Show Initial Image
hf = figure('Units','Normalized','OuterPosition',[0 0 1 1]);
subplot(1,2,1), imshow(B)
title('Initial Binary Image','fontSize',fontSize)
% This step is not required, but speeds up the use of boundary later.
Bperimeter = bwperim(B);
Bperimeter = imdilate(Bperimeter,strel('square',2));
subplot(1,2,2), imshow(Bperimeter)
title('Result of bwperim','fontSize',fontSize)
% Get x,y coordinates of perimeter (column index and row index,
% respectively)
[y,x] = find(Bperimeter);
k = boundary(x,y,1); %use boundary with shrink factor of 1 to find vertices
% Back to the Initial Binary image, add the polyline using recently
% obtained vertices
subplot(1,2,1)
drawpolyline('Position',[x(k(1:skip:end)) y(k(1:skip:end))])

3 Comments

If its area that you want you can get it easily with the regionprops function as follows:
load('Image.mat')
s = regionprops(preenc,'Area');
s.Area %number of pixels corresponding to area of shape
You will of course need to scale the result according to the resolution of your image in terms of mm^2/pixel, or similar.
Why do you need the polyline though? You can get area automatically using the code in my last comment.
As for the error. I image you have a version of matlab older than 2018b, drawpolyline was only introduced then.
You can ensure that it is closed by adding on the first index
clear
clc
close all
fontSize = 16;
skip = 160;
load('Image.mat')
B = preenc;
% Show Initial Image
hf = figure('Units','Normalized','OuterPosition',[0 0 1 1]);
subplot(1,2,1), imshow(B)
title('Initial Binary Image','fontSize',fontSize)
% This step is not required, but speeds up the use of boundary later.
Bperimeter = bwperim(B);
Bperimeter = imdilate(Bperimeter,strel('square',4));
subplot(1,2,2), imshow(Bperimeter)
title('Result of bwperim','fontSize',fontSize)
% Get x,y coordinates of perimeter (column index and row index,
% respectively)
[y,x] = find(Bperimeter);
k = boundary(x,y,1); %use boundary with shrink factor of 1 to find vertices
% Back to the Initial Binary image, add the polyline using recently
% obtained vertices
subplot(1,2,1)
% MODIFIED PART adding on k(1) to the end ensures polygon is closed
idx = [k(1:skip:end);k(1)];
drawpolygon('Position',[x(idx) y(idx)])
I also used drawpolygon instead of drawpolyline. As for approximating a semi ellipse, I would try using regionprops as Image Analyst suggested.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!