我正在尝试获取一个简单的OpenCV示例,该示例在Windows上的C ++中工作,而我的C ++不仅仅生锈。
该示例非常简单:
#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
在另一个答案中遵循转换建议之后,我尝试通过插入以下代码来使用ATL 7.0字符串转换类和宏:
char* filename = CT2A(argv[1]);
但是结果产生的内存一片混乱,当然不是ascii字符串的“ opencv-logo.jpg”:
fe fe fe fe fe fe fe fe fe fe ...
þþþþþþþþþþ ...
我应该使用哪种转换技术,函数或宏?
最快的解决方案是将签名更改为标准签名。更换:
int _tmain( int argc, _TCHAR* argv[] )
用
int main( int argc, char *argv[] )
这确实意味着在Windows上,命令行参数已转换为系统的区域设置编码,并且由于Windows不支持UTF-8,因此并非所有转换都能正确进行。但是,除非您真正需要国际化,否则可能不值得您花更多时间做更多的事情。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句