How to save images in a folder?

1 次查看(过去 30 天)
Isha Pandya
Isha Pandya 2016-12-14
回答: KSSV 2016-12-15
I want to read 2 images, do something to those images and then save those two images in a folder. I have used for loop. I am able to read both the images but only first image is getting saved in the folder. Following is my code. What changes should be done in order to save both the images in a folder.
c=cell(1,2);
for m=1:2
I=imread(sprintf('isha%d.jpg',m));
%To detect Mouth
MouthDetect = vision.CascadeObjectDetector('Mouth','MergeThreshold',120);
BB=step(MouthDetect,I);
figure,
imshow(I); hold on
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
x=imcrop(I,BB);
y= imcomplement(x);
figure, imshow(y);
imwrite(y,strcat('D:\implementation\featurescrop\mouth__',num2str(i),'.jpg'));
hold off;
%To detect Eyes
EyeDetect = vision.CascadeObjectDetector('EyePairBig');
BB=step(EyeDetect,I);
hold on
rectangle('Position',BB,'LineWidth',4,'LineStyle','-','EdgeColor','b')
x=imcrop(I,BB);
y= imcomplement(x);
figure, imshow(y);
imwrite(y,strcat('D:\implementation\featurescrop\eyes__',num2str(i),'.jpg'));
hold off;
%to detect nose
NoseDetect = vision.CascadeObjectDetector('Nose','MergeThreshold',85);
BB=step(NoseDetect,I);
figure,
imshow(I); hold on
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','b');
end
title('Nose Detection');
hold off;
x=imcrop(I,BB);
y= imcomplement(x);
figure, imshow(y);
imwrite(y,strcat('D:\implementation\featurescrop\nose__',num2str(i),'.jpg'));
hold off;
end
  4 个评论
Isha Pandya
Isha Pandya 2016-12-14
while debugging it says value of BB is [168,253,79,48] and min=48, max=253.
Ganesh Hegade
Ganesh Hegade 2016-12-14
编辑:Ganesh Hegade 2016-12-14
size command always return 1x2 double data. for example if
BB = [168,253,79,48];
>> size(BB)
ans =
1 4
size(BB,1) gives only the first value of the output.
BB = [168,253,79,48];
>> size(BB,1)
ans =
1
So in your code loop is running for only first element.

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2016-12-15
As the size(BB,1) is 1, your loop runs only for once....As you said you are reading two images....change loop to:
for i = 1:2
%%%%
end

Community Treasure Hunt

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

Start Hunting!

Translated by