⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testseq.cpp

📁 Using open CV draw color histogram, convert RGB To HSI
💻 CPP
📖 第 1 页 / 共 4 页
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                        Intel License Agreement
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of Intel Corporation may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/*
This file contain implementation of virtual interface of CvTestSeq
*/

#include "_cvaux.h" /* virtual interface if CvTestSeq */

#define FG_BG_THRESHOLD 3

#define SRC_TYPE_AVI 1
#define SRC_TYPE_IMAGE 0

/* Transformation structure */
typedef struct CvTSTrans
{
    float           T[6]; /* geometry transformation */
    CvPoint2D32f    Shift;
    CvPoint2D32f    Scale;
    float           I;
    float           C;
    float           GN; /* standart deviation of added gaussian noise */
    float           NoiseAmp; /* amplifier of noise power */
    float           angle;
}CvTSTrans;
void SET_TRANS_0(CvTSTrans *pT)
{
    memset(pT,0,sizeof(CvTSTrans));
    pT->C = 1;
    pT->Scale.x = 1;
    pT->Scale.y = 1;
    pT->T[4] = pT->T[0] = 1;
    pT->NoiseAmp = 1;
}

/* ===some defenitions and function for transformation update ===*/
#define P_ANGLE 0
#define P_S     1
#define P_SX    2
#define P_SY    3
#define P_DX    4
#define P_DY    5
#define P_I     6
#define P_C     7
#define P_GN    8
#define P_NAmp  9
static char*   param_name[] =    {"angle","s","sx","sy","dx","dy","I","C","GN","NoiseAmp", NULL};
static float   param_defval[] =  { 0,      1,  1,   1,   0,   0,   0,  1,  0,   1};
static void icvUpdateTrans(CvTSTrans* pTrans, int param, double val, float MaxX, float MaxY)
{
    assert(pTrans);
    if(param==P_ANGLE)
    {

        double  C = cos(3.1415926535897932384626433832795*val/180.0);
        double  S = sin(3.1415926535897932384626433832795*val/180.0);
        float*  T = pTrans->T;
        double  TR[6];
        int     i;
        pTrans->angle = (float)(pTrans->angle + val);
        TR[0] = C*T[0]-S*T[3];
        TR[1] = C*T[1]-S*T[4];
        TR[2] = C*T[2]-S*T[5];
        TR[3] = S*T[0]+C*T[3];
        TR[4] = S*T[1]+C*T[4];
        TR[5] = S*T[2]+C*T[5];
        for(i=0;i<6;++i)T[i]=(float)TR[i];
    }
    if(param==P_S)
    {
        int i;
        for(i=0;i<6;++i)pTrans->T[i] = (float)(pTrans->T[i]*val);
        pTrans->Scale.x = (float)(pTrans->Scale.x *val);
        pTrans->Scale.y = (float)(pTrans->Scale.y *val);
        pTrans->Shift.x = (float)(pTrans->Shift.x *val);
        pTrans->Shift.y = (float)(pTrans->Shift.y *val);
    }
    if(param==P_SX)
    {
        int i;
        for(i=0;i<3;++i)pTrans->T[i] = (float)(pTrans->T[i]*val);
        pTrans->Scale.x = (float)(pTrans->Scale.x*val);
        pTrans->Shift.x = (float)(pTrans->Shift.x*val);
    }
    if(param==P_SY)
    {
        int i;
        for(i=0;i<3;++i)pTrans->T[i+3] = (float)(pTrans->T[i+3]*val);
        pTrans->Scale.y = (float)(pTrans->Scale.y *val);
        pTrans->Shift.y = (float)(pTrans->Shift.y *val);
    }
    if(param==P_DX)
    {
        pTrans->Shift.x = (float)(pTrans->Shift.x +val);
        pTrans->T[2] = (float)(pTrans->T[2] +val*MaxX);
    }
    if(param==P_DY)
    {
        pTrans->Shift.y = (float)(pTrans->Shift.y +val);
        pTrans->T[5] = (float)(pTrans->T[5] +val*MaxY);
    }
    if(param==P_C)
    {
        pTrans->C = (float)(pTrans->C *val);
        pTrans->I = (float)(pTrans->I *val);
    }
    if(param==P_I) pTrans->I = (float)(pTrans->I +val);
    if(param==P_GN)
    {
        pTrans->GN = (float)sqrt(val*val+pTrans->GN*pTrans->GN);
    }
    if(param==P_NAmp) pTrans->NoiseAmp = (float)(pTrans->NoiseAmp *val);
}/* icvUpdateTrans */
/* === END some defenitions and function for transformation update ===*/

typedef struct CvTestSeqElem
{
    const char*     pObjName;
    const char*     pFileName;
    int             type; /* video or image */
    CvPoint2D32f*   pPos; /* positions of object in sequence */
    int             PosNum;
    CvPoint2D32f*   pSize; /* sizes of object in sequence */
    int             SizeNum;
    CvTSTrans*      pTrans; /* transforation of image in sequence */
    int             TransNum;
    int             ShiftByPos;
    CvPoint2D32f    ShiftBegin;
    CvPoint2D32f    ShiftEnd;
    int             FrameBegin;
    int             FrameNum;
    IplImage*       pImg;
    IplImage*       pImgMask;
    void*           pAVI;
    //CvCapture*      pAVI;
    int             AVILen;
    int             BG; /* flag is it background (1) or foreground (0) */
    int             Mask; /* flag is it foreground mask (1) or usual video (0) */
    CvTestSeqElem   *next;
    int             noise_type;
    CvRandState     rnd_state;
    int             ObjID;
 }CvTestSeqElem;
