【问题标题】:Changing Bounding Box Dimensions更改边界框尺寸
【发布时间】:2021-04-13 07:07:08
【问题描述】:

这里是 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

目前,脚本会找到这样的面孔:

并输出如下图像:

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

【问题讨论】:

    标签: matlab


    【解决方案1】:

    来自 MATLAB 矩形函数文档。

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

    如果您只是想通过矩形中心的某个比例因子来增加边界框,您可以缩放BB 中的w 和h 组件并调整矩形原点x 和@ 987654325@ 减去比例差的一半。如果您将以下代码放在代码中的 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;
    

    【讨论】:

      【解决方案2】:

      您可以使用 link 和 bboxresize 中描述的 Matlab 中的 imresize 函数来调整边界框的大小 以下是将图像大小调整为原始图像 3 倍的简单代码

      %% clean workspace
      clc;
      clear;
      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);
       BB2 = BB;
       %% Scale the rectangle to 3 times its original size
       scale = 3;
       %% Resize image
       ImgResized = imresize(img,scale);
       %% Resize bound box using the function named bboxresize in Matlab
       BBResized = bboxresize(BB,scale);
       figure(2),imshow(ImgResized);
       %% Draw Bounding Box
        for i=1:size(BBResized,1)
          rectangle('position',BBResized(i,:),'lineWidth',2,'LineStyle','- ','EdgeColor','y');
         end
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-26
        • 2017-08-20
        • 1970-01-01
        • 2017-09-30
        • 2011-01-09
        • 2021-11-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多