我正在尝试获取 a simple OpenCV sample在 Windows 上使用 C++ 工作,而我的 C++ 不仅仅是生锈的。
The sample相当简单:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], IMREAD_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
namedWindow( "Display window", WINDOW_AUTOSIZE ); // Create a window for display.
imshow( "Display window", image ); // Show our image inside it.
waitKey(0); // Wait for a keystroke in the window
return 0;
}
当我在 Visual Studio 2012 中创建一个新的简单 C++ 控制台应用程序(使用 ATL)时,我得到了一个不同的 main
模板:
int _tmain( int argc, _TCHAR* argv[] )
所以在我将文件名发送到 OpenCV 的 imread
函数之前,我需要将 _TCHAR*
arg[1]
转换为 字符*
。使用一个简单的文件名,'opencv-logo.jpg',从内存窗口的内存中我可以看到 _TCHAR 每个占用两个字节
o.p.e.n.c.v.-.l.o.g.o...j.p.g...
6f 00 70 00 65 00 6e 00 63 00 76 00 2d 00 6c 00 6f 00 67 00 6f 00 2e 00 6a 00 70 00 67 00 00 00
遵循 another answer 中的转换建议我正在尝试使用 ATL 7.0 String Conversion Classes and Macros通过插入以下代码:
char* filename = CT2A(argv[1]);
但是由此产生的内存是一团糟,肯定不是作为 ascii 字符串的 'opencv-logo.jpg':
fe fe fe fe fe fe fe fe fe fe ...
þþþþþþþþþþ ...
我应该使用哪种转换技术、函数或宏?
(注意 This 可能是一个相关问题,但我看不到如何在此处应用 the answer。)
最佳答案
最快的解决方案是将签名更改为标准签名。替换:
int _tmain( int argc, _TCHAR* argv[] )
与
int main( int argc, char *argv[] )
这确实意味着在 Windows 上命令行参数会转换为系统的语言环境编码,并且由于 Windows 不支持 UTF-8,因此并非所有内容都能正确转换。但是,除非您确实需要国际化,否则您可能不值得花时间做更多的事情。
关于c++ - 将 _TCHAR* 转换为 char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19301538/