0

我是使用 Qt 和 OpenCV 的新手。我正在尝试读取高清图像并显示它。它不是特定的图像,程序可以读取用户选择的任何图像。我的代码:

QString Imagename = QFileDialog::getOpenFileName(
                this,
                tr("Open Images"),
                "C://",
                tr("Tiff Files (*.tif);; Raw file (*.raw)"));

 if ( Imagename.isNull())
    {
         QMessageBox::warning(this,"Error!","Image not valid!");
    }


    cv::Mat src(filename);

Mat 配置为: Mat imread(const string& filename, int flags=1 )

我该如何解决这个问题?

4

2 回答 2

3

cv::Mat没有接受字符串的构造函数。改为使用imread。由于imread接受std::string,不QString,只是做:

cv::Mat yourImage = cv::imread(filename.toStdString());
于 2015-07-07T14:51:37.293 回答
0

你不能像以前那样使用 cv::Mat 变量。要解决这个问题,你应该使用“imread”函数。我认为下面的代码会帮助你解决这个问题。您必须包括以下库。

    #include<QFileDialog>
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <iostream>

    int main (){
    // Gets file name with QFileDialog
    QString file_name=QFileDialog::getOpenFileName(this,"Open Image File","C://","Image File (*.jpg *.tiff *.png *.bmp)"); 

    // Read image with Color Image Parameter and store on image variable which type is cv::Mat
    // You should convert file name from QString to StdString to use in imread function
    cv::Mat image = cv::imread(file_name.toStdString(),CV_LOAD_IMAGE_COLOR); 

   if(!image.data){   // Checks whether the image was read successfully 
      qDebug()<< "Could not open or find the image";  
      return -1;
                  }

    cv::namedWindow("Original Image",WINDOW_AUTOSIZE); // Creates a window which will display image 
    cv::imshow("Original Image",image);     // Shows image on created window

    cv::waitKey(0);    // Waits for a keystroke in the window
    return 0;  // if you created console app in qt you should use return a.exec() instead of this.
}
于 2015-12-27T13:17:01.157 回答