2次元グラフの面積取得方法
23 views (last 30 days)
Show older comments
2次元の座標データをもっていて
点に囲まれた座標の面積を求めたいです.
polyareaを使ってみましたが,形状が変わってしまいます. (sample.png)
左図:もとめたい形状
右図:plot した画像 → polyareaはこの形状の面積を求めている?
左図の形状を保った状態で,面積を取得する方法はありますか?
座標の分解能は保持したいです.
6 Comments
Atsushi Ueno
on 31 Mar 2023
Moved: Atsushi Ueno
on 2 Apr 2023
n = 100;
for k = 0:9 % ランダム値でサンプル点群データを生成
x(k*n+1:(k+1)*n) = rand(100,1)*n + randi(10,1,1)*n;
y(k*n+1:(k+1)*n) = rand(100,1)*n + randi(10,1,1)*n;
end
plot(x,y,'*')
hold on
h = plot(x(k),y(k));
for s = 0:0.01:1
[k,av] = boundary(x',y',s); % 境界を計算%[k,av] = convhull(x,y); % 凸包を計算
%av % 面積
h.XData = x(k);
h.YData = y(k);
drawnow
end
Accepted Answer
Hiroshi Iwamura
on 6 Apr 2023
外枠いらなければ、最後のループも不要ですね。
I = imread("https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1345389/sample_c.png");
BW = imbinarize(I(:,:,2));
se = strel('disk',2);
BW2 = imclose(BW,se);
BW3 = imopen(BW2,se);
montage({I,BW,BW2,BW3})
stats = regionprops(BW3,'basic');
L = bwlabel(BW3);
T = struct2table(stats);
imshow(L,[],Colormap=jet)
idx = (T.Area > 100);
text(T.BoundingBox(idx,1),T.Centroid(idx,2),num2str(T.Area(idx)),'Color','white','FontSize',10);
fprintf('Total Area = %d\n',sum(T.Area(T.Area>10)))
More Answers (1)
Hiroshi Iwamura
on 4 Apr 2023
Edited: Hiroshi Iwamura
on 4 Apr 2023
なかなか正確に求めるのは難しく調整が必要になりますが、Image Processing Toolbox をお持ちであればモフォロジーを使う手はあります。Simulink (Computer Vision Toolbox) でやった方が色々と調整が簡単かもしれません。
I = imread("sample_c.png");
BW = imbinarize(I(:,:,2));
se = strel('disk',2);
BW2 = imclose(BW,se);
BW3 = imopen(BW2,se);
montage({I,BW,BW2,BW3})
stats = regionprops(BW3,'basic');
L = bwlabel(BW3);
T = struct2table(stats);
T = sortrows(T,'Area','descend');
% imshow(I)
imshow(L,[],Colormap=jet)
hold on
n = 1;
while T.Area(n) > 100
rectangle('Position',T.BoundingBox(n,:),EdgeColor=[1 0.2 0])
text(T.BoundingBox(n,1),T.Centroid(n,2),num2str(T.Area(n)),'Color','white','FontSize',10)
n = n + 1;
end
hold off
fprintf('Total Area = %d\n',sum(T.Area(T.Area>10)))
See Also
Categories
Find more on LIDAR および点群の処理 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!