Alter and store images back into an excisting imagedatastore

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Arun Mathamkode
Arun Mathamkode il 31 Ott 2017
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 Commento
Aarti Bokade
Aarti Bokade il 31 Ott 2020
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

Accedi per commentare.

Più risposte (1)

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

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by