简体   繁体   English

调整大小的 opencv 图像在 QWidget C++ 上无法正确显示

[英]Resized opencv image is not displayed correctly on QWidget C++

I try to display OpenCV image of size 3000 * 4096 on QLabel.我尝试在 QLabel 上显示大小为3000 * 4096的 OpenCV 图像。 The image is resized before displaying.图像在显示前调整大小。 If the resize factor that the size is divisible (eg 2, 4, 8, 16, 32), the image is displayed correctly.如果尺寸可整除的resize 因子(例如2、4、8、16、32),则图像显示正确。 If the size is not divisible by the factor (eg 3, 5, 7, 10), the image is not displayed correctly.如果大小不能被因子整除(例如 3、5、7、10),则图像无法正确显示。 I also tried with rescale factor as input instead of cv:Size() but it behaves similar.我也尝试使用重新缩放因子而不是 cv:Size() 作为输入,但它的行为相似。 Eg: works correctly with fx = fy = 0.25 , but incorrectly with fx=fy=0.3 .例如:与fx = fy = 0.25一起工作正常,但与fx=fy=0.3不正确。

Dislay by OpenCV, scale factor = 4:按 OpenCV 显示,比例因子 = 4:

OpenCV 显示,比例因子 = 4

Display on QLabel, scale factor = 4:在 QLabel 上显示,比例因子 = 4:

在 QLabel 上显示,比例因子 = 4

Display by OpenCV, scale factor =10:通过 OpenCV 显示,比例因子 =10:

通过 OpenCV 显示,比例因子 =10

Display on QLabel, scale factor = 10:在 QLabel 上显示,比例因子 = 10:

在 QLabel 上显示,比例因子 = 10

Below is the summary of the code:以下是代码摘要:

resize(opencv_image, img_resize, cv::Size(opencv_image_.cols/4, opencv_image_.rows/4), 0, 0, cv::INTER_AREA);
ui->lbl_continuous_grab->setPixmap(QPixmap::fromImage(PutImage(img_resize)));

QImage PutImage(const Mat& mat)
{
    // 8-bits unsigned, NO. OF CHANNELS=3
    // 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, QImage::Format_RGB888);
    return img.rgbSwapped();
}

So the problem is OpenCV's step is a size_t, it needs to be cast to int when you convert the image into QImage.所以问题是OpenCV的步骤是一个size_t,当你把图像转换成QImage的时候需要把它转换成int。 However, I still don't know why it works when the image size is divisible by the resize factor.但是,我仍然不知道为什么当图像大小可被调整大小因子整除时它会起作用。

QImage img(qImageBuffer, mat.cols, mat.rows, static_cast<int>(mat.step), QImage::Format_RGB888);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM