0

我正在尝试做一些面部识别,因为我目前正试图找出我想要实施的想法

我以前从来没有从一个文件夹中读取多张图像,我尽力而为,但遇到了一个我似乎无法工作的错误

myPath = 'C:\Users\Callum\Desktop\Msc Computer Science\CN7023\CW\Faces_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:\Users\Callum\Desktop\Msc Computer Science\CN7023\CW\Faces_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 个图像,我不想要完成后全部打开,如果可能的话,我希望它们保存到某个文件夹或工作区

4

2 回答 2

0

打印“fileName{k}”以验证您是否获得了所需的文件路径。其次,不要在循环内更新文件名,您需要在循环内使用不同的变量。

于 2018-03-25T15:03:07.727 回答
0

如果您想对每张图像进行分析,您必须将您的逻辑放在循环本身中,并将它们的结果收集到一个单元数组或跨行的东西中。例如,您还可以简化文件夹搜索:

files = dir('C:\Users\Callum\Desktop\Msc Computer Science\CN7023\CW\Faces_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:\Users\Callum\Desktop\Msc Computer Science\CN7023\CW\Faces_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
于 2018-03-25T18:12:01.610 回答