I'm working on a homework for my Digital Image Processing class, and I'm using OpenCV with QT Framework.
I've created a class ImageDisplay
, which is as sub class of the QWidget
class.
I've using OpenCV to manipulate a grayscale image, and then creating a QImage
object from the Mat
object. After that I use the QWidget::drawImage()
to draw the image. But sometimes it shows a distorted, angled image.
I was experimenting and discovered that it has something to do with the image dimensions.
For instance, this image with 320x391 pixels is rendered normally
But if change the dimension to 321x391 (using Gimp), it shows up like this:
This is the code for the paintEvent
method:
void ImageDisplay::paintEvent(QPaintEvent *){
QPainter painter(this);
Mat tmp;
/* The mat in the next line is a Mat object that contains the image data */
cvtColor(mat, tmp, CV_GRAY2BGR);
QImage image(tmp.data, tmp.cols, tmp.rows, QImage::Format_RGB888);
painter.drawImage(rect(), image);
}
Does anyone has a clue what is the problem and how to fix it?
Thanks in advance!