testseq.cpp 46.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370
/*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 "precomp.hpp" /* virtual interface if CvTestSeq */


void cvAddNoise(IplImage* pImg, int noise_type, double Ampl, CvRandState* rnd_state = NULL);

#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;

static 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 definitions and functions 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 const 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 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 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
                cvCopy(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 transformation 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 transformation 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 transformation 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;
        if( cv_stricmp(pVideoName,"noise_salt_and_pepper") == 0) noise_type = CV_NOISE_SALT_AND_PEPPER;
    }

    if((pVideoName || pVideoObjName ) && noise_type == CV_NOISE_NONE)
    {   /* Read other elements: */
        if(pVideoName) pElem = icvTestSeqReadElemAll(pTS, fs, pVideoName);
        if(pVideoObjName)
        {
            CvTestSeqElem* pE;
            pElem = icvTestSeqReadElemAll(pTS, fs, pVideoObjName);
            for(pE=pElem;pE;pE=pE->next)
            {
                pE->ObjID = pTS->ObjNum;
                pE->pObjName = pVideoObjName;
            }
            pTS->ObjNum++;
        }
    }   /* Read other elements. */
    else
    {   /* Create new element: */
        CvFileNode* pPosNode = cvGetFileNodeByName( fs, node,"Pos");
        CvFileNode* pSizeNode = cvGetFileNodeByName( fs, node,"Size");
        int AutoSize = (pSizeNode && CV_NODE_IS_STRING(pSizeNode->tag) && cv_stricmp("auto",cvReadString(pSizeNode,""))==0);
        int AutoPos = (pPosNode && CV_NODE_IS_STRING(pPosNode->tag) && cv_stricmp("auto",cvReadString(pPosNode,""))==0);
        const char* pFileName = cvReadStringByName( fs, node,"File", NULL);
        pElem = (CvTestSeqElem*)cvAlloc(sizeof(CvTestSeqElem));
        memset(pElem,0,sizeof(CvTestSeqElem));

        pElem->ObjID = -1;
        pElem->noise_type = noise_type;
        cvRandInit( &pElem->rnd_state, 1, 0, 0,CV_RAND_NORMAL);

        if(pFileName && pElem->noise_type == CV_NOISE_NONE)
        {   /* If AVI or BMP: */
            size_t  l = strlen(pFileName);
            pElem->pFileName = pFileName;

            pElem->type = SRC_TYPE_IMAGE;
            if(cv_stricmp(".avi",pFileName+l-4) == 0)pElem->type = SRC_TYPE_AVI;

            if(pElem->type == SRC_TYPE_IMAGE)
            {
                //pElem->pImg = cvLoadImage(pFileName);
                if(pElem->pImg)
                {
                    pElem->FrameNum = 1;
                    if(pElem->pImgMask)cvReleaseImage(&(pElem->pImgMask));

                    pElem->pImgMask = cvCreateImage(
                        cvSize(pElem->pImg->width,pElem->pImg->height),
                        IPL_DEPTH_8U,1);
                    icvTestSeqCreateMask(pElem->pImg,pElem->pImgMask,FG_BG_THRESHOLD);
                }
            }

            if(pElem->type == SRC_TYPE_AVI && pFileName)
            {
                //pElem->pAVI = cvCaptureFromFile(pFileName);

                if(pElem->pAVI)
                {
                    IplImage* pImg = 0;//cvQueryFrame(pElem->pAVI);
                    pElem->pImg = cvCloneImage(pImg);
                    pElem->pImg->origin = 0;
                    //cvSetCaptureProperty(pElem->pAVI,CV_CAP_PROP_POS_FRAMES,0);
                    pElem->FrameBegin = 0;
                    pElem->AVILen = pElem->FrameNum = 0;//(int)cvGetCaptureProperty(pElem->pAVI, CV_CAP_PROP_FRAME_COUNT);
                    //cvReleaseCapture(&pElem->pAVI);
                    pElem->pAVI = NULL;
                }
                else
                {
                    printf("WARNING!!! Cannot open avi file %s\n",pFileName);
                }
            }

        }   /* If AVI or BMP. */

        if(pPosNode)
        {   /* Read positions: */
            if(CV_NODE_IS_SEQ(pPosNode->tag))
            {
                int num = pPosNode->data.seq->total;
                pElem->pPos = (CvPoint2D32f*)cvAlloc(sizeof(float)*num);
                cvReadRawData( fs, pPosNode, pElem->pPos, "f" );
                pElem->PosNum = num/2;
                if(pElem->FrameNum == 0) pElem->FrameNum = pElem->PosNum;
            }
        }

        if(pSizeNode)
        {   /* Read sizes: */
            if(CV_NODE_IS_SEQ(pSizeNode->tag))
            {
                int num = pSizeNode->data.seq->total;
                pElem->pSize = (CvPoint2D32f*)cvAlloc(sizeof(float)*num);
                cvReadRawData( fs, pSizeNode, pElem->pSize, "f" );
                pElem->SizeNum = num/2;
            }
        }

        if(AutoPos || AutoSize)
        {   /* Auto size and pos: */
            int     i;
            int     num = (pElem->type == SRC_TYPE_AVI)?pElem->AVILen:1;
            if(AutoSize)
            {
                pElem->pSize = (CvPoint2D32f*)cvAlloc(sizeof(CvPoint2D32f)*num);
                pElem->SizeNum = num;
            }
            if(AutoPos)
            {
                pElem->pPos = (CvPoint2D32f*)cvAlloc(sizeof(CvPoint2D32f)*num);
                pElem->PosNum = num;
            }

            for(i=0; i<num; ++i)
            {
                IplImage* pFG = NULL;
                CvPoint2D32f* pPos = AutoPos?(pElem->pPos + i):NULL;
                CvPoint2D32f* pSize = AutoSize?(pElem->pSize + i):NULL;

                icvTestSeqQureyFrameElem(pElem,i);
                pFG = pElem->pImgMask;

                if(pPos)
                {
                    pPos->x = 0.5f;
                    pPos->y = 0.5f;
                }
                if(pSize)
                {
                    pSize->x = 0;
                    pSize->y = 0;
                }

                if(pFG)
                {
                    double      M00;
                    CvMoments   m;
                    cvMoments( pElem->pImgMask, &m, 0 );
                    M00 = cvGetSpatialMoment( &m, 0, 0 );

                    if(M00 > 0 && pSize )
                    {
                        double X = cvGetSpatialMoment( &m, 1, 0 )/M00;
                        double Y = cvGetSpatialMoment( &m, 0, 1 )/M00;
                        double XX = (cvGetSpatialMoment( &m, 2, 0 )/M00) - X*X;
                        double YY = (cvGetSpatialMoment( &m, 0, 2 )/M00) - Y*Y;
                        pSize->x = (float)(4*sqrt(XX))/(pElem->pImgMask->width-1);
                        pSize->y = (float)(4*sqrt(YY))/(pElem->pImgMask->height-1);
                    }

                    if(M00 > 0 && pPos)
                    {
                        pPos->x = (float)(cvGetSpatialMoment( &m, 1, 0 )/(M00*(pElem->pImgMask->width-1)));
                        pPos->y = (float)(cvGetSpatialMoment( &m, 0, 1 )/(M00*(pElem->pImgMask->height-1)));
                    }

                    if(pPos)
                    {   /* Another way to calculate y pos
                         * using object median:
                         */
                        int y0=0, y1=pFG->height-1;
                        for(y0=0; y0<pFG->height; ++y0)
                        {
                            CvMat       tmp;
                            CvScalar    s = cvSum(cvGetRow(pFG, &tmp, y0));
                            if(s.val[0] > 255*7) break;
                        }

                        for(y1=pFG->height-1; y1>0; --y1)
                        {
                            CvMat tmp;
                            CvScalar s = cvSum(cvGetRow(pFG, &tmp, y1));
                            if(s.val[0] > 255*7) break;
                        }

                        pPos->y = (y0+y1)*0.5f/(pFG->height-1);
                    }
                }   /* pFG */
            }   /* Next frame. */

            //if(pElem->pAVI) cvReleaseCapture(&pElem->pAVI);

            pElem->pAVI = NULL;

        }   /* End auto position creation. */
    }   /*  Create new element. */

    if(pElem)
    {   /* Read transforms and: */
        int             FirstFrame, LastFrame;
        CvTestSeqElem*  p=pElem;
        CvFileNode*     pTransNode = NULL;
        CvFileNode*     pS = NULL;
        int             ShiftByPos = 0;
        int             KeyFrames[1024];
        CvSeq*          pTransSeq = NULL;
        int             KeyFrameNum = 0;

        pTransNode = cvGetFileNodeByName( fs, node,"Trans");

        while( pTransNode &&
               CV_NODE_IS_STRING(pTransNode->tag) &&
               cv_stricmp("auto",cvReadString(pTransNode,""))!=0)
        {   /* Trans is reference: */
            pTransNode = cvGetFileNodeByName( fs, NULL,cvReadString(pTransNode,""));
        }

        pS = cvGetFileNodeByName( fs, node,"Shift");
        ShiftByPos = 0;
        pTransSeq = pTransNode?(CV_NODE_IS_SEQ(pTransNode->tag)?pTransNode->data.seq:NULL):NULL;
        KeyFrameNum = pTransSeq?pTransSeq->total:1;

        if(   (pS && CV_NODE_IS_STRING(pS->tag) && cv_stricmp("auto",cvReadString(pS,""))==0)
            ||(pTransNode && CV_NODE_IS_STRING(pTransNode->tag) && cv_stricmp("auto",cvReadString(pTransNode,""))==0))
        {
            ShiftByPos = 1;
        }

        FirstFrame = pElem->FrameBegin;
        LastFrame = pElem->FrameBegin+pElem->FrameNum-1;

        /* Calculate length of video and reallocate
         * transformation array:
         */
        for(p=pElem; p; p=p->next)
        {
            int v;
            v = cvReadIntByName( fs, node, "BG", -1 );
            if(v!=-1)p->BG = v;
            v = cvReadIntByName( fs, node, "Mask", -1 );
            if(v!=-1)p->Mask = v;

            p->FrameBegin += cvReadIntByName( fs, node, "FrameBegin", 0 );
            p->FrameNum = cvReadIntByName( fs, node, "FrameNum", p->FrameNum );
            p->FrameNum = cvReadIntByName( fs, node, "Dur", p->FrameNum );
            {
                int lastFrame = cvReadIntByName( fs, node, "LastFrame", p->FrameBegin+p->FrameNum-1 );
                p->FrameNum = MIN(p->FrameNum,lastFrame - p->FrameBegin+1);
            }

            icvTestSeqAllocTrans(p);

            {   /* New range estimation: */
                int LF = p->FrameBegin+p->FrameNum-1;
                if(p==pElem || FirstFrame > p->FrameBegin)FirstFrame = p->FrameBegin;
                if(p==pElem || LastFrame < LF)LastFrame = LF;
            }   /* New range estimation. */
        }   /*  End allocate new transfrom array. */

        if(ShiftByPos)
        {
            for(p=pElem;p;p=p->next)
            {   /* Modify transformation to make autoshift: */
                int         i;
                int         num = p->FrameNum;
                assert(num <= p->TransNum);
                p->TransNum = MAX(1,num);

                for(i=0; i<num; ++i)
                {
                    CvTSTrans*  pT = p->pTrans+i;
                    //float   t = (num>1)?((float)i/(num-1)):0.0f;
                    float newx = p->pPos[i%p->PosNum].x;
                    float newy = p->pPos[i%p->PosNum].y;
                    pT->Shift.x = -newx*pT->Scale.x;
                    pT->Shift.y = -newy*pT->Scale.y;

                    if(p->pImg)
                    {
                        newx *= p->pImg->width-1;
                        newy *= p->pImg->height-1;
                    }

                    pT->T[2] = -(pT->T[0]*newx+pT->T[1]*newy);
                    pT->T[5] = -(pT->T[3]*newx+pT->T[4]*newy);
                }
            }   /* Modify transformation old. */
        }   /*  Next record. */

        /* Initialize frame number array: */
        KeyFrames[0] = FirstFrame;

        if(pTransSeq&&KeyFrameNum>1)
        {
            int i0,i1;
            for(int i=0; i<KeyFrameNum; ++i)
            {
                CvFileNode* pTN = (CvFileNode*)cvGetSeqElem(pTransSeq,i);
                KeyFrames[i] = cvReadIntByName(fs,pTN,"frame",-1);
            }

            if(KeyFrames[0]<0)KeyFrames[0]=FirstFrame;
            if(KeyFrames[KeyFrameNum-1]<0)KeyFrames[KeyFrameNum-1]=LastFrame;

            for(i0=0, i1=1; i1<KeyFrameNum;)
            {
                for(i1=i0+1; i1<KeyFrameNum && KeyFrames[i1]<0; i1++);

                assert(i1<KeyFrameNum);
                assert(i1>i0);

                for(int i=i0+1; i<i1; ++i)
                {
                    KeyFrames[i] = cvRound(KeyFrames[i0] + (float)(i-i0)*(float)(KeyFrames[i1] - KeyFrames[i0])/(float)(i1-i0));
                }
                i0 = i1;
                i1++;
            }   /* Next key run. */
        }   /*  Initialize frame number array. */

        if(pTransNode || pTransSeq)
        {   /* More complex transform. */
            int     param;
            CvFileNode* pTN = pTransSeq?(CvFileNode*)cvGetSeqElem(pTransSeq,0):pTransNode;

            for(p=pElem; p; p=p->next)
            {
                //int trans_num = p->TransNum;
                for(param=0; param_name[param]; ++param)
                {
                    const char*   name = param_name[param];
                    float   defv = param_defval[param];
                    if(KeyFrameNum==1)
                    {   /* Only one transform record: */
                        int     i;
                        double  val;
                        CvFileNode* fnode = cvGetFileNodeByName( fs, pTN,name);
                        if(fnode == NULL) continue;
                        val = cvReadReal(fnode,defv);

                        for(i=0; i<p->TransNum; ++i)
                        {
                            icvUpdateTrans(
                                p->pTrans+i, param, val,
                                p->pImg?(float)(p->pImg->width-1):1.0f,
                                p->pImg?(float)(p->pImg->height-1):1.0f);
                        }
                    }   /* Next record. */
                    else
                    {   /* Several transforms: */
                        int         i0,i1;
                        double      v0;
                        double      v1;

                        CvFileNode* pTN1 = (CvFileNode*)cvGetSeqElem(pTransSeq,0);
                        v0 = cvReadRealByName(fs, pTN1,name,defv);

                        for(i1=1,i0=0; i1<KeyFrameNum; ++i1)
                        {
                            int         f0,f1;
                            int         i;
                            CvFileNode* pTN2 = (CvFileNode*)cvGetSeqElem(pTransSeq,i1);
                            CvFileNode* pVN = cvGetFileNodeByName(fs,pTN2,name);

                            if(pVN)v1 = cvReadReal(pVN,defv);
                            else if(pVN == NULL && i1 == KeyFrameNum-1) v1 = defv;
                            else continue;

                            f0 = KeyFrames[i0];
                            f1 = KeyFrames[i1];

                            if(i1==(KeyFrameNum-1)) f1++;

                            for(i=f0; i<f1; ++i)
                            {
                                double   val;
                                double   t = (float)(i-f0);
                                int      li = i - p->FrameBegin;
                                if(li<0) continue;
                                if(li>= p->TransNum) break;
                                if(KeyFrames[i1]>KeyFrames[i0]) t /=(float)(KeyFrames[i1]-KeyFrames[i0]);
                                val = t*(v1-v0)+v0;

                                icvUpdateTrans(
                                    p->pTrans+li, param, val,
                                    p->pImg?(float)(p->pImg->width-1):1.0f,
                                    p->pImg?(float)(p->pImg->height-1):1.0f);

                            }   /* Next transform. */
                            i0 = i1;
                            v0 = v1;

                        }   /* Next value run. */
                    }   /*  Several transforms. */
                }   /*  Next parameter. */
            }   /*  Next record. */
        }   /*  More complex transform. */
    }   /*  Read transfroms. */

    return pElem;

}   /* icvTestSeqReadElemOne */

