資源簡介
利用opencV和C語言編寫,利用紋理特征比較兩幅圖像的相似度

代碼片段和文件信息
//?CompareImg1.cpp?:?定義控制臺應用程序的入口點。
//
/*2012-3-7?功能:比較兩幅圖的紋理特征,從而判斷兩幅圖像的相似度*/
#include?“stdafx.h“
#include?“cv.h“
#include?“highgui.h“
#include?“cxcore.h“
int?GetTexturePara(CvMat*?paraMatri[4]double?*texturePara);//
double?GetVar(double?ar[4]);//求紋理參數的方差
double?GetMean(double?ar[4]);//求紋理參數的均值
int?GetGLCM(CvMat?*?ImgMatdouble*?texturePara);//由共生矩陣提取各個紋理參數,
//并通過GetTexturePara函數調用GetVar和GetMean函數計算各個參數的均值和方差
double?comparePara(double*?textureArray1double*?textureArray2);//比較兩個圖像矩陣的紋理參數,返回代表相似度的值
double?ComputeTextureDiff(IplImage?*pImg1IplImage?*pImg2);//比較兩幅圖像的相似度,首先調用GetGLCM函數,然后調用函數comparePara
int?main()
{
IplImage?*pImg1?=?cvLoadImage(“testhigh2.jpg“-1);//加載兩幅圖像
IplImage?*pImg2?=?cvLoadImage(“testout3.jpg“-1);
if?(!pImg1?&&?!pImg2?)
{
return?-1;
}
double?dDiff?=?0.0;?//兩幅圖的差別
dDiff?=?ComputeTextureDiff(pImg1pImg2);
???printf(“%f“dDiff);
return?0;
}
double?comparePara(double*?textureArray1double*?textureArray2)
{
double?paraDis[16];
double?sumPara=0;
double?tempmax=?0;
for(int?i=0;i<16;i++)
{
tempmax=textureArray1[i]?>?textureArray2[i]???textureArray1[i]?:?textureArray2[i];
if?(tempmax==0)
{
tempmax?=?1;
}
paraDis[i]=?(textureArray1[i]-textureArray2[i])*(textureArray1[i]-textureArray2[i])/(tempmax*tempmax);
sumPara?+=?paraDis[i];
}
sumPara?=1-?sqrt(sumPara)/16;//相似度?值越大相似度越大
return?sumPara;
}
double?GetMean(double?ar[4])//計算均值,GetGLCM函數中會用到
{
double?sumValue=0;
for?(int?i=0;i<4;i++)
{
sumValue?+=?ar[i];
}
return?(sumValue/4.0);
}
double?GetVar(double?ar[4])//計算方均值,GetGLCM函數中會用到
{
double?sumValue=0;
for?(int?i=0;i<4;i++)
{
sumValue?+=?ar[i]*ar[i];
}
return?(sumValue/4.0);
}
int?GetTexturePara(CvMat*?paraMatri[4]double?*texturePara)
{
double?tempArray[4]={0};
/*static?double?texturePara[16]={0};*/
int?textureNum=0;
for(int?paraNum=0;paraNum<8;paraNum++)
{
for?(int?i=0;i<4;i++)
{
tempArray[i]?=?cvGetReal1D(paraMatri[i]paraNum);???//矩陣每一列都表示相同紋理參數,只是在不同的角度下而已
}
*(texturePara+textureNum)=?GetMean(tempArray);
*(texturePara+textureNum)=GetVar(tempArray)?-?(*(texturePara+textureNum))*?(*(texturePara+textureNum));
textureNum++;
}
return?0;
}
int?GetGLCM(CvMat?*?ImgMatdouble*?texturePara)//生成共生矩陣并求出相關的紋理參數
{
int?ijmndim;
double??minvalue?=?0.0maxvalue?=?0.0;
int?imgWidthimgHeight;
imgWidth?=?ImgMat->width;
imgHeight?=?ImgMat->height;
CvMat*?ImgMatUchar?=?cvCreateMat(imgHeightimgWidthCV_8UC1);//圖像灰度值范圍0-255
cvMinMaxLoc(ImgMat&minvalue&maxvalue);//尋找最大值最小值
//--------------------以下兩行代碼實現圖像歸一化,將像素值歸一化到0-255
cvConvertScale(ImgMatImgMat1-minvalue);//線性變換轉換數組
cvConvertScale(ImgMatImgMat255/(maxvalue-minvalue)0);
cvConvert(ImgMatImgMatUchar);
//--------------------為了提高計算速度,必須對圖像進行量化------------
for(j=0;j for(i=0;i for?(n=0;n<16;n++)//像素分級
{
uchar?temp?=?CV_MAT_ELEM(*ImgMat
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2012-03-08?08:35??CompareImg1\
?????目錄???????????0??2012-03-08?08:47??CompareImg1\CompareImg1\
?????文件????16034816??2012-03-08?08:48??CompareImg1\CompareImg1.ncb
?????文件?????????899??2012-03-07?16:39??CompareImg1\CompareImg1.sln
?????文件???????13312??2012-03-08?08:48??CompareImg1\CompareImg1.suo
?????文件???????12686??2012-03-08?08:47??CompareImg1\CompareImg1\CompareImg1.cpp
?????文件????????4582??2012-03-07?16:41??CompareImg1\CompareImg1\CompareImg1.vcproj
?????文件????????1407??2012-03-08?08:48??CompareImg1\CompareImg1\CompareImg1.vcproj.RMTYL.Administrator.user
?????目錄???????????0??2012-03-08?08:40??CompareImg1\CompareImg1\Debug\
?????文件????????7040??2012-03-08?08:40??CompareImg1\CompareImg1\Debug\BuildLog.htm
?????文件?????????663??2012-03-08?08:32??CompareImg1\CompareImg1\Debug\CompareImg1.exe.em
?????文件?????????728??2012-03-08?08:32??CompareImg1\CompareImg1\Debug\CompareImg1.exe.em
?????文件?????????621??2012-03-08?08:40??CompareImg1\CompareImg1\Debug\CompareImg1.exe.intermediate.manifest
?????文件???????96201??2012-03-08?08:40??CompareImg1\CompareImg1\Debug\CompareImg1.obj
?????文件?????3211264??2012-03-08?08:32??CompareImg1\CompareImg1\Debug\CompareImg1.pch
?????文件??????????65??2012-03-08?08:40??CompareImg1\CompareImg1\Debug\mt.dep
?????文件???????12025??2012-03-08?08:32??CompareImg1\CompareImg1\Debug\stdafx.obj
?????文件??????642048??2012-03-08?08:40??CompareImg1\CompareImg1\Debug\vc90.idb
?????文件??????602112??2012-03-08?08:40??CompareImg1\CompareImg1\Debug\vc90.pdb
?????文件????????1207??2012-03-07?16:39??CompareImg1\CompareImg1\ReadMe.txt
?????文件?????????216??2012-03-07?16:39??CompareImg1\CompareImg1\stdafx.cpp
?????文件?????????233??2012-03-07?16:39??CompareImg1\CompareImg1\stdafx.h
?????文件?????????498??2012-03-07?16:39??CompareImg1\CompareImg1\targetver.h
?????文件???????12772??2012-03-07?15:15??CompareImg1\CompareImg1\testhigh2.jpg
?????文件????????7678??2012-03-07?15:15??CompareImg1\CompareImg1\testlow2.jpg
?????文件????????6539??2012-03-07?15:16??CompareImg1\CompareImg1\testout3.jpg
?????目錄???????????0??2012-03-08?08:34??CompareImg1\Debug\
?????文件???????40448??2012-03-08?08:40??CompareImg1\Debug\CompareImg1.exe
?????文件??????401000??2012-03-08?08:40??CompareImg1\Debug\CompareImg1.ilk
?????文件?????1018880??2012-03-08?08:40??CompareImg1\Debug\CompareImg1.pdb
評論
共有 條評論