我发现这个问题在这里被问了很多次,但我还没有找到这个问题的任何解决方案或变通办法。下面是我的代码(从这里复制:http://docs.opencv.org/doc/tutorials/introduction/display_image/display_image.html):
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int _tmain(int argc, char** argv)
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_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 2008和2010对其进行了编译,得到了不同的结果(两者都不起作用)。使用VS2008编译的程序在imread()有运行时错误,另一个显示消息“无法打开或找到图像”。
有人能帮我吗?
发布于 2014-12-11 15:41:25
这里的问题是C++中不存在您的main() function._tmain。main需要。
_tmain是微软的一个扩展。下面是这两种方法的nice explanation。此外,如果你想在Visual studio中添加默认参数,请按照以下步骤操作。
希望这能解决你的问题!
#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
if( argc != 2)
{
cout <<"No Commandline Aurgument Found!: Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_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;
}发布于 2014-12-11 13:46:14
将argv1设置为已知的图片页面"C:\test.jpg“
发布于 2014-12-11 15:25:32
好了,我已经读完了所有的评论,我将回答主要问题和次要问题。
为什么我的代码不能在VS2008中运行?
您的代码不能在VS2008中运行的原因是因为您使用的是2010年的编译库,至少我认为这是一个相当准确的假设。如果你想要完全准确,那么对于你正在使用的编译器,可以使用build the libraries,。
什么是tmain &什么是main
This堆栈溢出问题比我回答这个问题要好得多,但它实际上是一个特定于windows的main,实际上并不存在于C++中。它会在编译时被编译器移除,并被转换为main。
https://stackoverflow.com/questions/27415978
复制相似问题