無作為な画像の切り抜き

セマンティックセグメンテーションを行うにあたって,データ数を増やすために1枚の画像からランダムで複数の画像を切り出したいと考えているのですがどのようなコードで可能なのでしょうか

1 Comment

Kodai Sato
Kodai Sato on 12 Jan 2020
例えば800×1000の画像からランダムに100×100で100サンプル取り出すといったものです.

Sign in to comment.

 Accepted Answer

Kenta
Kenta on 12 Jan 2020
Edited: Kenta on 12 Jan 2020

1 vote

こんにちは、 randomCropWindow2d という関数を使えばできます。
ひとまずデータを拡張して、セマンティックセグメンテーションの性能を試したいということでしたら、
以下のようにしてもよいかもしれません。補助関数が入っていないので、添付のライブエディターを実行してください。
例えば、したのURLが参考になると思います。
ご質問にある、複数の画像の切り出しは以下のもので実行可能と思います。
また、randomPatchExtractionDatastoreを使うのもよいと思います。
練習のために、たくさん数を切り出したいのなら上の方法が、直感的でわかりやすいかもしれません。ただ、コーディング自体は下のやり方に倣うほうがシンプルでよいかもしれません。
詳細は下のURLをご覧ください。
clear;clc
numObservations = 4;
filenameImage = 'kobi.png';
trainImages = repelem({filenameImage},numObservations,1);
filenameLabels = 'kobiPixelLabeled.png';
trainLabels = repelem({filenameLabels},numObservations,1);
classes = ["floor","dog"];
ids = [1 2];
imds = imageDatastore(trainImages);
pxds = pixelLabelDatastore(trainLabels,classes,ids);
trainingData = combine(imds,pxds);
augmentedTrainingData = transform(trainingData,@jitterImageColorAndWarp);
data = readall(augmentedTrainingData);
inputSize=size(read(imds));
targetSize = [400 400];
I = imread(filenameImage);
L = imread(filenameLabels);
C = categorical(L,ids,classes);
resizedI = imresize(I,targetSize);
resizedC = imresize(C,targetSize);
B = labeloverlay(resizedI,resizedC);
figure;imshow(B)
imwrite(B,'dog.jpg')
preprocessedTrainingData = transform(augmentedTrainingData,...
@(data)randomCropImageAndLabel(data,targetSize));
data = readall(preprocessedTrainingData);
rgb = cell(numObservations,1);
for k = 1:numObservations
I = data{k,1};
C = data{k,2};
rgb{k} = labeloverlay(I,C);
end
figure;montage(rgb)
Cropped_I=getframe;
imwrite(imresize(Cropped_I.cdata,.5),'cropped.jpg')

6 Comments

Kodai Sato
Kodai Sato on 13 Jan 2020
randomPatchExtractionDatastoreを用いて行おうとしたのですが以下のようなエラーが発生しました.
>> i_learning
エラー: trainNetwork (line 170)
入力データストアに対する read() の出力は、入力ファイル C:\Users\baris\OneDrive\デスクトップ\研究\画像解析\元データ\1.JPG C:\Users\baris\OneDrive\デスクトップ\研究\画像解析\教師データ\(1).jpg に対し同じサイズのイ
メージを返さなければなりません
エラー: i_learning (line 43)
net = trainNetwork(patchds,lgraph,options);
原因:
エラー: randomPatchExtractionDatastore/validateImageSizes (line 1129)
入力データストアに対する read() の出力は、入力ファイル C:\Users\baris\OneDrive\デスクトップ\研究\画像解析\元データ\1.JPG C:\Users\baris\OneDrive\デスクトップ\研究\画像解析\教師データ\(1).jpg に対し同じサイズ
のイメージを返さなければなりません
Kodai Sato
Kodai Sato on 13 Jan 2020
以下i_learningを示します.
%ランダムパッチ抽出
%patchds = randomPatchExtractionDatastore(imds,pxds,a, ...
% 'PatchesPerImage',b);a行a列ピクセルをb個抽出
patchds = randomPatchExtractionDatastore(imds,pxds,100, ...
'PatchesPerImage',96);
%ネットワークの作成
imageSize = [96 96 3];
%imageSize = [352 480 3];
%imageSize = [180 240 3];
numClasses = numel(classes);
lgraph = unetLayers(imageSize,numClasses);
%クラスの重み付けを使用したクラスのバランス調整
imageFreq = tbl.PixelCount ./ tbl.ImagePixelCount;
classWeights = median(imageFreq) ./ imageFreq;
pxLayer = pixelClassificationLayer('Name','labels','ClassNames',tbl.Name,'ClassWeights',classWeights);
lgraph = removeLayers(lgraph,'Segmentation-Layer');
lgraph = addLayers(lgraph, pxLayer);
lgraph = connectLayers(lgraph,'Softmax-Layer','labels');
%学習オプションの選択
options = trainingOptions('sgdm', ...
'Momentum',0.9, ...
'InitialLearnRate',1e-3, ...
'L2Regularization',0.0005, ...
'MaxEpochs',10, ...
'MiniBatchSize',2, ...
'Shuffle','never', ... %'Shuffle','every-epoch', ...
'VerboseFrequency',2);
%データ拡張
augmenter = imageDataAugmenter('RandXReflection',true,...
'RandXTranslation',[-10 10],'RandYTranslation',[-10 10]);
%学習の開始
%pximds = pixelLabelImageDatastore(imdsTrain,pxdsTrain,'DataAugmentation',augmenter);
%net= trainNetwork(pximds,lgraph,options);
net = trainNetwork(patchds,lgraph,options);
Kenta
Kenta on 13 Jan 2020
Edited: Kenta on 13 Jan 2020
patchds = randomPatchExtractionDatastore(imds,pxds,[96 96]);
としたらどうですかね?
あとは、imds, pxdsも正しくないという可能性もあります。
Kodai Sato
Kodai Sato on 13 Jan 2020
変更しても同様のエラーが発生しました
Kenta
Kenta on 13 Jan 2020
Edited: Kenta on 13 Jan 2020
そうですか…コードを眺めてぱっと言えるのはこれくらいです。すいません。
Kodai Sato
Kodai Sato on 13 Jan 2020
確認したところimds, pxdsの画像サイズが1ピクセルずれていたため,それが原因だと思います.
ありがとうございました.

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2019b

Asked:

on 12 Jan 2020

Commented:

on 13 Jan 2020

Community Treasure Hunt

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

Start Hunting!