I'm using the code suggested in ( how to convert an opencv cv::Mat to qimage ) to display a cv::Mat
in my Qt application. However, I'm getting strange results. The black parts are displayed as black, but all other values are inverted.
Conversion code:
QImage ImgConvert::matToQImage(Mat_<double> src)
{
double scale = 255.0;
QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
for (int y = 0; y < src.rows; ++y) {
const double *srcrow = src[y];
QRgb *destrow = (QRgb*)dest.scanLine(y);
for (int x = 0; x < src.cols; ++x) {
unsigned int color = srcrow[x] * scale;
destrow[x] = qRgba(color, color, color, 255);
}
}
return dest;
}
Display code:
void MainWindow::redraw()
{
static QImage image = ImgConvert::matToQImage(im);
static QGraphicsPixmapItem item( QPixmap::fromImage(image));
static QGraphicsScene* scene = new QGraphicsScene;
scene->addItem(&item);
ui->graphicsView->setScene(scene);
ui->graphicsView->repaint();
}
Right now I'm using if(color>0) color = 255-color;
to correct for this effect, but I'd much rather understand where it's coming from.
Also, a second mini-question: if I remove the static
declarations in redraw()
, the image gets removed from memory immediately when the method exits. Is this the best way to fix this, and am I going to have any unintended side effects if I display multiple frames?