把检测出的人脸用椭圆和矩形框画出

来源:互联网 发布:工业数据采集的重要性 编辑:程序博客网 时间:2024/04/30 00:26

把检测出的人脸用椭圆画出。使用opencv中自带的haarcascades

<pre name="code" class="cpp"><pre name="code" class="cpp">#include "opencv2/core/core.hpp" #include "opencv2/objdetect/objdetect.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <iostream> #include <stdio.h> using namespace std;using namespace cv;string face_cascade_name = "D:\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml";//该文件存在于OpenCV安装目录下的\sources\data\haarcascades内,需要将该xml文件复制到当前工程目录下CascadeClassifier face_cascade;void detectAndDisplay(Mat frame);int main(int argc, char** argv){Mat image;image = imread("E:\\Work\\libfacedetection-master\\8.jpg", 1);  //当前工程的image目录下的mm.jpg文件,注意目录符号if (!face_cascade.load(face_cascade_name)){printf("级联分类器错误,可能未找到文件,拷贝该文件到工程目录下!\n");return -1;}detectAndDisplay(image); //调用人脸检测函数waitKey(0);//暂停显示一下。}void detectAndDisplay(Mat face){std::vector<Rect> faces;Mat face_gray;cvtColor(face, face_gray, CV_BGR2GRAY);  //rgb类型转换为灰度类型equalizeHist(face_gray, face_gray);   //直方图均衡化face_cascade.detectMultiScale(face_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(1, 1));//用椭圆框画出/*for (int i = 0; i < faces.size(); i++){Point center(faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5);ellipse(face, center, Size(faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar(255, 0, 0), 2, 7, 0);}*///用矩形框画出for (int i = 0; i < faces.size(); ++i){rectangle(face, Rect(faces[i].x, faces[i].y, faces[i].width, faces[i].height), Scalar(0, 0, 255), 3);}namedWindow("人脸检测", CV_WINDOW_NORMAL);imshow("人脸检测", face);}


 

Ellipse

绘制椭圆圆弧和椭圆扇形。

void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle,                double start_angle, double end_angle, CvScalar color,                int thickness=1, int line_type=8, int shift=0 );

C++: void ellipse(Mat&img, Pointcenter, Size axes, doubleangle, doublestartAngle, double endAngle, const Scalar&color, intthickness=1, int lineType=8, intshift=0)

C++: void ellipse(Mat&img, const RotatedRect&box, const Scalar& color, int thickness=1, intlineType=8)

img
图像。
center
椭圆圆心坐标。
axes
轴的长度。
angle
偏转的角度。
start_angle
圆弧起始角的角度。.
end_angle
圆弧终结角的角度。
color
线条的颜色。
thickness
线条的粗细程度。
line_type
线条的类型,见CVLINE的描述。
shift
圆心坐标点和数轴的精度。


rectangle

绘制矩形框

C:       void cvRectangle(CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 )

C++: void rectangle(Mat& img, Rect rec, const Scalar& color, int thickness=1, int lineType=8, int shift=0 )

参数介绍:

img
图像.
pt1
矩形的一个顶点。
pt2
矩形对角线上的另一个顶点
color
线条颜色 (RGB) 或亮度(灰度图像 )(grayscale image)。
thickness
组成矩形的线条的粗细程度。取负值时(如 CV_FILLED)函数绘制填充了色彩的矩形。
line_type
线条的类型。见cvLine的描述
shift
坐标点的小数点位数。

1 0
原创粉丝点击