How to solve error "too many input arguments" when using imwrite('WriteMode', 'append')

3 次查看(过去 30 天)
Someone suggested me to add 'WriteMode' and 'append' when using imwrite function to save multiple images in specific folder. However, I'm getting error of "too many input arguments". I don't really understand the issue since this is my first time using it. This error comes from the 7th-8th last lines. Please anyone help me, stucked in this code for many days :(
srcFolder='C:\Users\User\Documents\FYP\Matlab\cnPVG\';
srcFile = dir(fullfile(srcFolder, '*.bmp'));
for ck=1:numel(srcFile)
filenamecn=strcat(srcFolder, 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
file=strcat('C:\Users\User\Documents\FYP\Matlab\bbbPVG\',srcFile(ck).name);
imwrite(I12,file,'WriteMode', 'append');
end
% figure;imshow(I12);
% figure;imshow(obj);
end
end

回答(1 个)

Jan
Jan 2022-6-12
编辑:Jan 2022-6-12
"'WriteMode' and 'append' when using imwrite function to save multiple images in specific folder" - this was a bad advice. Appending let you store multiple images in one file, not folder. This works e.g. for TIFF, HDF4 and GIF images, but not for BMPs.
The solution is easy: Omit the 'WriteMode' argument.
Good programming practice:
  • Use fullfile instead of strcat to concatenate file names.
  • This is inefficient:
I12=zeros(227,227);
I12=uint8(I12)
You create an array of zeros with 227*227 elements in double format, which requires 412 kB of RAM. The next line creates an UINT8 array of the same size using 51 kB and copies the converted values. The direct approach is faster and nicer:
I12=zeros(227, 227, 'uint8');
  • What is the purpose of this part:
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
? You have limited the size of obj to be a [227 x 227] image before already. So the elementwise copy to I12 is a complete waste of time. Faster and cleaner:
I12 = zeros(227, 227, 'uint8');
I12(1:m, 1:n, 1:t) = uint8(obj);

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by