码迷,mamicode.com
首页 > 编程语言 > 详细

C++实现CVPR2010 LLC(局部约束线性编码)

时间:2015-08-13 18:12:54      阅读:402      评论:0      收藏:0      [点我收藏+]

标签:图像分类   llc   matlab   

<div style="orphans: auto; widows: 1;"><span style="font-family:Times New Roman;font-size:18px;"><strong></strong></span></div><span style="font-family:Times New Roman;font-size:18px;"><strong>因自己论文研究需要用到LLC,但作者Jinjun Wang好像只给出了matlab的实现,自己尝试用C++,用到了OpenCV中的Mat类,但速度实在是忒慢了,每个1000*2000左右的图像需要2000多秒,这怎能容忍!谁来帮忙看下哪里可以简化加速嘞?</strong></span>
void LLC_coding_appr(Mat& dic,Mat& x,int knn,vector<double>& His)
{
	double beta=1e-4;
	int nframe=x.rows;
	int nbase=dic.rows;
	Mat sumx(x),sumdic(dic);	
	sumx=x.mul(x);
	sumdic=dic.mul(dic);
	Mat sum_row_x=Mat::zeros(nframe,1,CV_32F);
	Mat sum_row_dic=Mat::zeros(nbase,1,CV_32F);
	float x_row;
	time_t time1,time2;
	time1=time(NULL);
	for(int i=0;i<nframe;++i)
		for(int j=0;j<x.cols;++j)
			sum_row_x.at<float>(i,0)+=sumx.at<float>(i,j);
	for(int i=0;i<nbase;++i)
		for(int j=0;j<dic.cols;++j)
			sum_row_dic.at<float>(i,0)+=sumdic.at<float>(i,j);	
	Mat dict;
	transpose(dic,dict);
	//cout<<dict;
	Mat x_dic=x*dict;	
	Mat sum_row_dict;
	transpose(sum_row_dic,sum_row_dict);
	Mat D=repeat(sum_row_x,1,nbase)+repeat(sum_row_dict,nframe,1)-2*x_dic;
	Mat IDX=Mat::zeros(nframe,knn,CV_8U);
	Mat d;
	multimap<float,int> imap;
	for(int i=0;i<nframe;++i)
	{
		d=D.rowRange(i,i+1);		
		for(int j=0;j<d.cols;++j)
			imap.insert(make_pair(d.at<float>(0,j),j));
		multimap<float,int>::iterator it=imap.begin();
		for(int j=0;j<knn;++j,++it)
			IDX.at<uchar>(i,j)=it->second;
	}
		
	Mat II=Mat::eye(knn,knn,CV_32F);
	Mat Coeff=Mat::zeros(nframe,nbase,CV_32F);
	Mat idx,z,zt,C,w,wt;
	z=Mat::zeros(knn,dic.cols,CV_32F);

	
	for(int i=0;i<nframe;++i)
	{
		idx=IDX.rowRange(i,i+1);
		for(int j=0;j<knn;++j)			
				dic.row(idx.at<uchar>(0,j)).copyTo(z.row(j));
		z=z-repeat(x.row(i),knn,1);
		transpose(z,zt);
		C=z*zt;		
		C=C+II*beta*trace(C).val[0];
		w=C.inv()*Mat::ones(knn,1,CV_32F);
		w/=sum(w).val[0];
		transpose(w,wt);
		for(int j=0;j<knn;++j)
			Coeff.at<float>(i,idx.at<uchar>(0,j))=wt.at<float>(0,j);
	}
		
	Coeff=abs(Coeff);	
	for(int i=0;i<Coeff.cols;++i)
	{
		for(int j=0;j<x.rows;++j)
			His[i]+=Coeff.at<float>(j,i);
		His[i]/=Coeff.rows;
	}	
	time2=time(NULL);
	cout<<time2-time1<<endl;	
}
<span style="font-family:Times New Roman;font-size:18px;"><strong>原文章:Locality-constrained Linear Coding for Image Classification</strong></span>
<span style="font-family:Times New Roman;font-size:18px;"><strong>作者给出的matlab实现:http://www.ifp.illinois.edu/~jyang29/LLC.htm</strong></span>

版权声明:本文为博主原创文章,未经博主允许不得转载。

C++实现CVPR2010 LLC(局部约束线性编码)

标签:图像分类   llc   matlab   

原文地址:http://blog.csdn.net/yiqiwangxi/article/details/47614385

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!