我编写代码,我希望如何在检测到面到人类的头部和颈部之后展开矩形
#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;
}
答案 0 :(得分:14)
要展开OpenCV面部矩形,您还可以使用以下运算符(1)到cv::Rect
类
//expanding or shrinking a rectangle by a certain amount
cv::Rect += cv::Size
//shifting a rectangle by a certain offset
cv::Rect -= cv::Point
将面部矩形扩展10%的示例将是
cv::Size deltaSize( faces[i].width * 0.1f, faces[i].height * 0.1f ); // 0.1f = 10/100
cv::Point offset( deltaSize.width/2, deltaSize.height/2);
faces[i] += deltaSize;
faces[i] -= offset;
答案 1 :(得分:7)
您需要调整高度并适当地开始y坐标。
简而言之,我觉得盒子的高度应该在两侧(顶部和底部)增加0.3倍。所以我从0.3*h
中减去y
并将0.3*2*h
(即0.6 * h)添加到h
。
这只是我的粗略计算。对于微调,您可以针对不同的面进行分析,并提出最适合您目的的测量。
以下是更改后的代码:
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%
以下是我得到的结果。绿色显示原始我得到。蓝色是修改过的:
请记住,您想要选择多少,取决于您。调整以上值以获得所需的结果。