5枚の画像データを読​み込み、それぞれのデ​ータを記憶していきた​いのですが………

5枚の画像をそれぞれ読み込んでデータを記憶させたいのですが、なぜか5枚目のデータしか表示されません。どうしたらよいのか教えていただけませんか?

10 Comments

michio
michio on 12 Dec 2017
Edited: michio on 12 Dec 2017
添付の MATLAB.txt を転記します。
clear all;
for j=1:5
abc = strcat(num2str(j),'.jpg');
BW=imread(abc);
image(BW)
BW2=rgb2gray(BW)>0;
end
%%regionprops を使用してイメージ内の連結要素の重心を計算します。
s = regionprops(BW2,'centroid');
%%各重心位置計算されたエリアの面積
Areas = regionprops(BW2,'Area');
%%角度計算
Orientations = regionprops(BW2,'Orientation');
%%重心を格納する構造体配列を単一の行列に連結します。
centroids = cat(1, s.Centroid);
centroids2 = cat(1, Areas.Area);
centroids(:,3) = centroids2;
length(centroids)
j=1;
for i=1:length(centroids)
if (centroids(i,3)>1000 && centroids(i,3)<10000)
medaka_index = i;
end
end
%%重心の位置を重ね合わせたバイナリ イメージを表示します。
imshow(BW)
hold on
plot(centroids(medaka_index,1),centroids(medaka_index,2), 'b*')
hold off
Jiro Doke
Jiro Doke on 12 Dec 2017
プログラムをよく見ていただくと分かると思いますが、最初のループの実行が終わったら、 BW2 には何が入っているでしょうか。一番最後(5枚目)の画像が白黒に変換されたものですよね。その画像がそれ以降使われているわけです。
要点だけ言いますと、以降の処理を全部最初のループの中に入れてあげればいいと思います。
試してみてください。
Takuya Nagai
Takuya Nagai on 13 Dec 2017
以降の処理を最初のループに入れて実行してみたのですが、結果は同じく、5枚目のデータしか表示されませんでした。途中途中で1~4枚目のデータを保存しながらやっていく必要があるのでしょうか?そのようなやり方が分からないのでよろしければ教えていただけないでしょうか?
mizuki
mizuki on 13 Dec 2017
こんな感じでインデクスを使ってやればよいのでは。 1枚目から5枚目の 01 行列が並ぶような感じです。
for i = 1:3
BW2(:,:,i) = rgb2gray(BW)>0;
end
Jiro Doke
Jiro Doke on 13 Dec 2017
MATLAB では通常は表示用のコマンド (imshow など)は前の表示を上書きする仕様になってます。その場合、 figure コマンドが役立つかもしれません。
Takuya Nagai
Takuya Nagai on 13 Dec 2017
figureを使用したところ、エラーが出たのですが、
エラー: figure
最初の引数は figure オブジェクトまたは正の整数でなければなりません
エラー: test (line 5) BW=figure(abc);
このように表示されます。どうすればよいのでしょうか?
clear all;
for g=1:5
abc = strcat(num2str(g),'.jpg');
BW=figure(abc);
figure(BW)
BW2=rgb2gray(BW)>0;
%%regionprops を使用してイメージ内の連結要素の重心を計算します。
s = regionprops(BW2,'centroid');
%%各重心位置計算されたエリアの面積
Areas = regionprops(BW2,'Area');
%%角度計算
Orientations = regionprops(BW2,'Orientation');
%%重心を格納する構造体配列を単一の行列に連結します。
centroids = cat(1, s.Centroid);
centroids2 = cat(1, Areas.Area);
centroids(:,3) = centroids2;
length(centroids)
j=1;
if true
% code
end
for i=1:length(centroids)
if (centroids(i,3)>1000 && centroids(i,3)<10000)
medaka_index = i;
end
end
%%重心の位置を重ね合わせたバイナリ イメージを表示します。
figure(BW)
hold on
plot(centroids(medaka_index,1),centroids(medaka_index,2), 'b*')
hold off
end
Jiro Doke
Jiro Doke on 13 Dec 2017
もう少しなので頑張ってみましょう!
figure コマンドを紹介しましたが、 imread image imshow すべてを figure に変える、という事ではありません。もう一度 figure のドキュメンテーションをご覧になり、 figure コマンドの役割を把握してみましょう。
(ドキュメンテーションから)
figure - Figure ウィンドウの作成
Takuya Nagai
Takuya Nagai on 13 Dec 2017
できました!こういうことですよね?
clear all;
for g=1:5
abc = strcat(num2str(g),'.jpg');
BW=imread(abc);
imshow(BW)
BW2=rgb2gray(BW)>0;
%%regionprops を使用してイメージ内の連結要素の重心を計算します。
s = regionprops(BW2,'centroid');
%%各重心位置計算されたエリアの面積
Areas = regionprops(BW2,'Area');
%%角度計算
Orientations = regionprops(BW2,'Orientation');
%%重心を格納する構造体配列を単一の行列に連結します。
centroids = cat(1, s.Centroid);
centroids2 = cat(1, Areas.Area);
centroids(:,3) = centroids2;
length(centroids)
j=1;
for i=1:length(centroids)
if (centroids(i,3)>1000 && centroids(i,3)<10000)
medaka_index = i;
end
end
%%重心の位置を重ね合わせたバイナリ イメージを表示します。
imshow(BW)
hold on
plot(centroids(medaka_index,1),centroids(medaka_index,2), 'b*')
hold off
BW=figure;
end
結果的に、5枚の画像を表示させることはできたのですが、 それぞれの位置座標を確かめようと思い、ワークスペースにある 関数から確認したのですが、 なぜかその値は5枚目のデータで、 ほかの4枚のデータ値がありませんでした。 これはどうすれば改善できるのでしょうか?
Jiro Doke
Jiro Doke on 13 Dec 2017
figure コマンドは単体で使えます。 BW=figure とやる必要はありません。
最後のデータしか残らないのは、ループが回る度に centroids が更新(上書き)されるからです。もし単に座標データを目で確認したいだけならば、ループの中で centroid をコマンドウィンドウに表示するようにすれば良いと思います。ヒント: disp
Takuya Nagai
Takuya Nagai on 14 Dec 2017
ヒント、ありがとうございました!おかげで成功しました!今後ともよろしくお願いいたします。

Sign in to comment.

Answers (0)

Asked:

on 12 Dec 2017

Commented:

on 14 Dec 2017

Community Treasure Hunt

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

Start Hunting!