本文主要是使用Qt與opencv將圖像進行顯示在QT界面上。
程序運行后的界面如下所示:
(由於只有打開圖像之后,才能對圖像進行翻轉,所以程序設置為讀取圖像成功之后才能點擊翻轉圖像按鈕)
點擊打開圖像:
打開圖像,獲取圖像名稱代碼:
QString filename = QFileDialog::getOpenFileName(this, tr("open image"), ".", tr("Image file(*.png *.jpg *.bmp)")); image = imread(filename.toLocal8Bit().data());
顯示效果:
顯示圖像代碼:
QImage img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888); //設定圖像大小自適應label窗口的大小 img = img.scaled(ui->label->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); ui->label->setPixmap(QPixmap::fromImage(img));
點擊翻轉圖像按鈕:
flip(image, image, 1); QImage img = QImage((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888); img = img.scaled(ui->label->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); ui->label->clear(); ui->label->setPixmap(QPixmap::fromImage(img));
沒有打開圖像時, 翻轉圖像按鈕設置為不能點擊:
ui->pushButton_2->setEnabled(false);
判斷圖像是否打開,打開設置為能夠點擊狀態:
if(image.data) { ui->pushButton_2->setEnabled(true); }