Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Offset calculation in traincascade

Hi,

offset is calculated as int offset = winSize.width + 1; in line 123 of https://github.com/Itseez/opencv/blob/master/apps/traincascade/haarfeatures.cpp file.

This offset is later used to access to image as a 1-D array as the step variable in https://github.com/Itseez/opencv/blob/master/apps/traincascade/traincascade_features.h file by multiplying with combinations of some parameters of the rect rectangle in CV_SUM_OFFSETS definition:

#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step )                  \
/* (x, y) */                                                          \
(p0) = (rect).x + (step) * (rect).y;                                  \
/* (x + w, y) */                                                      \
(p1) = (rect).x + (rect).width + (step) * (rect).y;                   \
/* (x + w, y) */                                                      \
(p2) = (rect).x + (step) * ((rect).y + (rect).height);                \
/* (x + w, y + h) */                                                  \
(p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);

what is the purpose of this +1 in int offset = winSize.width + 1;, why is it not int offset = winSize.width;?

Offset calculation in traincascadefor a Mat object (traincascade)

Hi,

offset is calculated as int offset = winSize.width + 1; in line 123 of https://github.com/Itseez/opencv/blob/master/apps/traincascade/haarfeatures.cpp file.

This offset is later used to access to image as a 1-D array as the step variable in https://github.com/Itseez/opencv/blob/master/apps/traincascade/traincascade_features.h file by multiplying with combinations of some parameters of the rect rectangle in CV_SUM_OFFSETS definition:

#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step )                  \
/* (x, y) */                                                          \
(p0) = (rect).x + (step) * (rect).y;                                  \
/* (x + w, y) */                                                      \
(p1) = (rect).x + (rect).width + (step) * (rect).y;                   \
/* (x + w, y) */                                                      \
(p2) = (rect).x + (step) * ((rect).y + (rect).height);                \
/* (x + w, y + h) */                                                  \
(p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);

what is the purpose of this +1 in int offset = winSize.width + 1;, why is it not int offset = winSize.width;?

Offset calculation for a Mat object (traincascade)

Hi,

offset is calculated as int offset = winSize.width + 1; in line 123 of https://github.com/Itseez/opencv/blob/master/apps/traincascade/haarfeatures.cpp file.

This offset is later used to access to image as a 1-D array as the step variable in https://github.com/Itseez/opencv/blob/master/apps/traincascade/traincascade_features.h file by multiplying with combinations of some parameters of the rect rectangle in CV_SUM_OFFSETS definition:

#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step )                  \
/* (x, y) */                                                          \
(p0) = (rect).x + (step) * (rect).y;                                  \
/* (x + w, y) */                                                      \
(p1) = (rect).x + (rect).width + (step) * (rect).y;                   \
/* (x + w, y) */                                                      \
(p2) = (rect).x + (step) * ((rect).y + (rect).height);                \
/* (x + w, y + h) */                                                  \
(p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);

These p values are later used in line 79 of https://github.com/Itseez/opencv/blob/master/apps/traincascade/haarfeatures.h file, to access locations in integral sum images (tilted or untilted one), like:

inline float CvHaarEvaluator::Feature::calc( const cv::Mat &_sum, const cv::Mat &_tilted, size_t y) const
{
    const int* img = tilted ? _tilted.ptr<int>((int)y) : _sum.ptr<int>((int)y);
    float ret = rect[0].weight * (img[fastRect[0].p0] - img[fastRect[0].p1] - img[fastRect[0].p2] + img[fastRect[0].p3] ) +
        rect[1].weight * (img[fastRect[1].p0] - img[fastRect[1].p1] - img[fastRect[1].p2] + img[fastRect[1].p3] );
    if( rect[2].weight != 0.0f )
        ret += rect[2].weight * (img[fastRect[2].p0] - img[fastRect[2].p1] - img[fastRect[2].p2] + img[fastRect[2].p3] );
    return ret;
}

what is the purpose of this +1 in int offset = winSize.width + 1;, why is it not int offset = winSize.width;?? By adding this one I suspect that we are calculating incorrect p values. An explanation on these p values is available at http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#integral

Offset calculation for a Mat object (traincascade)

Hi,

offset is calculated as int offset = winSize.width + 1; in line 123 of https://github.com/Itseez/opencv/blob/master/apps/traincascade/haarfeatures.cpp file.

This offset is later renamed as step and then step is used to access to image as a 1-D array as the step variable calculate some p values in https://github.com/Itseez/opencv/blob/master/apps/traincascade/traincascade_features.h file by multiplying with combinations of some parameters of the rect rectangle in CV_SUM_OFFSETS definition:

#define CV_SUM_OFFSETS( p0, p1, p2, p3, rect, step )                  \
/* (x, y) */                                                          \
(p0) = (rect).x + (step) * (rect).y;                                  \
/* (x + w, y) */                                                      \
(p1) = (rect).x + (rect).width + (step) * (rect).y;                   \
/* (x + w, y) */                                                      \
(p2) = (rect).x + (step) * ((rect).y + (rect).height);                \
/* (x + w, y + h) */                                                  \
(p3) = (rect).x + (rect).width + (step) * ((rect).y + (rect).height);

These p values are later used in line 79 of https://github.com/Itseez/opencv/blob/master/apps/traincascade/haarfeatures.h file, to access locations in integral sum images (tilted or untilted one), like:

inline float CvHaarEvaluator::Feature::calc( const cv::Mat &_sum, const cv::Mat &_tilted, size_t y) const
{
    const int* img = tilted ? _tilted.ptr<int>((int)y) : _sum.ptr<int>((int)y);
    float ret = rect[0].weight * (img[fastRect[0].p0] - img[fastRect[0].p1] - img[fastRect[0].p2] + img[fastRect[0].p3] ) +
        rect[1].weight * (img[fastRect[1].p0] - img[fastRect[1].p1] - img[fastRect[1].p2] + img[fastRect[1].p3] );
    if( rect[2].weight != 0.0f )
        ret += rect[2].weight * (img[fastRect[2].p0] - img[fastRect[2].p1] - img[fastRect[2].p2] + img[fastRect[2].p3] );
    return ret;
}

what is the purpose of this +1 in int offset = winSize.width + 1;, why is it not int offset = winSize.width;? By adding this one I suspect that we are calculating incorrect p values. An explanation on these p values is available at http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#integral