更改边界框尺寸

DrP*_*ple 1 matlab

刚接触 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)

目前,该脚本找到的面孔如下所示: 在此输入图像描述

并输出如下图像: 在此输入图像描述

这很好,我只是想扩展裁剪区域的边界以捕捉更多的脸部(例如头发和下巴)。

jod*_*dag 6

来自 MATLAB 矩形函数文档。

  • 矩形('Position',pos) 在二维坐标中创建一个矩形。将 pos 指定为以数据单位表示的 [xywh] 形式的四元素向量。x 和 y 元素确定位置,w 和 h 元素确定大小。该函数绘制到当前轴中,而不清除轴中的现有内容。

如果您只是想按矩形中心的某个比例因子增加边界框,则可以缩放 和w组件hBB调整矩形原点xy减去一半比例差。如果将以下代码放在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)