Fastest method to convert IplImage IPL_DEPTH_32S to QImage Format_RGB32

前端 未结 3 656
失恋的感觉
失恋的感觉 2021-01-28 08:27

What is the fastest method to convert IplImage IPL_DEPTH_32S to QImage Format_RGB32?

I need to catch pictures from cam and show it on form with frequency 30 frames in se

3条回答
  •  梦毁少年i
    2021-01-28 09:02

    Time ago I found some examples in the internet of how to do that. I improved the examples a bit for how to copy the images in a more easy way. Qt works normally only with char pixels (other image formats are normally called HDR images). I was wondering how you get a video buffer of 32 bit rgb in opencv... I have never seen that !

    If you have a color image in opencv you could use this function to allocate memory:

    QImage* allocateqtimagefromcv(IplImage* cvimg)
        {
            //if (cvimg->nChannels==1)
            //  {
            //  return new QImage(cvimg->width,cvimg->height,QImage::Format_Indexed8);
            //  }
            if (cvimg)
                {
                return new QImage(cvimg->width,cvimg->height,QImage::Format_ARGB32);
                }
        }
    

    to just copy the IplImage to the qt image you could go the easy way and use this function:

    void  IplImage2QImage(IplImage *iplImg,QImage* qimg)
    {
    
    char *data = iplImg->imageData;
        int channels = iplImg->nChannels;
    int h = iplImg->height;
    int w = iplImg->width;
    for (int y = 0; y < h; y++, data += iplImg->widthStep)
    {
    for (int x = 0; x < w; x++)
    {
    char r, g, b, a = 0;
    if (channels == 1)
    {
    r = data[x * channels];
    g = data[x * channels];
    b = data[x * channels];
    }
    else if (channels == 3)
    {
    r = data[x * channels + 2];
    g = data[x * channels + 1];
    b = data[x * channels];
    }
    
    if (channels == 4)
    {
    a = data[x * channels + 3];
    qimg->setPixel(x, y, qRgba(r, g, b, a));
    }
    
    }
    }
    
    
    }
    

    the following function could be a quicker solution:

    static QImage IplImage2QImage(const IplImage *iplImage)
    {
      int height = iplImage->height;
      int width = iplImage->width;
      if (iplImage->depth == IPL_DEPTH_8U && iplImage->nChannels == 3)
      {
          const uchar *qImageBuffer =(const uchar*)iplImage->imageData;
          QImage img(qImageBuffer, width, height, QImage::Format_RGB888);
          return img.rgbSwapped();
      }else{
        //qWarning() << "Image cannot be converted.";
       return QImage();
      }
    } 
    

    hope my function helped. Maybe someone know better ways of doing that :)

提交回复
热议问题