0

I wrote this template class:

.hpp:

#ifndef BIVARIATEGAUSSIANPDF_LIB_H
#define BIVARIATEGAUSSIANPDF_LIB_H

#if defined BIVARIATEGAUSSIANPDF_LIB_EXPORTS
#define BIVARIATEGAUSSIANPDFDLL_API __declspec(dllexport)
#else
#define BIVARIATEGAUSSIANPDFDLL_API __declspec(dllimport)
#endif

#include <vector>
#include <opencv2\core\core.hpp>

namespace cv
{
    class Mat;
}

#define _USE_MATH_DEFINES
#include <math.h>

template<class T, class N>
class BivariateGaussianPDF
{
public:
    BIVARIATEGAUSSIANPDFDLL_API BivariateGaussianPDF(const std::vector<T> & i_v1,
                                                     const std::vector<N> & i_v2);

    BIVARIATEGAUSSIANPDFDLL_API ~BivariateGaussianPDF();

    BIVARIATEGAUSSIANPDFDLL_API void compute();

    BIVARIATEGAUSSIANPDFDLL_API inline std::vector<double> getPDF() const { return m_pdf; }

private:
    cv::Mat m_matrix;
    std::vector<double> m_pdf;
};

template class BivariateGaussianPDF<int, int>;
template class BivariateGaussianPDF<int, float>;
template class BivariateGaussianPDF<int, double>;
template class BivariateGaussianPDF<float, int>;
template class BivariateGaussianPDF<float, float>;
template class BivariateGaussianPDF<float, double>;
template class BivariateGaussianPDF<double, int>;
template class BivariateGaussianPDF<double, float>;
template class BivariateGaussianPDF<double, double>;

#endif

and .cpp:

#include "stdafx.h"
#include "bivariateGaussianPDF_lib.h"
#include <iterator>

template<class T, class N>
BivariateGaussianPDF<T, N>::BivariateGaussianPDF(const std::vector<T>& i_v1,
                                                 const std::vector<N>& i_v2)
    :m_pdf(i_v1.size())
{
    std::vector<double> temp_v1(std::begin(i_v1), std::end(i_v1));
    std::vector<double> temp_v2(std::begin(i_v2), std::end(i_v2));
    m_matrix.create(2, i_v1.size(), CV_64F);
    double * r0_ptr = m_matrix.ptr<double>(0);
    double * r1_ptr = m_matrix.ptr<double>(1);
    std::copy(temp_v1.begin(), temp_v1.end(), r0_ptr);
    std::copy(temp_v2.begin(), temp_v2.end(), r1_ptr);
}

template<class T, class N>
BivariateGaussianPDF<T, N>::~BivariateGaussianPDF()
{
    m_matrix.release();
}

template<class T, class N>
void BivariateGaussianPDF<T, N>::compute()
{
    cv::Mat row_mean, cov, inverseCov, temp_pdf;

    // compute covariance matrix
    cv::calcCovarMatrix(m_matrix, cov, row_mean, CV_COVAR_ROWS | CV_COVAR_NORMAL);
    // scale covariance matrix
    cov = cov / (m_matrix.cols - 1);
    // inverse of covariance matrix
    inverseCov = cov.inv();

    // determinant of covariance matrix
    const double det = cv::determinant(cov);

    // X = [x1; x2 ... ; xN] --> [x1 - mu1; x2 - mu2; ... ; xN - muN ]
    cv::subtract(m_matrix.row(0), row_mean.row(0), m_matrix.row(0));
    cv::subtract(m_matrix.row(1), row_mean.row(1), m_matrix.row(1));

    // (inv*X).*X
    cv::reduce((inverseCov*m_matrix).mul(m_matrix), temp_pdf, 0, CV_REDUCE_SUM);
    temp_pdf = -0.5*temp_pdf;
    cv::exp(temp_pdf, temp_pdf);

    // bivariate gaussian pdf
    temp_pdf = (0.5 / (M_PI*std::sqrt(det)))*temp_pdf;

    const double * d_ptr = temp_pdf.ptr<double>(0);
    std::copy(d_ptr, d_ptr + temp_pdf.cols, m_pdf.begin());
}

When I compile I get this message:

Error C4996 'std::_Copy_impl': Function call with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators' bivariateGaussianPDF_lib c:\program files (x86)\microsoft visual studio 14.0\vc\include\xutility 2229

Reading this answer I understood because I get the error (I'm not calling std::copy according to msdn function signature) but I don't know how to fix it. I think the error lines are:

std::copy(temp_v1.begin(), temp_v1.end(), r0_ptr);
std::copy(temp_v2.begin(), temp_v2.end(), r1_ptr);

Any help is appreciated.

Community
  • 1
  • 1
BugsFree
  • 540
  • 1
  • 6
  • 24
  • 1
    If you are going to use something from `` then you should include that header. Try that an see what happens. – NathanOliver Apr 04 '16 at 17:35
  • 1
    See [this KB article](https://msdn.microsoft.com/en-us/library/ttcz0bys%28v=vs.120%29.aspx?f=255&MSPPError=-2147217396). The problem is that you're passing a plain pointer as an output iterator. Note that the solution (and the problem) are both MSVS specific. – Dan Mašek Apr 04 '16 at 18:09
  • Thanks @DanMašek. Following the article you posted, I've fixed the problem. – BugsFree Apr 05 '16 at 12:30

1 Answers1

3

I solved the problem following instructions in this article suggested by @DanMašek in the comment.

I added the header <iterator> and then I used the class stdext::checked_array_iterator. The result is something like that:

    double * r0_ptr = m_matrix.ptr<double>(0);
    std::copy(temp_v1.begin(), temp_v1.end(), stdext::checked_array_iterator<double *>(r0_ptr, m_matrix.cols));
BugsFree
  • 540
  • 1
  • 6
  • 24