Skip to content

Commit e076afe

Browse files
committed
Fix CodeQL warnings
1 parent a322489 commit e076afe

7 files changed

Lines changed: 59 additions & 66 deletions

File tree

src/Detector/Subsense/BackgroundSubtractorSuBSENSE.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ void BackgroundSubtractorSuBSENSE::operator()(cv::InputArray _image, cv::OutputA
297297
CV_Assert(oInputImg.isContinuous());
298298
_fgmask.create(m_oImgSize,CV_8UC1);
299299
cv::Mat oCurrFGMask = _fgmask.getMat();
300-
memset(oCurrFGMask.data,0,oCurrFGMask.cols*oCurrFGMask.rows);
300+
memset(oCurrFGMask.data,0, static_cast<size_t>(oCurrFGMask.cols) * static_cast<size_t>(oCurrFGMask.rows));
301301
size_t nNonZeroDescCount = 0;
302302
const float fRollAvgFactor_LT = 1.0f/std::min(++m_nFrameIndex,m_nSamplesForMovingAvgs);
303303
const float fRollAvgFactor_ST = 1.0f/std::min(m_nFrameIndex,m_nSamplesForMovingAvgs/4);
@@ -716,8 +716,8 @@ void BackgroundSubtractorSuBSENSE::getBackgroundImage(cv::OutputArray background
716716
oAvgBGImg.convertTo(backgroundImage,CV_8U);
717717
}
718718

719-
void BackgroundSubtractorSuBSENSE::getBackgroundDescriptorsImage(cv::OutputArray backgroundDescImage) const {
720-
CV_Assert(LBSP::DESC_SIZE==2);
719+
void BackgroundSubtractorSuBSENSE::getBackgroundDescriptorsImage(cv::OutputArray backgroundDescImage) const
720+
{
721721
CV_Assert(m_bInitialized);
722722
cv::Mat oAvgBGDesc = cv::Mat::zeros(m_oImgSize,CV_32FC((int)m_nImgChannels));
723723
for(size_t n=0; n<m_voBGDescSamples.size(); ++n) {

src/Detector/Subsense/LBSP.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ class LBSP : public cv::DescriptorExtractor {
102102
//! utility function, used to filter out bad pixels in a ROI that would trigger out of bounds error because they're too close to the image border
103103
static void validateROI(cv::Mat& oROI);
104104
//! utility, specifies the pixel size of the pattern used (width and height)
105-
static const size_t PATCH_SIZE = 5;
105+
static constexpr size_t PATCH_SIZE = 5;
106106
//! utility, specifies the number of bytes per descriptor (should be the same as calling 'descriptorSize()')
107-
static const size_t DESC_SIZE = 2;
107+
static constexpr size_t DESC_SIZE = 2;
108108

109109
protected:
110110
//! classic 'compute' implementation, based on the regular DescriptorExtractor::computeImpl arguments & expected output

src/Detector/pedestrians/c4-pedestrian-detector.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ double UseSVM_CD_FastEvaluationStructure(const char* modelfile, const int m, con
8787

8888
// Data
8989
cv::Mat mat(rows, cols, type);
90-
fs.read((char*)mat.data, CV_ELEM_SIZE(type) * rows * cols);
90+
fs.read((char*)mat.data, CV_ELEM_SIZE(type) * static_cast<size_t>(rows) * static_cast<size_t>(cols));
9191

9292
int num_dim = m;
9393

src/Detector/vibe_src/vibe.cpp

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,7 @@ namespace vibe
2121
}
2222

2323
///
24-
VIBE::~VIBE()
25-
{
26-
}
27-
28-
///
29-
cv::Vec2i VIBE::getRndNeighbor(int i, int j)
24+
cv::Vec<size_t, 2> VIBE::getRndNeighbor(int i, int j)
3025
{
3126
int neighbor_count = (m_pixelNeighbor * 2 + 1) * (m_pixelNeighbor * 2 + 1);
3227
int rnd = m_rng[m_rngIdx = (m_rngIdx + 1) % RANDOM_BUFFER_SIZE] % neighbor_count;
@@ -47,24 +42,27 @@ namespace vibe
4742

4843
m_size = img.size();
4944

50-
m_model.resize(m_channels * m_samples * m_size.width * m_size.height, 0);
45+
const size_t imWidth = static_cast<size_t>(m_size.width);
46+
const size_t imHeight = static_cast<size_t>(m_size.height);
47+
48+
m_model.resize(m_channels * m_samples * imWidth * imHeight, 0);
5149

5250
m_mask = cv::Mat(m_size, CV_8UC1, cv::Scalar::all(0));
5351

5452
const uchar* image = img.data;
55-
for (int i = 0; i < img.rows; i++)
53+
for (size_t i = 0; i < imHeight; ++i)
5654
{
57-
for (int j = 0; j < img.cols; j++)
55+
for (size_t j = 0; j < imWidth; j++)
5856
{
5957
for (int c = 0; c < m_channels; c++)
6058
{
61-
m_model[m_channels * m_samples * m_size.width * i + m_channels * m_samples * j + c] = image[m_channels * m_size.width * i + m_channels * j + c];
59+
m_model[m_channels * m_samples * imWidth * i + m_channels * m_samples * j + c] = image[m_channels * imWidth * i + m_channels * j + c];
6260
}
6361
for (int s = 1; s < m_samples; s++)
6462
{
65-
cv::Vec2i rnd_pos = getRndNeighbor(i, j);
66-
int img_idx = m_channels * m_size.width * rnd_pos[0] + m_channels * rnd_pos[1];
67-
int model_idx = m_channels * m_samples * m_size.width * i + m_channels * m_samples * j + m_channels * s;
63+
cv::Vec<size_t, 2> rnd_pos = getRndNeighbor(static_cast<int>(i), static_cast<int>(j));
64+
size_t img_idx = m_channels * imWidth * rnd_pos[0] + m_channels * rnd_pos[1];
65+
size_t model_idx = m_channels * m_samples * imWidth * i + m_channels * m_samples * j + m_channels * s;
6866
for (int c = 0; c < m_channels; c++)
6967
{
7068
m_model[model_idx + c] = image[img_idx + c];

src/Detector/vibe_src/vibe.hpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
#include <opencv2/core/core.hpp>
55
#include <memory>
66

7-
#define RANDOM_BUFFER_SIZE (65535)
8-
97
namespace vibe
108
{
9+
constexpr int RANDOM_BUFFER_SIZE = 65535;
10+
1111

1212
class VIBE
1313
{
1414
public:
1515
VIBE(int channels, int samples, int pixel_neighbor, int distance_threshold, int matching_threshold, int update_factor);
16-
~VIBE();
16+
~VIBE() = default;
1717

1818
void update(const cv::Mat& img);
1919
cv::Mat& getMask();
@@ -42,10 +42,9 @@ class VIBE
4242
unsigned int m_rng[RANDOM_BUFFER_SIZE];
4343
int m_rngIdx = 0;
4444

45-
cv::Vec2i getRndNeighbor(int i, int j);
45+
cv::Vec<size_t, 2> getRndNeighbor(int i, int j);
4646
void init(const cv::Mat& img);
4747
};
48-
4948
}
5049

5150
#endif /*__VIBE_HPP__*/

src/Tracker/staple/fhog.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,22 @@ float* acosTable() {
7474

7575
// compute gradient magnitude and orientation at each location (uses sse)
7676
void gradMag( float *I, float *M, float *O, int h, int w, int d, bool full ) {
77-
int x, y, y1, c, h4, s; float *Gx, *Gy, *M2; __m128 *_Gx, *_Gy, *_M2, _m;
77+
int y;
78+
__m128 *_Gx, *_Gy, *_M2, _m;
7879
float *acost = acosTable(), acMult=10000.0f;
7980
// allocate memory for storing one column of output (padded so h4%4==0)
80-
h4=(h%4==0) ? h : h-(h%4)+4; s=d*h4*sizeof(float);
81-
M2=(float*) alMalloc(s,16); _M2=(__m128*) M2;
82-
Gx=(float*) alMalloc(s,16); _Gx=(__m128*) Gx;
83-
Gy=(float*) alMalloc(s,16); _Gy=(__m128*) Gy;
81+
int h4=(h%4==0) ? h : h-(h%4)+4;
82+
int s = static_cast<size_t>(d) * static_cast<size_t>(h4) * sizeof(float);
83+
float* M2=(float*) alMalloc(s,16); _M2=(__m128*) M2;
84+
float* Gx=(float*) alMalloc(s,16); _Gx=(__m128*) Gx;
85+
float* Gy=(float*) alMalloc(s,16); _Gy=(__m128*) Gy;
8486
// compute gradient magnitude and orientation for each column
85-
for( x=0; x<w; x++ ) {
87+
for(int x=0; x<w; x++ ) {
8688
// compute gradients (Gx, Gy) with maximum squared magnitude (M2)
87-
for(c=0; c<d; c++) {
89+
for(int c=0; c<d; c++) {
8890
grad1( I+x*h+c*w*h, Gx+c*h4, Gy+c*h4, h, w, x );
8991
for( y=0; y<h4/4; y++ ) {
90-
y1=h4/4*c+y;
92+
int y1=h4/4*c+y;
9193
_M2[y1]=sse::ADD(sse::MUL(_Gx[y1],_Gx[y1]),sse::MUL(_Gy[y1],_Gy[y1]));
9294
if( c==0 ) continue; _m = sse::CMPGT( _M2[y1], _M2[y] );
9395
_M2[y] = sse::OR( sse::AND(_m,_M2[y1]), sse::ANDNOT(_m,_M2[y]) );
@@ -106,7 +108,7 @@ void gradMag( float *I, float *M, float *O, int h, int w, int d, bool full ) {
106108
// compute and store gradient orientation (O) via table lookup
107109
if( O!=0 ) for( y=0; y<h; y++ ) O[x*h+y] = acost[(int)Gx[y]];
108110
if( O!=0 && full ) {
109-
y1=((~size_t(O+x*h)+1)&15)/4; y=0;
111+
int y1=((~size_t(O+x*h)+1)&15)/4; y=0;
110112
for( ; y<y1; y++ ) O[y+x*h]+=(Gy[y]<0)*PI;
111113
for( ; y<h-4; y+=4 ) sse::STRu( O[y+x*h],
112114
sse::ADD( sse::LDu(O[y+x*h]), sse::AND(sse::CMPLT(sse::LDu(Gy[y]),sse::SET(0.f)),sse::SET(PI)) ) );
@@ -256,13 +258,14 @@ void gradHist( float *M, float *O, float *H, int h, int w,
256258

257259
// HOG helper: compute 2x2 block normalization values (padded by 1 pixel)
258260
float* hogNormMatrix( float *H, int nOrients, int hb, int wb, int bin ) {
259-
float *N, *N1, *n; int o, x, y, dx, dy, hb1=hb+1, wb1=wb+1;
261+
int o, x, y, dx, dy, hb1=hb+1, wb1=wb+1;
260262
float eps = 1e-4f/4/bin/bin/bin/bin; // precise backward equality
261-
N = (float*) wrCalloc(hb1*wb1,sizeof(float)); N1=N+hb1+1;
263+
float* N = (float*) wrCalloc(static_cast<size_t>(hb1) * static_cast<size_t>(wb1), sizeof(float));
264+
float* N1=N+hb1+1;
262265
for( o=0; o<nOrients; o++ ) for( x=0; x<wb; x++ ) for( y=0; y<hb; y++ )
263266
N1[x*hb1+y] += H[o*wb*hb+x*hb+y]*H[o*wb*hb+x*hb+y];
264267
for( x=0; x<wb-1; x++ ) for( y=0; y<hb-1; y++ ) {
265-
n=N1+x*hb1+y; *n=1/float(sqrt(n[0]+n[1]+n[hb1]+n[hb1+1]+eps)); }
268+
float* n=N1+x*hb1+y; *n=1/float(sqrt(n[0]+n[1]+n[hb1]+n[hb1+1]+eps)); }
266269
x=0; dx= 1; dy= 1; y=0; N[x*hb1+y]=N[(x+dx)*hb1+y+dy];
267270
x=0; dx= 1; dy= 0; for(y=0; y<hb1; y++) N[x*hb1+y]=N[(x+dx)*hb1+y+dy];
268271
x=0; dx= 1; dy=-1; y=hb1-1; N[x*hb1+y]=N[(x+dx)*hb1+y+dy];
@@ -322,16 +325,15 @@ void fhog( float *M, float *O, float *H, int h, int w, int binSize,
322325
int nOrients, int softBin, float clip )
323326
{
324327
const int hb=h/binSize, wb=w/binSize, nb=hb*wb, nbo=nb*nOrients;
325-
float *N, *R1, *R2; int o, x;
326328
// compute unnormalized constrast sensitive histograms
327-
R1 = (float*) wrCalloc(wb*hb*nOrients*2 + 2,sizeof(float));
329+
float* R1 = (float*) wrCalloc(static_cast<size_t>(wb) * static_cast<size_t>(hb) * static_cast<size_t>(nOrients) * 2 + 2, sizeof(float));
328330
gradHist( M, O, R1, h, w, binSize, nOrients*2, softBin, true );
329331
// compute unnormalized contrast insensitive histograms
330-
R2 = (float*) wrCalloc(wb*hb*nOrients,sizeof(float));
331-
for( o=0; o<nOrients; o++ ) for( x=0; x<nb; x++ )
332+
float* R2 = (float*) wrCalloc(static_cast<size_t>(wb) * static_cast<size_t>(hb) * static_cast<size_t>(nOrients), sizeof(float));
333+
for(int o=0; o<nOrients; o++ ) for(int x=0; x<nb; x++ )
332334
R2[o*nb+x] = R1[o*nb+x]+R1[(o+nOrients)*nb+x];
333335
// compute block normalization values
334-
N = hogNormMatrix( R2, nOrients, hb, wb, binSize );
336+
float* N = hogNormMatrix( R2, nOrients, hb, wb, binSize );
335337
// normalized histograms and texture channels
336338
hogChannels( H+nbo*0, R1, N, hb, wb, nOrients*2, clip, 1 );
337339
hogChannels( H+nbo*2, R2, N, hb, wb, nOrients*1, clip, 1 );
@@ -456,9 +458,10 @@ float* fhog(float *M,float* O,int height,int width,int /*channel*/,int *h,int *w
456458
*h = height/binSize;
457459
*w = width/binSize;
458460
*d = nOrients*3+5;
461+
const size_t allSize = static_cast<size_t>(*h) * static_cast<size_t>(*w) * static_cast<size_t>(*d);
459462

460-
float* H = new float[(*h)*(*w)*(*d)];
461-
memset(H,0,(*h)*(*w)*(*d)*sizeof(float));
463+
float* H = new float[allSize];
464+
memset(H, 0, allSize * sizeof(float));
462465

463466
fhog( M, O, H, height, width, binSize, nOrients, -1, clip );
464467

src/Tracker/staple/staple_tracker.cpp

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -353,12 +353,10 @@ void STAPLE_TRACKER::updateHistModel(bool new_model, cv::Mat &patch, double lear
353353
cv::calcHist(&patch, imgCount, channels, bg_mask_new, bg_hist, dims, sizes, ranges);
354354
cv::calcHist(&patch, imgCount, channels, fg_mask_new, fg_hist, dims, sizes, ranges);
355355

356-
int bgtotal = cv::countNonZero(bg_mask_new);
357-
(bgtotal == 0) && (bgtotal = 1);
356+
int bgtotal = std::max(1, cv::countNonZero(bg_mask_new));
358357
bg_hist = bg_hist / bgtotal;
359358

360-
int fgtotal = cv::countNonZero(fg_mask_new);
361-
(fgtotal == 0) && (fgtotal = 1);
359+
int fgtotal = std::max(1, cv::countNonZero(fg_mask_new));
362360
fg_hist = fg_hist / fgtotal;
363361
} else { // update the model
364362
cv::MatND bg_hist_tmp;
@@ -367,12 +365,10 @@ void STAPLE_TRACKER::updateHistModel(bool new_model, cv::Mat &patch, double lear
367365
cv::calcHist(&patch, imgCount, channels, bg_mask_new, bg_hist_tmp, dims, sizes, ranges);
368366
cv::calcHist(&patch, imgCount, channels, fg_mask_new, fg_hist_tmp, dims, sizes, ranges);
369367

370-
int bgtotal = cv::countNonZero(bg_mask_new);
371-
(bgtotal == 0) && (bgtotal = 1);
368+
int bgtotal = std::max(1, cv::countNonZero(bg_mask_new));
372369
bg_hist_tmp = bg_hist_tmp / bgtotal;
373370

374-
int fgtotal = cv::countNonZero(fg_mask_new);
375-
(fgtotal == 0) && (fgtotal = 1);
371+
int fgtotal = std::max(1, cv::countNonZero(fg_mask_new));
376372
fg_hist_tmp = fg_hist_tmp / fgtotal;
377373

378374
// xxx
@@ -1142,8 +1138,8 @@ void STAPLE_TRACKER::getColourMap(const cv::Mat &patch, cv::Mat& output)
11421138

11431139
// xxx
11441140
*pDst = profg / (profg + probg);
1145-
1146-
isnan(*pDst) && (*pDst = 0.0);
1141+
if (isnan(*pDst))
1142+
*pDst = 0.0;
11471143

11481144
pSrc += d;
11491145
++pDst;
@@ -1172,8 +1168,8 @@ void STAPLE_TRACKER::getColourMap(const cv::Mat &patch, cv::Mat& output)
11721168

11731169
// xxx
11741170
*pDst = profg / (profg + probg);
1175-
1176-
isnan(*pDst) && (*pDst = 0.0);
1171+
if (isnan(*pDst))
1172+
*pDst = 0.0;
11771173

11781174
pSrc += d;
11791175
++pDst;
@@ -1325,7 +1321,7 @@ cv::RotatedRect STAPLE_TRACKER::Update(const cv::Mat &im, float& confidence)
13251321
{
13261322
//hf = bsxfun(@rdivide, hf_num, sum(hf_den, 3)+p.lambda);
13271323

1328-
std::vector<float> DIM1(w * h, cfg.lambda);
1324+
std::vector<float> DIM1(static_cast<size_t>(w) * static_cast<size_t>(h), cfg.lambda);
13291325

13301326
for (int ch = 0; ch < xt_windowed.channels(); ++ch)
13311327
{
@@ -1517,22 +1513,19 @@ cv::RotatedRect STAPLE_TRACKER::Update(const cv::Mat &im, float& confidence)
15171513
}
15181514

15191515
// use new scale to update bboxes for target, filter, bg and fg models
1520-
target_sz.width = round(base_target_sz.width * scale_factor);
1521-
target_sz.height = round(base_target_sz.height * scale_factor);
1522-
1523-
float avg_dim = (target_sz.width + target_sz.height)/2.0;
1516+
target_sz.width = cvRound(base_target_sz.width * scale_factor);
1517+
target_sz.height = cvRound(base_target_sz.height * scale_factor);
15241518

1525-
bg_area.width= round(target_sz.width + avg_dim);
1526-
bg_area.height = round(target_sz.height + avg_dim);
1519+
float avg_dim = (target_sz.width + target_sz.height) / 2.0f;
15271520

1528-
(bg_area.width > im.cols) && (bg_area.width = im.cols - 1);
1529-
(bg_area.height > im.rows) && (bg_area.height = im.rows - 1);
1521+
bg_area.width = std::min(im.cols - 1, cvRound(target_sz.width + avg_dim));
1522+
bg_area.height = std::min(im.rows - 1, cvRound(target_sz.height + avg_dim));
15301523

15311524
bg_area.width = bg_area.width - (bg_area.width - target_sz.width) % 2;
15321525
bg_area.height = bg_area.height - (bg_area.height - target_sz.height) % 2;
15331526

1534-
fg_area.width = round(target_sz.width - avg_dim * cfg.inner_padding);
1535-
fg_area.height = round(target_sz.height - avg_dim * cfg.inner_padding);
1527+
fg_area.width = cvRound(target_sz.width - avg_dim * cfg.inner_padding);
1528+
fg_area.height = cvRound(target_sz.height - avg_dim * cfg.inner_padding);
15361529

15371530
fg_area.width = fg_area.width + int(bg_area.width - fg_area.width) % 2;
15381531
fg_area.height = fg_area.height + int(bg_area.height - fg_area.height) % 2;

0 commit comments

Comments
 (0)