資源簡(jiǎn)介
利用opencV和C語(yǔ)言編寫(xiě),利用紋理特征比較兩幅圖像的相似度

代碼片段和文件信息
//?CompareImg1.cpp?:?定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
/*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]);//求紋理參數(shù)的方差
double?GetMean(double?ar[4]);//求紋理參數(shù)的均值
int?GetGLCM(CvMat?*?ImgMatdouble*?texturePara);//由共生矩陣提取各個(gè)紋理參數(shù),
//并通過(guò)GetTexturePara函數(shù)調(diào)用GetVar和GetMean函數(shù)計(jì)算各個(gè)參數(shù)的均值和方差
double?comparePara(double*?textureArray1double*?textureArray2);//比較兩個(gè)圖像矩陣的紋理參數(shù),返回代表相似度的值
double?ComputeTextureDiff(IplImage?*pImg1IplImage?*pImg2);//比較兩幅圖像的相似度,首先調(diào)用GetGLCM函數(shù),然后調(diào)用函數(shù)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])//計(jì)算均值,GetGLCM函數(shù)中會(huì)用到
{
double?sumValue=0;
for?(int?i=0;i<4;i++)
{
sumValue?+=?ar[i];
}
return?(sumValue/4.0);
}
double?GetVar(double?ar[4])//計(jì)算方均值,GetGLCM函數(shù)中會(huì)用到
{
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);???//矩陣每一列都表示相同紋理參數(shù),只是在不同的角度下而已
}
*(texturePara+textureNum)=?GetMean(tempArray);
*(texturePara+textureNum)=GetVar(tempArray)?-?(*(texturePara+textureNum))*?(*(texturePara+textureNum));
textureNum++;
}
return?0;
}
int?GetGLCM(CvMat?*?ImgMatdouble*?texturePara)//生成共生矩陣并求出相關(guān)的紋理參數(shù)
{
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);//尋找最大值最小值
//--------------------以下兩行代碼實(shí)現(xiàn)圖像歸一化,將像素值歸一化到0-255
cvConvertScale(ImgMatImgMat1-minvalue);//線性變換轉(zhuǎn)換數(shù)組
cvConvertScale(ImgMatImgMat255/(maxvalue-minvalue)0);
cvConvert(ImgMatImgMatUchar);
//--------------------為了提高計(jì)算速度,必須對(duì)圖像進(jìn)行量化------------
for(j=0;j for(i=0;i for?(n=0;n<16;n++)//像素分級(jí)
{
uchar?temp?=?CV_MAT_ELEM(*ImgMat
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????目錄???????????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
評(píng)論
共有 條評(píng)論