Matlab 更改边界框尺寸
这里是Matlab新手。我正在尝试实现一些代码来检测图像中的人脸并对其进行裁剪。我运行了脚本,但是它在检测到的人脸周围放置的边界框有点小。是否有任何方法可以更改边界框的尺寸以捕获更多的面Matlab 更改边界框尺寸,matlab,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),i
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矩形函数文档
- 矩形('Position',pos)在二维坐标中创建一个矩形。 将pos指定为数据中形式为[x y w h]的四元素向量 单位。x和y元素确定位置以及w和h 元素决定大小。该函数将绘制到当前轴中 不清除轴上的现有内容
BB
中缩放w
和h
组件,并通过减去一半的比例差来调整矩形原点x
和y
。如果您将以下代码放在BB=步骤(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;
来自MATLAB矩形函数文档
- 矩形('Position',pos)在二维坐标中创建一个矩形。
将pos指定为数据中形式为[x y w h]的四元素向量
单位。x和y元素确定位置以及w和h
元素决定大小。该函数将绘制到当前轴中
不清除轴上的现有内容
如果您只是想在矩形中心附近增加边界框的某个比例因子,则可以在BB
中缩放w
和h
组件,并通过减去一半的比例差来调整矩形原点x
和y
。如果您将以下代码放在BB=步骤(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;
您可以使用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
您可以使用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