简体   繁体   中英

open image using opencv and Qt using QFileDialog::getOpenFileName

I created the simplest Gui with only one button to read and show an image using Qt, the slot of the button is:

    void MainWindow::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this,
                                                    tr("Open image"),
                                                    tr("."),
                                                    tr("Image Files (*.png *.jpg *.jpeg *.bmp * .tif)"));

    image= cv::imread(fileName);
    cv::namedWindow("Original Image");
    cv::imshow("Original Image", image);
}

I have the following error:

error: invalid initialization of reference of type 'const string& {aka const std::basic_string<char>&}' from expression of type 'QString'
     image= cv::imread(fileName);
                               ^

How to use successfully the QFileDialog class to pass a valid path to the imread?

as you can see in the opencv documentation , imread method must be used as it follows: Mat cv::imread ( const String & filename, int flags = IMREAD_COLOR ); QString is a type of string compatible only with the qt libraries, so you must convert QString to String in order to properly use the imread method.

use : imread(fileName.toStdString());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM