怎么修改代码可以循环保存在原图上画直线的图片?请大家指点一下!

clear
clc
%%%%一系列的处理
。。。。。
%提取线段信息
lines=houghlines(I1,T1,R1,Peaks,'fillgap',20,'minlength',90);
figure,imshow(I),hold on %I是原图
%绘制线段
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
end

[H2,T2,R2] = hough(I1,'Theta',-25:0.01:-20);
%求极值点
Peaks=houghpeaks(H2,1,'threshold',ceil(0.5*max(H2(:))));

%得到线段信息
lines=houghlines(I1,T2,R2,Peaks,'fillgap',20,'minlength',280);

%绘制线段
for k=1:length(lines)
    xy=[lines(k).point1;lines(k).point2];
    plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
end
hold off
f=getframe(gcf);
imwrite(f.cdata,[str2,num2str(k),'.jpg']);
close

end

目前循环能够实现,但是保存时只能保存当前窗口的一张,达不到循环保存的作用。

%绘制线段
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',1,'Color','green');
f=getframe(gcf);
imwrite(f.cdata,[str2,num2str(k),'.jpg']);
end
hold off
close