qt判断窗口是否已显示_如何在C ++和QT的另一个窗口中显示主窗口的结果图像?...

在QT Creator项目中,作者尝试从硬盘选择一幅RGB图像,将其转换为灰度图像,然后在另一个窗口中显示。通过点击“浏览”按钮加载图像并进行颜色转换,同时打开新窗口SecondDialog。然而,图像没有成功显示。问题在于在SecondDialog中创建了新的MainWindow对象,导致grayImage为空。解决方案包括在SecondDialog中添加一个setter方法以接收图像,并将QImage::Format_RGB888更改为QImage::Format_Indexed8。
摘要由CSDN通过智能技术生成

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.

解决方案

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值