粒子滤波 演示与opencv代码及解决logger.h(66): error C4996问题

opencv代码

#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <cvaux.h>
 
#include <cmath>
#include <vector>
#include <iostream>

using namespace std;
 
const int winHeight=600;
const int winWidth=800;
 
 
CvPoint mousePosition=cvPoint(winWidth>>1,winHeight>>1);
 
//mouse event callback
void mouseEvent(int event,int x,int y,int flags,void *param )
{
    if (event==CV_EVENT_MOUSEMOVE) {
        mousePosition=cvPoint(x,y);
    }
}
 
int main (void)
{
    //1.condensation setup
    const int stateNum=4;
    const int measureNum=2;
    const int sampleNum=9000;
 
    CvConDensation* condens = cvCreateConDensation(stateNum,measureNum,sampleNum);
    CvMat* lowerBound;
    CvMat* upperBound;
    lowerBound = cvCreateMat(stateNum, 1, CV_32F);
    upperBound = cvCreateMat(stateNum, 1, CV_32F);
    cvmSet(lowerBound,0,0,0.0 ); 
    cvmSet(upperBound,0,0,winWidth );
    cvmSet(lowerBound,1,0,0.0 ); 
    cvmSet(upperBound,1,0,winHeight );
    cvmSet(lowerBound,2,0,0.0 ); 
    cvmSet(upperBound,2,0,0.0 );
    cvmSet(lowerBound,3,0,0.0 ); 
    cvmSet(upperBound,3,0,0.0 );
    float A[stateNum][stateNum] ={
        1,0,1,0,
        0,1,0,1,
        0,0,1,0,
        0,0,0,1
    };
    memcpy(condens->DynamMatr,A,sizeof(A));
    cvConDensInitSampleSet(condens, lowerBound, upperBound);
 
    CvRNG rng_state = cvRNG(0xffffffff);
    for(int i=0; i < sampleNum; i++){
        condens->flSamples[i][0] = float(cvRandInt( &rng_state ) % winWidth); //width
        condens->flSamples[i][1] = float(cvRandInt( &rng_state ) % winHeight);//height
    }
 
    CvFont font;
    cvInitFont(&font,CV_FONT_HERSHEY_SCRIPT_COMPLEX,1,1);
 
    char* winName="condensation";
    cvNamedWindow(winName);
    cvSetMouseCallback(winName,mouseEvent);
    IplImage* img=cvCreateImage(cvSize(winWidth,winHeight),8,3);
    bool isPredictOnly=false;//trigger for prediction only,press SPACEBAR
    while (1){
        //2.condensation prediction
        CvPoint predict_pt=cvPoint((int)condens->State[0],(int)condens->State[1]);
 
        float variance[measureNum]={0};     
        //get variance/standard deviation of each state
        for (int i=0;i<measureNum;i++) {
            //sum
            float sumState=0;
            for (int j=0;j<condens->SamplesNum;j++) {
                sumState+=condens->flSamples[i][j];
            }
            //average
            sumState/=sampleNum;
            //variance
            for (int j=0;j<condens->SamplesNum;j++) {
                variance[i]+=(condens->flSamples[i][j]-sumState)*
                    (condens->flSamples[i][j]-sumState);
            }
            variance[i]/=sampleNum-1;
        }
        //3.update particals confidence
        CvPoint pt;
        if (isPredictOnly) {
            pt=predict_pt;
        }else{
            pt=mousePosition;
        }
        for (int i=0;i<condens->SamplesNum;i++) {
            float probX=(float)exp(-1*(pt.x-condens->flSamples[i][0])
                *(pt.x-condens->flSamples[i][0])/(2*variance[0]));
            float probY=(float)exp(-1*(pt.y-condens->flSamples[i][1])
                *(pt.y-condens->flSamples[i][1])/(2*variance[1]));
            condens->flConfidence[i]=probX*probY;
        }
        //4.update condensation
        cvConDensUpdateByTime(condens);
         
        //draw 
        cvSet(img,cvScalar(255,255,255,0));
        cvCircle(img,predict_pt,5,CV_RGB(0,255,0),3);//predicted point with green
        char buf[256];
        sprintf_s(buf,256,"predicted position:(%3d,%3d)",predict_pt.x,predict_pt.y);
        cvPutText(img,buf,cvPoint(10,30),&font,CV_RGB(0,0,0));
        if (!isPredictOnly) {
            cvCircle(img,mousePosition,5,CV_RGB(255,0,0),3);//current position with red
            sprintf_s(buf,256,"real position :(%3d,%3d)",mousePosition.x,mousePosition.y);
            cvPutText(img,buf,cvPoint(10,60),&font,CV_RGB(0,0,0));
        }
         
        cvShowImage(winName, img);
        int key=cvWaitKey(30);
        if (key==27){//esc   
            break;
        }else if (key==' ') {//trigger for prediction
            //isPredict=!isPredict;
            if (isPredictOnly) {
                isPredictOnly=false;
            }else{
                isPredictOnly=true;
            }
        }
    }      
 
    cvReleaseImage(&img);
    cvReleaseConDensation(&condens);
    return 0;
}


视屏演示:http://v.youku.com/v_show/id_XMjU4MzE0ODgw.html





 配置OpenCV产生flann\logger.h(66): error C4996: 'fopen': This function or variable may be unsafe问题


分类: OpenCV2014-03-11 09:58     VS2012opencv

本文的编译环境是:win7 64位;opencv2.4.6; VS2012


今天使用vs2012配置opencv编译出现问题:


1>------ 已启动生成:  配置: Debug Win32 ------
1>  stdafx.cpp
1>  Win32ForOpenCV245.cpp
1>f:\softs\opencv245\opencv\build\include\opencv2\flann\logger.h(66): error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\stdio.h(218) : 参见“fopen”的声明
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========


解决办法它也写的很清楚,这里截图给大家看

粒子滤波 演示与opencv代码及解决logger.h(66): error C4996问题_第1张图片粒子滤波 演示与opencv代码及解决logger.h(66): error C4996问题_第2张图片

上图填入:

[plain] view plaincopy

  1. WIN32  

  2. _DEBUG  

  3. _CONSOLE  

  4. _CRT_SECURE_NO_WARNINGS  

    ok,也就是加入一个_CRT_SECURE_NO_WARNINGS的问题。

    这个问题只有在Visual Studio 2012中才会有,2010中只是一个warning。

    这个问题不管在OpenCV2.4.4还是OpenCV2.4.5都有,其它版本可能也有。


你可能感兴趣的:(粒子滤波 演示与opencv代码及解决logger.h(66): error C4996问题)