I try to display OpenCV image of size 3000 * 4096
on QLabel. The image is resized before displaying. If the resize factor that the size is divisible (e.g 2, 4, 8, 16, 32), the image is displayed correctly. If the size is not divisible by the factor (e.g 3, 5, 7, 10), the image is not displayed correctly.
I also tried with rescale factor as input instead of cv:Size() but it behaves similar. E.g: works correctly with fx = fy = 0.25
, but incorrectly with fx=fy=0.3
.
Dislay by OpenCV, scale factor = 4:
Display on QLabel, scale factor = 4:
Display by OpenCV, scale factor =10:
Display on QLabel, scale factor = 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. 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);