Alter and store images back into an excisting imagedatastore

5 ビュー (過去 30 日間)
Bert
Bert 2017 年 10 月 26 日
コメント済み: Aarti Bokade 2020 年 10 月 31 日
Hi All,
I'm new to Matlab and need some help on how to alter and store images back into an (excisting) imagedatastore.
I already figured out how to read a bunch of images (which are labeled) and then, based on the largest object in the image, calculate and crop/resize the image for further processing.
I now need to update the original image in the imagedatastore with the new crop/resized image. Is this possible and how.
Kind regards
Bert
Code :
ORIGINAL = imageDatastore(digitDatasetPath,...
'IncludeSubfolders',true,'LabelSource','foldernames');
while hasdata(ORIGINAL)
% Clean up image
B = read(ORIGINAL);
BW = rgb2gray(B);
BW2 = imbinarize(BW);
% Label the image
labeledImage = bwlabel(BW2);
measurements = regionprops(BW2, 'BoundingBox', 'Area');
% Extract the biggest blob
allAreas = [measurements.Area];
[sortedAreas, sortingIndexes] = sort(allAreas, 'descend');
% Draw boundingbox arround biggest blob
Bigblob = measurements(sortingIndexes(1)).BoundingBox;
% Cut out biggest blob
DLim = imcrop(B,[Bigblob(1) Bigblob(2) Bigblob(3) Bigblob(4)]);
% Square image before resizing to maintain aspect ratio
Wblob = Bigblob(3);
Lblob = Bigblob(4);
if Wblob == Lblob
% do nothing
else
if Wblob > Lblob
DLim = padarray(DLim,[Wblob-Lblob 0],'pre');
else
DLim = padarray(DLim,[0 Lblob-Wblob],'pre');
end
end
% Resize - this result needs to replace the original image in the ORIGINAL imagedatastore ...
DLim = imresize(DLim,[1024,1024]);
end

採用された回答

Arun Mathamkode
Arun Mathamkode 2017 年 10 月 31 日
I can see that you are reading one image at a time (readSize=1). Hence the easy way is to write the image to the location specified by the imageDatastore object.
Get the file info as the second argument while using the 'read' function. Then use the 'Filename' property in the file info to update the cropped/resized image back to the original location.
ORIGINAL = imageDatastore(digitDatasetPath,'IncludeSubfolders',true,'LabelSource','foldernames');
while hasdata(ORIGINAL)
% Clean up image
[B,info] = read(ORIGINAL); % get the File info
BW = rgb2gray(B);
BW2 = imbinarize(BW);
% Label the image
labeledImage = bwlabel(BW2);
measurements = regionprops(BW2, 'BoundingBox', 'Area');
% Extract the biggest blob
allAreas = [measurements.Area];
[sortedAreas, sortingIndexes] = sort(allAreas, 'descend');
% Draw boundingbox arround biggest blob
Bigblob = measurements(sortingIndexes(1)).BoundingBox;
% Cut out biggest blob
DLim = imcrop(B,[Bigblob(1) Bigblob(2) Bigblob(3) Bigblob(4)]);
% Square image before resizing to maintain aspect ratio
Wblob = Bigblob(3);
Lblob = Bigblob(4);
if Wblob == Lblob
% do nothing
else
if Wblob > Lblob
DLim = padarray(DLim,[Wblob-Lblob 0],'pre');
else
DLim = padarray(DLim,[0 Lblob-Wblob],'pre');
end
end
% Resize - this result needs to replace the original image in the ORIGINAL imagedatastore ...
DLim = imresize(DLim,[1024,1024]);
imwrite(DLim,info.Filename); % Update the image in the original location
end
When you are reading multiple image files simultaneously, you may need to use a loop while writing the image.
  1 件のコメント
Aarti Bokade
Aarti Bokade 2020 年 10 月 31 日
Sir can you please write how to do it for all the images stored in a folder and preprocess all the images and store them back to same folder after preprocessing

サインインしてコメントする。

その他の回答 (1 件)

prasanth s
prasanth s 2018 年 6 月 11 日
You can use augmented image datastore method to resize the image before training(matlab 2017b and after).

カテゴリ

Help Center および File ExchangeImages についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by