【陆地移动距离】// HistogramCompare.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
#include
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cvcam.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")
//获得指定图像的直方图
CvHistogram *GetImageHistogram(IplImage *pSourceImage)
{
assert(pSourceImage != NULL);
IplImage *pHSV = cvCreateImage(cvGetSize(pSourceImage),IPL_DEPTH_8U,pSourceImage->nChannels);
assert(pHSV != NULL);
IplImage *pH_Plane = cvCreateImage(cvGetSize(pSourceImage),IPL_DEPTH_8U,1);
IplImage *pS_Plane = cvCreateImage(cvGetSize(pSourceImage),IPL_DEPTH_8U,1);
IplImage *pV_Plane = cvCreateImage(cvGetSize(pSourceImage),IPL_DEPTH_8U,1);
IplImage *p_PlaneArray[] = {pH_Plane,pS_Plane};
cvCvtPixToPlane(pSourceImage,pH_Plane,pS_Plane,pV_Plane,NULL);
int nHBins = 30;
int nSBins = 32;
CvHistogram *pResultHistogram = NULL;
{
int szHistSize[] = { nHBins, nSBins };
float dHRanges[] = { 0, 180 };
float dSRanges[] = { 0, 255 };
float *pRanges[] = { dHRanges, dSRanges};
pResultHistogram = cvCreateHist(
2,
szHistSize,
CV_HIST_ARRAY,
pRanges
);
//计算直方图
cvCalcHist(p_PlaneArray,pResultHistogram,0,NULL);
if (pResultHistogram != NULL)
{
return pResultHistogram;
}
else
{
return NULL;
}
}
}
//根据所传来的直方图,计算出所需要的EMD矩阵
CvMat *GetEMDMat(CvHistogram *pHist,int nHBins,int nSBins)
{
assert(pHist != NULL);
int nNumRows = nHBins * nSBins;
CvMat *pResultMat = cvCreateMat(nNumRows,3,CV_32FC1);
for (int h = 0;
h < nHBins;
h++)
for (int s = 0;
s < nSBins;
s++)
{
float dBinValue = https://www.it610.com/article/cvQueryHistValue_2D(pHist,h,s);
cvSet2D(pResultMat, h * nSBins + s, 0, cvScalar(dBinValue));
cvSet2D(pResultMat, h * nSBins + s, 1, cvScalar(h));
cvSet2D(pResultMat, h * nSBins + s, 2, cvScalar(s));
}
if (pResultMat != NULL)
{
return pResultMat;
}
else
{
return NULL;
}
}
//陆地移动距离 主函数
int main(int argc, char* argv[])
{
IplImage *pSourceImage1 = cvLoadImage(argv[1]);
assert(pSourceImage1 != NULL);
CvHistogram *pResultHistogram1 = GetImageHistogram(pSourceImage1);
assert(pResultHistogram1 != NULL);
IplImage *pSourceImage2 = cvLoadImage(argv[2]);
assert(pSourceImage2 != NULL);
CvHistogram *pResultHistogram2 = GetImageHistogram(pSourceImage2);
assert(pResultHistogram2 != NULL);
CvMat *pSourceImage1Sign = GetEMDMat(pResultHistogram1,30,32);
CvMat *pSourceImage2Sign = GetEMDMat(pResultHistogram1,30,32);
float dResult = cvCalcEMD2(pSourceImage1Sign,pSourceImage2Sign,CV_DIST_L2);
printf("%f/n",dResult);
return 0;
}