简体   繁体   English

如何使用Visual Studio将OpenCV图像查看窗口推入QT GUI?

[英]How to push an OpenCV image viewing window into a QT GUI with Visual studio?

I want to create a GUI with 2 rectangles for viewing videos (one where you see the input video, one where you see the post-processed video). 我想创建一个带有2个矩形的GUI来观看视频(一个用于查看输入视频,一个用于查看经过后处理的视频)。

I want it to be integrated into a QT-made GUI, but I want these video areas to be populated from OpenCV, as an alternative to OpenCV's cv::nameWindow method. 我希望将它集成到QT制作的GUI中,但我希望从OpenCV中填充这些视频区域,作为OpenCV的cv::nameWindow方法的替代方案。

How can I do this? 我怎样才能做到这一点?

The basic workflow to do what you desire is: 做你想做的基本工作流程是:

  1. Open the video with OpenCV API (cvCreateFileCapture, for example) 使用OpenCV API打开视频(例如cvCreateFileCapture)
  2. Grab IplImage frames from video (cvQueryFrame) 从视频中获取IplImage帧(cvQueryFrame)
  3. Convert them to QImage (see attached code bellow) 将它们转换为QImage(参见附后的代码)
  4. Show QImage on within a QLabel (QLabel::setPixmap and QPixmap::fromImage) 在QLabel中显示QImage(QLabel :: setPixmap和QPixmap :: fromImage)
  5. Loop the frame update (using a QTimer, for example, with video framerate) 循环帧更新(例如,使用QTimer,使用视频帧速率)

Code to convert IplImage to QImage (assuming RGB32Bits images): 将IplImage转换为QImage的代码(假设RGB32Bits图像):

QImage *IplImageToQImage(IplImage *input)
{
    if (!input)
        return 0;

    QImage image(input->width, input->height, QImage::Format_RGB32);

    uchar* pBits = image.bits();
    int nBytesPerLine = image.bytesPerLine();

    for (int n = 0; n < input->height; n++)
    {
        for (int m = 0; m < input->width; m++)
        {
            CvScalar s = cvGet2D(input, n, m);
            QRgb value = qRgb((uchar)s.val[2], (uchar)s.val[1], (uchar)s.val[0]);

            uchar* scanLine = pBits + n * nBytesPerLine;
            ((uint*)scanLine)[m] = value;
        }
    }

    return image;
}

The understanding of the code above should be straightforward. 理解上面的代码应该是直截了当的。 Any doubts just let us know. 任何疑惑只是让我们知道。

This "low level" option allows you to manipulate each individual frame before displaying it. 此“低级别”选项允许您在显示每个帧之前对其进行操作。 If you just want to display a video via Qt, you can use the Phonon framework . 如果您只想通过Qt显示视频,可以使用Phonon框架

Here is code that converts a cv::Mat into a QImage. 这是将cv :: Mat转换为QImage的代码。 Methods are for 24bit RGB or grayscale floating point, respectively. 方法分别用于24位RGB或灰度浮点。

QImage Mat2QImage(const cv::Mat3b &src) {
        QImage dest(src.cols, src.rows, QImage::Format_ARGB32);
        for (int y = 0; y < src.rows; ++y) {
                const cv::Vec3b *srcrow = src[y];
                QRgb *destrow = (QRgb*)dest.scanLine(y);
                for (int x = 0; x < src.cols; ++x) {
                        destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255);
                }
        }
        return dest;
}


QImage Mat2QImage(const cv::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;
}

Then, you can use the QImage inside a Qt widget. 然后,您可以在Qt小部件中使用QImage。 See borges' answer. 见borges的回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM