文件名 文件目录与字符串表达 在c++ csharp和opencv中的转换

本文介绍了在C++环境下,如何在ANSI和Unicode字符集中将CString转换为适合OpenCV的imread函数的字符串格式。在Unicode字符集的项目中,需要包含并使用USES_CONVERSION宏来完成转换。同时,文章提供了一个在Unicode字符集的C++项目中使用OpenCV进行图像批处理的代码示例。
摘要由CSDN通过智能技术生成

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中的使用方法未完待续

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值