刚接触 Matlab。我正在尝试实现一些代码来检测图像中的面部并裁剪它。我正在运行脚本,但它在检测到的脸部周围放置的边界框有点小。有没有办法改变边界框的尺寸以捕获更多的脸部?
clc;
% cd into the a folder with pictures
cd 'C:\Users\abc\Desktop\folder'
files = dir('*.jpg');
for file = files'
img = imread(file.name);
figure(1),imshow(img);
FaceDetect = vision.CascadeObjectDetector;
FaceDetect.MergeThreshold = 7;
BB = step(FaceDetect,img);
figure(2),imshow(img);
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',2,'LineStyle','- ','EdgeColor','r');
end
for i = 1:size(BB,1)
rectangle('Position',BB(i,:),'LineWidth',2,'LineStyle','- ','EdgeColor','r');
J = imcrop(img,BB(i,:));
figure(3);
imshow(J);
a = 'edited\'
b = file.name
output = strcat(a,b);
imwrite(J,output);
end
%Code End
end
Run Code Online (Sandbox Code Playgroud)
这很好,我只是想扩展裁剪区域的边界以捕捉更多的脸部(例如头发和下巴)。
来自 MATLAB 矩形函数文档。
如果您只是想按矩形中心的某个比例因子增加边界框,则可以缩放 和w
组件h
并BB
调整矩形原点x
并y
减去一半比例差。如果将以下代码放在BB = step(FaceDetect,img);
代码中该行的后面,则它应该可以工作。我目前没有可用的 MATLAB,但我很确定这会起作用。
% Scale the rectangle to 1.2 times its original size
scale = 1.2;
% Adjust the lower left corner of the rectangles
BB(:,1:2) = BB(:,1:2) - BB(:,3:4)*0.5*(scale - 1)
% Adjust the width and height of the rectangles
BB(:,3:4) = BB(:,3:4)*scale;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1826 次 |
最近记录: |