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