2次元グラフの面積取得方法
Show older comments
2次元の座標データをもっていて
点に囲まれた座標の面積を求めたいです.
polyareaを使ってみましたが,形状が変わってしまいます. (sample.png)
左図:もとめたい形状
右図:plot した画像 → polyareaはこの形状の面積を求めている?
左図の形状を保った状態で,面積を取得する方法はありますか?
座標の分解能は保持したいです.
6 Comments
Hernia Baby
on 31 Mar 2023
面積というのは総面積ですか?それとも各領域ごとの面積ですか?
H.O
on 31 Mar 2023
横やり失礼します。
点群の凸包やその面積の演算はMATLABの関数で出来ますが、部分的な領域をどう認識させるかが問題ですね。
予めどの塊がどういう順番で来るかが把握できていれば楽ですが、そうでなければ認識させる必要があります。また点群の塊が凸形状でない場合や、点群の密度が荒い場合は、正確な面積が出せないおそれがあります。
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
[k,av] = convhull(x,y); % 凸包を計算
av % 面積
plot(x,y,'*')
hold on
plot(x(k),y(k))
上記なら上手く計算出来そうです。
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
shp = alphaShape(x',y'); % alphaShape オブジェクトを作成
A = area(shp) % 面積を計算
plot(shp)
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
H.O
on 2 Apr 2023
Moved: Atsushi Ueno
on 2 Apr 2023
Accepted Answer
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)))
Categories
Find more on Process Point Clouds 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!





