📄 imageenvalue.cpp
字号:
// ImageEnvalue.cpp: implementation of the CImageEnvalue class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "diblook.h"
#include "ImageEnvalue.h"
#include "dibapi.h"
#include "math.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CImageEnvalue::CImageEnvalue()
{
}
CImageEnvalue::~CImageEnvalue()
{
}
int CImageEnvalue::ImageAverage(LPSTR img)
{
int AverGray;
int bit=DIBBitCount(img);//全色图像为8,彩色图像为24;
if (bit==8)
{
// AfxMessageBox("请打开彩色图像。",MB_ICONSTOP);
// return NULL;
double sum=0;
LONG wide,height;
wide=::DIBWidth(img);//取宽的像素个数
height=::DIBHeight(img);
LPSTR map = ::FindDIBBits(img);//打开图像的位图指针
long size=wide*height;
for (int i=0;i<height;i++)
for (int j=0;j<wide;j++)
{
BYTE image=map[i*wide+j ];
sum+=image;
}
AverGray= (int )sum/size;
}
if (bit==24)
{
double sum=0;
LONG wide,height,DibWidth;
wide=::DIBWidth(img);//取宽的像素个数
height=::DIBHeight(img);
DibWidth=3*wide+( sizeof(DWORD)- 3*wide %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPSTR map = ::FindDIBBits(img); //打开图像的位图指针
long size=wide*height;
for (int i=0;i<height;i++)
for (int j=0;j<DibWidth;j+=3)
{
BYTE image1=map[i*DibWidth+j ];
BYTE image2=map[i*DibWidth+j+1];
BYTE image3=map[i*DibWidth+j+2];
sum+= (double)(image1+ image2+ image3)/3;
}
AverGray= (int )sum/size;
}
return AverGray;
}
double CImageEnvalue::standerd(LPSTR img)
{
double sta=0;
int bit=DIBBitCount(img);//全色图像为8,彩色图像为24;
if (bit==8)
{
LONG wide,height;
wide=::DIBWidth(img);//取宽的像素个数
height=::DIBHeight(img);
LPSTR map = ::FindDIBBits(img);//打开图像的位图指针
double sum=0,u=0;
long size=wide*height;
int ave=ImageAverage(img);
for (int i=0;i<height;i++)
for (int j=0;j<wide;j++)
{
BYTE B=map[i*wide+j];
u= (double)(B-ave);
sum += u*u;
}
sta=sqrt(sum/size);
}
if (bit==24)
{
LONG wide,height,DibWidth;
wide=::DIBWidth(img);//bmfHeader.biWidth;//取宽的像素个数
height=::DIBHeight(img);
DibWidth=3*wide+( sizeof(DWORD)- 3*wide %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPSTR map = ::FindDIBBits(img);// //打开图像的位图指针
double sum=0,u=0;
long size=wide*height;
int ave=ImageAverage(img);
for (int i=0;i<height;i++)
for (int j=0;j<DibWidth;j+=3)
{
BYTE B=map[i*DibWidth+j ];
BYTE G=map[i*DibWidth+j+1];
BYTE R=map[i*DibWidth+j+2];
u= (double)((R+ G+ B)/3-ave);
sum += u*u;
}
sta=sqrt(sum/size);
}
return sta;
}
double CImageEnvalue::Shannon(LPSTR img)
{
//计算出现该像素值的概率
double H=0;
int bit=DIBBitCount(img);//全色图像为8位,彩色图像为24位;
if (bit==8)
{
long wide,height;
wide=::DIBWidth(img);//取宽的像素个数
height=::DIBHeight(img);
LPSTR map = ::FindDIBBits(img); //打开图像的位图指针
int sum=0;
int gray[256];//灰度
double p[256];//概率
for(int k=0;k<256;k++)
{
gray[k]=0;
p[k]=0;
}
for (int i=0;i<height;i++)
for (int j=0;j<wide;j++)
{
BYTE B=map[i*wide+j];
sum= (int)B;
gray[sum]++;
}
for (i=0;i<256;i++)
{
p[i]= (gray[i]/(height*wide*1.0));
if(gray[i] !=0)
H+= (double)(-1)*p[i]*(log(p[i])/log(2));
}
}
if (bit==24)
{
long wide,height,DibWidth;
wide=::DIBWidth(img);//取宽的像素个数
height=::DIBHeight(img);
DibWidth=3*wide+( sizeof(DWORD)- 3*wide %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPSTR map = ::FindDIBBits(img); //打开图像的位图指针
int sum=0;
int gray[256];
double p[256];
for(int k=0;k<256;k++)
{
gray[k]=0;
p[k]=0;
}
for (int i=0;i<height;i++)
for (int j=0;j<DibWidth;j+=3)
{
BYTE B=map[i*DibWidth+j ];
BYTE G=map[i*DibWidth+j+1];
BYTE R=map[i*DibWidth+j+2];
sum= (int)(R+ G+ B)/3;
gray[sum]++;
}
for (i=0;i<256;i++)
{
p[i]= (gray[i]/(height*wide*1.0));
if(gray[i] !=0)
H+= (double)(-1)*p[i]*(log(p[i])/log(2));
}
}
return H;
}
double CImageEnvalue::grade(LPSTR img)
{
double grade=0;
int bit=DIBBitCount(img);//全色图像为8位,彩色图像为24位;
if (bit==8)
{
long wide,height;
wide=::DIBWidth(img);//取宽的像素个数
height=::DIBHeight(img);
LPSTR map = ::FindDIBBits(img);//打开图像的位图指针
for(int i=0;i<height-1;++i)
for(int j=0;j<wide-1;++j)
{
double chazhi=((map[i*wide+j]-map[i*wide+j+1])*(map[i*wide+j]-map[i*wide+j+1])
+(map[i*wide+j]-map[(i+1)*wide+j])*(map[i*wide+j]-map[(i+1)*wide+j]))/2;
grade+=sqrt(chazhi);
}
grade=grade/((height-2)*(wide-1));
}
if (bit==24)
{
long wide,height,DibWidth;
wide=::DIBWidth(img);//取宽的像素个数
height=::DIBHeight(img);
DibWidth=3*wide+( sizeof(DWORD)- 3*wide %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPSTR map = ::FindDIBBits(img);//打开图像的位图指针
LPBYTE temp=new BYTE[wide*height];
int i,j;
for (i=0;i<height;i++)
for (j=0;j<wide;j++)
{
BYTE B=map[i*DibWidth+j*3 ];
BYTE G=map[i*DibWidth+j*3+1];
BYTE R=map[i*DibWidth+j*3+2];
temp[i*wide+j]= (int)(R+ G+ B)/3;
}
for(i=0;i<height-1;++i)
for(j=0;j<wide-1;++j)
{
double chazhi=((temp[i*wide+j]-temp[i*wide+j+1])*(temp[i*wide+j]-temp[i*wide+j+1])
+(temp[i*wide+j]-temp[(i+1)*wide+j])*(temp[i*wide+j]-temp[(i+1)*wide+j]))/2;
grade+=sqrt(chazhi);
}
grade=grade/((height-2)*(wide-1));
delete []temp;
}
return grade;
}
double CImageEnvalue::relation(HDIB hmul,HDIB hout)
{
//img1为原始影像,img2为融合后影像
LPSTR mulDIB;
LPSTR outDIB;
mulDIB= (LPSTR) ::GlobalLock((HGLOBAL) hmul);
outDIB= (LPSTR) ::GlobalLock((HGLOBAL) hout);
if( mulDIB==NULL )
{
AfxMessageBox("多光谱图象为空!",MB_ICONSTOP);
return NULL;
}
if( outDIB==NULL )
{
AfxMessageBox("融合图象为空!",MB_ICONSTOP);
return NULL;
}
LPSTR map1 = ::FindDIBBits(mulDIB);
LPSTR map2 = ::FindDIBBits(outDIB);
LONG wide1,height1,DibWidth1;
wide1=::DIBWidth(mulDIB);//取宽的像素个数
height1=::DIBHeight(mulDIB);
DibWidth1=3*wide1+( sizeof(DWORD)- 3*wide1 %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPBYTE temp1=new BYTE[wide1*height1];
double grade1=0;
int i,j,sum1=0;
for (i=0;i<height1;i++)
for (j=0;j<wide1;j++)
{
BYTE B=map1[i*DibWidth1+j*3 ];
BYTE G=map1[i*DibWidth1+j*3+1];
BYTE R=map1[i*DibWidth1+j*3+2];
temp1[i*wide1+j]= (int)(R+ G+ B)/3;
sum1+=temp1[i*wide1+j];
}
int average1=(int)(sum1/(wide1*height1));//均值
//计算融合后图像
LONG wide2,height2,DibWidth2;
wide2=::DIBWidth(outDIB);//取宽的像素个数
height2=::DIBHeight(outDIB);
DibWidth2=3*wide2+( sizeof(DWORD)- 3*wide2 %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPBYTE temp2=new BYTE[wide2*height2];
int sum2=0;
for (i=0;i<height2;i++)
for (j=0;j<wide2;j++)
{
BYTE B=map2[i*DibWidth2+j*3 ];
BYTE G=map2[i*DibWidth2+j*3+1];
BYTE R=map2[i*DibWidth2+j*3+2];
temp2[i*wide2+j]= (int)(R+ G+ B)/3;
sum2+=temp2[i*wide2+j];
}
int average2=(int)(sum2/(wide2*height2));
//计算相关系数
int he1=0,he2=0,he3=0;
if (height1==height2&&wide1==wide2)
{
for ( i=0;i<height1;i++)
for (j=0;j<wide1;j++)
{
he1 +=(temp1[i*wide1+j]-average1)*(temp2[i*wide2+j]-average2);
he2 +=(temp1[i*wide1+j]-average1)*(temp1[i*wide1+j]-average1);
he3 +=(temp2[i*wide2+j]-average2)*(temp2[i*wide2+j]-average2);
}
}
else return NULL;
double m_linshi1,m_linshi2;
m_linshi1=sqrt(he2);
m_linshi2=sqrt(he3);
double relation=he1/m_linshi2;
relation/=m_linshi1;
delete []temp1;
delete []temp2;
return relation;
}
double CImageEnvalue::piancha(HDIB hmul, HDIB hout)
{
//img1为原始影像,img2为融合后影像
LPSTR mulDIB;
LPSTR outDIB;
mulDIB= (LPSTR) ::GlobalLock((HGLOBAL) hmul);
outDIB= (LPSTR) ::GlobalLock((HGLOBAL) hout);
if( mulDIB == NULL )
{
AfxMessageBox("没有指定多光谱图象!",MB_ICONSTOP);
return NULL;
}
if( outDIB == NULL )
{
AfxMessageBox("没有融合图象!",MB_ICONSTOP);
return NULL;
}
LPSTR map1 = ::FindDIBBits(mulDIB);
LPSTR map2 = ::FindDIBBits(outDIB);
LONG wide1,height1,DibWidth1;
wide1=::DIBWidth(mulDIB);//取宽的像素个数
height1=::DIBHeight(mulDIB);
DibWidth1=3*wide1+( sizeof(DWORD)- 3*wide1 %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPBYTE temp1=new BYTE[wide1*height1];
int i,j,sum1=0;
for (i=0;i<height1;i++)
for (j=0;j<wide1;j++)
{
BYTE B=map1[i*DibWidth1+j*3 ];
BYTE G=map1[i*DibWidth1+j*3+1];
BYTE R=map1[i*DibWidth1+j*3+2];
temp1[i*wide1+j]= (int)(R+ G+ B)/3;
}
//计算融合后图像
LONG wide2,height2,DibWidth2;
wide2=::DIBWidth(outDIB);//取宽的像素个数
height2=::DIBHeight(outDIB);
DibWidth2=3*wide2+( sizeof(DWORD)- 3*wide2 %sizeof(DWORD) )%sizeof(DWORD);//取得原图的每行总位数
LPBYTE temp2=new BYTE[wide2*height2];
for (i=0;i<height2;i++)
for (j=0;j<wide2;j++)
{
BYTE B=map2[i*DibWidth2+j*3 ];
BYTE G=map2[i*DibWidth2+j*3+1];
BYTE R=map2[i*DibWidth2+j*3+2];
temp2[i*wide2+j]= (int)(R+ G+ B)/3;
}
//计算差值
double difference=0;
if (height1==height2&&wide1==wide2)
{
for ( i=0;i<height1;i++)
for (j=0;j<wide1;j++)
{
difference +=(temp1[i*wide1+j]-temp2[i*wide2+j])*(temp1[i*wide1+j]-temp2[i*wide2+j]);
}
}
else
return NULL;
double cha;
difference=difference/(wide1*height1);
cha=sqrt(difference);
delete []temp1;
delete []temp2;
return cha;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -