MATLAB有助于读取多个文件图像并将最终图像保存到目录



我试图做一些面部识别,因为我当前试图找出我想提出的想法

ive以前从来不必从文件夹中读取多个图像,我给了我最好的镜头,但遇到了一个错误,我似乎无法工作

myPath = 'C:UsersCallumDesktopMsc Computer ScienceCN7023CWFaces_easy';
a = dir(fullfile(myPath, '*.jpg'));
fileName = arrayfun( @(x) fullfile( myPath, x.name ), a, 'UniformOutput', false );
for k = 1:length(fileName)
    I = imread(fileName{k});
end

// Code to add in
    files = dir('C:UsersCallumDesktopMsc Computer ScienceCN7023CWFaces_easy*.jpg');
    files_len = numel(files);
    result = cell(files_len,2);
    for ii = 1:files_len
        file = fullfile(files(ii).folder,files(ii).name);
        img = imread(file);
        result(ii,:) = {file perform_analysis(img)}; % or whatever your function is called
    end
//
faceDetect = vision.CascadeObjectDetector();
bbox=step(faceDetect,I);
face=imcrop(I,bbox);
centerx=size(face,1)/2+bbox(1);
centery=size(face,2)/2+bbox(2);
eyeDetect = vision.CascadeObjectDetector('RightEye');
eyebox=step(eyeDetect,face);
n=size(eyebox,1);
e=[];
for it=1:n
    for j=1:n
        if (j > it)
          if ((abs(eyebox(j,2)-eyebox(it,2))<68)&& (abs(eyebox(j,1)-eyebox(it,1))>40))
            e(1,:)=eyebox(it,:);
            e(2,:)=eyebox(j,:);
            d=1;break;
          end
        end
    end
    if(d == 1)
        break;
    end
end
eyebox(1,:)=e(1,:);
eyebox(2,:)=e(2,:);
c=eyebox(1,3)/2;
d=eyebox(1,4)/2;
eyeCenter1x=eyebox(1,1)+c+bbox(1);
eyeCenter1y=eyebox(1,2)+d+bbox(2);
e=eyebox(2,3)/2;
f=eyebox(2,4)/2;
eyeCenter2x=eyebox(2,1)+e+bbox(1);
eyeCenter2y=eyebox(2,2)+f+bbox(2);
ndetect=vision.CascadeObjectDetector('Nose','MergeThreshold',16);
nosebox=step(ndetect,face);
noseCenterx=nosebox(1,1)+(nosebox(1,3)/2)+bbox(1);
noseCentery=nosebox(1,2)+(nosebox(1,4)/2);
m=[1,noseCentery,size(face,1),((size(face,2))-noseCentery)];
mouth=imcrop(face,m);
mdetect=vision.CascadeObjectDetector('Mouth','MergeThreshold' ,20);
mouthbox=step(mdetect,mouth);
for it=1:size(mouthbox,1)
    if(mouthbox(it,2)>20)
        mouthbox(1,:)=mouthbox(it,:);
        break;
    end
end
mouthbox(1,2)=mouthbox(1,2)+noseCentery;
noseCentery=noseCentery+bbox(2);
mouthCenterx=mouthbox(1,1)+(mouthbox(1,3)/2)+bbox(1);
mouthCentery=mouthbox(1,2)+(mouthbox(1,4)/2)+bbox(2);
shape=[centerx centery;eyeCenter1x eyeCenter1y;eyeCenter2x eyeCenter2y;noseCenterx noseCentery;mouthCenterx mouthCentery];
imshow(I);
hold on;
plot(shape(:,1),shape(:,2),'+','MarkerSize',10);
eyebox(1,1:2)=eyebox(1,1:2)+bbox(1,1:2);
eyebox(2,1:2)=eyebox(2,1:2)+bbox(1,1:2);
nosebox(1,1:2)=nosebox(1,1:2)+bbox(1,1:2);
mouthbox(1,1:2)=mouthbox(1,1:2)+bbox(1,1:2);
all_points=[eyebox(1,:);eyebox(2,:);nosebox(1,:);mouthbox(1,:)];
dpoints=size(all_points,1);
label=cell(dpoints,1);
i=1;
for i = 1: dpoints
label{i}= num2str(i);
end
videoout=insertObjectAnnotation(I,'rectangle',all_points,label,'TextBoxOpacity',0.3,'Fontsize',9);
imshow(videoout);hold on;plot(shape(:,1),shape(:,2),'+','MarkerSize',10);
dt=delaunayTriangulation(shape(:,1),shape(:,2));
imshow(videoout);hold on;triplot(dt);hold off

所以我将其更改为当前的功能,但仅在上一个图像上,我想在文件夹中的所有图像上进行操作...我在调用的文件夹中有435张图像,我不想全部打开一旦完成,我希望它们保存到某个文件夹或工作区,如果可能的话

