c++ - 如何将灰度/二进制 Mat 转换为 QImage?

标签 c++ qt opencv image-processing

我已经开始学习 Qt 并且正在尝试制作一个简单的视频播放器来加载视频并播放它。它工作得很好。现在为其添加了阈值功能。阈值将从 spinBox 中获取。 代码的编写方式是,阈值操作将使用 spinBox 中的值完成,但值 0 除外(显示正常视频)。 所以这是我的功能:

void Player::run()
{

while(!stop )
{

    if(!capture.read(frame))
        stop = true;
    // convert RGB to gray
    if(frame.channels() == 3)
    {
        if(thresh == 0)
         {
            cvtColor(frame, RGBframe, CV_BGR2RGB);
            img = QImage((const unsigned char*)(RGBframe.data),
                          RGBframe.cols,RGBframe.rows,QImage::Format_RGB888);

         }
        else
        {
            Mat temp;
            cvtColor(frame, temp, CV_BGR2GRAY);
            threshold(temp, binary, thresh, 255, 0);
            img = QImage((const unsigned char*)(binary.data),
                              binary.cols, binary.rows, QImage::Format_Indexed8);




            bool save = img.save("/home/user/binary.png");

             cout<<"threshold value = "<<thresh<<endl;

            //imshow("Binary", binary);
        }
     }
     else
     {
        if(thresh == 0) // original Image
        {
            img = QImage((const unsigned char*)(frame.data),
                             frame.cols,frame.rows,QImage::Format_Indexed8);

        }
        else // convert to Binary Image
        {

            threshold(frame, binary, thresh, 255, 0);

            img = QImage((const unsigned char*)(binary.data),
                              binary.cols, binary.rows, QImage::Format_Indexed8);
        }




     }

     emit processedImage(img);
     this->msleep(delay);
}
}  

对于 spinBox 值等于 0,它运行良好,但是当 spinBox 值增加时,我只得到黑屏。我尝试了 imshow(cv::Mat binary) 它显示了正确的二进制图像,但是当我尝试保存 QImage img 时,它是一些随机的黑白像素(尽管与原始框架大小相同)。

最佳答案

您似乎缺少索引图像的颜色表。您需要添加一个颜色表(在 while 循环之前):

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

从二进制Mat创建QImage后,您需要添加

 img.setColorTable(sColorTable);

或者,正如 @KubaOber 所指出的那样,从 Qt 5.5 开始,您还可以使用 QImage::Format_Grayscale8 格式:

// From Qt 5.5
QImage image(inMat.data, inMat.cols, inMat.rows, 
             static_cast<int>(inMat.step),
             QImage::Format_Grayscale8);

一般来说,你可以将所有的MatQImage的转换包装在一个函数中。下面是 错误更正更新 版本的 cvMatToQImage 最初发现 here .

然后您可以从您的代码中删除所有到 QImage 的转换并改用此函数。

QImage cvMatToQImage(const cv::Mat &inMat)
{
    switch (inMat.type())
    {
        // 8-bit, 4 channel
    case CV_8UC4:
    {
        QImage image(inMat.data,
            inMat.cols, inMat.rows,
            static_cast<int>(inMat.step),
            QImage::Format_ARGB32);

        return image;
    }

    // 8-bit, 3 channel
    case CV_8UC3:
    {
        QImage image(inMat.data,
            inMat.cols, inMat.rows,
            static_cast<int>(inMat.step),
            QImage::Format_RGB888);

        return image.rgbSwapped();
    }

    // 8-bit, 1 channel
    case CV_8UC1:
    {
#if QT_VERSION >= 0x050500

        // From Qt 5.5
        QImage image(inMat.data, inMat.cols, inMat.rows, 
                     static_cast<int>(inMat.step),
                     QImage::Format_Grayscale8);
#else
        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(inMat.data,
            inMat.cols, inMat.rows,
            static_cast<int>(inMat.step),
            QImage::Format_Indexed8);

        image.setColorTable(sColorTable);
#endif
    }

    default:
        qWarning() << "cvMatToQImage() - cv::Mat image type not handled in switch:" << inMat.type();
        break;
    }

    return QImage();
}

关于c++ - 如何将灰度/二进制 Mat 转换为 QImage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40742197/

相关文章:

qt - 使用Qt通过记事本打开txt文件

c++ - 是否有必要在 Qt 中删除对话框窗口指针?

c++ - OpenCV 求解 PnPRansac

opencv - ffmpeg 失败 - 455 方法在此状态下无效

c++ - 如何使用 Qt 的静态构建为 Windows 创建静态链接版本?

image - 在不使用 Hough 变换的情况下查找图像中的圆圈

c++ - 我的定制比较不起作用?

c++ - 为什么这两个整数变量和两个 double 变量(看似)在内存中共享相同的地址?

c++ - 获取 std::string 字符串的大小(以字节为单位)

c++ - 需要帮助找出 kd-tree 实现中的 C++ 代码段