检测到人脸opencv后如何扩展矩形 [英] how expand rectangle after detected faces opencv
问题描述
我写代码,我想如何将检测到人脸后的矩形扩展到到人的头部和颈部
I write code and i want How to expand the rectangle after the detection of the faces to up to the head and neck of human
#include <opencv2/imgproc/imgproc.hpp> //this code detetced faces ///
#include <opencv2/objdetect/objdetect.hpp>///////////
#include <opencv2/highgui/highgui.hpp>
////////////////////
using namespace cv;
using namespace std;
int main()
{
CascadeClassifier cascade;
if (!cascade.load("haarcascade_frontalface_alt2.xml")) //load harcascade xml
return -1;
Mat src = imread("11.jpg"); //read image
if (src.empty())
return -1;
cv::resize(src,src,cv::Size(600,600)); resize image
Mat gray;
cvtColor(src, gray, CV_BGR2GRAY);
equalizeHist(gray, gray);
vector<Rect> faces;
cascade.detectMultiScale(gray, faces, 1.2, 3,0,Size(30,30));
for (size_t i = 0; i < faces.size(); i++)
{
/////////////////////////////
Rect r = faces[i];
//////////////////////////////
Mat faceROI = gray( faces[i] );
int x = faces[i].x;
int y = faces[i].y;
int h =0.3*y+faces[i].height;
int w = faces[i].width;
printf("Orig dimensions after h * w crop 1: %dx%d\n",h,w);
printf("Orig dimensions after x* y crop 2: %dx%d\n",x,y);
rectangle(src,Point (x,y),Point(x + w,y +h),Scalar(255,0,255),1,4,0);
imshow("mmmmmmmmmmm.jpg",src ); //show image in windows
}
///////////////////////////////
waitKey(0);
return 0;
}
推荐答案
需要适当调整高度和起始y坐标.
You need to adjust the height and starting y coordinate appropriately.
短时间看,我觉得盒子的高度应该在两侧(顶部和底部)增加 0.3 倍.所以我从 y
中减去 0.3*h
并将 0.3*2*h
(即 0.6*h)添加到 h
>.
On a short look, I felt height of box should be increases by a factor of 0.3 on both the sides (top and bottom). So I subtracted 0.3*h
from y
and added 0.3*2*h
(i.e. 0.6*h) to h
.
这只是我粗略的计算.要进行微调,您可以针对不同的面孔对其进行分析,然后得出最适合您目的的测量值.
It is just my rough calculation. For fine adjustment, you analyze it for different faces and come up with a measurement that best suits your purpose.
这是更改后的代码:
int x = faces[i].x;
int h_temp = faces[i].height; // storing original height
int y = faces[i].y - h_temp*0.3; // y is reduced by 0.3*h
int w = faces[i].width;
int h = h_temp*1.6; // height is increases by 60%
下面是我得到的结果.绿色显示我得到的原件.蓝色是修改后的:
And below is the result I got for example. Green shows original I got. Blue is the modified:
请记住,您要选择多少取决于您.对以上值进行调整以获得所需的结果.
Remember, how much you want to select, depends on you. Make adjustments to above values to get your required result.
这篇关于检测到人脸opencv后如何扩展矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!