MATLAB: Creation of an automatic polyline

ellipseimage processingImage Processing Toolboximage segmentation

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!

Best 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))])