Path: blob/master/modules/video/src/compat_video.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000, Intel Corporation, all rights reserved.13// Copyright (C) 2013, OpenCV Foundation, all rights reserved.14// Third party copyrights are property of their respective owners.15//16// Redistribution and use in source and binary forms, with or without modification,17// are permitted provided that the following conditions are met:18//19// * Redistribution's of source code must retain the above copyright notice,20// this list of conditions and the following disclaimer.21//22// * Redistribution's in binary form must reproduce the above copyright notice,23// this list of conditions and the following disclaimer in the documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "precomp.hpp"43#include "opencv2/video/tracking_c.h"444546/////////////////////////// Meanshift & CAMShift ///////////////////////////4748CV_IMPL int49cvMeanShift( const void* imgProb, CvRect windowIn,50CvTermCriteria criteria, CvConnectedComp* comp )51{52cv::Mat img = cv::cvarrToMat(imgProb);53cv::Rect window = windowIn;54int iters = cv::meanShift(img, window, criteria);5556if( comp )57{58comp->rect = cvRect(window);59comp->area = cvRound(cv::sum(img(window))[0]);60}6162return iters;63}646566CV_IMPL int67cvCamShift( const void* imgProb, CvRect windowIn,68CvTermCriteria criteria,69CvConnectedComp* comp,70CvBox2D* box )71{72cv::Mat img = cv::cvarrToMat(imgProb);73cv::Rect window = windowIn;74cv::RotatedRect rr = cv::CamShift(img, window, criteria);7576if( comp )77{78comp->rect = cvRect(window);79cv::Rect roi = rr.boundingRect() & cv::Rect(0, 0, img.cols, img.rows);80comp->area = cvRound(cv::sum(img(roi))[0]);81}8283if( box )84*box = cvBox2D(rr);8586return rr.size.width*rr.size.height > 0.f ? 1 : -1;87}8889///////////////////////////////// Kalman ///////////////////////////////9091CV_IMPL CvKalman*92cvCreateKalman( int DP, int MP, int CP )93{94CvKalman *kalman = 0;9596if( DP <= 0 || MP <= 0 )97CV_Error( CV_StsOutOfRange,98"state and measurement vectors must have positive number of dimensions" );99100if( CP < 0 )101CP = DP;102103/* allocating memory for the structure */104kalman = (CvKalman *)cvAlloc( sizeof( CvKalman ));105memset( kalman, 0, sizeof(*kalman));106107kalman->DP = DP;108kalman->MP = MP;109kalman->CP = CP;110111kalman->state_pre = cvCreateMat( DP, 1, CV_32FC1 );112cvZero( kalman->state_pre );113114kalman->state_post = cvCreateMat( DP, 1, CV_32FC1 );115cvZero( kalman->state_post );116117kalman->transition_matrix = cvCreateMat( DP, DP, CV_32FC1 );118cvSetIdentity( kalman->transition_matrix );119120kalman->process_noise_cov = cvCreateMat( DP, DP, CV_32FC1 );121cvSetIdentity( kalman->process_noise_cov );122123kalman->measurement_matrix = cvCreateMat( MP, DP, CV_32FC1 );124cvZero( kalman->measurement_matrix );125126kalman->measurement_noise_cov = cvCreateMat( MP, MP, CV_32FC1 );127cvSetIdentity( kalman->measurement_noise_cov );128129kalman->error_cov_pre = cvCreateMat( DP, DP, CV_32FC1 );130131kalman->error_cov_post = cvCreateMat( DP, DP, CV_32FC1 );132cvZero( kalman->error_cov_post );133134kalman->gain = cvCreateMat( DP, MP, CV_32FC1 );135136if( CP > 0 )137{138kalman->control_matrix = cvCreateMat( DP, CP, CV_32FC1 );139cvZero( kalman->control_matrix );140}141142kalman->temp1 = cvCreateMat( DP, DP, CV_32FC1 );143kalman->temp2 = cvCreateMat( MP, DP, CV_32FC1 );144kalman->temp3 = cvCreateMat( MP, MP, CV_32FC1 );145kalman->temp4 = cvCreateMat( MP, DP, CV_32FC1 );146kalman->temp5 = cvCreateMat( MP, 1, CV_32FC1 );147148#if 1149kalman->PosterState = kalman->state_pre->data.fl;150kalman->PriorState = kalman->state_post->data.fl;151kalman->DynamMatr = kalman->transition_matrix->data.fl;152kalman->MeasurementMatr = kalman->measurement_matrix->data.fl;153kalman->MNCovariance = kalman->measurement_noise_cov->data.fl;154kalman->PNCovariance = kalman->process_noise_cov->data.fl;155kalman->KalmGainMatr = kalman->gain->data.fl;156kalman->PriorErrorCovariance = kalman->error_cov_pre->data.fl;157kalman->PosterErrorCovariance = kalman->error_cov_post->data.fl;158#endif159160return kalman;161}162163164CV_IMPL void165cvReleaseKalman( CvKalman** _kalman )166{167CvKalman *kalman;168169if( !_kalman )170CV_Error( CV_StsNullPtr, "" );171172kalman = *_kalman;173if( !kalman )174return;175176/* freeing the memory */177cvReleaseMat( &kalman->state_pre );178cvReleaseMat( &kalman->state_post );179cvReleaseMat( &kalman->transition_matrix );180cvReleaseMat( &kalman->control_matrix );181cvReleaseMat( &kalman->measurement_matrix );182cvReleaseMat( &kalman->process_noise_cov );183cvReleaseMat( &kalman->measurement_noise_cov );184cvReleaseMat( &kalman->error_cov_pre );185cvReleaseMat( &kalman->gain );186cvReleaseMat( &kalman->error_cov_post );187cvReleaseMat( &kalman->temp1 );188cvReleaseMat( &kalman->temp2 );189cvReleaseMat( &kalman->temp3 );190cvReleaseMat( &kalman->temp4 );191cvReleaseMat( &kalman->temp5 );192193memset( kalman, 0, sizeof(*kalman));194195/* deallocating the structure */196cvFree( _kalman );197}198199200CV_IMPL const CvMat*201cvKalmanPredict( CvKalman* kalman, const CvMat* control )202{203if( !kalman )204CV_Error( CV_StsNullPtr, "" );205206/* update the state */207/* x'(k) = A*x(k) */208cvMatMulAdd( kalman->transition_matrix, kalman->state_post, 0, kalman->state_pre );209210if( control && kalman->CP > 0 )211/* x'(k) = x'(k) + B*u(k) */212cvMatMulAdd( kalman->control_matrix, control, kalman->state_pre, kalman->state_pre );213214/* update error covariance matrices */215/* temp1 = A*P(k) */216cvMatMulAdd( kalman->transition_matrix, kalman->error_cov_post, 0, kalman->temp1 );217218/* P'(k) = temp1*At + Q */219cvGEMM( kalman->temp1, kalman->transition_matrix, 1, kalman->process_noise_cov, 1,220kalman->error_cov_pre, CV_GEMM_B_T );221222/* handle the case when there will be measurement before the next predict */223cvCopy(kalman->state_pre, kalman->state_post);224225return kalman->state_pre;226}227228229CV_IMPL const CvMat*230cvKalmanCorrect( CvKalman* kalman, const CvMat* measurement )231{232if( !kalman || !measurement )233CV_Error( CV_StsNullPtr, "" );234235/* temp2 = H*P'(k) */236cvMatMulAdd( kalman->measurement_matrix, kalman->error_cov_pre, 0, kalman->temp2 );237/* temp3 = temp2*Ht + R */238cvGEMM( kalman->temp2, kalman->measurement_matrix, 1,239kalman->measurement_noise_cov, 1, kalman->temp3, CV_GEMM_B_T );240241/* temp4 = inv(temp3)*temp2 = Kt(k) */242cvSolve( kalman->temp3, kalman->temp2, kalman->temp4, CV_SVD );243244/* K(k) */245cvTranspose( kalman->temp4, kalman->gain );246247/* temp5 = z(k) - H*x'(k) */248cvGEMM( kalman->measurement_matrix, kalman->state_pre, -1, measurement, 1, kalman->temp5 );249250/* x(k) = x'(k) + K(k)*temp5 */251cvMatMulAdd( kalman->gain, kalman->temp5, kalman->state_pre, kalman->state_post );252253/* P(k) = P'(k) - K(k)*temp2 */254cvGEMM( kalman->gain, kalman->temp2, -1, kalman->error_cov_pre, 1,255kalman->error_cov_post, 0 );256257return kalman->state_post;258}259260///////////////////////////////////// Optical Flow ////////////////////////////////261262CV_IMPL void263cvCalcOpticalFlowPyrLK( const void* arrA, const void* arrB,264void* /*pyrarrA*/, void* /*pyrarrB*/,265const CvPoint2D32f * featuresA,266CvPoint2D32f * featuresB,267int count, CvSize winSize, int level,268char *status, float *error,269CvTermCriteria criteria, int flags )270{271if( count <= 0 )272return;273CV_Assert( featuresA && featuresB );274cv::Mat A = cv::cvarrToMat(arrA), B = cv::cvarrToMat(arrB);275cv::Mat ptA(count, 1, CV_32FC2, (void*)featuresA);276cv::Mat ptB(count, 1, CV_32FC2, (void*)featuresB);277cv::Mat st, err;278279if( status )280st = cv::Mat(count, 1, CV_8U, (void*)status);281if( error )282err = cv::Mat(count, 1, CV_32F, (void*)error);283cv::calcOpticalFlowPyrLK( A, B, ptA, ptB, st,284error ? cv::_OutputArray(err) : (cv::_OutputArray)cv::noArray(),285winSize, level, criteria, flags);286}287288289CV_IMPL void cvCalcOpticalFlowFarneback(290const CvArr* _prev, const CvArr* _next,291CvArr* _flow, double pyr_scale, int levels,292int winsize, int iterations, int poly_n,293double poly_sigma, int flags )294{295cv::Mat prev = cv::cvarrToMat(_prev), next = cv::cvarrToMat(_next);296cv::Mat flow = cv::cvarrToMat(_flow);297CV_Assert( flow.size() == prev.size() && flow.type() == CV_32FC2 );298cv::calcOpticalFlowFarneback( prev, next, flow, pyr_scale, levels,299winsize, iterations, poly_n, poly_sigma, flags );300}301302303