Gansroy Gansroy - 3 years ago 154
C++ Question

How to display resulting image of mainwindow in another window in C++ and QT?

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.

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


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 not 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.

Answer Source

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

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

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

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

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

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

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

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

}

Edit:

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()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download