Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash with zero map size #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/kcftracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ cv::Mat KCFTracker::createGaussianPeak(int sizey, int sizex)
return FFTTools::fftd(res);
}

static inline int round_2_size(int val, int sz)
{
return (val / sz) * sz + sz;
}

// Obtain sub-window from image, with replication-padding and extract features
cv::Mat KCFTracker::getFeatures(const cv::Mat & image, bool inithann, float scale_adjust)
{
Expand Down Expand Up @@ -378,8 +383,29 @@ cv::Mat KCFTracker::getFeatures(const cv::Mat & image, bool inithann, float scal

if (_hogfeatures) {
// Round to cell size and also make it even
_tmpl_sz.width = ( ( (int)(_tmpl_sz.width / (2 * cell_size)) ) * 2 * cell_size ) + cell_size*2;
_tmpl_sz.height = ( ( (int)(_tmpl_sz.height / (2 * cell_size)) ) * 2 * cell_size ) + cell_size*2;
int round_size = 2 * cell_size;

int round_w = round_2_size(_tmpl_sz.width, round_size);
int round_h = round_2_size(_tmpl_sz.height, round_size);

//
// XXX:
//
// This is workaround for crash caused by _tmpl_sz.width or _tmpl_sz.height be equal to 2*cell_size.
// Later in 'getFeatureMaps(&z_ipl, cell_size, &map)' dimensions of the 'CvLSVMFeatureMapCaskade *map' will be set to
// [_tmpl_sz.width / cell_size, _tmpl_sz.height / cell_size] and in 'normalizeAndTruncate(map, 0.2f)'
// dimensions of this map will be changed to map.sizeX -= 2 and map.sizeY -= 2, so if,
// for example, _tmpl_sz.height == 2*cell_size then final 'map' dimension will be map.sizeY == 0,
// that does not make sense and there will be crash in 'createHanningMats' during multiplacation of two matrixes.
//
if (round_w == round_size || round_h == round_size) {
round_size = 4 * cell_size;
round_w = round_2_size(_tmpl_sz.width, round_size);
round_h = round_2_size(_tmpl_sz.height, round_size);
}

_tmpl_sz.width = round_w;
_tmpl_sz.height = round_h;
}
else { //Make number of pixels even (helps with some logic involving half-dimensions)
_tmpl_sz.width = (_tmpl_sz.width / 2) * 2;
Expand Down