static CvTestSeqElem* icvTestSeqReadElemAll(CvTestSeq_* pTS, CvFileStorage* fs, const char* name)
{
    CvTestSeqElem*  pElem = NULL;
    CvFileNode*     node;

    if(name == NULL) return NULL;

    node = cvGetFileNodeByName( fs, NULL, name );

    if(node == NULL)
    {
        printf("WARNING!!! - Video %s does not exist!\n", name);
        return NULL;
    }

    printf("Read node %s\n",name);

    if(CV_NODE_IS_SEQ(node->tag))
    {   /* Read all element in sequence: */
        int             i;
        CvSeq*          seq = node->data.seq;
        CvTestSeqElem*  pElemLast = NULL;

        for(i=0; i<seq->total; ++i)
        {
            CvFileNode*     next_node = (CvFileNode*)cvGetSeqElem( seq, i );
            CvTestSeqElem*  pElemNew = icvTestSeqReadElemOne(pTS, fs, next_node );
            CvFileNode*     pDurNode = cvGetFileNodeByName( fs, next_node,"Dur");

            if(pElemNew == NULL )
            {
                printf("WARNING in parsing %s record!!! Cannot read array element\n", name);
                continue;
            }

            if(pElem && pElemLast)
            {
                pElemLast->next = pElemNew;
                if(pDurNode)
                {
                    pElemNew->FrameBegin = pElemLast->FrameBegin + pElemLast->FrameNum;
                }
            }
            else
            {
                pElem = pElemNew;
            }

            /* Find last element: */
            for(pElemLast=pElemNew;pElemLast && pElemLast->next;pElemLast= pElemLast->next);

        }   /* Next element. */
    }   /*  Read all element in sequence. */
    else
    {   /* Read one element: */
        pElem = icvTestSeqReadElemOne(pTS, fs, node );
    }

    return pElem;

}   /* icvTestSeqReadElemAll */

