![](/img/trans.png)
[英]Qt - How To access the MainWindow from another child Window or Widget?
[英]How to display resulting image of mainwindow in another window in C++ and QT?
我目前正在研究Qt創作者。 我只想通過從主窗口中的硬盤瀏覽器獲取圖像,然后將RGB彩色圖像轉換為灰色圖像,然后在另一個窗口中顯示該灰色圖像。
通過單擊按鈕“瀏覽”,可以加載彩色圖像,其中將應用彩色到灰色圖像的轉換。 這里grayImage
是公共Mat類型變量。 同時,另一個名為SecondDialog
窗口的實例將被調用執行。
void MainWindow::on_Browse_clicked()
{
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
fileName = QFileDialog::getOpenFileName(this, tr("Open Images"), "/home/rpi/Desktop/Picture/Sample Pictures", tr("Image Files (*.png *.jpg *.bmp)"));
if (!fileName.isEmpty())
{
String image_path=fileName.toLocal8Bit().constData();
Mat image= imread(image_path);
cvtColor(image, grayImage, CV_BGR2GRAY);
SecondDialog obj;
obj.setModal(true);
obj.exec();
}
}
在seconddialog.cpp中,我已將Mat圖像轉換為QImage以顯示在名為label_img
的QLabel上。
SecondDialog::SecondDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SecondDialog)
{
ui->setupUi(this);
MainWindow object;
Mat src= object.grayImage;
Mat temp(src.cols,src.rows,src.type());
QImage dest((const uchar *) temp.data, temp.cols, temp.rows, temp.step, QImage::Format_RGB888);
dest.bits();
ui->label_img->setPixmap(QPixmap::fromImage(dest));
}
SecondDialog::~SecondDialog()
{
delete ui;
}
當我運行該程序時,沒有編譯錯誤,但是現在它在第二個窗口中顯示任何圖像。 我無法弄清楚代碼中是否有錯誤。 如果有人可以解決此問題,那將非常有幫助。 提前致謝。
根據您的代碼,您正在創建MainWindow類型的新對象:
[...]
ui(new Ui::SecondDialog)
{
ui->setupUi(this);
MainWindow object;
[...]
而且它具有空的grayImage
屬性,因此您可以得到此行為。
另一個問題是使用的格式,必須從QImage::Format_RGB888
更改為QImage::Format_Indexed8
。
Format_RGB888:使用24位RGB格式(8-8-8)存儲圖像。
Format_Indexed8:使用8位索引將圖像存儲到顏色表中。
您要做的就是創建一個setter方法,並將圖像傳遞到新窗口,您必須執行以下操作:
SecondDialog.h
public:
void setImage(const Mat &image);
SecondDialog.cpp
SecondDialog::SecondDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SecondDialog)
{
ui->setupUi(this);
}
void SecondDialog::setImage(const Mat &image){
QImage dest((const uchar *) image.data, image.cols, image.rows, image.step, QImage::Format_Indexed8);
ui->label_img->setPixmap(QPixmap::fromImage(dest));
}
因此,最后您應該在MainWindow.cpp中運行以下命令:
void MainWindow::on_Browse_clicked()
{
QFileDialog dialog(this);
dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
dialog.setViewMode(QFileDialog::Detail);
fileName = QFileDialog::getOpenFileName(this,
tr("Open Images"), "/home/rpi/Desktop/Picture/Sample Pictures", tr("Image Files (*.png *.jpg *.bmp)"));
if (!fileName.isEmpty())
{
String image_path=fileName.toLocal8Bit().constData();
Mat image= imread(image_path);
cvtColor(image, grayImage, CV_BGR2GRAY);
SecondDialog obj;
obj.setImage(grayImage);
obj.setModal(true);
obj.exec();
}
}
編輯:
就我而言,我使用以下函數將cv::Mat
轉換為QImage
:
# https://github.com/eyllanesc/Mirosot-Peru/blob/master/Mirosot-PC/MatToQImage.cpp
QImage MatToQImage(const cv::Mat& mat)
{
// 8-bits unsigned, NO. OF CHANNELS=1
if(mat.type()==CV_8UC1)
{
// Set the color table (used to translate colour indexes to qRgb values)
QVector<QRgb> colorTable;
for (int i=0; i<256; i++)
colorTable.push_back(qRgb(i,i,i));
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8);
img.setColorTable(colorTable);
return img;
}
// 8-bits unsigned, NO. OF CHANNELS=3
if(mat.type()==CV_8UC3)
{
// Copy input Mat
const uchar *qImageBuffer = (const uchar*)mat.data;
// Create QImage with same dimensions as input Mat
QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888);
return img.rgbSwapped();
}
else
{
qDebug() << "ERROR: Mat could not be converted to QImage.";
return QImage();
}
} // MatToQImage()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.