显示多幅图像
文件夹中存了很多幅图像,我需要选中这个文件夹,然后程序可以自动的从这个文件夹中连续读入图像,每读入一幅图像都要进行相应的处理,处理完后再读入下一副图像,直到都处理完结束。图像的命名是有规则的,并且每幅图象556*406象素,需要都读入进行处理,或读一幅处理一幅
可以用CFileFind类得到目录下所有文件,可以根据文件名字特征、扩展名筛选出你所要处理的文件,然后遍历处理
CFileFind ff;
BOOL bF = ff.FindFile(szPath + "\\*.*");
while(bF)
{
bF = ff.FindNextFile();
filePath = ff.GetFilePath();
if(/*file为你所要处理的图像*/)
{
/* 处理*/
}
}
VC代码,功能很完整。可以只选择文件夹中的一幅图像,然后点OK,会自动遍历
整个文件夹。读取各个文件的完整路径到一个vector中,并不把所有图像读进内存,这样可以
节省不必要的内存开销。 在处理函数中,根据这些完整的路径,打开一个处理一个释放一个。
直到处理完所有图像。
- C/C++ code
int CBatch::FileOpenEx()
{
int counter=0;
CString ss="";
pair<map<CString,CString>::iterator,bool> Pair_Insert;
int nimg=0;
CString ftitle,fname,fpath,fpathname,froot;
CString mp[7];
static char szFilter[] = "*.BMP|*.BMP|All Files(*.*)|*.*||";
CFileDialog FileDlg( TRUE, NULL, NULL,OFN_HIDEREADONLY, szFilter );
if( FileDlg.DoModal() == IDOK )
{
fpathname=FileDlg.GetPathName();
CFileFind finder;
finder.FindFile(fpathname);
finder.FindNextFile();
froot=finder.GetRoot();
BOOL bResult;
fpathname=froot+_T("\\*.*"); //找到该文件的文件夹,
bResult=finder.FindFile(fpathname); //然后从头开始一个一个遍历
while (bResult)
{
counter++;
bResult=finder.FindNextFile();
if(finder.IsDots()) continue;
if(finder.IsDirectory()) continue;
fname=finder.GetFileName();
if("bmp" !=fname.Right(3) && "BMP" !=fname.Right(3) ) continue;
//找到了一个bmp文件,开始提取相关信息
FPRO fpro;//自定义的一个结构体
fpro.filePath =finder.GetFilePath(); // 路径和文件名,包括扩展名。
fpro.fileName = finder.GetFileName();
fpro.fileTitle = finder.GetFileTitle();
m_imvec.push_back(fpro);
}// while
}// if
BatchProcess();
return 1 ;
}
int CBatch::BatchProcess()
{
CMainFrame * pMain = (CMainFrame*) AfxGetApp()->m_pMainWnd;
CBlurEstimation3View *pView = (CBlurEstimation3View*) pMain->GetActiveView();
IMIter it_start,it_end,it;
for (it= m_imvec.begin(); it!=m_imvec.end(); ++it)
{
CString Dir = it->filePath;
if (dib.Load(Dir))
{
CClientDC dc(pView);
dib.SetPalette(&dc);
dib.Draw(&dc);
}
else
continue;
int width = dib.m_Width;
int height = dib.m_Height;
int srcLineBytes = dib.m_DataSize/height;
int residual= 4 - width%4;
if(residual ==4)
residual =0;
if(dib.m_DataSize == width*height)
residual = 0;
width = width - width%4;
#pragma region
//分配内存,读取1D图像数据
BYTE *pBits = new BYTE[width*height];
//分配内存,读取2D 图像数据
// BYTE **p2DBits = new BYTE *[height];
// for(int i=0; i<height; ++i)
// p2DBits[i] = new BYTE[width];
BYTE pixel;
for(int i=0; i<height;++i)
for(int j=0; j<width; ++j)
{
pixel = dib.m_pDibBits[srcLineBytes*i +j];
// p2DBits[i][j] = pixel;
pBits[i*width+j] = pixel;
}
#pragma endregion
float param = 0;
//***************************************************************
// 此处添加代码
// 处理
// end此处添加代码
//****************************************************************
CString str;
str.Format("%f",param);
it->param = str;
// for(i=0; i<height;++i)
// delete []p2DBits[i];
// delete []p2DBits;
delete []pBits;
}
return 1;
}

浙公网安备 33010602011771号