void CHshejiView::ZHIFANGTU(HDIB hDIB)
{
if (DIBBitCount(hDIB)!=24)
{
ConvertDIBFormat(hDIB,24,NULL);
}
for (int j=0;j<64;j++)
{
radius[j]=0;
point[j].x=0;
point[j].y=0;
huidurate[j]=0;
}
long cxline=BytesPerLine(hDIB);
int tr,tg,tb;//颜色所属
LPBYTE lpDIB = (LPBYTE)GlobalLock(hDIB);
LPBYTE p_data ;
p_data=FindDIBBits(lpDIB);
unsigned char r,g,b;
long cx=DIBWidth(hDIB);
long cy=DIBHeight(hDIB);
long tongji[64]={0};
long sumxp[64]={0},sumyp[64]={0};
long aspectu[64]={1500},aspectl[64]={1500},aspectr[64]={0},aspectd[64]={0};
// huidurate[64]={0};
for (long i=0;i<cy;i++)
{
for (long j=0;j<cx;j++)
{
r=*(p_data+i*cxline+j*3);
g=*(p_data+i*cxline+j*3+1);
b=*(p_data+i*cxline+j*3+2);
if (r<=64){tr=1;}else if(r<=128){tr=2;}
else if(r<=192){tr=3;}else{tr=4;}
if (g<=64){tg=1;}else if(g<=128){tg=2;}
else if(g<=192){tg=3;}else{tg=4;}
if (b<=64){tb=1;}else if(b<=128){tb=2;}
else if(b<=192){tb=3;}else{tb=4;}
tongji[tr*tg*tb-1]++;
sumxp[tr*tg*tb-1]+=j;
sumyp[tr*tg*tb-1]+=i;
aspectu[tr*tg*tb-1]=min(i,aspectu[tr*tg*tb-1]);
aspectl[tr*tg*tb-1]=min(j,aspectl[tr*tg*tb-1]);
aspectr[tr*tg*tb-1]=max(i,aspectr[tr*tg*tb-1]);
aspectd[tr*tg*tb-1]=max(i,aspectd[tr*tg*tb-1]);
}
}
for ( i=0;i<64;i++)
{
if (tongji[i]!=0)
{
huidurate[i]=tongji[i]/(cx*cy*1.0f);
point[i].x=sumxp[i]/tongji[i];
point[i].y=sumyp[i]/tongji[i];
long tempx1,tempx2,tempy1,tempy2;
tempx1=point[i].x-aspectl[i];
tempx2=aspectr[i]-point[i].x;
tempy1=point[i].y-aspectu[i];
tempy2=aspectd[i]-point[i].y;
radius[i]=max(max(tempx1,tempx2),max(tempy1,tempy2));
}
}
}
空白CHshejiView::ZHIFANGTU(HDIB HDIB)
{
如果(DIBBitCount(hDIB)! = 24)
{
ConvertDIBFormat(hDIB,24岁,NULL);
}
(int j = 0;< 64;j + +)
{
半径[j]= 0;
点[j].x = 0;
点[j].y = 0;
huidurate[j]= 0;
}
长cxline = BytesPerLine(hDIB);
int tr、tg、结核病;/ /颜色所属
LPBYTE lpDIB =(LPBYTE)GlobalLock(hDIB);
LPBYTE p_data;
p_data = FindDIBBits(lpDIB);
无符号字符r,g,b;
长残雪= DIBWidth(hDIB);
长cy = DIBHeight(hDIB);
长同济[64]= { 0 };
长sumxp[64]= { 0 },sumyp[64]= { 0 };
长aspectu[64]= { 1500 },aspectl[64]= { 1
无非就是在MFC的View中化一个直方图
无非就是在MFC的View中画一个直方图
其实是图像检索程序,他的特征提取,我想根据程序来写原理:
//计算图象的直方图,颜色矩,颜色熵
void CHshejiView::ZHIFANGTU(HDIB hDIB)
{
if (DIBBitCount(hDIB)!=24)
{
ConvertDIBFormat(hDIB,24,NULL);
}
for (int j=0;j<64;j++)
{
radius[j]=0;
point[j].x=0;
point[j].y=0;
huidurate[j]=0;
}
long cxline=BytesPerLine(hDIB);
int tr,tg,tb;//颜色所属
LPBYTE lpDIB = (LPBYTE)GlobalLock(hDIB);
LPBYTE p_data ;
p_data=FindDIBBits(lpDIB);
unsigned char r,g,b;
long cx=DIBWidth(hDIB);
long cy=DIBHeight(hDIB);
long tongji[64]={0};
long sumxp[64]={0},sumyp[64]={0};
long aspectu[64]={1500},aspectl[64]={1500},aspectr[64]={0},aspectd[64]={0};
// huidurate[64]={0};
for (long i=0;i<cy;i++)
{
for (long j=0;j<cx;j++)
{
r=*(p_data+i*cxline+j*3);
g=*(p_data+i*cxline+j*3+1);
b=*(p_data+i*cxline+j*3+2);
if (r<=64){tr=1;}else if(r<=128){tr=2;}
else if(r<=192){tr=3;}else{tr=4;}
if (g<=64){tg=1;}else if(g<=128){tg=2;}
else if(g<=192){tg=3;}else{tg=4;}
if (b<=64){tb=1;}else if(b<=128){tb=2;}
else if(b<=192){tb=3;}else{tb=4;}
tongji[tr*tg*tb-1]++;
sumxp[tr*tg*tb-1]+=j;
sumyp[tr*tg*tb-1]+=i;
aspectu[tr*tg*tb-1]=min(i,aspectu[tr*tg*tb-1]);
aspectl[tr*tg*tb-1]=min(j,aspectl[tr*tg*tb-1]);
aspectr[tr*tg*tb-1]=max(i,aspectr[tr*tg*tb-1]);
aspectd[tr*tg*tb-1]=max(i,aspectd[tr*tg*tb-1]);
}
}
for ( i=0;i<64;i++)
{
if (tongji[i]!=0)
{
huidurate[i]=tongji[i]/(cx*cy*1.0f);
point[i].x=sumxp[i]/tongji[i];
point[i].y=sumyp[i]/tongji[i];
long tempx1,tempx2,tempy1,tempy2;
tempx1=point[i].x-aspectl[i];
tempx2=aspectr[i]-point[i].x;
tempy1=point[i].y-aspectu[i];
tempy2=aspectd[i]-point[i].y;
radius[i]=max(max(tempx1,tempx2),max(tempy1,tempy2));
}
}
}
//计算图象的颜色矩
void CHshejiView::HueSquare(HDIB hDIB)
{
if (DIBBitCount(hDIB)!=24)
{
ConvertDIBFormat(hDIB,24,NULL);
}
long cr=0,cg=0,cb=0;//颜色分量
long cxline=BytesPerLine(hDIB);
LPBYTE lpDIB = (LPBYTE)GlobalLock(hDIB);
LPBYTE p_data ;
p_data=FindDIBBits(lpDIB);
unsigned char r,g,b;
long cx=DIBWidth(hDIB);
long cy=DIBHeight(hDIB);
for (long i=0;i<cy;i++)
{
for (long j=0;j<cx;j++)
{
r=*(p_data+i*cxline+j*3);
g=*(p_data+i*cxline+j*3+1);
b=*(p_data+i*cxline+j*3+2);
cr+=(int)r;
cg+=(int)g;
cb+=(int)b;
}
}
mean[0]=meanr1=cr/(cx*cy*1.0f);
mean[1]=meang1=cg/(cx*cy*1.0f);
mean[2]=meanb1=cb/(cx*cy*1.0f);
double cr2=0,cg2=0,cb2=0,cr3=0,cg3=0,cb3=0;//二三阶颜色分量
for (i=0;i<cy;i++)
{
for (long j=0;j<cx;j++)
{
r=*(p_data+i*cxline+j*3);
g=*(p_data+i*cxline+j*3+1);
b=*(p_data+i*cxline+j*3+2);
cr2+=((double)r-(double)meanr1)*((double)r-(double)meanr1);
cr3+=fabs(((double)r-(double)meanr1))*((double)r-(double)meanr1)*((double)r-(double)meanr1);
cg2+=((double)g-(double)meang1)*((double)g-(double)meang1);
cg3+=fabs(((double)g-(double)meang1))*((double)g-(double)meang1)*((double)g-(double)meang1);
cb2+=((double)b-(double)meanb1)*((double)b-(double)meanb1);
cb3+=fabs(((double)b-(double)meanb1))*((double)b-(double)meanb1)*((double)b-(double)meanb1);
}
}
mean[3]=sqrt(cr2/(cx*cy*1.0f));//=variancer2
mean[4]=sqrt(cg2/(cx*cy*1.0f));//=varianceg2
mean[5]=sqrt(cb2/(cx*cy*1.0f));//=varianceb2
mean[6]=pow(cr3/(cx*cy*1.0f),(double)(1/3.0f));//=skewnessr3
mean[7]=pow(cg3/(cx*cy*1.0f),(double)(1/3.0f));//=skewnessg3
mean[8]=pow(cb3/(cx*cy*1.0f),(double)(1/3.0f));//=skewnessb3
}
BOOL CHshejiView::Disposal()
{
CString pathWild =m_merdesfile+"\\*.jpg" ;
long hFile;
struct _finddata_t c_file;
if( (hFile = _findfirst( LPCTSTR(pathWild), &c_file )) == -1L )
{
//::AfxMessageBox("No image files in current directory!\n" ) ;
return false;
}else
{
FILE *pFile1=fopen("D:\\ssfig.txt","wb+");
FILE *pFile2=fopen("D:\\mean.txt","wb+");
FILE *pFile3=fopen("D:\\path.txt","w+");
FILE *pFile4=fopen("D:\\entropy.txt","wb+");
FILE *pFile5=fopen("D:\\areaentropy.txt","wb+");
do
{
AfxGetMainWnd()->SetWindowText(c_file.name);
CString path=m_merdesfile+"\\"+(CString)c_file.name;
CJpeg jpeg;
if (! jpeg.Load(LPCTSTR(path)))
return FALSE;
HDIB hDIB = CopyHandle(jpeg.GetDib()->GetHandle());
if (hDIB == NULL)
return FALSE;
//FILE *pFile1=fopen("H:\\guo\\ssfig.txt","ab+");
ZHIFANGTU(hDIB);
for (int i=0;i<64;i++)
{
CString s;
s.Format(" %f ",huidurate[i]);
fwrite(s,1,strlen(s),pFile1);
//fseek(pFile1,10-strlen(s),SEEK_CUR);
//fseek(pFile,0,SEEK_END);
}
//fclose(pFile1);
//FILE *pFile5=fopen("H:\\guo\\areaentropy.txt","ab+");
ZHIFANGTU(hDIB);
for (i=0;i<64;i++)
{ CString sd="";
double ds=0;
int color=0;
if (radius[i]>0)
{ color=i+1;
ds=ZhiArea(hDIB,point[i],radius[i],color);
}
sd.Format(" %f ",ds);
fwrite(sd,1,strlen(sd),pFile5);
// fseek(pFile5,10-strlen(sd),SEEK_CUR);
}
// fclose(pFile5);
HueSquare(hDIB);
for (i=0;i<9;i++)
{
CString s1;
s1.Format(" %f",mean[i]);
fwrite(s1,1,strlen(s1),pFile2);
fseek(pFile2,15-strlen(s1),SEEK_CUR);
}
ColorEntropy(hDIB);
for (i=0;i<3;i++)
{
CString s1;
s1.Format(" %f",entropy[i]);
fwrite(s1,1,strlen(s1),pFile4);
fseek(pFile4,15-strlen(s1),SEEK_CUR);
}
fwrite(path,1,strlen(path),pFile3);
char ch=10;
fwrite(&ch,1,1,pFile3);
} while (_findnext(hFile, &c_file) == 0);
fclose(pFile1);
fclose(pFile2);
fclose(pFile3);
fclose(pFile4);
fclose(pFile5);
}
_findclose(hFile);
return TRUE;
}
BOOL CHshejiView::ReadmeanFile()
{
FILE pFile2=fopen("D:\mean.txt","rb");
double temp=0;
/*for (int c=0;c<9;c++)
{
mean[c]=0;
}/
// memset(similitude,0,sizeof(similitude));
char ch1[16],ch2[256],ch;
if (pFile2==NULL)
{
MessageBox("阶矩txt文件为空,请初始化文件");
return false;
}
else
{
fseek(pFile2,0,SEEK_END);
long len=ftell(pFile2);
rewind(pFile2);
maxsort=0;
maxsort=len/(15*9)+1;
for (long i=0;i<maxsort;i++)
{
temp=0;
for (long j=0;j<9;j++)
{
fread(ch1,1,15,pFile2);
//ch1[15]=0;
CString sd=ch1;
mean[j]=atof(sd);
temp+=fabs(mean[j]-Msource[j]);
}
similitude[i]=temp/(255*3.0f);
}
fclose(pFile2);
FILE *pFile3=fopen("D:\path.txt","r");
for (int x=0;x<maxsort;x++)
{ int y=0;
BOOL readline=false;
while (!readline)
{
fread(&ch,1,1,pFile3);
if (ch!=10)
{
ch2[y]=ch;
y++;
}
else
{
readline=true;
}
}
ch2[y]=0;
pathimage[x]=ch2;
//strcpy((char *)pathimage[x],ch2);
}
fclose(pFile3);
}
return true;
}
BOOL CHshejiView::ReadssfigFile()
{
FILE *pFile1=fopen("D:\ssfig.txt","rb");
if (pFile1==NULL)
{
MessageBox("直方图txt文件为空,请初始化文件");
return false;
}
else
{
double temp=0;
char ch1[10],ch2[256];
char ch;
fseek(pFile1,0,SEEK_END);
long len=ftell(pFile1);
rewind(pFile1);
maxsort=0;
maxsort=len/(10*64);
for (int i=0;i<maxsort;i++)
{
similitude[i]=0;
for (int j=0;j<64;j++)
{
fread(ch1,1,9,pFile1);
ch1[9]=0;
huidurate[j]=atof(ch1);
temp+=min(huidurate[j],Ssource[j]);
//memset(ch1,0,8);
fseek(pFile1,1,SEEK_CUR);
}
similitude[i]=1-temp;
temp=0;
}
fclose(pFile1);
FILE *pFile3=fopen("D:\\path.txt","r");
for (int x=0;x<maxsort;x++)
{
pathimage[x]="";
int y=0;
BOOL readline=false;
while (!readline)
{
fread(&ch,1,1,pFile3);
if (ch!=10)
{
ch2[y]=ch;
y++;
}
else
{
readline=true;
}
}
ch2[y]=0;
pathimage[x]=ch2;
//strcpy((char *)pathimage[x],ch2);
}
fclose(pFile3);
}
return true;
}
void CHshejiView::SortqM(double a[], int n, CString path1[])
{
int i,j,k;
double t;
CString temp;
for(i=1;i {
for(k=0;k t=a[i];
temp=path1[i];
for(j=i;t>a[j-1]&&j>0;j--)
{
a[j]=a[j-1];
path1[j]=path1[j-1];
}
path1[j]=temp;
a[j]=t;
}
}
void CHshejiView::Sortq(double a[], int n, CString path1[])
{
int i,j,k;
double t;
CString temp;
for(i=1;i {
for(k=0;k t=a[i];
temp=path1[i];
for(j=i;t0;j--)
{
a[j]=a[j-1];
path1[j]=path1[j-1];
}
path1[j]=temp;
a[j]=t;
}
}
BOOL CHshejiView::Showsimage()
{
int id[20]={IDC_OUT_IMAGE1,IDC_OUT_IMAGE2,IDC_OUT_IMAGE3,IDC_OUT_IMAGE4,IDC_OUT_IMAGE5,IDC_OUT_IMAGE6,IDC_OUT_IMAGE7,IDC_OUT_IMAGE8,IDC_OUT_IMAGE9
,IDC_OUT_IMAGE10,IDC_OUT_IMAGE11,IDC_OUT_IMAGE12,IDC_OUT_IMAGE13,IDC_OUT_IMAGE14,IDC_OUT_IMAGE15,IDC_OUT_IMAGE16
,IDC_OUT_IMAGE17,IDC_OUT_IMAGE18,IDC_OUT_IMAGE19,IDC_OUT_IMAGE20};
for (int i=0;i {
CJpeg jpeg;
//m_pDib=NULL;
CString s= pathimage[i];
if (!jpeg.Load((LPCTSTR)s))
{
return false;
}
//if (! jpeg.Load((LPCSTR)s))
//return FALSE;
HDIB hDIB = CopyHandle(jpeg.GetDib()->GetHandle());
if (hDIB == NULL)
return FALSE;
m_pDib1->Attach(hDIB);
CRect rcDest,rcDIB;
CWnd pWnd = (CWnd) GetDlgItem(id[i]);
CDC * pDC = pWnd->GetDC();
rcDIB.top=0;
rcDest.top=1;
rcDIB.left=0;
rcDest.left=1;
rcDIB.bottom=m_pDib1->GetHeight();
rcDIB.right=m_pDib1->GetWidth();
rcDest.bottom=101;
rcDest.right=101;
pDC->Rectangle(0,0,102,102);
// pWnd->GetClientRect( &rcDest);
m_pDib1->Display(pDC,rcDest,rcDIB,WHITENESS);
m_pDib1->Display(pDC,rcDest,rcDIB,SRCCOPY);
m_pDib1->Destroy();
}
return true;
}
void CHshejiView::OnSideSearch()
{
// TODO: Add your command handler code here
showpicture();
if (ReadssfigFile()&&maxsort>20)
{
Sortq(similitude,maxsort,pathimage);
Showsimage();
showradio=true;
HideRadio();
InitializeRadio();
UpdateData(false);
}
showpre=showtarget=showtaxis=true;
showstate=1;
m_but1.EnableWindow(false);
}
void CHshejiView::OnSqurSreach()
{
// TODO: Add your command handler code here
showpicture();
if (ReadmeanFile()&&maxsort>20)
{
Sortq(similitude,maxsort,pathimage);
Showsimage();
showradio=true;
HideRadio();
InitializeRadio();
UpdateData(false);
}
showstate=2;
showpre=showtarget=showtaxis=true;
m_but1.EnableWindow(false);
}
//计算颜色熵
void CHshejiView::ColorEntropy(HDIB hDIB)
{
if (DIBBitCount(hDIB)!=24)
{
ConvertDIBFormat(hDIB,24,NULL);
}
long cxline=BytesPerLine(hDIB);
LPBYTE lpDIB = (LPBYTE)GlobalLock(hDIB);
LPBYTE p_data ;
p_data=FindDIBBits(lpDIB);
unsigned char r,g,b;
long cx=DIBWidth(hDIB);
long cy=DIBHeight(hDIB);
long rtongji[256]={0},gtongji[256]={0},btongji[256]={0};
// double rp,gp,bp;
double tempr=0,tempg=0,tempb=0;
for (long i=0;i<cy;i++)
{
for (long j=0;j<cx;j++)
{
r=*(p_data+i*cxline+j*3);
g=*(p_data+i*cxline+j*3+1);
b=*(p_data+i*cxline+j*3+2);
rtongji[r]++;
gtongji[g]++;
btongji[b]++;
}
}
for (int j=0;j<3;j++)
{
entropy[j]=0;
}
for (int x=0;x<256;x++)
{
tempr=rtongji[x]/(cx*cy*1.0f);
if (tempr>0)
entropy[0]+=-tempr*(log(tempr)/log(256.0));//log10(2.0)/
tempg=gtongji[x]/(cx*cy*1.0f);
if (tempg>0)
entropy[1]+=-tempg*(log(tempg)/log(256.0));//log10(2.0) /log(256)
tempb=btongji[x]/(cx*cy*1.0f);
if (tempb>0)
entropy[2]+=-tempb*(log(tempb)/log(256.0));//log10(2.0)/log(256)
}
}
BOOL CHshejiView::ReadEntropyFile()
{
FILE *pFile4=fopen("D:\entropy.txt","rb");
double temp=0;
char ch1[16],ch2[256],ch;
memset(similitude,0,sizeof(similitude));
if (pFile4==NULL)
{
MessageBox("颜色熵txt文件为空,请初始化文件");
return false;
}
else
{
fseek(pFile4,0,SEEK_END);
long len=ftell(pFile4);
rewind(pFile4);
maxsort=0;
maxsort=len/(15*3)+1;
for (long i=0;i<maxsort;i++)
{
for (long j=0;j<3;j++)
{
fread(ch1,1,15,pFile4);
//ch1[15]=0;
CString sd=ch1;
entropy[j]=atof(sd);
temp+=fabs(entropy[j]-Mentropy[j]);
}
similitude[i]=temp/(3.0f);
temp=0;
}
fclose(pFile4);
FILE *pFile3=fopen("D:\\path.txt","r");
for (int x=0;x<maxsort;x++)
{ int y=0;
BOOL readline=false;
while (!readline)
{
fread(&ch,1,1,pFile3);
if (ch!=10)
{
ch2[y]=ch;
y++;
}
else
{
readline=true;
}
}
ch2[y]=0;
pathimage[x]=ch2;
//strcpy((char *)pathimage[x],ch2);
}
fclose(pFile3);
}
return true;
}
void CHshejiView::OnEntrorySearch()
{
// TODO: Add your command handler code here
showpicture();
if (ReadEntropyFile()&&maxsort>20)
{
Sortq(similitude,maxsort,pathimage);
showradio=true;
HideRadio();
Showsimage();
InitializeRadio();
UpdateData(false);
}
showstate=3;
showpre=showtarget=showtaxis=true;
m_but1.EnableWindow(false);
}
//DEL void CHshejiView::OnShowDiaf()
//DEL {
//DEL // TODO: Add your control notification handler code here
//DEL for (int i=0;i<64;i++)
//DEL {
//DEL huidu[i]=(long)(huidurate[i]*200);
//DEL }
//DEL HistogramShow(IDC_HISTOGRAM);
//DEL m_but1.EnableWindow(false);
//DEL for (i=0;i<64;i++)
//DEL {
//DEL huidu[i]=0;
//DEL }
//DEL }
void CHshejiView::OnUpdateEntrorySearch(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (butents)
{
pCmdUI->Enable(true);
}
else
{
pCmdUI->Enable(false);
}
}