How to traverse along image?

2 次查看(过去 30 天)
asdf
asdf 2018-5-21
编辑: asdf 2018-5-24
I am using this code to highlight the boundaries of an image
I=imread(image);
boundaries=bwboundaries(I);
numboundaries=size(boundaries,1);
for k=1:numboundaries
thisboundary=boundaries{k};
L=size(thisboundary(:,2));
for j=1:100:L(1)
plot(thisboundary(j,2), thisboundary(j,1), '*g')
end
end
I have an example image:
This is an example of how the order in which the `for j=1:100:L(1)` traverses:
What I would like instead is to traverse along the boundary in the order specified in the image below. I would like to be able to draw straight lines and get their coordinates, like the green straight lines I've drawn
Or is it possible to convert the white thick curve using imerode and/or imdilate so that the white curve consists of only one white pixels. For example, for the 3rd image, is it possible to thin the white thick part so that the green straight lines only go through one white pixel?
Is this possible in Matlab? How can I do this?
  1 个评论
Walter Roberson
Walter Roberson 2018-5-21
imread() never returns a cell array, so I is not going to be a cell array. You assign I to boundaries, so boundaries is not going to be a cell array. But you access boundaries{k} as if it were a cell array.

请先登录,再进行评论。

回答(2 个)

Walter Roberson
Walter Roberson 2018-5-21
For you I would suggest https://www.mathworks.com/help/images/ref/bwtraceboundary.html which allows you to specify an initial position and initial search direction.

Image Analyst
Image Analyst 2018-5-21
I think your whole approach is wrong. I think what you really want is the mean width or the distribution of widths. To do that you'd call bwdist() to get the Euclidean distance transform, then call bwmorph on the binary image to get the skeleton, then use the skeleton as a mask/index on the EDT image to extract the radii. Then get the mean of those or histogram them. The steps are (untested):
edtImage = bwdist(binaryImage);
skelImage = bwmorph(binaryImage, 'skel', inf);
radiiValues = edtImage(skelImage);
diameterValues = 2 * radiiValues;
histogram(diameterValues );
meanDiameter= mean(diameterValues);
  1 个评论
Image Analyst
Image Analyst 2018-5-21
编辑:Image Analyst 2018-5-22
You can get the skeleton like I said. Then get the endpoints and use bwtraceboundary() to move along the skeleton. You can move along the curve and fit the points in a window to a quadratic. Then get the slope and find the endpoints of a line perpendicular to the curve at that point. You can find the width of the perpendicular line by taking the max of the EDT image - that will tell you how long to make the cross sectional line. Then with the endpoints of the cross section you can call imline() or improfile() and with that you can find the width. Not too hard. Do you want to try it yourself?
I'm attaching code to give you a start. See if you can finish it.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by