c++中,有不同的字符集。在c++中有CString,在unicode字符集中相当于WCHAR* CStringW
在ANSI字符集中相当于CHAR* CStringA
在opencv中imread的说明
Mat cv::imread ( const String & filename,
int flags = IMREAD_COLOR
)
如果在多字节字符集的工程中,可以这样调用
CString fileName;
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
if(dlg.DoModal() == IDOK)
{
fileName= dlg.GetPathName();
const char* fpath = CStringA(fileName);
m_BGRimg=imread(fpath);
}
如果在unicode字符集的项目中
需要#include <atlconv.h>
然后使用USES_CONVERSION;语句
CString fileName;
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
if(dlg.DoModal() == IDOK)
{
fileName= dlg.GetPathName();
USES_CONVERSION;
string fileNameBK(W2A(fileName));
m_sReadfileName=fileNameBK.c_str();
m_srcBGRImg=imread(fileNameBK);
}
附加一段在unicode字符集的c++项目中,用opencv做图像批处理的代码
//弹出对话框选择读图文件夹
CString path;
BROWSEINFO bi;
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner = GetSafeHwnd();
CString cstr = _T("请选择JPG图像文件夹 ");
//USES_CONVERSION;
bi.lpszTitle = (LPCTSTR)cstr;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl == NULL){ return;}
SHGetPathFromIDList(pidl,path.GetBuffer(10000));
path.ReleaseBuffer();
if (path[path.GetLength()-1] != '\\')
{
path += "\\*.jpg";
}
else
{
path += "*.jpg";
}
CFileFind find;
int iIndex,iFindIndex;
iIndex = 0;
iFindIndex = 0;
//设置存储结果图像路径
TCHAR ExePath[MAX_PATH];
GetModuleFileName(NULL,ExePath,MAX_PATH);//得到应用程序的全路径
CString theAppPath,theAppName;
theAppPath=(CString)ExePath;
theAppName=AfxGetApp()->m_pszAppName;
theAppName+=".exe";
//把最后的文件名去掉
int length1,length2;
length1=theAppPath.GetLength();
length2=theAppName.GetLength();
theAppPath.Delete(length1-length2,length2); //exe文件的目录
FILE* pSampleRecFile;
CString sWriteFileName =theAppPath+"Sample.txt";//样本分析文件名称及目录
USES_CONVERSION;
//函数T2A和W2A均支持ATL和MFC中的字符
char * pFileName = T2A(sWriteFileName);
pSampleRecFile = fopen(pFileName,"w+");
DWORD sdkf= GetLastError();
//为批处理设定相应的文件保存、读取路径
CString strBatchDir= theAppPath + "BatchImage\\";
if(!PathFileExists(strBatchDir))
{
::CreateDirectory(strBatchDir, NULL);
}
string strFiledir;
//USES_CONVERSION;
strFiledir =W2A(strBatchDir); //存在当前目录下的Batch文件夹
m_ImgProc->SetSaveDir(strFiledir);
::SetCursor(LoadCursor(NULL, IDC_WAIT));
//遍历批处理目录下的所有jpg文件
BOOL bFind = find.FindFile(path);
while (bFind)
{
CString sReadPath;
iIndex++;
bFind = find.FindNextFile();
sReadPath = find.GetFilePath();
m_ImgProc->readSrcImg(sReadPath);
}
fprintf(pSampleRecFile,"共有%d幅图像",iIndex);
fclose(pSampleRecFile);
其中
bool CMatImgProc::readSrcImg(CString strImg)
{
USES_CONVERSION;
string fileNameBK(W2A(strImg));
m_sReadfileName=fileNameBK.c_str();
m_srcBGRImg=imread(fileNameBK);
}
csharp中的使用方法未完待续