Creation of an automatic polyline
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
Rik
2020-6-17
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!
采纳的回答
Turlough Hughes
2020-3-12
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 个评论
Turlough Hughes
2020-3-13
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.
Turlough Hughes
2020-3-14
编辑:Turlough Hughes
2020-3-14
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!