在OpenCV中读取

时间:2014-12-11 05:29:43

标签: c++ visual-studio opencv

我发现这个问题在这里已被多次询问,但我没有找到任何解决方案或解决这个问题。这是我的代码(从这里复制: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编译了这个,并得到了不同的结果(两者都不起作用)。使用VS 2008编译的程序在imread()处有运行时错误,另一个显示消息“无法打开或找到图像”。

任何人都可以帮我这个吗?

4 个答案:

答案 0 :(得分:1)

这里的问题是你的main()函数。在C ++中不存在._tmain。主要做。

_tmain是Microsoft扩展。这是这两种方法的nice explanation。 如果您想在Visual Studio中添加默认参数,请执行以下步骤。

  1. 在解决方案资源管理器中右键单击您的项目,然后从菜单中选择“属性”

  2. 转到配置属性 - &gt;调试

  3. 在属性列表中设置命令参数。

  4. 希望这能解决你的问题!


    #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;
    }
    

答案 1 :(得分:0)

将argv [1]设置为已知图像页面&#34; C:\ test.jpg&#34;

答案 2 :(得分:0)

好的,我已阅读所有评论,我将回答主要问题以及子问题。

为什么我的代码在VS2008中不起作用?

您的代码在VS2008中不起作用的原因是因为您正在使用2010年的编译库,至少我认为这是一个非常准确的假设。如果您想要完全准确,那么build the libraries,用于您正在使用的编译器。

什么是tmain&amp;什么是主要

This堆栈溢出问题比我以前更好地回答了主题,但实际上它是一个特定于Windows的主要内容并且实际上并不存在于C ++中。它在编译时被编译器删除,转换器到main。

答案 3 :(得分:0)

请你试试这个:使用已知的工作图像和绝对路径,直到它工作,这样你就可以确定图像或相对路径没有问题。

http://upload.wikimedia.org/wikipedia/en/2/24/Lenna.png下载到C:\Lenna.png

将您的main函数重命名为其他内容并尝试此操作:如果它不起作用,请告诉我输出窗口的显示名称。

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
    namedWindow( "Display window", WINDOW_AUTOSIZE );   // Create a window for display.

    Mat image;
    image = imread("C:/Lenna.png", CV_LOAD_IMAGE_COLOR);   // Read the file
    if(! image.cols )                              // Check for invalid input
    {
        cout <<  "Could not open or find the image. Press any key to exit." << std::endl ;
        cv::waitKey(0)
        return -1;
    }
    imshow( "Display window", image );                   // Show our image inside it.
    waitKey(0);                                          // Wait for a keystroke in the window
    return 0;
}

请尝试这个,如果它不起作用,请告诉我输出窗口的显示名称。