[Interest] Problem with cv::Mat grayscale to QImage

Jason H jhihn at gmx.com
Thu Jun 20 23:05:16 CEST 2019


Simple code:

cv::Mat left_image = cv::imread(filename, cv::IMREAD_COLOR );
cv::cvtColor(mat, mat, cv::COLOR_BGR2GRAY);
cv::imwrite("dummy_gray_cv.png", left_image); // ok
QImage test((unsigned char*) left_image.data, left_image.cols, left_image.rows, QImage::Format_Grayscale8);
test.save("dummy_gray_qt.png"); // skewed


However the dummy_gray_qt.png image is not aligned correctly, it's skewed as if bytes are missing/being skipped.

I have to convert it back to color to work:
cv::Mat dst;
cv::cvtColor(left_image, dst, cv::COLOR_GRAY2BGRA);
QImage result = QImage((unsigned char*) dst.data, dst.cols, dst.rows, QImage::Format_RGB32);
result.save("dummy_color_qt.png")

Is there a way I can avoid needing cv::cvtColor(left_image, dst, cv::COLOR_GRAY2BGRA)?




More information about the Interest mailing list