static void icvTestSeqReleaseAll(CvTestSeqElem** ppElemList)
{
    CvTestSeqElem* p = ppElemList[0];

    while(p)
    {
        CvTestSeqElem* pd = p;
        if(p->pAVI)
        {
            //cvReleaseCapture(&p->pAVI);
        }
        if(p->pImg)cvReleaseImage(&p->pImg);
        if(p->pImgMask)cvReleaseImage(&p->pImgMask);
        if(p->pPos)cvFree(&p->pPos);
        if(p->pTrans)cvFree(&p->pTrans);
        if(p->pSize)cvFree(&p->pSize);
        p=p->next;
        cvFree(&pd);

    }   /* Next element. */

    ppElemList[0] = NULL;

}   /* icvTestSeqReleaseAll */

CvTestSeq* cvCreateTestSeq(char* pConfigfile, char** videos, int numvideo, float Scale, int noise_type, double noise_ampl)
{
    int             size = sizeof(CvTestSeq_);
    CvTestSeq_*     pTS = (CvTestSeq_*)cvAlloc(size);
    CvFileStorage*  fs = cvOpenFileStorage( pConfigfile, NULL, CV_STORAGE_READ);
    int         i;

    if(pTS == NULL || fs == NULL) return NULL;
    memset(pTS,0,size);

    pTS->pFileStorage = fs;
    pTS->noise_ampl = noise_ampl;
    pTS->noise_type = noise_type;
    pTS->IVar_DI = 0;
    pTS->ObjNum = 0;

    /* Read all videos: */
    for (i=0; i<numvideo; ++i)
    {
        CvTestSeqElem*  pElemNew = icvTestSeqReadElemAll(pTS, fs, videos[i]);

        if(pTS->pElemList==NULL)pTS->pElemList = pElemNew;
        else
        {
            CvTestSeqElem* p = NULL;
            for(p=pTS->pElemList;p->next;p=p->next);
            p->next = pElemNew;
        }
    }   /* Read all videos. */

    {   /* Calculate elements and image size and video length: */
        CvTestSeqElem*  p = pTS->pElemList;
        int             num = 0;
        CvSize          MaxSize = {0,0};
        int             MaxFN = 0;

        for(p = pTS->pElemList; p; p=p->next, num++)
        {
            int     FN = p->FrameBegin+p->FrameNum;
            CvSize  S = {0,0};

            if(p->pImg && p->BG)
            {
                S.width = p->pImg->width;
                S.height = p->pImg->height;
            }

            if(MaxSize.width < S.width) MaxSize.width = S.width;
            if(MaxSize.height < S.height) MaxSize.height = S.height;
            if(MaxFN < FN)MaxFN = FN;
        }

        pTS->ListNum = num;

        if(MaxSize.width == 0)MaxSize.width = 320;
        if(MaxSize.height == 0)MaxSize.height = 240;

        MaxSize.width = cvRound(Scale*MaxSize.width);
        MaxSize.height = cvRound(Scale*MaxSize.height);

        pTS->pImg = cvCreateImage(MaxSize,IPL_DEPTH_8U,3);
        pTS->pImgMask = cvCreateImage(MaxSize,IPL_DEPTH_8U,1);
        pTS->FrameNum = MaxFN;

        for(p = pTS->pElemList; p; p=p->next)
        {
            if(p->FrameNum<=0)p->FrameNum=MaxFN;
        }
    }   /* Calculate elements and image size. */

    return (CvTestSeq*)pTS;

}   /* cvCreateTestSeq */

