【发布时间】:2013-10-18 13:17:32
【问题描述】:
我正在尝试让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] 转换为 char*。从内存窗口的内存中使用一个简单的文件名“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]);
但是生成的内存是一团糟,肯定不是 'opencv-logo.jpg' 作为 ascii 字符串:
fe fe fe fe fe fe fe fe fe fe ...
þþþþþþþþþþ ...
我应该使用哪种转换技术、函数或宏?
(注:This 可能是一个相关问题,但我看不到如何在此处申请 the answer。)
【问题讨论】:
-
您的项目模板正在尝试创建一个 C++ 项目以支持 unicode。我不记得具体的项目/编译器选项,但应该有一些东西可以指定是否使用 unicode。
-
你需要一个USES_CONVERSION;使用转换宏的每个函数顶部的语句?
标签: c++ string visual-c++ atl tchar