簡體   English   中英

讀入OpenCV

[英]imread in 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()處出現運行時錯誤,其他程序顯示消息“無法打開或找到圖像”。

有人可以幫助我嗎?

這里的問題是您的main()函數._tmain在C ++中不存在。 主要的。

_tmain是Microsoft擴展名。 這是對這兩種方法的很好的解釋 此外,如果要在Visual Studio中添加默認參數,請按照以下步驟操作。

  1. 在解決方案資源管理器中右鍵單擊您的項目,然后從菜單中選擇“屬性”

  2. 轉到配置屬性->調試

  3. 在屬性列表中設置命令參數。

希望這能解決您的問題!


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

將argv [1]設置為已知的圖像頁面“ C:\\ test.jpg”

好的,我已經閱讀了所有評論,我將回答主要問題以及子問題。

為什么我的代碼在VS2008中不起作用?

您的代碼在VS2008中不起作用的原因是因為您使用的是2010年的編譯庫,至少我認為這是一個非常准確的假設。 如果要完全准確則為您使用的編譯器構建庫

什么是主要和什么是主要

這個堆棧溢出問題比我以往所能回答的要好得多,但實際上這是Windows特定的main,實際上在C ++中不存在。 它在編譯時被編譯器刪除並轉換為main。

您能嘗試一下嗎:使用已知的工作圖像和絕對路徑,直到它起作用為止,這樣可以確保圖像或相對路徑沒有問題。

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

將主函數重命名為其他函數,然后嘗試以下方法:如果它不起作用,請告訴我輸出窗口的顯示名稱。

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

請嘗試此操作,如果它不起作用,請告訴我輸出窗口的顯示名稱。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM