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 CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by