打印" filename {k}"以验证是否获得了文件所需的路径。其次,请勿更新内部循环中的文件名,您需要在此处使用不同的变量。

如果要在每个图像上执行分析,则必须将逻辑放在环路本身中,并将其结果收集到一个单元格数或其他线上。例如,您还可以简化文件夹搜索:

files = dir('C:UsersCallumDesktopMsc Computer ScienceCN7023CWFaces_easy*.jpg');
files_len = numel(files);
result = cell(files_len,2);
for ii = 1:files_len
    file = fullfile(files(ii).folder,files(ii).name);
    img = imread(file);
    result(ii,:) = {file perform_analysis(img)}; % or whatever your function is called
end

完成此操作后,result变量的每一行都将包含第一列中的文件名和第二列中的分析结果。

编辑

files = dir('C:UsersCallumDesktopMsc Computer ScienceCN7023CWFaces_easy*.jpg');
files_len = numel(files);
for ii = 1:files_len
    file = fullfile(files(ii).folder,files(ii).name);
    I = imread(file);
    faceDetect = vision.CascadeObjectDetector();
    bbox=step(faceDetect,I);
    face=imcrop(I,bbox);
    centerx=size(face,1)/2+bbox(1);
    centery=size(face,2)/2+bbox(2);
    eyeDetect = vision.CascadeObjectDetector('RightEye');
    eyebox=step(eyeDetect,face);
    n=size(eyebox,1);
    e=[];
    for it=1:n
    for j=1:n
        if (j > it)
          if ((abs(eyebox(j,2)-eyebox(it,2))<68)&& (abs(eyebox(j,1)-eyebox(it,1))>40))
        e(1,:)=eyebox(it,:);
        e(2,:)=eyebox(j,:);
        d=1;break;
          end
        end
    end
    if(d == 1)
        break;
    end
    end
    eyebox(1,:)=e(1,:);
    eyebox(2,:)=e(2,:);
    c=eyebox(1,3)/2;
    d=eyebox(1,4)/2;
    eyeCenter1x=eyebox(1,1)+c+bbox(1);
    eyeCenter1y=eyebox(1,2)+d+bbox(2);
    e=eyebox(2,3)/2;
    f=eyebox(2,4)/2;
    eyeCenter2x=eyebox(2,1)+e+bbox(1);
    eyeCenter2y=eyebox(2,2)+f+bbox(2);
    ndetect=vision.CascadeObjectDetector('Nose','MergeThreshold',16);
    nosebox=step(ndetect,face);
    noseCenterx=nosebox(1,1)+(nosebox(1,3)/2)+bbox(1);
    noseCentery=nosebox(1,2)+(nosebox(1,4)/2);
    m=[1,noseCentery,size(face,1),((size(face,2))-noseCentery)];
    mouth=imcrop(face,m);
    mdetect=vision.CascadeObjectDetector('Mouth','MergeThreshold' ,20);
    mouthbox=step(mdetect,mouth);
    for it=1:size(mouthbox,1)
    if(mouthbox(it,2)>20)
        mouthbox(1,:)=mouthbox(it,:);
        break;
    end
    end
    mouthbox(1,2)=mouthbox(1,2)+noseCentery;
    noseCentery=noseCentery+bbox(2);
    mouthCenterx=mouthbox(1,1)+(mouthbox(1,3)/2)+bbox(1);
    mouthCentery=mouthbox(1,2)+(mouthbox(1,4)/2)+bbox(2);
    shape=[centerx centery;eyeCenter1x eyeCenter1y;eyeCenter2x eyeCenter2y;noseCenterx noseCentery;mouthCenterx mouthCentery];
    imshow(I);
    hold on;
    plot(shape(:,1),shape(:,2),'+','MarkerSize',10);
    eyebox(1,1:2)=eyebox(1,1:2)+bbox(1,1:2);
    eyebox(2,1:2)=eyebox(2,1:2)+bbox(1,1:2);
    nosebox(1,1:2)=nosebox(1,1:2)+bbox(1,1:2);
    mouthbox(1,1:2)=mouthbox(1,1:2)+bbox(1,1:2);
    all_points=[eyebox(1,:);eyebox(2,:);nosebox(1,:);mouthbox(1,:)];
    dpoints=size(all_points,1);
    label=cell(dpoints,1);
    i=1;
    for i = 1: dpoints
    label{i}= num2str(i);
    end
    videoout=insertObjectAnnotation(I,'rectangle',all_points,label,'TextBoxOpacity',0.3,'Fontsize',9);
    imshow(videoout);hold on;plot(shape(:,1),shape(:,2),'+','MarkerSize',10);
    dt=delaunayTriangulation(shape(:,1),shape(:,2));
    imshow(videoout);hold on;triplot(dt);hold off
end

最新更新