📄 testdlg.cpp
字号:
//m_lCount1[imge_count][(*lpSrc1)]++;
//m_lCount2[imge_count][*(lpSrc1+1)]++;
m_lCount3[imge_count][*(lpSrc1+2)]++;
}
//归一化处理
for(i=0;i<256;i++)
{//m_lCount1[imge_count][i]=m_lCount1[imge_count][i]/(m_lHeight*m_lWidth);
//m_lCount2[imge_count][i]=m_lCount2[imge_count][i]/(m_lHeight*m_lWidth);
m_lCount3[imge_count][i]=m_lCount3[imge_count][i]/(m_lHeight*m_lWidth);
}
//图像指针加一
imge_count++;
//解除锁定
::GlobalUnlock((HGLOBAL)hDIB);
}
writedatabase();//将特征写入特征库
m_time2=GetCurrentTime();
int ntime2_m=m_time2.GetMinute();
int ntime2_s=m_time2.GetSecond();
ntime=(ntime2_m-ntime1_m)*60+(ntime2_s-ntime1_s);
SetTimer(1,50,0);
}
void CTestDlg::writedatabase()
{
//定义文件对象
CFile f1,f2,f3;
CFileException fe;
//创建文件
/* f1.Open(str_Temp+"\\feature\\feature1.dat",CFile::modeReadWrite|CFile::modeCreate,&fe);
f1.WriteHuge(m_lCount1,450*256*sizeof(double));
f1.Close();
f2.Open(str_Temp+"\\feature\\feature2.dat",CFile::modeReadWrite|CFile::modeCreate,&fe);
f2.WriteHuge(m_lCount2,450*256*sizeof(double));
f2.Close();*/
f3.Open(str_Temp+"\\feature\\feature3.dat",CFile::modeReadWrite|CFile::modeCreate,&fe);
f3.WriteHuge(m_lCount3,450*256*sizeof(float));
//向文件中写入
f3.Close();
UpdateWindow();
}
void CTestDlg::OnSearch()
{
// TODO: Add your control notification handler code here
//取两幅图像的直方图在各个灰度级上的较小值,累加后即表示图像之间的相似程度。
//这种相似度实际上表示两幅图像的公共部分。
Dflag=docufile.OnOpenDocument(strNamaFile);
//指向DIB的指针
LPSTR lpDIB;
//指向DIB像素指针
LPSTR lpDIBBits;
//获取DIB
HDIB hDIB =docufile.m_hDIB ;
//锁定DIB
lpDIB=(LPSTR)::GlobalLock((HGLOBAL)hDIB);
//找到DIB图像像素起始位置
lpDIBBits=::FindDIBBits(lpDIB);
//图像每行的字节数
long lLineBytes;
//计算图像的宽度和高度
long m_lWidth=::DIBWidth(lpDIB);
long m_lHeight=::DIBHeight(lpDIB);
//计算图像每行的字节数
lLineBytes=WIDTHBYTES(m_lWidth*8);
//循环变量
long i;
long j;
//记录被检索图像的特征
float m_search1[256];
float m_search2[256];
float m_search3[256];
for(i=0;i<256;i++)
{ m_search1[i]=0;
m_search2[i]=0;
m_search3[i]=0;
}
unsigned char *lpSrc1;
//计算各个灰度值的计数
for(i=0;i<m_lHeight;i++)
for(j=0;j<m_lWidth;j++)
{
lpSrc1 = (unsigned char*)lpDIBBits + lLineBytes * i*3 + j*3;
m_search1[(*lpSrc1)]++;
m_search2[*(lpSrc1+1)]++;
m_search3[*(lpSrc1+2)]++;
}
//归一化处理
for(i=0;i<256;i++)
{m_search1[i]=m_search1[i]/(m_lHeight*m_lWidth);
m_search2[i]=m_search2[i]/(m_lHeight*m_lWidth);
m_search3[i]=m_search3[i]/(m_lHeight*m_lWidth);
}
//解除锁定
::GlobalUnlock((HGLOBAL)hDIB);
//打开库文件
CFile f1,f2,f3;
CFileException fe;
// f1.Open(str_Temp+"\\feature\\feature1.dat",CFile::modeRead,&fe);
// f2.Open(str_Temp+"\\feature\\feature2.dat",CFile::modeRead,&fe);
f3.Open(str_Temp+"\\feature\\feature3.dat",CFile::modeRead,&fe);
// unsigned int size1=f1.GetLength();
// unsigned int size2=f2.GetLength();
unsigned int size3=f3.GetLength();
// double *lib1=new double[size1/sizeof(double)];
// double *lib2=new double[size2/sizeof(double)];
float *lib3=new float[size3/sizeof(float)];
// if (f1.Read(lib1,size1)!=size1)
// {
// MessageBox("file opening error");
// return;
// }
// f1.Close();
// if (f2.Read(lib2,size2)!=size2)
// {
// MessageBox("file opening error");
// return;
// }
// f2.Close();
if (f3.Read(lib3,size3)!=size3)
{
MessageBox("file opening error");
return;
}
f3.Close();
//图像编号
int psim[450];
//两幅图像之间的相似度
float sim[450];
//初始化
for(i=0;i<450;i++)
psim[i]=i;
for(i=0;i<450;i++)
sim[i]=0;
//中间变量
float ttemp1=0,ttemp2=0,ttemp3=0;
//计算被检索图像与库中图像的相似性
for(i=0;i<450;i++)
{
for(j=0;j<256;j++)
{
// ttemp1=lib1[256*i+j]-m_search1[j];
// ttemp2=lib2[256*i+j]-m_search2[j];
ttemp3=lib3[256*i+j]-m_search3[j];
// if (ttemp1<0)
// ttemp1*=-1;
// if (ttemp2<0)
// ttemp2*=-1;
if (ttemp3<0)
ttemp3*=-1;
sim[i]+=(0.11*ttemp1+0.59*ttemp2+0.3*ttemp3);
}
}
//相似度
float tempf;
//编号
int tempp;
for(i=450;i>0;i--)
for(j=0;j<449;j++)
{
if(sim[j]>sim[j+1])
{
tempf=sim[j];
tempp=psim[j];
sim[j]=sim[j+1];
psim[j]=psim[j+1];
sim[j+1]=tempf;
psim[j+1]=tempp;
}
}
//输出结果图片
// CBmpProc bmp;
// CBmpProc *pDestBmp;
// CWnd *pWnd;
// pWnd=GetDlgItem(IDC_STATIC);
// pWnd->Invalidate();
// pWnd->UpdateWindow();
// CDC* pDC=pWnd->GetDC();
// CClientDC dc(pWnd);
CString pic_path;
// int space=-128;
// int space1=-128;
// int space2=-128;
//定义图片地址的字符串
///////////////
// comp_pic_path.Format(str_Temp+"\\image\\%d.bmp",comp_pic);
// strNamaFile = dlg.GetPathName();
/////////////////
CString strall;
CString picn;
int num=0;
char * a[256];
//将相似度最大的4幅图像输出
for(int tt=0;tt<counts;tt++)
{
strall.Format(str_Temp+"\\image\\%d.bmp",psim[tt]);
m_imgSearchall.Load(strall, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
num++;
CWnd* pWnd1 = GetDlgItem(IDC_STATIC_TOTAL);
CDC* pDC1 = pWnd1->GetDC();
picn.Format("%d",num);
pDC1->TextOut(0,70,picn);
//LPTSTR lpsz = new TCHAR[strall.GetLength()+1];
//_tcscpy(lpsz, strall);
LPTSTR lpszCurDir=new char[MAX_PATH];
GetCurrentDirectory(MAX_PATH,lpszCurDir);
DlgDirList(lpszCurDir,IDC_LIST1,0,DDL_DIRECTORY);
//DlgDirList(_T("e:\\"),IDC_LIST1,0,DDL_EXCLUSIVE|DDL_DIRECTORY);
}
str0.Format(str_Temp+"\\image\\%d.bmp",psim[0]);
//从库中读入位图
m_imgSearch1.Load(str0, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str1.Format(str_Temp+"\\image\\%d.bmp",psim[1]);
//从库中读入位图
m_imgSearch2.Load(str1, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str2.Format(str_Temp+"\\image\\%d.bmp",psim[2]);
//从库中读入位图
m_imgSearch3.Load(str2, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str3.Format(str_Temp+"\\image\\%d.bmp",psim[3]);
//从库中读入位图
m_imgSearch4.Load(str3, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str4.Format(str_Temp+"\\image\\%d.bmp",psim[4]);
//从库中读入位图
m_imgSearch5.Load(str4, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str5.Format(str_Temp+"\\image\\%d.bmp",psim[5]);
//从库中读入位图
m_imgSearch6.Load(str5, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str6.Format(str_Temp+"\\image\\%d.bmp",psim[6]);
//从库中读入位图
m_imgSearch7.Load(str6, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str7.Format(str_Temp+"\\image\\%d.bmp",psim[7]);
//从库中读入位图
m_imgSearch8.Load(str7, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str8.Format(str_Temp+"\\image\\%d.bmp",psim[8]);
//从库中读入位图
m_imgSearch9.Load(str8, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
str9.Format(str_Temp+"\\image\\%d.bmp",psim[9]);
//从库中读入位图
m_imgSearch10.Load(str9, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
// }
//删除类目标
// delete(CBmpProc*)pDestBmp;
// delete[] lib1;
// delete[] lib2;
delete[] lib3;
}
void CTestDlg::StartDir(const CString &strfile1)
{
BOOL yesno;
CFileFind find;
char tempFileFind[200];
sprintf(tempFileFind,"%s\\*.*",strfile1);
RunDir(strfile1);
yesno = (BOOL)find.FindFile(tempFileFind);
//查找下级目录
while(yesno)
{
yesno = find.FindNextFile();
if (find.IsDots() != TRUE)
{
char foundFileName[200];
strcpy(foundFileName,find.GetFileName().GetBuffer(200));
if((find.IsDirectory() == TRUE))
{
char tempDir[200];
sprintf(tempDir,"%s\\%s",strfile1,foundFileName);
// 递归调用
StartDir(tempDir);
}
}
}
find.Close();
return;
}
void CTestDlg::RunDir(const CString &strfile2)
{
BOOL yesno;
CFileFind find;
char tempFileFind[200];
sprintf(tempFileFind,"%s\\*.bmp",strfile2);
yesno = find.FindFile(tempFileFind);
while(yesno)
{
yesno = find.FindNextFile();
char foundFileName[200];
strcpy(foundFileName,find.GetFileName().GetBuffer(200));
if(!find.IsDots())
{
char tempFileName[200];
sprintf(tempFileName,"%s\\%s",strfile2,foundFileName);
CString strfilepath1;
strfilepath1.Format("%s",tempFileName);
counts++;
// temp[tempi] = new CString(strfilepath1);
// tempi++;
}
}
find.Close();
return;
}
void CTestDlg::OnStaticSearchimage1()
{
ShellExecute(NULL,_T("open"),str0,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage2()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str1,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage3()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str2,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage4()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str3,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage5()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str4,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage6()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str5,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage7()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str6,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage8()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str7,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage9()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str8,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimage10()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),str9,0,0,SW_SHOW);
}
BOOL CTestDlg::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
{
// TODO: Add your specialized code here and/or call the base class
return CDialog::Create(IDD, pParentWnd);
}
void CTestDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if((iPos=m_progress.GetPos())<ntime)
{
iPos++;
m_progress.SetPos(iPos);
}
else
{KillTimer(1);
AfxMessageBox("特征提取完毕!",MB_OK,0);
}
CDialog::OnTimer(nIDEvent);
}
void CTestDlg::OnSelchangeList1()
{
// TODO: Add your control notification handler code here
CString strTemp1;
CString strTemp2;
// CString strArr;
m_ListFound.GetText(m_ListFound.GetCurSel(), strTemp1);
strArr=str_Temp+"\\image\\"+strTemp1;
m_imgSearchall.Load(strArr, CXIMAGE_FORMAT_BMP);
RefreshDisplay();
}
void CTestDlg::OnStaticKeyimage()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),strNamaFile,0,0,SW_SHOW);
}
void CTestDlg::OnStaticSearchimageall()
{
// TODO: Add your control notification handler code here
ShellExecute(NULL,_T("open"),strArr,0,0,SW_SHOW);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -