Opencv中的vector

来源:互联网 发布:linux c 获取ntp时间 编辑:程序博客网 时间:2024/06/13 07:38
1.vec_Rect的定义
typedef vector<cv::Rect> vec_Rect;


2.程序中对vector的使用

vec_Rect feats(MAX_NFACES_IN_IMG);cascade->detectMultiScale(roi, feats, scale_factor, min_neighbors, flags,                              cvSize(minwidth_pixels, minwidth_pixels));static CvScalar color={{0,255,255}};//CvPoint point1,point2;point1.x=feats[0].x;point1.y=feats[0].y;point2.x=feats[0].x+feats[0].width;point2.y=feats[0].y+feats[0].height;cvRectangle(roi,point1,point2,color,3,8,0);//cvShowImage("try",roi);

3.一开始,对vector对象feats的调用,使用的是下面的代码:

point1.x=feats.x;point1.y=feats.y;point2.x=feats.x+feats.width;point2.y=feats.y+feats.height;

结果出现下面的错误:

1>e:\c++\copy_keypoint1\keypoint\stasm\misc.cpp(728) : error C2039: “x”: 不是“std::vector<_Ty>”的成员
1>        with
1>        [
1>            _Ty=cv::Rect
1>        ]
1>e:\c++\cop


查找C++书发现vector是用来创建数组对象的,并且创建数组对象的格式为:

vector<元素类型>数组对象名(数组长度)

也就是说,

vec_Rect feats(MAX_NFACES_IN_IMG);
可替换成
vector<cv::Rect> feats(MAX_NFACES_IN_IMG);
也就是说,我们创建了一个名为feats的,大小为MAX_NFACES_IN_IMG的数组,数组中每一个元素并不是整型或double型,而是Rect型。我们在调试过程城中,也可以看到feats中的元素概况:


即名为feats的数组中,一共有一个元素,该元素名为feats[0],是一个Rect类型的对象,我们可以通过

feats[0].xfeats[0].yfeats[0].weightfeats[0].height

来访问该Rect类对象的左上角坐标和宽、高。


4.再举一个在stasm中关于Detpar_的例子

定义:

    vector<DetPar>  detpars_;     // all the valid faces in the current image

struct DetPar // the structure describing a face detection{    double x, y;           // center of detector shape    double width, height;  // width and height of detector shape    double lex, ley;       // center of left eye, left and right are wrt the viewer    double rex, rey;       // ditto for right eye    double mouthx, mouthy; // center of mouth    double rot;            // in-plane rotation    double yaw;            // yaw    EYAW   eyaw;           // yaw as an enum    DetPar() // constructor sets all fields to INVALID        : x(INVALID),          y(INVALID),          width(INVALID),          height(INVALID),          lex(INVALID),          ley(INVALID),          rex(INVALID),          rey(INVALID),          mouthx(INVALID),          mouthy(INVALID),          rot(INVALID),          yaw(INVALID),          eyaw(EYAW(INVALID))    {    };

在程序中,我们假设通过检测,获得图像中4个人脸位置的中间点坐标和长宽,并把它们保存在depar_命名的数组中。则depar_[0]~depar_[3]分别存储着四个人脸位置的信息,每个人连信息用DePar类型的对象表示,即我们可理解为如下形式:
DetPar depar_[0],depar_[1],depar_[2],depar_[3];

detpar[0]~depar[3]均为DetPar类的对象。

我们可以通过如下程序加深理解:

sort(detpars_.begin(), detpars_.end(), DecreasingWidth);//对数组元素进行排列
detpars_.resize(1);//设置数组中元素个数。这里的意思是,取第一个元素,而把其他的元素都删除。

0 0
原创粉丝点击