Ejemplo n.º 1
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.º 2
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);
}