void cvReleaseTestSeq(CvTestSeq** ppTestSeq)
{
    CvTestSeq_* pTS = (CvTestSeq_*)ppTestSeq[0];

    icvTestSeqReleaseAll(&pTS->pElemList);
    if(pTS->pImg) cvReleaseImage(&pTS->pImg);
    if(pTS->pImgMask) cvReleaseImage(&pTS->pImgMask);
    if(pTS->pFileStorage)cvReleaseFileStorage(&pTS->pFileStorage);

    cvFree(ppTestSeq);

}   /* cvReleaseTestSeq */

void cvTestSeqSetFrame(CvTestSeq* pTestSeq, int n)
{
    CvTestSeq_*     pTS = (CvTestSeq_*)pTestSeq;
    pTS->CurFrame = n;
}

IplImage* cvTestSeqQueryFrame(CvTestSeq* pTestSeq)
{
    CvTestSeq_*     pTS = (CvTestSeq_*)pTestSeq;
    CvTestSeqElem*  p = pTS->pElemList;
    IplImage*       pImg = pTS->pImg;
    IplImage*       pImgAdd = cvCloneImage(pTS->pImg);
    IplImage*       pImgAddG = cvCreateImage(cvSize(pImgAdd->width,pImgAdd->height),IPL_DEPTH_8U,1);
    IplImage*       pImgMask = pTS->pImgMask;
    IplImage*       pImgMaskAdd = cvCloneImage(pTS->pImgMask);
    CvMat*          pT = cvCreateMat(2,3,CV_32F);

    if(pTS->CurFrame >= pTS->FrameNum) return NULL;
    cvZero(pImg);
    cvZero(pImgMask);

    for(p=pTS->pElemList; p; p=p->next)
    {
        int             DirectCopy = FALSE;
        int             frame = pTS->CurFrame - p->FrameBegin;
        //float           t = p->FrameNum>1?((float)frame/(p->FrameNum-1)):0;
        CvTSTrans*      pTrans = p->pTrans + frame%p->TransNum;

        assert(pTrans);

        if( p->FrameNum > 0 && (frame < 0 || frame >= p->FrameNum) )
        {   /* Current frame is out of range: */
            //if(p->pAVI)cvReleaseCapture(&p->pAVI);
            p->pAVI = NULL;
            continue;
        }

        cvZero(pImgAdd);
        cvZero(pImgAddG);
        cvZero(pImgMaskAdd);

        if(p->noise_type == CV_NOISE_NONE)
        {   /* For not noise:  */
            /* Get next frame: */
            icvTestSeqQureyFrameElem(p, frame);
            if(p->pImg == NULL) continue;

#if 1 /* transform using T filed in Trans */
            {   /* Calculate transform matrix: */
                float   W = (float)(pImgAdd->width-1);
                float   H = (float)(pImgAdd->height-1);
                float   W0 = (float)(p->pImg->width-1);
                float   H0 = (float)(p->pImg->height-1);
                cvZero(pT);
                {   /* Calcualte inverse matrix: */
                    CvMat   mat = cvMat(2,3,CV_32F, pTrans->T);
                    mat.width--;
                    pT->width--;
                    cvInvert(&mat, pT);
                    pT->width++;
                }

                CV_MAT_ELEM(pT[0], float, 0, 2) =
                    CV_MAT_ELEM(pT[0], float, 0, 0)*(W0/2-pTrans->T[2])+
                    CV_MAT_ELEM(pT[0], float, 0, 1)*(H0/2-pTrans->T[5]);

                CV_MAT_ELEM(pT[0], float, 1, 2) =
                    CV_MAT_ELEM(pT[0], float, 1, 0)*(W0/2-pTrans->T[2])+
                    CV_MAT_ELEM(pT[0], float, 1, 1)*(H0/2-pTrans->T[5]);

                CV_MAT_ELEM(pT[0], float, 0, 0) *= W0/W;
                CV_MAT_ELEM(pT[0], float, 0, 1) *= H0/H;
                CV_MAT_ELEM(pT[0], float, 1, 0) *= W0/W;
                CV_MAT_ELEM(pT[0], float, 1, 1) *= H0/H;

            }   /* Calculate transform matrix. */
#else
            {   /* Calculate transform matrix: */
                float   SX = (float)(p->pImg->width-1)/((pImgAdd->width-1)*pTrans->Scale.x);
                float   SY = (float)(p->pImg->height-1)/((pImgAdd->height-1)*pTrans->Scale.y);
                float   DX = pTrans->Shift.x;
                float   DY = pTrans->Shift.y;;
                cvZero(pT);
                ((float*)(pT->data.ptr+pT->step*0))[0]=SX;
                ((float*)(pT->data.ptr+pT->step*1))[1]=SY;
                ((float*)(pT->data.ptr+pT->step*0))[2]=SX*(pImgAdd->width-1)*(0.5f-DX);
                ((float*)(pT->data.ptr+pT->step*1))[2]=SY*(pImgAdd->height-1)*(0.5f-DY);
            }   /* Calculate transform matrix. */
#endif


            {   /* Check for direct copy: */
                DirectCopy = TRUE;
                if( fabs(CV_MAT_ELEM(pT[0],float,0,0)-1) > 0.00001) DirectCopy = FALSE;
                if( fabs(CV_MAT_ELEM(pT[0],float,1,0)) > 0.00001) DirectCopy = FALSE;
                if( fabs(CV_MAT_ELEM(pT[0],float,0,1)) > 0.00001) DirectCopy = FALSE;
                if( fabs(CV_MAT_ELEM(pT[0],float,0,1)) > 0.00001) DirectCopy = FALSE;
                if( fabs(CV_MAT_ELEM(pT[0],float,0,2)-(pImg->width-1)*0.5) > 0.5) DirectCopy = FALSE;
                if( fabs(CV_MAT_ELEM(pT[0],float,1,2)-(pImg->height-1)*0.5) > 0.5) DirectCopy = FALSE;
            }

            /* Extract image and mask: */
            if(p->pImg->nChannels == 1)
            {
                if(DirectCopy)
                {
                    cvCvtColor( p->pImg,pImgAdd,CV_GRAY2BGR);
                }
                else
                {
                    cvGetQuadrangleSubPix( p->pImg, pImgAddG, pT);
                    cvCvtColor( pImgAddG,pImgAdd,CV_GRAY2BGR);
                }
            }

            if(p->pImg->nChannels == 3)
            {
                if(DirectCopy)
                    cvCopy(p->pImg, pImgAdd);
                else
                    cvGetQuadrangleSubPix( p->pImg, pImgAdd, pT);
            }

            if(p->pImgMask)
            {
                if(DirectCopy)
                    cvCopy(p->pImgMask, pImgMaskAdd);
                else
                    cvGetQuadrangleSubPix( p->pImgMask, pImgMaskAdd, pT);

                cvThreshold(pImgMaskAdd,pImgMaskAdd,128,255,CV_THRESH_BINARY);
            }

            if(pTrans->C != 1 || pTrans->I != 0)
            {   /* Intensity transformation: */
                cvScale(pImgAdd, pImgAdd, pTrans->C,pTrans->I);
            }   /* Intensity transformation: */

            if(pTrans->GN > 0)
            {   /* Add noise: */
                IplImage* pImgN = cvCloneImage(pImgAdd);
                cvRandSetRange( &p->rnd_state, pTrans->GN, 0, -1 );
                cvRand(&p->rnd_state, pImgN);
                cvAdd(pImgN,pImgAdd,pImgAdd);
                cvReleaseImage(&pImgN);
            }   /* Add noise. */

            if(p->Mask)
            {   /* Update only mask: */
                cvOr(pImgMaskAdd, pImgMask, pImgMask);
            }
            else
            {   /* Add image and mask to exist main image and mask: */
                if(p->BG)
                {   /* If image is background: */
                    cvCopy( pImgAdd, pImg, NULL);
                }
                else
                {   /* If image is foreground: */
                    cvCopy( pImgAdd, pImg, pImgMaskAdd);
                    if(p->ObjID>=0)
                        cvOr(pImgMaskAdd, pImgMask, pImgMask);
                }
            }   /* Not mask. */
        }   /*  For not noise. */
        else
        {   /* Process noise video: */

            if( p->noise_type == CV_NOISE_GAUSSIAN ||
                p->noise_type == CV_NOISE_UNIFORM)

            {   /* Gaussan and uniform additive noise: */
                cvAddNoise(pImg,p->noise_type,pTrans->NoiseAmp * pTrans->C, &p->rnd_state);
            }   /* Gaussan and uniform additive noise. */

            if( p->noise_type == CV_NOISE_SPECKLE)
            {   /* Speckle -- multiplicative noise: */
                if(pTrans->I != 0)cvSubS(pImg,cvScalar(pTrans->I,pTrans->I,pTrans->I),pImg);
                cvAddNoise(pImg,p->noise_type,pTrans->NoiseAmp, &p->rnd_state);
                if(pTrans->I != 0)cvAddS(pImg,cvScalar(pTrans->I,pTrans->I,pTrans->I),pImg);
            }   /* Speckle -- multiplicative noise. */

            if( p->noise_type == CV_NOISE_SALT_AND_PEPPER)
            {   /* Salt and pepper: */
                cvAddNoise(pImg,p->noise_type,pTrans->NoiseAmp, &p->rnd_state);
            }   /* Salt and pepper. */
        }   /*  Process noise video.*/
    }   /*  Next item. */

    if(pImg)
    {
        if(pTS->noise_type != CV_NOISE_NONE)
        {   /* Add noise: */
            cvAddNoise(pImg,pTS->noise_type,pTS->noise_ampl);
        }

        if(pTS->IVar_DI != 0)
        {   /* Change intensity: */
            float   I = MIN(pTS->IVar_CurI,pTS->IVar_MaxI);
            I = MAX(I,pTS->IVar_MinI);
            cvScale(pImg,pImg,1,I);

            if(pTS->IVar_CurI >= pTS->IVar_MaxI)
                pTS->IVar_CurDI = (float)-fabs(pTS->IVar_DI);

            if(pTS->IVar_CurI <= pTS->IVar_MinI)
                pTS->IVar_CurDI = (float)+fabs(pTS->IVar_DI);

            pTS->IVar_CurI += pTS->IVar_CurDI;
        }
    }


    pTS->CurFrame++;
    cvReleaseImage(&pImgAdd);
    cvReleaseImage(&pImgAddG);
    cvReleaseImage(&pImgMaskAdd);
    cvReleaseMat(&pT);
    return pImg;

}   /*cvTestSeqQueryFrame*/

