Ejemplo n.º 1
0
void EnsembleTracker::addAppTemplate(const Mat* frame_set,Rect iniWin)
{
	setAddNew(true);
	_recentHitRecord.at<double>(0,_record_idx)=1.0;
	_recentHitRecord.at<double>(1,_record_idx)=1.0;

	//Thêm vào _template_list
	AppTemplate* tra_template=new AppTemplate(frame_set,iniWin,_template_count);
	_template_list.push_back(tra_template);

	//cập nhật window size 60% _widown_size 40% là iniWin
	Size2f detection_size((float)iniWin.width,(float)iniWin.height);
	_window_size.width= (_template_list.size()==1) ? (float)iniWin.width : (float)(_window_size.width*(1-SCALE_UPDATE_RATE)+detection_size.width*SCALE_UPDATE_RATE);
	_window_size.height= (_template_list.size()==1) ? (float)iniWin.height : (float)(_window_size.height*(1-SCALE_UPDATE_RATE)+detection_size.height*SCALE_UPDATE_RATE);

	// Cập nhật track
	if (
		_result_history.size()==0|| // Nếu là track mới
		getIsNovice()) // Nếu là novice
	{
		_result_temp=iniWin;
		_result_last_no_sus=iniWin;
		_result_bodysize_temp = scaleWin(iniWin,1/TRACKING_TO_BODYSIZE_RATIO);
		_retained_template = new AppTemplate(*tra_template);
	}
	_template_count++;
}
Ejemplo n.º 2
0
void EnsembleTracker::calcConfidenceMap(const Mat* frame_set,Mat& occ_map)
{
	//Dự đoán với kalman filter
	_kf.predict();
	Point center((int)_kf.statePre.at<float>(0,0),(int)_kf.statePre.at<float>(1,0));
	
	double w = _window_size.width/TRACKING_TO_BODYSIZE_RATIO;
	double h = _window_size.height/TRACKING_TO_BODYSIZE_RATIO; 
	h += 2*w;
	w += 2*w;

	Rect roi_win((int)(center.x-0.5*w), (int)(center.y-0.5*h),(int)w,(int)h);
	_cm_win = roi_win;
	_confidence_map = Mat::zeros((int)h,(int)w,CV_32FC1);

	
	Mat final_occ_map;
	occ_map.copyTo(final_occ_map);
	//duyệt các neighbors
	for (list<EnsembleTracker*>::iterator it=_neighbors.begin();it!=_neighbors.end();it++)
	{
		// Tính mask neighbors' nếu chúng không phải novice
		if ((*it)==NULL || (*it)->getIsNovice() || (*it)->getTemplateNum() < ( int)_template_list.size())
			continue;

		Rect r = scaleWin((*it)->getBodysizeResult(),1.0);
		//Đánh dấu đối tượng đã được dùng
		ellipse(final_occ_map,Point((int)(r.x+0.5*r.width),(int)(r.y+0.5*r.height)),Size((int)(0.5*r.width),(int)(0.5*r.height)),0,0,360,Scalar(1),-1);
	}

	//Trường hợp không phải là novice hoặc còn template
	if (!getIsNovice() || _template_list.size()>0)
	{
		list<AppTemplate*>::iterator it;
		float c=0;
		for (it = _template_list.begin();it != _template_list.end(); it++)
		{
			AppTemplate* tr = *it;
			Point shift_vector = tr->getShiftVector()*_window_size.width; //tính toán shiftvector
			tr->calcBP(frame_set,final_occ_map,roi_win+shift_vector);
			_confidence_map += tr->getConfidenceMap();
			c+=1;
		} 
		_confidence_map /= MAX(c,0.0001);
	}
	else//Khi track đứng lại
	{
		Point shift_vector = _retained_template->getShiftVector()*_window_size.width;
		_retained_template->calcBP(frame_set,final_occ_map,roi_win+shift_vector);
		_confidence_map += _retained_template->getConfidenceMap();
	}	
}
Ejemplo n.º 3
0
void EnsembleTracker::track(const Mat* frame_set,Mat& occ_map)
{
	// cập nhật ma trận phương sai kalman filter
	updateKfCov(getBodysizeResult().width);

	_record_idx = (_record_idx+1)-_recentHitRecord.cols*((_record_idx+1)/_recentHitRecord.cols);
	_recentHitRecord.at<double>(0,_record_idx)=0.0;
	_recentHitRecord.at<double>(1,_record_idx)=0.0;

	//Cập nhật cờ nếu là track được match
	setAddNew(false);

	//_kf.predict(); //TEST

	//Tăng biến đếm nếu là novicce
	if (getIsNovice())
		_novice_status_count++;

	double alpha;

	//Cập nhật lại bán kính của track
	alpha = MIN(_phi1_*sqrt(_kf.errorCovPre.at<float>(0,0))/(double)_result_bodysize_temp.width+_phi2_,_phi_max_);
	_match_radius=alpha*_result_bodysize_temp.width;

	Rect iniWin(
		(int)(0.5*(_confidence_map.cols-_window_size.width)),
		(int)(0.5*(_confidence_map.rows-_window_size.height)),
		(int)_window_size.width,
		(int)_window_size.height);

	//meanShift(_confidence_map,iniWin,TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));

	_result_temp = iniWin + Point(_cm_win.x,_cm_win.y);
	_result_bodysize_temp = scaleWin(_result_temp,1/TRACKING_TO_BODYSIZE_RATIO);

	if (getIsNovice()) //nếu là track chưa chắc chắn
	{
		init_kf(this->getResultLastNoSus());
		//_kf.errorCovPre.copyTo(_kf.errorCovPost);
		//_kf.statePost.at<float>(0,0)=(float)(_result_temp.x+0.5*_result_temp.width);
		//_kf.statePost.at<float>(1,0)=(float)(_result_temp.y+0.5*_result_temp.height);
	}
	else
	{		
		// cập nhật lại vị trí
		_result_last_no_sus = _result_temp;
		//correct_kf(_kf,_result_temp);
		init_kf(this->getResultLastNoSus());
	}	
}
Ejemplo n.º 4
0
void EnsembleTracker::calcScore()
{
	Rect roi_result=_result_temp-Point(_cm_win.x,_cm_win.y);
	Rect roi_bodysize = scaleWin(roi_result,1/TRACKING_TO_BODYSIZE_RATIO);

	if (getIsNovice())
		return;

	list<AppTemplate*>::iterator it;
	for (it=_template_list.begin();it!=_template_list.end();it++)
	{
		if (!getIsNovice())
		{
			(*it)->calcScore(roi_result,roi_bodysize);
		}		
	}
	_template_list.sort(compareTemplate);//high to low
}
Ejemplo n.º 5
0
//Cập nhật hist
void EnsembleTracker::updateMatchHist(Mat& frame)
{
	Rect roi_result=getResult();
	Rect roi_result_bodysize = scaleWin(roi_result,1/TRACKING_TO_BODYSIZE_RATIO);
	Rect win = roi_result_bodysize & Rect(0,0,frame.cols,frame.rows);
	Mat roi(frame,win);
	Mat temp;
	Mat mask_win = Mat::zeros(roi.size(),CV_8UC1);
	ellipse(mask_win,Point((int)(0.5*mask_win.cols),(int)(0.5*mask_win.rows)),Size((int)(0.35*mask_win.cols),(int)(0.35*mask_win.rows)),0,0,360,Scalar(1),-1);
	calcHist(&roi,1,channels,mask_win,temp,3,histSize,hRange);
	normalize(temp,temp,1,0,NORM_L1);

	if (_result_history.size()==1)//Nếu là tracker mới
	{
		hist_match_score=1;
		hist=temp;
		return;
	}

	hist_match_score = compareHist(hist,temp,CV_COMP_INTERSECT);
	hist += HIST_MATCH_UPDATE*temp;
	normalize(hist,hist,1,0,NORM_L1);
}
Ejemplo n.º 6
0
AppTemplate::AppTemplate(const Mat* frame_set, const Rect iniWin,int ID)
	:ID(ID)//bgr,hsv,lab
{	
	//get roi out of frame set
	Rect body_win=scaleWin(iniWin,1/TRACKING_TO_BODYSIZE_RATIO);
	Rect roi_win(body_win.x-body_win.width,body_win.y-body_win.width,3*body_win.width,2*body_win.width+body_win.height);
	body_win= body_win&Rect(0,0,frame_set[0].cols,frame_set[0].rows);
	roi_win=roi_win&Rect(0,0,frame_set[0].cols,frame_set[0].rows);
	Mat roi_set[]={Mat(frame_set[0],roi_win),Mat(frame_set[1],roi_win),Mat(frame_set[2],roi_win)};

	
	Rect iniWin_roi=iniWin-Point(roi_win.x,roi_win.y);

	//scores for each channel
	list<ChannelScore> channel_score;
	
	Mat mask_roi(roi_set[0].rows,roi_set[0].cols,CV_8UC1,Scalar(0));
	rectangle(mask_roi,iniWin_roi,Scalar(255),-1);
	Mat inv_mask_roi(roi_set[0].rows,roi_set[0].cols,CV_8UC1,Scalar(255));
	rectangle(inv_mask_roi,body_win-Point(roi_win.x,roi_win.y),Scalar(0),-1);

	//calculate score for each channel
	Mat temp_hist;
	Mat temp_bp;
	int hist_size[]={BIN_NUMBER};
	for (int i=0;i<9;i++)
	{
		float range1[]={0,255};
		if (i==3)
		{
			range1[1]=179;
		}
		const float* hist_range[]={range1};
		
		calcHist(roi_set,3,&i,inv_mask_roi,temp_hist,1,hist_size,hist_range);
		normalize(temp_hist,temp_hist,255,0.0,NORM_L1);//scale to 255 for display

		calcBackProject(roi_set,3,&i,temp_hist,temp_bp,hist_range);
		int c[]={0};
		int hs[]={BIN_NUMBER};
		float hr[]={0,255};
		const float* hrr[]={hr};
		Mat hist_fore;
		Mat hist_back;
		calcHist(&temp_bp,1,c,mask_roi,hist_fore,1,hs,hrr);
		calcHist(&temp_bp,1,c,inv_mask_roi,hist_back,1,hs,hrr);
		normalize(hist_fore,hist_fore,1.0,0.0,NORM_L1);
		normalize(hist_back,hist_back,1.0,0.0,NORM_L1);
		//deal with gray image to get rid of #IND
		double score=getVR(hist_back,hist_fore);
		score=score==score ? score:0;
		channel_score.push_back(ChannelScore(i,score));
	}

	//choose the 2 highest scored channels
	channel_score.sort(compareChannel);
	channels[0]=channel_score.back().idx;
	channel_score.pop_back();
	channels[1]=channel_score.back().idx;
	
	//using 2 best channel to calculate histogram
	for (int i=0;i<2;++i)
	{
		_hRang[i][0]=0;
		if (channels[i]==3)
			_hRang[i][1]=179;	
		else
			_hRang[i][1]=255;	
		hRange[i]=_hRang[i];
	}
	calcHist(roi_set,3,channels,inv_mask_roi,temp_hist,2,hSize,hRange);
	normalize(temp_hist,temp_hist,255,0,NORM_L1);
	Mat final_mask;//mask for sampling
	calcBackProject(roi_set,3,channels,temp_hist,final_mask,hRange);
	threshold(final_mask,final_mask,5,255,CV_THRESH_BINARY_INV);
	          
	final_mask=min(final_mask,mask_roi);

	//choose the best two feature space for foreground****************
	Mat hist_fore,hist_back;
	channel_score.clear();
	double sum_score=0;
	for (int i=0;i<9;i++)
	{
		float range1[]={0,255};
		if (i==3)
		{
			range1[1]=179;
		}
		const float* hist_range[]={range1};
		Mat temp_hist_neg;
		calcHist(roi_set,3,&i,final_mask,temp_hist,1,hist_size,hist_range);
		normalize(temp_hist,temp_hist,255,0,NORM_L1);
		calcHist(roi_set,3,&i,inv_mask_roi,temp_hist_neg,1,hist_size,hist_range);
		normalize(temp_hist_neg,temp_hist_neg,255,0,NORM_L1);
		log(temp_hist,temp_hist);		
		log(temp_hist_neg,temp_hist_neg);
		temp_hist=temp_hist-temp_hist_neg;
		threshold(temp_hist,temp_hist,0,255,CV_THRESH_TOZERO);
		normalize(temp_hist,temp_hist,255,0.0,NORM_L1);//scale to 255 for display

		calcBackProject(roi_set,3,&i,temp_hist,temp_bp,hist_range);
		int c[]={0};
		int hs[]={BIN_NUMBER};
		float hr[]={0,255};
		const float* hrr[]={hr};
		calcHist(&temp_bp,1,c,final_mask,hist_fore,1,hs,hrr);
		calcHist(&temp_bp,1,c,inv_mask_roi,hist_back,1,hs,hrr);
		normalize(hist_fore,hist_fore,1.0,0.0,NORM_L1);
		normalize(hist_back,hist_back,1.0,0.0,NORM_L1);
		double score=getVR(hist_back,hist_fore);
		score=score==score ? score:0;
		channel_score.push_back(ChannelScore(i,score));
		sum_score+=exp(score);
	}


	channel_score.sort(compareChannel);
	channels[0]=channel_score.back().idx;
	channel_score.pop_back();
	channels[1]=channel_score.back().idx;

	for (int i=0;i<2;++i)
	{
		_hRang[i][0]=0;
		if (channels[i]==3)
			_hRang[i][1]=179;	
		else
			_hRang[i][1]=255;	
		hRange[i]=_hRang[i];
	}
	calcHist(roi_set,3,channels,final_mask,hist,2,hSize,hRange);///////////////////
	normalize(hist,hist,255,0,NORM_L1);

	//recover the shift_vector
	Mat backPro;
	calcBackProject(roi_set,3,channels,hist,backPro,hRange);
	iniWin_roi=iniWin-Point(roi_win.x,roi_win.y);
	Point2f origin_point_roi((float)(iniWin_roi.x+0.5*iniWin_roi.width),(float)(iniWin_roi.y+0.5*iniWin_roi.height));
	meanShift(backPro,iniWin_roi,TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));

	Point2f shift_point_roi((float)(iniWin_roi.x+0.5*iniWin_roi.width),(float)(iniWin_roi.y+0.5*iniWin_roi.height));
	shift_vector=(shift_point_roi-origin_point_roi)*(1/(float)iniWin.width);
}