Alter and store images back into an excisting imagedatastore
조회 수: 5 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
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
2018년 6월 11일
You can use augmented image datastore method to resize the image before training(matlab 2017b and after).
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!