IplImage* cvTestSeqGetFGMask(CvTestSeq* pTestSeq)
{
    return ((CvTestSeq_*)pTestSeq)->pImgMask;
}

IplImage* cvTestSeqGetImage(CvTestSeq* pTestSeq)
{
    return ((CvTestSeq_*)pTestSeq)->pImg;
}

int cvTestSeqGetObjectNum(CvTestSeq* pTestSeq)
{
    //return ((CvTestSeq_*)pTestSeq)->ListNum;
    return ((CvTestSeq_*)pTestSeq)->ObjNum;
}

int cvTestSeqGetObjectPos(CvTestSeq* pTestSeq, int ObjIndex, CvPoint2D32f* pPos)
{
    CvTestSeq_*     pTS = (CvTestSeq_*)pTestSeq;
    CvTestSeqElem*  p = pTS->pElemList;
    if(pTS->CurFrame > pTS->FrameNum) return 0;

    for(p=pTS->pElemList; p; p=p->next)
    {
        int frame = pTS->CurFrame - p->FrameBegin - 1;
        if(ObjIndex==p->ObjID && frame >= 0 && frame < p->FrameNum) break;
    }

    if(p && p->pPos && p->PosNum>0)
    {
        CvTSTrans*  pTrans;
        int         frame = pTS->CurFrame - p->FrameBegin - 1;
        if(frame < 0 || frame >= p->FrameNum) return 0;
        //float t = (p->FrameNum>1)?((float)frame / (p->FrameNum-1)):0;
        pTrans = p->pTrans + frame%p->TransNum;
        pPos[0] = p->pPos[frame%p->PosNum];

#if 1   /* Transform using T filed in Trans: */
        {
            float x = pPos->x * (p->pImg?(p->pImg->width-1):1);
            float y = pPos->y * (p->pImg?(p->pImg->height-1):1);

            pPos->x = pTrans->T[0]*x+pTrans->T[1]*y+pTrans->T[2];
            pPos->y = pTrans->T[3]*x+pTrans->T[4]*y+pTrans->T[5];

            if(p->pImg)
            {
                pPos->x /= p->pImg->width-1;
                pPos->y /= p->pImg->height-1;
            }

        }


#else
        pPos->x = pPos->x * pTrans->Scale.x + pTrans->Shift.x;
        pPos->y = pPos->y * pTrans->Scale.y + pTrans->Shift.y;
#endif
        pPos->x *= pTS->pImg->width-1;
        pPos->y *= pTS->pImg->height-1;
        return 1;
    }
    return 0;

}   /* cvTestSeqGetObjectPos */