/* test seq main structure */
typedef struct CvTestSeq_
{
    int             ID;
    CvFileStorage*  pFileStorage;
    CvTestSeqElem*  pElemList;
    int             ListNum;
    IplImage*       pImg;
    IplImage*       pImgMask;
    int             CurFrame;
    int             FrameNum;
    int             noise_type;
    double          noise_ampl;
    float           IVar_DI;
    float           IVar_MinI;
    float           IVar_MaxI;
    float           IVar_CurDI;
    float           IVar_CurI;
    int             ObjNum;

}CvTestSeq_;


CvSize cvTestSeqGetImageSize(CvTestSeq* pTestSeq){return cvSize(((CvTestSeq_*)(pTestSeq))->pImg->width,((CvTestSeq_*)(pTestSeq))->pImg->height);}
int cvTestSeqFrameNum(CvTestSeq* pTestSeq){return ((CvTestSeq_*)(pTestSeq))->FrameNum;}
static void icvTestSeqCreateMask(IplImage* pImg,IplImage* pImgMask, int threshold)
{
    if(pImg->nChannels > 1)
    {
        cvCvtColor( pImg,pImgMask,CV_BGR2GRAY);
        cvThreshold(pImgMask,pImgMask,threshold,255,CV_THRESH_BINARY);
    }
    else
    {
        cvThreshold(pImg,pImgMask,threshold,255,CV_THRESH_BINARY);
    }
}/* icvTestSeqCreateMask */


static void icvTestSeqQureyFrameElem(CvTestSeqElem* p, int /*frame*/)
{/* read next frame from avi for one record */
    if(p->type == SRC_TYPE_AVI)
    {
        IplImage*   pI = NULL;
        //int         frameNum = p->AVILen;

        if(p->pAVI == NULL && p->pFileName)
        {/* open avi file if it is necessary */
            p->pAVI = 0;//cvCaptureFromFile(p->pFileName);
            if(p->pAVI == NULL)
            {
                printf("WARNING!!! Can not open avi file %s\n",p->pFileName);
                return;
            }
        }/* open avi file if it is necessary */

        assert(p->pAVI);
        //if(frame >= frameNum)
        {/* set new position */
            //int N = frame%frameNum;

            /*if( N==0 ||
                N != (int)cvGetCaptureProperty(p->pAVI,CV_CAP_PROP_POS_FRAMES))
            {
                cvSetCaptureProperty(p->pAVI,CV_CAP_PROP_POS_FRAMES,N);
            }*/
        }/* set new position */

        //pI = cvQueryFrame(p->pAVI);
        if(pI)
        {
            if(pI->origin != p->pImg->origin)
                cvFlip( pI, p->pImg, 0 );
            else
                cvCopyImage(pI, p->pImg);
        }

        if(p->pImg)
        {
            if(p->pImgMask==NULL)
            {
                p->pImgMask = cvCreateImage(
                    cvSize(p->pImg->width,p->pImg->height),
                    IPL_DEPTH_8U,1);
            }
            icvTestSeqCreateMask(p->pImg,p->pImgMask,p->Mask?128:FG_BG_THRESHOLD);
        }
    }

}/* icvTestSeqQureyFrameElem */

/*------------- recursive function to read all images,------------------------*/
/*-------------------videos and objects from config file ---------------------*/
static CvTestSeqElem* icvTestSeqReadElemAll(CvTestSeq_* pTS, CvFileStorage* fs, const char* name);
static void icvTestSeqAllocTrans(CvTestSeqElem* p)
{/*allocate transformation array if necessary */
    /* work with transformation */
    if(p->pTrans == NULL/* && p->FrameNum>0*/)
    {/* allocate transforamtion array */
        int num = MAX(1,p->FrameNum);
        p->pTrans = (CvTSTrans*)cvAlloc(sizeof(CvTSTrans)*num);
        p->TransNum = num;
        while(num--)SET_TRANS_0(p->pTrans+num);
    }

    if(p->FrameNum > p->TransNum)
    {/* allocate new transforamtion array */
        int         i;
        int         num = p->FrameNum;
        CvTSTrans*  pNewTrans = (CvTSTrans*)cvAlloc(sizeof(CvTSTrans)*num);
        for(i=0;i<num;++i)
        {
            if(p->pTrans)
                pNewTrans[i] = p->pTrans[i%p->TransNum];
            else
                SET_TRANS_0(pNewTrans+i);
        }
        if(p->pTrans)cvFree(&p->pTrans);
        p->pTrans = pNewTrans;
        p->TransNum = num;
    }/* allocate new transforamtion array */
}/*allocate transformation array if necessary */

static CvTestSeqElem* icvTestSeqReadElemOne(CvTestSeq_* pTS, CvFileStorage* fs, CvFileNode* node)
{
    int             noise_type = CV_NOISE_NONE;;
    CvTestSeqElem*  pElem = NULL;
    const char*     pVideoName = cvReadStringByName( fs, node,"Video", NULL);
    const char*     pVideoObjName = cvReadStringByName( fs, node,"VideoObj", NULL);

    if(pVideoName)
    {/* check to noise flag */
        if( cv_stricmp(pVideoName,"noise_gaussian") == 0 ||
            cv_stricmp(pVideoName,"noise_normal") == 0) noise_type = CV_NOISE_GAUSSIAN;
        if( cv_stricmp(pVideoName,"noise_uniform") == 0) noise_type = CV_NOISE_UNIFORM;
        if( cv_stricmp(pVideoName,"noise_speckle") == 0) noise_type = CV_NOISE_SPECKLE;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -