0
votes

I am using a camera to get a imgTomo1 image which is a cv::Mat object. It is a CV_32F image. I am trying to show it on a QLabel using a QPixmap. Here is my code :

   cv::Mat imgTomo;
   imgTomo1.convertTo(imgTomo,CV_8UC1);

  static QVector<QRgb>  sColorTable;

  // only create our color table the first time
 if ( sColorTable.isEmpty() )

   sColorTable.resize( 256 );

       for ( int i = 0; i < 256; ++i )
       {
           sColorTable[i] = qRgb( i, i, >i );
       }
   }

   QImage image( imgTomo.data,
                 imgTomo.cols, imgTomo.rows,
                 static_cast<int>(imgTomo.step),
                 QImage::Format_Indexed8);

   image.setColorTable( sColorTable );
   _afficheImg->setPixmap(QPixmap::fromImage(image));

Unfortunately, the image displayed remain black. I am a bit lost in the format as i'm new to OpenCV. I think the conversion should work so i don't really know what i am mising.

EDIT : i have deleted the fllowing line :

imgTomo1.convertTo(imgTomo,CV_8UC1);

It resulted in a loss of information. Now i dont have a black screen anymore but some "snow" (pixel that are witching form 1 to 0 very quicly i guess) and i can't really see what my camera is suposed to show.

Thank you for your answers, Grégoire

2

2 Answers

0
votes

I am not exactly sure what's wrong with your code but I used the following code to convert a cv::Mat image into a QImage.

if (frame.channels()== 3){
        cv::cvtColor(frame, RGBframe, CV_BGR2RGB);
        img = QImage((const unsigned char*)(RGBframe.data),
                         RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);
}
else
{
        img = QImage((const unsigned char*)(frame.data),
                         frame.cols,frame.rows,QImage::Format_Indexed8);
}

You can even check out the follwing link for more information on how to convert Mat image to QImage.

0
votes

1.Convert CV_8U type

       Mat Temp;

       CurrentMat.convertTo(Temp, CV_8U);

2.Check the channel number:

 int theType = Temp.type();

    int channel_number = (theType / 8) + 1;

    if(channel_number == 4){

        cvtColor(Temp, Temp, CV_BGRA2BGR);
    }

3.You can convert Mat type to QImage using this code:

        QImage putImage(const Mat& mat)
        {
        // 8-bits unsigned, NO. OF CHANNELS=1
        if (mat.type() == CV_8UC1)
        {

            // Set the color table (used to translate colour indexes to qRgb values)
            QVector<QRgb> colorTable;
            for (int i = 0; i < 256; i++)
                colorTable.push_back(qRgb(i, i, i));
            // 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, mat.step, QImage::Format_Indexed8);
            img.setColorTable(colorTable);
            return img;
        }
        // 8-bits unsigned, NO. OF CHANNELS=3
        if (mat.type() == CV_8UC3)
        {
            // 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, mat.step, QImage::Format_RGB888);
            return img.rgbSwapped();
        }
        else
        {
            qDebug() << "ERROR: Mat could not be converted to QImage.";
            return QImage();
        }
        }