int cvTestSeqGetObjectSize(CvTestSeq* pTestSeq, int ObjIndex, CvPoint2D32f* pSize)
{
    CvTestSeq_*     pTS = (CvTestSeq_*)pTestSeq;
    CvTestSeqElem*  p = pTS->pElemList;
    if(pTS->CurFrame > pTS->FrameNum) return 0;

    for(p=pTS->pElemList; p; p=p->next)
    {
        int frame = pTS->CurFrame - p->FrameBegin - 1;
        if(ObjIndex==p->ObjID && frame >= 0 && frame < p->FrameNum) break;
    }

    if(p && p->pSize && p->SizeNum>0)
    {
        CvTSTrans*  pTrans;
        int         frame = pTS->CurFrame - p->FrameBegin - 1;

        if(frame < 0 || frame >= p->FrameNum) return 0;

        //float t = (p->FrameNum>1)?((float)frame / (p->FrameNum-1)):0;
        pTrans = p->pTrans + frame%p->TransNum;
        pSize[0] = p->pSize[frame%p->SizeNum];

#if 1   /* Transform using T filed in Trans: */
        {
            float x = pSize->x * (p->pImg?(p->pImg->width-1):1);
            float y = pSize->y * (p->pImg?(p->pImg->height-1):1);
            float   dx1, dx2;
            float   dy1, dy2;

            dx1 = (float)fabs(pTrans->T[0]*x+pTrans->T[1]*y);
            dy1 = (float)fabs(pTrans->T[3]*x+pTrans->T[4]*y);

            dx2 = (float)fabs(pTrans->T[0]*x - pTrans->T[1]*y);
            dy2 = (float)fabs(pTrans->T[3]*x - pTrans->T[4]*y);

            pSize->x = MAX(dx1,dx2);
            pSize->y = MAX(dy1,dy2);

            if(p->pImg)
            {
                pSize->x /= p->pImg->width-1;
                pSize->y /= p->pImg->height-1;
            }

        }


#else
        pSize->x = pSize->x * pTrans->Scale.x;
        pSize->y = pSize->y * pTrans->Scale.y;
#endif
        pSize->x *= pTS->pImg->width-1;
        pSize->y *= pTS->pImg->height-1;
        return 1;
    }

    return 0;

}   /* cvTestSeqGetObjectSize */

