首页 > 代码库 > 基于块的反射投影

基于块的反射投影

#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <iostream>
#include "function.h"
CvHistogram* histogram2(IplImage* src,int HSize,int SSize) //返回归一化的histogram
{
IplImage* SrcH=cvCreateImage(cvGetSize(src),8,1);
IplImage* SrcS=cvCreateImage(cvGetSize(src),8,1);
//分割HSV
cvSplit(src,SrcH,SrcS,NULL,NULL);
//创建histogram
CvHistogram* hist;
int dims=2;
int size[]={HSize,SSize};
float RangeH[]={0,180};
float RangeS[]={0,360};
float* ranges[]={RangeH,RangeS};
hist=cvCreateHist(dims,size,CV_HIST_ARRAY,ranges);
//计算histogram
IplImage* image[]={SrcH,SrcS};
cvCalcHist(image,hist);
return hist;
}
int BackProjection(int argc,char** argv) //projection:投射
{
IplImage* temp=cvLoadImage("e:\\picture\\333.jpg");
CvHistogram* hist=histogram(temp,30,32);
IplImage* src=http://www.mamicode.com/cvLoadImage("e:\\picture\\3.jpg");
IplImage* ImageH=cvCreateImage(cvGetSize(src),8,1);
IplImage* ImageS=cvCreateImage(cvGetSize(src),8,1);
IplImage* SrcHSV=cvCreateImage(cvGetSize(src),8,3);
cvCvtColor(src,SrcHSV,CV_BGR2HSV);
cvSplit(SrcHSV,ImageH,ImageS,NULL,NULL);
IplImage* image[]={ImageH,ImageS};
//IplImage* BackProjectResult=cvCreateImage(cvGetSize(src),8,1);
IplImage* BackProjectResult=cvCreateImage(cvSize(src->width-temp->width+1,src->height-temp->height+1),IPL_DEPTH_32F,1); //结果的类型要和输入图像具有同样类型,并且hist注意不要归一化,否则所有的值会被四舍五入为0
cvCalcBackProjectPatch(image,BackProjectResult,cvSize(temp->width,temp->height),hist,CV_COMP_CORREL,1);
double max=0;
double min=0;
cvMinMaxLoc(BackProjectResult,&min,&max);
std::cout<<"max:"<<max<<"min:"<<min<<std::endl;
cvNamedWindow("src");
cvNamedWindow("temp");
cvNamedWindow("BackProjectResult");
cvShowImage("src",src);
cvShowImage("temp",temp);
cvShowImage("BackProjectResult",BackProjectResult);
cvWaitKey(0);
return 0;
}


本文出自 “flyclc” 博客,请务必保留此出处http://flyclc.blog.51cto.com/1385758/1539759