Opencv c ++ resize函数:new Width应该乘以3

问题描述 投票:0回答:1

我在Qt环境中编程,我有一个尺寸为2592x2048的Mat图像,我想将其调整为我所拥有的“标签”的大小。但是当我想要显示图像时,我必须将宽度乘以3,因此图像以正确的大小显示。那有什么解释吗?

这是我的代码:

//Here I get image from the a buffer and save it into a Mat image.
//img_width is 2592 and img_height is 2048
Mat image = Mat(cv::Size(img_width, img_height), CV_8UC3, (uchar*)img, Mat::AUTO_STEP);
Mat cimg;
double r; int n_width, n_height;
//Get the width of label (lbl) into which I want to show the image
n_width = ui->lbl->width();
r = (double)(n_width)/img_width;
n_height = r*(img_height);
cv::resize(image, cimg, Size(n_width*3, n_height), INTER_AREA);

谢谢。

c++ qt opencv resize
1个回答
1
投票

调整大小功能运行良好,因为如果您保存调整大小的图像作为文件正确显示。由于您希望在QLabel上显示它,我假设您必须先将图像转换为QImage,然后转换为QPixmap。我认为问题在于步骤或图像格式。

如果我们确保传入图像数据

Mat image = Mat(cv::Size(img_width, img_height), CV_8UC3, (uchar*)img, Mat::AUTO_STEP);

确实是一个RGB图像,然后下面的代码应该工作:

ui->lbl->setPixmap(QPixmap::fromImage(QImage(cimg.data, cimg.cols, cimg.rows, *cimg.step.p, QImage::Format_RGB888 )));

最后,您可以使用构造函数构造一个QImage对象,而不是使用OpenCV

QImage((uchar*)img, img_width, img_height, QImage::Format_RGB888)

然后使用scaledToWidth方法进行调整大小。 (请注意,此方法返回缩放图像,并且不会对图像本身执行调整大小操作)

© www.soinside.com 2019 - 2024. All rights reserved.