#include "cv.h"
#include "highgui.h"
#include
#include
iplimage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0;//用hsv中的hue分量進行跟蹤
cvhistogram *hist = 0;//直方圖類
int backproject_mode = 0;
int select_object = 0;
int track_object = 0;
int show_hist = 1;
cvpoint origin;
cvrect selection;
cvrect track_window;
cvbox2d track_box; // meanshift跟蹤演算法返回的box類
cvconnectedcomp track_comp;
int hdims = 50; // 劃分直方圖bins的個數,越多越精確
float hranges_arr = ;//畫素值的範圍
float* hranges = hranges_arr;//用於初始化cvhistogram類
int vmin = 10, vmax = 256, smin = 30;
void on_mouse( int event, int x, int y, int flags,void *notused)//該函式用於選擇跟蹤目標
}cvscalar hsv2rgb( float hue )//用於將hue量轉換成rgb量
, , , , , };
hue *= 0.033333333333333333333333333333333f;
sector = cvfloor(hue);
p = cvround(255*(hue - sector));
p ^= sector & 1 ? 255 : 0;
rgb[sector_data[sector][0]] = 255;
rgb[sector_data[sector][1]] = 0;
rgb[sector_data[sector][2]] = p;
return cvscalar(rgb[2], rgb[1], rgb[0],0);//返回對應的顏色值
}int main( int argc, char** ar** )
cvcopy( frame, image, 0 );
cvcvtcolor( image, hsv, cv_bgr2hsv ); // 把影象從rgb表色系轉為hsv表色系
if( track_object )// 如果當前有需要跟蹤的物體
}cvcalcbackproject( &hue, backproject, hist ); // 得到hue的反向投影圖
cvand( backproject, mask, backproject, 0 );得到反向投影圖mask內的內容
cvcamshift( backproject, track_window,cvtermcriteria( cv_termcrit_eps | cv_termcrit_iter, 10, 1 ),&track_comp, &track_box );//使用meanshift演算法對backproject中的內容進行搜尋,返回跟蹤結果
track_window = track_comp.rect;//得到跟蹤結果的矩形框
if( backproject_mode )
cvcvtcolor( backproject, image, cv_gray2bgr ); // 顯示模式
if( image->origin )
track_box.angle = -track_box.angle;
cvellipsebox( image, track_box, cv_rgb(255,0,0), 3, cv_aa, 0 );//畫出跟蹤結果的位置
}if( select_object && selection.width > 0 && selection.height > 0 )//如果正處於物體選擇,畫出選擇框
c = cvwaitkey(10);
if( c == 27 )
break;
switch( c )
}cvreleasecapture( &capture );
cvdestroywindow("camshiftdemo");
return 0;
}
基於Opencv的SIFT SURF HOG的實現
sift實現 surf的實現 include include includeusing namespace std using namespace cv using namespace cv xfeatures2d int main int argc,char ar namedwindow inpu...
基於opencv的meanshift演算法的初步學習
meanshift 均值漂移。其核心是利用概率密度的梯度來找到區域性最優。影象上用,其實現過程就是在乙個給定的範圍,一直按要求 靠近重心的方向 迭代直到達到預期效果。由於本人以後碩士階段做影象識別和跟蹤方面的研究,索性在目標跟蹤上進一步學習。在opencv2.3中這樣定義 cvapi int cvm...
基於Opencv的MeanShift跟蹤演算法實現
include cv.h include highgui.h include include iplimage image 0,hsv 0,hue 0,mask 0,backproject 0,histimg 0 用hsv中的hue分量進行跟蹤 cvhistogram hist 0 直方圖類 int...