How to add loop for saving multiple images into a specific folder?

Hi. I've run this code. No error occur, but it save only the last cropped image in bounding box while the other cropped images are not. Fyi, in my case, an image of bounding box will produce few cropped images which I named it as 'obj'. Meanwhile 'I12' is the resize of the cropped image. Thus, where should I add the looping and what are the codes should I write for the loop? Please help me. Thanks in advance.
srcFile=dir('C:\Users\User\Documents\FYP\Matlab\cnPVG\*.bmp');
for ck=1:length(srcFile)
filenamecn=strcat('C:\Users\User\Documents\FYP\Matlab\cnPVG\',srcFile(ck).name);
img_src =imread(filenamecn);
.
.
.
.
.
.
% Bounding box
imbwlabel=bwlabel(I8); %bwlabel works only in binary (b&w,double) image
% figure;
% % imshow(label2rgb(imbwlabel)); %this is important bcs it helps to create the bounding box
% %in colored (uint8,unit16 etc) image and not in binary (b&w) image
%
bboxes=regionprops(imbwlabel,'BoundingBox');
[L, n]=bwlabel(I8);
bboxes=regionprops(I8,'BoundingBox','Centroid');
%
% figure;imshow(I10);%title('Image with Bounding box');
axis image off
hold on
for k=1 : length(bboxes)
CurrBB=bboxes(k).BoundingBox;
% cent=cat(1,bboxes.BoundingBox);
% plot(cent(:,1),cent(:,2),'g*')
rectangle('Position', [CurrBB(1),CurrBB(2),CurrBB(3),CurrBB(4)], 'EdgeColor','m','LineWidth',2)
end
hold off
%%crop and zero padding
if ~isempty(bboxes)
for p=1:length(bboxes)
CurrBB=bboxes(p).BoundingBox;
obj = imcrop(I10,CurrBB);
[m, n, l]=size(obj);
if (m > 227 || n > 227)
obj=imresize(obj,[227 227]);
end
I12=zeros(227,227);
I12=uint8(I12);
for i=1:m
for j=1:n
for t=1:3
if (m > 227 || n > 227)
obj=imresize(obj,[227 227]);
else I12(i,j,t)=obj(i,j,t);
end
end
end
end
path=strcat('C:\Users\User\Documents\FYP\Matlab\bbbPVG\',srcFile(ck).name);
imwrite(I12,path);
end
% figure;imshow(I12);
% figure;imshow(obj);
end
end

8 Comments

It looks like the imwrite is inside the loop where the filename (path) would be the same for each itteration: the file would only contain the last since it is recreated in each itteration unless you add ,'WriteMode','append' parameters to the imwrite.
Just a hint: Use fullfile instead of strcat to concatenate path names:
srcFolder = 'C:\Users\User\Documents\FYP\Matlab\cnPVG\';
srcFile=dir(fullfile(srcFolder, *.bmp'));
for ck = 1:numel(srcFile)
filenamecn = strcat(srcFolder, srcFile(ck).name);
Avoid using "path" as name of a variable. This is not an error, but path is a fundamental Matlab command and during debugging it can cause strange effects to shadow this function by a variable.
I'm sorry, I don't really understand that @Jeffrey Clark However, I've added the 'WriteMode','append' as stated and an error appear, "too many input arguments". Hopefully you can explain more at this part, much appreciated if you can share the codes too. Thanks!
Thanks a lot for the correction @Jan However it didn't help on the saving part. Still encounter the same issue where only the last cropped image is saved. It would be much appreciated if you can help me more. Thanks!
@Erza Naziha: As I've said, my comment was a hint only, not a solution.
As mentioned in your other question, this part is useless:
I12=zeros(227,227);
I12=uint8(I12);
for i=1:m
for j=1:n
for t=1:3
if (m > 227 || n > 227)
obj=imresize(obj,[227 227]);
else I12(i,j,t)=obj(i,j,t);
end
end
end
end
Accoprding to the code you have posted, you import the contents of all files, but do not use it anywhere:
img_src =imread(filenamecn);
"img_src" does not appear later on. Maybe this is hidden in the ". . . " part, but I cannot guess such details.
The code overwrites the variable "bboxes":
bboxes=regionprops(imbwlabel,'BoundingBox');
[L, n]=bwlabel(I8);
bboxes=regionprops(I8,'BoundingBox','Centroid'); % Overwrite former result
Is this really wanted and useful?
@Erza Naziha, sorry I cnfused imwrite with something else, the parameters to add at the end of imwrite are ,'WriteMode','append'; note that 'overwrite' is the default as seen here.
@Jan Thanks for all the explanations. I've omitted the useless part and "img_src" as you said. It does reduced the clutters. I would say it is helpful but yet to solve the saving part. Thanks!

Sign in to comment.

Answers (1)

Try
props = regionprops(I8, 'BoundingBox', 'Centroid');
% Extract from structure into more convenient 2-D matrices.
allXyCentroids = vertcat(props.Centroid) % N-by-2 list of all (x,y) centroid coordinates.
allBBs = vertcat(props.BoundingBox) % N-by-4 matrix of all bounding boxes. Each row is one box.

3 Comments

@Image Analyst Hi. I've tried the given codes and modified it like below.
props = regionprops(I8, 'BoundingBox', 'Centroid');
% Extract from structure into more convenient 2-D matrices.
allXyCentroids = vertcat(props.Centroid) % N-by-2 list of all (x,y) centroid coordinates.
allBBs = vertcat(props.BoundingBox) % N-by-4 matrix of all bounding boxes. Each row is one box.
%
% figure;imshow(I10);%title('Image with Bounding box');
axis image off
hold on
for k=1 : length(allBBs)
CurrBB=allBBs(k).BoundingBox;
rectangle('Position', [CurrBB(1),CurrBB(2),CurrBB(3),CurrBB(4)], 'EdgeColor','m','LineWidth',2)
end
hold off
However there was an error as below
Dot indexing is not supported for variables of this type.
Error in pre_processing (line 189)
CurrBB=allBBs(k).BoundingBox;
Same thing happen to the %%crop and zero padding codes when I run it. What should I do and modify to run the codes smoothly? Thanks!
I think you didn't see the comment on the last line:
% N-by-4 matrix of all bounding boxes. Each row is one box.
So it's an N row matrix where there is one row for every one of the N boxes. The first column is the xLeft coordinate. The second column is the yTop row of the blobs. The third column is the widths of the boxes (in columns of pixels from center to center), and the fourth column is the height of the boxes (in rows or lines of pixels).

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products

Release

R2021a

Asked:

on 11 Jun 2022

Commented:

on 13 Jun 2022

Community Treasure Hunt

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

Start Hunting!