Sorting a 2d matrix according to the distance between each successive point.
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I have a 2d matrix of points that represents a 2d slice of a 3d model. Ultimately I would like to calculate the distance of perimeter of the 2d shape.
In order to do this, I think I need to order the matrix, starting with one point and then find the closest point to that first point and then the closest point to the next point. Once the matrix is sorted, I will be able to calculate the distance between each successive points in the sorted matrix and this should give me the distance of the perimeter.
I have tried using a convex hull approach but the 2d matrix is not always convex and I would loose important data points.
Thanks in advance for any guidance.
Eoin
2 Kommentare
Andrew Newell
am 1 Mär. 2011
Could you please clarify what you mean by distance to the perimeter? In particular - distance of what to the perimeter? Is the location of the perimeter known in advance?
Antworten (4)
Jan
am 4 Mär. 2011
There is no simple, general and unique solution: If the points are distributed on an 8 shape, the behaviour in the center is not well defined.
Brett Shoelson
am 4 Mär. 2011
Do you have the Image Processing Toolbox? Consider using REGIONPROPS, and requesting the PERIMETER of the region.
Cheers, Brett
Brett Shoelson
am 5 Mär. 2011
Yes!
Try this instead:
stats = regionprops(cut_surface,'perimeter');
perims = [stats.Perimeter];
That will give you (in "perims") the primeter of each ROI in your segmented (binary) image. If you have more than one ROI in your image, you'll need to make sure that you're selecting the correct element of perims--the one corresponding the shape you want to measure.
Cheers, Brett
Brett Shoelson
am 9 Mär. 2011
Eoin, I'm not sure what those three non-zero values refer to, since I can't see your code. But if the object you are interested in has a perimeter of 400 (as you pointed out in your email to me), then it's clear that you did something wrong. Perhaps this will help:
% TRY THIS: Find the object with the largest perimeter in the % image "pillsetc.png" (ships with the IPT)
%Get/display image
img = rgb2gray(imread('pillsetc.png'));
imshow(img)
% Is it binary?
islogical(img) % (no)
% Convert to binary
% (i.e., create a segmentation mask of the objects in the
% image)
img = im2bw(img,graythresh(img));
islogical(img) % Now it's binary!
imshow(img)
% Now get perimeters
stats = regionprops(img,'Perimeter','Centroid');
perims = [stats.Perimeter]
% And, for illustrative purposes:
centroids = [stats.Centroid];
centroids = reshape(centroids,2,[])';
fprintf('You calculated the perimeters of %d objects.\n',numel(perims));
% Note that every white region is a "blob", or ROI. Some are real objects,
% others are specs. We could have cleaned them up (BWAREAOPEN, for
% instance), but that's not necessary. The values in perims correspond the
% those 22 objects. Their positions are determined by the order in which
% the first white pixel in the region is found, going down the columns,
% from left to right.
% Mark the location of the object having the largest perimeter
idx = find(perims == max(perims));
hold on
text(centroids(idx,1),centroids(idx,2),sprintf('* P = %0.2f',perims(idx)),'color','r')
Cheers, Brett
0 Kommentare
Siehe auch
Kategorien
Mehr zu Image Segmentation and Analysis finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!