【问题标题】:open image using opencv and Qt using QFileDialog::getOpenFileName使用 opencv 和 Qt 使用 QFileDialog::getOpenFileName 打开图像
【发布时间】:2015-02-19 20:16:48
【问题描述】:

我创建了一个最简单的 Gui,只有一个按钮来使用 Qt 读取和显示图像,按钮的插槽是:

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

我有以下错误:

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

如何成功使用 QFileDialog 类将有效路径传递给 imread?

【问题讨论】:

标签: c++ qt opencv


【解决方案1】:

正如您在 opencv 文档中看到的那样,必须使用 imread 方法,如下所示: Mat cv::imread ( const String & 文件名, int 标志 = IMREAD_COLOR ); QString 是一种只与 qt 库兼容的字符串类型,因此必须将 QString 转换为 String 才能正确使用 imread 方法。

使用: imread(fileName.toStdString());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多