# 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... file.

This offset is renamed as step and then step is used to calculate some p values in https://github.com/Itseez/opencv/blob... 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... 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.weight * (img[fastRect.p0] - img[fastRect.p1] - img[fastRect.p2] + img[fastRect.p3] ) +
rect.weight * (img[fastRect.p0] - img[fastRect.p1] - img[fastRect.p2] + img[fastRect.p3] );
if( rect.weight != 0.0f )
ret += rect.weight * (img[fastRect.p0] - img[fastRect.p1] - img[fastRect.p2] + img[fastRect.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/imgpro...

edit retag close merge delete

Sort by » oldest newest most voted This is due to cv::integral(). It creates output of (rows + 1, cols + 1) size (adds extra 0 row and column), so the stride between rows of integral sum images grows to width + 1.

more

Official site

GitHub

Wiki

Documentation