OPENCV C 调整大小函数:新宽度应乘以3

Opencv c++ resize function: new Width should be multiplied by 3

本文关键字:新宽度 函数 调整 OPENCV      更新时间:2023-10-16

我正在qt环境中编程,我有一个带有2592x2048的垫子图像,我想将其调整到我拥有的"标签"的大小。但是,当我想显示图像时,我必须将宽度乘以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);

谢谢。

调整大小函数运行良好,因为如果您将调整大小的图像保存为文件,则将正确显示。由于您想在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 )));

最后,您可以使用constructor

来构造一个QIMAGE对象,而不是使用OpenCV
QImage((uchar*)img, img_width, img_height, QImage::Format_RGB888)

,然后使用scaledtowidth方法进行调整大小。(当心认为此方法返回缩放图像,并且不执行调整大小的操作到图像本身本身)