/* Add noise to finile image: */
void cvTestSeqAddNoise(CvTestSeq* pTestSeq, int noise_type, double noise_ampl)
{
    CvTestSeq_*     pTS = (CvTestSeq_*)pTestSeq;
    pTS->noise_type = noise_type;
    pTS->noise_ampl = noise_ampl;
}

/* Add Intensity variation: */
void cvTestSeqAddIntensityVariation(CvTestSeq* pTestSeq, float DI_per_frame, float MinI, float MaxI)
{
    CvTestSeq_* pTS = (CvTestSeq_*)pTestSeq;
    pTS->IVar_CurDI = pTS->IVar_DI = DI_per_frame;
    pTS->IVar_MaxI = MaxI;
    pTS->IVar_MinI = MinI;
}

void cvAddNoise(IplImage* pImg, int noise_type, double Ampl, CvRandState* rnd_state)
{   /* Add noise to image: */
    CvSize      S = cvSize(pImg->width,pImg->height);
    IplImage*   pImgAdd = cvCreateImage(S,pImg->depth,pImg->nChannels);
    static CvRandState local_rnd_state;
    static int  first = 1;

    if(first)
    {
        first = 0;
        cvRandInit( &local_rnd_state, 1, 0, 0,CV_RAND_NORMAL);
    }

    if(rnd_state == NULL)rnd_state = &local_rnd_state;

    if( noise_type == CV_NOISE_GAUSSIAN ||
        noise_type == CV_NOISE_UNIFORM)
    {   /* Gaussan and uniform additive noise: */
        int set_zero = 0;

        if( noise_type == CV_NOISE_GAUSSIAN)
        {
            rnd_state->disttype = CV_RAND_NORMAL;
            cvRandSetRange( rnd_state,  Ampl, 0, -1 );
            if(Ampl <= 0) set_zero = 1;
        }

        if( noise_type == CV_NOISE_UNIFORM)
        {
            double max_val =
                1.7320508075688772935274463415059 * Ampl;
            rnd_state->disttype = CV_RAND_UNI;
            cvRandSetRange( rnd_state, -max_val, max_val, -1 );
            if(max_val < 1) set_zero = 1;
        }

        if(!set_zero)
        {
            IplImage*   pImgNoise = cvCreateImage(S,IPL_DEPTH_32F,pImg->nChannels);
            IplImage*   pImgOrg = cvCreateImage(S,IPL_DEPTH_32F,pImg->nChannels);
            cvConvert(pImg, pImgOrg);
            cvRand(rnd_state, pImgNoise);
            cvAdd(pImgOrg,pImgNoise,pImgOrg);
            cvConvert(pImgOrg,pImg);
            cvReleaseImage(&pImgNoise);
            cvReleaseImage(&pImgOrg);
        }
    }   /* Gaussan and uniform additive noise. */

    if( noise_type == CV_NOISE_SPECKLE)
    {   /* Speckle -- multiplicative noise: */
        IplImage* pImgSP = cvCreateImage( S,IPL_DEPTH_32F, pImg->nChannels );
        IplImage* pImgTemp = cvCreateImage(S,IPL_DEPTH_32F, pImg->nChannels );
        rnd_state->disttype = CV_RAND_NORMAL;
        cvRandSetRange( rnd_state, Ampl, 0, -1 );
        cvRand(rnd_state, pImgSP);
        cvConvert(pImg,pImgTemp);
        cvMul(pImgSP,pImgTemp,pImgSP);
        cvAdd(pImgTemp,pImgSP,pImgTemp);
        cvConvert(pImgTemp,pImg);
        cvReleaseImage(&pImgSP);
        cvReleaseImage(&pImgTemp);
    }   /* Speckle -- multiplicative noise. */

    if( noise_type == CV_NOISE_SALT_AND_PEPPER && Ampl > 0)
    {   /* Salt and pepper: */
        IplImage* pImgMask = cvCreateImage( S,IPL_DEPTH_32F, 1 );
        IplImage* pImgMaskBin = cvCreateImage( S,IPL_DEPTH_8U, 1 );
        IplImage* pImgVal = cvCreateImage( S,IPL_DEPTH_8U, 1 );
        rnd_state->disttype = CV_RAND_UNI;

        /* Create mask: */
        cvRandSetRange( rnd_state, 0, 1, -1 );
        cvRand(rnd_state, pImgMask);
        cvThreshold(pImgMask,pImgMask, Ampl, 255, CV_THRESH_BINARY_INV );
        cvConvert(pImgMask,pImgMaskBin);

        /* Create vals: */
        cvRandSetRange( rnd_state, 0, 255, -1 );
        cvRand(rnd_state, pImgVal);
        cvThreshold(pImgVal,pImgVal,128, 255, CV_THRESH_BINARY );
        cvMerge(
            pImgAdd->nChannels>0?pImgVal:NULL,
            pImgAdd->nChannels>1?pImgVal:NULL,
            pImgAdd->nChannels>2?pImgVal:NULL,
            pImgAdd->nChannels>3?pImgVal:NULL,
            pImgAdd);
        cvCopy(pImgAdd, pImg, pImgMaskBin);
        cvReleaseImage(&pImgMask);
        cvReleaseImage(&pImgMaskBin);
        cvReleaseImage(&pImgVal);

    }   /* Salt and pepper. */

    cvReleaseImage(&pImgAdd);

}   /* cvAddNoise */