Sorting a 2d matrix according to the distance between each successive point.
조회 수: 6 (최근 30일)
이전 댓글 표시
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
Andrew Newell
2011년 3월 1일
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?
답변 (4개)
Jan
2011년 3월 4일
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
2011년 3월 4일
Do you have the Image Processing Toolbox? Consider using REGIONPROPS, and requesting the PERIMETER of the region.
Cheers, Brett
Brett Shoelson
2011년 3월 5일
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
2011년 3월 9일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!