openCV與Qt:經過目錄打開並顯示一張圖片

void MainWindow::displayMat(Mat &image)
{
    Mat rgb;
    QImage img;
    if(image.channels() == 3)
    {
        cvtColor(image, rgb,CV_BGR2RGB);  //顏色空間轉換
        img =QImage((const unsigned char*)(rgb.data),
                            rgb.cols,rgb.rows,
                            rgb.cols*rgb.channels(),
                            QImage::Format_RGB888);
    }else
    {
        img =QImage((const unsigned char*)(image.data),
                            image.cols,image.rows,
                            image.cols*image.channels(),
                            QImage::Format_RGB888);

    }
    ui->label->setPixmap(QPixmap::fromImage(img));
    ui->label->resize(ui->label->pixmap()->size());
    ui->label->show();


}
void MainWindow::on_OpenAction_triggered()
{
    QString file_Name = QFileDialog::getOpenFileName(this,
                                                        tr("open image"),
                                                        ".",
                                                        tr("Image file(*.png *.jpg *.bmp)"));
    if(file_Name.isEmpty())
    {
         QMessageBox::information(NULL, "file open error", "請從新打開圖像", QMessageBox::Yes, QMessageBox::Yes);
    }else
    {
        image = imread(file_Name.toLocal8Bit().data());
        displayMat(image);
    }
}
相關文章
相關標籤/搜索