怎么把kalman与camshift合并的matlab
1个回答
展开全部
// CamShift+Kalman.cpp : 定义控制台应拍衡用程序的入口点。
//
#include "stdafx.h"
//#include
#include
#include
#include
/
#define region 50
#define calc_point(kalman) cvPoint( cvRound(kalman[0]),cvRound(kalman[1]))
IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *histimg = 0;
CvHistogram *hist;
IplImage *backproject;
CvPoint lastpoint; //上颂信一帧的中心坐标
CvPoint predictpoint, measurepoint;//预测坐标以及当前帧的坐标
int select_object = 0;
int track_object;
CvPoint origin;
CvPoint point_text;
CvRect selection;
CvRect origin_box;
CvRect track_window;
CvBox2D track_box; // tracking 返回的区域 box,带角度
CvConnectedComp track_comp;
int hdims = 256; // 划分HIST的个数,越高越精确
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;
int vmin = 10, vmax = 256, smin = 30;
int start_track=0;
CvFont font;//显示的文本字体
char nchar[10];//显示数字字符串用
CvMat* measurement;
CvMat* realposition;
const CvMat* prediction;
CvRect search_window;
int filename1_n=0;
int frame_count=0;
void on_mouse( int event, int x, int y, int flags ,void *p)
{
if( !image )
return;
if( image->origin )
y = image->height - y;
if( select_object )
{
selection.x = MIN(x,origin.x);
selection.y = MIN(y,origin.y);
selection.width = selection.x + CV_IABS(x - origin.x);
selection.height = selection.y + CV_IABS(y - origin.y);
selection.x = MAX( selection.x, 0 );
selection.y = MAX( selection.y, 0 );
selection.width = MIN( selection.width, image->width );
selection.height = MIN( selection.height, image->height );
selection.width -= selection.x;
selection.height -= selection.y;
}
switch( event )
{
case CV_EVENT_LBUTTONDOWN:
origin = cvPoint(x,y);
selection = cvRect(x,y,0,0);
select_object = 1;
break;
case CV_EVENT_LBUTTONUP:
select_object = 0;
if( selection.width > 0 && selection.height > 0 )
track_object = -1;
origin_box=selection;
#ifdef _DEBUG
printf("\n # 鼠野贺轮标的选择区域:");
printf("\n X = %d, Y = %d, Width = %d, Height = %d",
selection.x, selection.y, selection.width, selection.height);
#endif
break;
}
}
int main( int argc, char** argv )
{
CvCapture* capture = 0;
IplImage* frame = 0;
capture = cvCaptureFromCAM(0);
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...\n");
return -1;
}
cvNamedWindow( "CamShiftDemo", 1 );
cvSetMouseCallback( "CamShiftDemo", on_mouse, NULL ); // on_mouse 自定义事件
cvCreateTrackbar( "Vmin", "CamShiftDemo", &vmin, 256, 0 );
cvCreateTrackbar( "Vmax", "CamShiftDemo", &vmax, 256, 0 );
cvCreateTrackbar( "Smin", "CamShiftDemo", &smin, 256, 0 );
//初始化kalman
CvKalman* kalman =0;
kalman= InitializeKalman(kalman);
measurement = cvCreateMat( 2, 1, CV_32FC1 );//Z(k)
realposition = cvCreateMat( 4, 1, CV_32FC1 );//real X(k)
for(;;)
{
int i=0,c;
frame = cvQueryFrame( capture );
if( !frame )
break;
if( !image )
{
image = cvCreateImage( cvGetSize(frame), 8, 3 );
image->origin = frame->origin;
hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
hue = cvCreateImage( cvGetSize(frame), 8, 1 );
mask = cvCreateImage( cvGetSize(frame), 8, 1 );
backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
backproject->origin = frame->origin;
hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 ); // 计算直方图
}
cvCopy( frame, image, 0 );
cvCvtColor( image, hsv, CV_BGR2HSV ); // 彩色空间转换 BGR to HSV
frame_count++;
/
cvMeanShift( backproject, track_window,
cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
&track_comp);
/, realposition, 0, measurement );
cvKalmanCorrect( kalman, measurement );
cvRectangle( image , cvPoint(track_window.x,track_window.y),
cvPoint(track_window.x+track_window.width,track_window.y+track_window.height),
CV_RGB(255,0,0),2, 8, 0 );
//把目标是第几个标出来
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,1.0F,1.0F,0,3,8);
sprintf(nchar,"ID:%d",i+1);
point_text=cvPoint(track_window.x,track_window.y+track_window.height);
cvPutText(image,nchar,point_text,&font,CV_RGB(255,255,0));
}
//***********************************************************************************************
if( select_object && selection.width > 0 && selection.height > 0 )
{
cvSetImageROI( image, selection );
cvXorS( image, cvScalarAll(255), image, 0 );
cvResetImageROI( image );
}
cvShowImage( "CamShiftDemo", image );
c = cvWaitKey(30);
//根据键盘关闭跟踪目标
if( c == 27 )
break; // exit from for-loop
}
cvReleaseCapture( &capture );
cvDestroyWindow("CamShiftDemo");
return 0;
}
//
#include "stdafx.h"
//#include
#include
#include
#include
/
#define region 50
#define calc_point(kalman) cvPoint( cvRound(kalman[0]),cvRound(kalman[1]))
IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *histimg = 0;
CvHistogram *hist;
IplImage *backproject;
CvPoint lastpoint; //上颂信一帧的中心坐标
CvPoint predictpoint, measurepoint;//预测坐标以及当前帧的坐标
int select_object = 0;
int track_object;
CvPoint origin;
CvPoint point_text;
CvRect selection;
CvRect origin_box;
CvRect track_window;
CvBox2D track_box; // tracking 返回的区域 box,带角度
CvConnectedComp track_comp;
int hdims = 256; // 划分HIST的个数,越高越精确
float hranges_arr[] = {0,180};
float* hranges = hranges_arr;
int vmin = 10, vmax = 256, smin = 30;
int start_track=0;
CvFont font;//显示的文本字体
char nchar[10];//显示数字字符串用
CvMat* measurement;
CvMat* realposition;
const CvMat* prediction;
CvRect search_window;
int filename1_n=0;
int frame_count=0;
void on_mouse( int event, int x, int y, int flags ,void *p)
{
if( !image )
return;
if( image->origin )
y = image->height - y;
if( select_object )
{
selection.x = MIN(x,origin.x);
selection.y = MIN(y,origin.y);
selection.width = selection.x + CV_IABS(x - origin.x);
selection.height = selection.y + CV_IABS(y - origin.y);
selection.x = MAX( selection.x, 0 );
selection.y = MAX( selection.y, 0 );
selection.width = MIN( selection.width, image->width );
selection.height = MIN( selection.height, image->height );
selection.width -= selection.x;
selection.height -= selection.y;
}
switch( event )
{
case CV_EVENT_LBUTTONDOWN:
origin = cvPoint(x,y);
selection = cvRect(x,y,0,0);
select_object = 1;
break;
case CV_EVENT_LBUTTONUP:
select_object = 0;
if( selection.width > 0 && selection.height > 0 )
track_object = -1;
origin_box=selection;
#ifdef _DEBUG
printf("\n # 鼠野贺轮标的选择区域:");
printf("\n X = %d, Y = %d, Width = %d, Height = %d",
selection.x, selection.y, selection.width, selection.height);
#endif
break;
}
}
int main( int argc, char** argv )
{
CvCapture* capture = 0;
IplImage* frame = 0;
capture = cvCaptureFromCAM(0);
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...\n");
return -1;
}
cvNamedWindow( "CamShiftDemo", 1 );
cvSetMouseCallback( "CamShiftDemo", on_mouse, NULL ); // on_mouse 自定义事件
cvCreateTrackbar( "Vmin", "CamShiftDemo", &vmin, 256, 0 );
cvCreateTrackbar( "Vmax", "CamShiftDemo", &vmax, 256, 0 );
cvCreateTrackbar( "Smin", "CamShiftDemo", &smin, 256, 0 );
//初始化kalman
CvKalman* kalman =0;
kalman= InitializeKalman(kalman);
measurement = cvCreateMat( 2, 1, CV_32FC1 );//Z(k)
realposition = cvCreateMat( 4, 1, CV_32FC1 );//real X(k)
for(;;)
{
int i=0,c;
frame = cvQueryFrame( capture );
if( !frame )
break;
if( !image )
{
image = cvCreateImage( cvGetSize(frame), 8, 3 );
image->origin = frame->origin;
hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
hue = cvCreateImage( cvGetSize(frame), 8, 1 );
mask = cvCreateImage( cvGetSize(frame), 8, 1 );
backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
backproject->origin = frame->origin;
hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 ); // 计算直方图
}
cvCopy( frame, image, 0 );
cvCvtColor( image, hsv, CV_BGR2HSV ); // 彩色空间转换 BGR to HSV
frame_count++;
/
cvMeanShift( backproject, track_window,
cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
&track_comp);
/, realposition, 0, measurement );
cvKalmanCorrect( kalman, measurement );
cvRectangle( image , cvPoint(track_window.x,track_window.y),
cvPoint(track_window.x+track_window.width,track_window.y+track_window.height),
CV_RGB(255,0,0),2, 8, 0 );
//把目标是第几个标出来
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,1.0F,1.0F,0,3,8);
sprintf(nchar,"ID:%d",i+1);
point_text=cvPoint(track_window.x,track_window.y+track_window.height);
cvPutText(image,nchar,point_text,&font,CV_RGB(255,255,0));
}
//***********************************************************************************************
if( select_object && selection.width > 0 && selection.height > 0 )
{
cvSetImageROI( image, selection );
cvXorS( image, cvScalarAll(255), image, 0 );
cvResetImageROI( image );
}
cvShowImage( "CamShiftDemo", image );
c = cvWaitKey(30);
//根据键盘关闭跟踪目标
if( c == 27 )
break; // exit from for-loop
}
cvReleaseCapture( &capture );
cvDestroyWindow("CamShiftDemo");
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询