没有合适的资源?快使用搜索试试~ 我知道了~
首页meanshift实现视频中动态目标的跟踪
资源详情
资源评论
资源推荐

//#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#dene u_char unsigned char
#dene DIST 0.5
#dene NUM 20
//全局变量
bool pause = false;
bool is_tracking = false;
CvRect drawing_box;
IplImage *current;
double *hist1, *hist2;
double *m_wei;
double C = 0.0;
void init_target(double *hist1, double *m_wei, IplImage *current)
{
IplImage *pic_hist = 0;
int t_h, t_w, t_x, t_y;
double h, dist;
int i, j;
int q_r, q_g, q_b, q_temp;
t_h = drawing_box.height;
t_w = drawing_box.width;
t_x = drawing_box.x;
t_y = drawing_box.y;
//pow 函数是求每个矩阵元素 src(I) 的 p 次幂 dst[I] = src(I)p
h = pow(((double)t_w) / 2, 2) + pow(((double)t_h) / 2, 2); //带宽
pic_hist = cvCreateImage(cvSize(300, 200), IPL_DEPTH_8U, 3); //生成直方图图像
//初始化权值矩阵和目标直方图
for (i = 0; i < t_w*t_h; i++)
{
m_wei[i] = 0.0;
}
for (i = 0; i < 4096; i++)

{
hist1[i] = 0.0;
}
for (i = 0; i < t_h; i++)
{
for (j = 0; j < t_w; j++)
{
dist = pow(i - (double)t_h / 2, 2) + pow(j - (double)t_w / 2, 2);
m_wei[i*t_w + j] = 1 - dist / h;
C += m_wei[i*t_w + j];
}
}
//计算目标权值直方
for (i = t_y; i < t_y + t_h; i++)
{
for (j = t_x; j < t_x + t_w; j++)
{
//rgb 颜色空间量化为 16*16*16 bins ???
q_r = ((u_char)current->imageData[i*current->widthStep + j * 3 + 2]) / 16;
q_g = ((u_char)current->imageData[i*current->widthStep + j * 3 + 1]) / 16;
q_b = ((u_char)current->imageData[i*current->widthStep + j * 3 + 0]) / 16;
q_temp = q_r * 256 + q_g * 16 + q_b;
hist1[q_temp] = hist1[q_temp] + m_wei[(i - t_y)*t_w + (j - t_x)];
}
}
//归一化直方图
for (i = 0; i < 4096; i++)
{
hist1[i] = hist1[i] / C;
}
//生成目标直方图
double temp_max = 0.0;
for (i = 0; i < 4096; i++) //求直方图最大值,为了归一化
{
if (temp_max < hist1[i])
{

temp_max = hist1[i];
}
}
//画直方图
CvPoint p1, p2;
double bin_width = (double)pic_hist->width / 4096;
double bin_unith = (double)pic_hist->height / temp_max;
for (i = 0; i < 4096; i++)
{
p1.x = i * bin_width;
p1.y = pic_hist->height;
p2.x = (i + 1)*bin_width;
p2.y = pic_hist->height - hist1[i] * bin_unith;
cvRectangle(pic_hist, p1, p2, cvScalar(0, 255, 0), -1, 8, 0);
}
cvSaveImage("hist1.jpg", pic_hist);
cvReleaseImage(&pic_hist);
}
void MeanShift_Tracking(IplImage *current)
{
int num = 0, i = 0, j = 0;
int t_w = 0, t_h = 0, t_x = 0, t_y = 0;
double *w = 0, *hist2 = 0;
double sum_w = 0, x1 = 0, x2 = 0, y1 = 2.0, y2 = 2.0;
int q_r, q_g, q_b;
int *q_temp;
IplImage *pic_hist = 0;
t_w = drawing_box.width;
t_h = drawing_box.height;
pic_hist = cvCreateImage(cvSize(300, 200), IPL_DEPTH_8U, 3);
hist2 = (double *)malloc(sizeof(double) * 4096);
w = (double *)malloc(sizeof(double) * 4096);
q_temp = (int *)malloc(sizeof(int)*t_w*t_h);
while ((pow(y2, 2) + pow(y1, 2) > 0.5) && (num < NUM))
剩余10页未读,继续阅读
















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0