複数粒子の頂点を得たい

23 Ansichten (letzte 30 Tage)
Naoki Hashiguchi
Naoki Hashiguchi am 19 Apr. 2023
Kommentiert: Naoki Hashiguchi am 24 Apr. 2023
下記のコードを実行してregionprops関数のBoundingBoxを用いて粒子の頂点を求めようしたのですが粒子が斜めになっているとBoundingBoxでは頂点を正しく認識することができません.斜めの粒子にも適用できる方法はありますか.
方法がありましたらご教示お願いします.
% 画像を読み込みます。
im = imread('02Al_S0002_2304_1158000001.bmp');
% 画像をグレースケールに変換します。
im_gray = rgb2gray(im);
% 画像を二値化します。
im_bw = imbinarize(im_gray);
% 画像内のオブジェクト(粒子)を見つけます。
stats = regionprops('table', im_bw, 'Centroid', 'Area', 'BoundingBox');
% オブジェクトの形状を解析して、四角形であるかどうかを確認します。
imshow(im);
hold on;
for k = 1:height(stats)
boundingBox = stats.BoundingBox(k,:);
%aspectRatio = boundingBox(3)/boundingBox(4);
%if aspectRatio >= 0.95 && aspectRatio <= 1.05
% 四角形である場合、頂点を見つけます。
x = boundingBox(1);
y = boundingBox(2);
w = boundingBox(3);
h = boundingBox(4);
vertex_x = [x, x+w, x+w, x];
vertex_y = [y, y, y+h, y+h];
plot(vertex_x, vertex_y, 'r*');
%end
end

Akzeptierte Antwort

Shunichi Kusano
Shunichi Kusano am 20 Apr. 2023
Bearbeitet: Shunichi Kusano am 20 Apr. 2023
回転を許して面積が最小になる外接矩形を求めるプログラムを作成してみました(たぶんちゃんと動いていると思いますが、何らか極端なケースで変な結果が出る可能性もありますので、常に可視化して結果の妥当性ご確認ください)。下記が添付のプログラムを動かす例となります。
clear, close, clc;
%% generate test data
im = imread('hirn0vxp.png');
% 画像をグレースケールに変換します。
im_gray = rgb2gray(im);
% 画像を二値化します。
bw = imbinarize(im_gray);
bw = imclearborder(bw); % 画像端にかかる粒子は除く
%% 画像中の全ての小領域に対して、最小面積となる外接矩形を計算
% 幅、高さ、回転角度、4隅の座標を出力
minrect_info = im_minarearect(bw);
%% 結果の可視化
imshow(bw)
hold on;
for rgi = 1:height(minrect_info)
pt_corners = minrect_info.Corner_Points{rgi};
temp = [pt_corners;pt_corners(1,:)];
plot(temp(:,1),temp(:,2),'g-o');
pt_center = mean(pt_corners,1);
text(pt_center(1),pt_center(2),...
{sprintf('area:%5.1f',minrect_info.Min_Area(rgi)),sprintf('rot:%3.1f',minrect_info.Rotation_Angle(rgi))},...
"Color",'g');
text(pt_corners(:,1),pt_corners(:,2),num2str([1:4]'),'Color','y');
end
hold off;
これを動かすと下記のような図が出てくるかと思います。
座標の並び順や回転角度の定義などは下記の通りです。
  1 Kommentar
Naoki Hashiguchi
Naoki Hashiguchi am 24 Apr. 2023
Shunichi Kusano 様,プログラムを作製して頂きありがとうございました.
無事解決することが出来ました.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!