如何在C ++和QT的另一个窗口中显示主窗口的结果图像? [英] How to display resulting image of mainwindow in another window in C++ and QT?

查看:763
本文介绍了如何在C ++和QT的另一个窗口中显示主窗口的结果图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在研究Qt创作者.我只想通过从主窗口中的硬盘浏览器获取图像,然后将RGB彩色图像转换为灰色图像,然后在另一个窗口中显示该灰色图像.

I am currently working on Qt creator. I want to just get an image by browsing from hard drive in mainwindow and then after converting the RGB color image to gray image, I want to display the gray image in the another window.

通过单击浏览"按钮,可以加载彩色图像,其中将应用彩色到灰色图像的转换.这里的grayImage是公共Mat类型的变量.同时,将调用另一个名为SecondDialog的窗口的实例.

By clicking the button "Browse", color image can be loaded where color to gray image conversion will be applied. Here grayImage is a public Mat type variable. At the same time an instance of another window named SecondDialog will be called to be executed.

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

In the seconddialog.cpp, I have converted the Mat image to QImage to display on a QLabel named label_img

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

当我运行该程序时,没有编译错误,但是现在它在第二个窗口中显示任何图像. 我无法弄清楚我的代码中是否有任何错误.如果任何人都可以解决此问题,那将非常有帮助. 预先感谢.

When I run this program, there is no compilation error, but it is now displaying any image in the second window. I am not able to figure out if there is any mistake in my code. It would be really helpful if anyone could fix this problem. Thanks in advance.

推荐答案

根据您的代码,您正在创建MainWindow类型的新对象:

According to your code you are creating a new object of type MainWindow:

[...]
ui(new Ui::SecondDialog)
{
ui->setupUi(this);    
MainWindow object;
[...]

并且它具有空的grayImage属性,因此您会遇到这种情况.

And this has the empty grayImage attribute so you get this behavior.

另一个问题是您使用的格式,必须从QImage::Format_RGB888更改为QImage::Format_Indexed8.

another problem is the format you use, you must change from QImage::Format_RGB888 to QImage::Format_Indexed8.

Format_RGB888:使用24位RGB格式(8-8-8)存储图像.

Format_RGB888: The image is stored using a 24-bit RGB format (8-8-8).

Format_Indexed8:使用8位索引将图像存储到颜色表中.

Format_Indexed8: The image is stored using 8-bit indexes into a colormap.

您需要做的是创建一个setter方法,并将图像传递到新窗口,您必须执行以下操作:

What you have to do is create a setter method and pass the image to the new window for it you must do the following:

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中运行以下命令:

So in the end you should run the following in 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:

In my case I use the following function to do the conversion of cv::Mat to 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()

这篇关于如何在C ++和QT的另一个窗口中显示主窗口的结果图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