Qt+ OpenCV 视频在 Qt Gui 上不显示,但在 OpenCV 显示应用程序中显示

发布于 2025-01-07 11:26:37 字数 3539 浏览 0 评论 0原文

我附加了代码的某些部分,

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "colorrecognition.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
 {

ui->setupUi(this);
//this->setStyleSheet("background-color: black;"); // set the background
//capture = 0;
// frame = 0;


//interface
//tabWidget = new QTabWidget;



//start capturing a video
//capture = cvCaptureFromCAM(0);
//capture = cvCaptureFromAVI("videoExample.avi");




//frame = cvQueryFrame(capture);

tabWidget = new QTabWidget(ui->centralWidget);
tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
tabWidget->setGeometry(QRect(20, 0, 801, 571));
tabWidget->addTab(new ColorRecognition(), tr("Color Recognition"));

Contour contour1;



}


MainWindow::~MainWindow()
{
    delete ui;
}


//========================================================


ColorRecognition::ColorRecognition(QWidget *parent){

storage1 = cvCreateMemStorage(0);

storage2 = cvCreateMemStorage(0);

 /*QFrame* frame1 = new QFrame(this);
 frame1->setObjectName(QString::fromUtf8("frame1"));
 frame1->setGeometry(QRect(20, 20, 761, 501));
 frame1->setFrameShape(QFrame::StyledPanel);
 frame1->setFrameShadow(QFrame::Raised);*/

 //start capturing a video
 capture = cvCaptureFromCAM(0);
 //capture = cvCaptureFromAVI("videoExample.avi");

 //print an error message in case you can't grab the frame
 if (!cvGrabFrame(capture)) { // capture a frame
         printf("couldn't grab a frame");
 }


 //First screen --- Color Identification ---

//QObject::connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(color_Recoginition()));


 //set timer for 50ms intervals

 QTimer* m_timer = new QTimer(this);

 connect(m_timer, SIGNAL(timeout()), this, SLOT(color()));

 m_timer->start(100);
}

    void ColorRecognition::paintEvent(QPaintEvent* e) {

    QPainter painter(this);

    painter.drawImage(10,10, qt_img);
}



void ColorRecognition::color(){

   frame = cvQueryFrame(capture);

   //CvSize imageSize = cvSize(frame->width,frame->height);

  //IplImage* resultImage = cvCreateImage(imageSize, 8, 3);

  //qDebug("Befroe manipulation");

  cvCvtColor(frame, frame, CV_BGR2RGB);

//CvSize imageSize = cvSize(frame->width, frame->height);

IplImage  **thresholdedImage = contour1.GetThresholdedImage(frame);

//insert the resulted frame from the above function into the find contour function

cvFindContours(thresholdedImage[0], storage1, &contours1, sizeof(CvContour),
            CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));

cvFindContours(thresholdedImage[1], storage2, &contours2, sizeof(CvContour),
            CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));

contour1.drawContour(contours1, storage1, frame, CV_RGB(255, 0, 0));

contour1.drawContour(contours2, storage2, frame, CV_RGB(0, 255, 0));


qt_img = QImage((unsigned char *)frame->imageDataOrigin,frame->width,frame->height,QImage::Format_RGB888);


    //release temp memory

    //cvReleaseMemStorage(&storage1);

    //cvReleaseMemStorage(&storage2);

    //cvReleaseMemStorage(&contours1->storage); // check if that is correct

    //cvReleaseMemStorage(&contours2->storage);*/


//resultImage = contour1.colorIdentification(frame);

//cvCvtColor(resultImage, resultImage, CV_BGR2RGB);

//qDebug("After manipulation");



this->update();

}

问题是当我运行此代码时,屏幕上没有显示任何内容,但是当我运行 Qt 代码(仅在 OpenCV 中)时,代码运行正常,没有

任何 问题知道这是怎么回事吗?

I am attaching some parts of my code,

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "colorrecognition.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
 {

ui->setupUi(this);
//this->setStyleSheet("background-color: black;"); // set the background
//capture = 0;
// frame = 0;


//interface
//tabWidget = new QTabWidget;



//start capturing a video
//capture = cvCaptureFromCAM(0);
//capture = cvCaptureFromAVI("videoExample.avi");




//frame = cvQueryFrame(capture);

tabWidget = new QTabWidget(ui->centralWidget);
tabWidget->setObjectName(QString::fromUtf8("tabWidget"));
tabWidget->setGeometry(QRect(20, 0, 801, 571));
tabWidget->addTab(new ColorRecognition(), tr("Color Recognition"));

Contour contour1;



}


MainWindow::~MainWindow()
{
    delete ui;
}


//========================================================


ColorRecognition::ColorRecognition(QWidget *parent){

storage1 = cvCreateMemStorage(0);

storage2 = cvCreateMemStorage(0);

 /*QFrame* frame1 = new QFrame(this);
 frame1->setObjectName(QString::fromUtf8("frame1"));
 frame1->setGeometry(QRect(20, 20, 761, 501));
 frame1->setFrameShape(QFrame::StyledPanel);
 frame1->setFrameShadow(QFrame::Raised);*/

 //start capturing a video
 capture = cvCaptureFromCAM(0);
 //capture = cvCaptureFromAVI("videoExample.avi");

 //print an error message in case you can't grab the frame
 if (!cvGrabFrame(capture)) { // capture a frame
         printf("couldn't grab a frame");
 }


 //First screen --- Color Identification ---

//QObject::connect(ui->tabWidget, SIGNAL(currentChanged(int)), this, SLOT(color_Recoginition()));


 //set timer for 50ms intervals

 QTimer* m_timer = new QTimer(this);

 connect(m_timer, SIGNAL(timeout()), this, SLOT(color()));

 m_timer->start(100);
}

    void ColorRecognition::paintEvent(QPaintEvent* e) {

    QPainter painter(this);

    painter.drawImage(10,10, qt_img);
}



void ColorRecognition::color(){

   frame = cvQueryFrame(capture);

   //CvSize imageSize = cvSize(frame->width,frame->height);

  //IplImage* resultImage = cvCreateImage(imageSize, 8, 3);

  //qDebug("Befroe manipulation");

  cvCvtColor(frame, frame, CV_BGR2RGB);

//CvSize imageSize = cvSize(frame->width, frame->height);

IplImage  **thresholdedImage = contour1.GetThresholdedImage(frame);

//insert the resulted frame from the above function into the find contour function

cvFindContours(thresholdedImage[0], storage1, &contours1, sizeof(CvContour),
            CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));

cvFindContours(thresholdedImage[1], storage2, &contours2, sizeof(CvContour),
            CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));

contour1.drawContour(contours1, storage1, frame, CV_RGB(255, 0, 0));

contour1.drawContour(contours2, storage2, frame, CV_RGB(0, 255, 0));


qt_img = QImage((unsigned char *)frame->imageDataOrigin,frame->width,frame->height,QImage::Format_RGB888);


    //release temp memory

    //cvReleaseMemStorage(&storage1);

    //cvReleaseMemStorage(&storage2);

    //cvReleaseMemStorage(&contours1->storage); // check if that is correct

    //cvReleaseMemStorage(&contours2->storage);*/


//resultImage = contour1.colorIdentification(frame);

//cvCvtColor(resultImage, resultImage, CV_BGR2RGB);

//qDebug("After manipulation");



this->update();

}

the problem is that when I run this code I don't get anything on the screen, however when I run the code out Qt (only in OpenCV) the code was working with no problems

Can anyone know what is the deal ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

跨年 2025-01-14 11:26:37

所以我弄清楚了我的代码出了什么问题,这确实是一个愚蠢的错误 -->

我立即将帧图像传递给我所做的函数,

frame = cvQueryFrame(capture);

所以解决我的问题的方法是将帧图像复制到 IplImage 中,然后它完美地工作了 -->

cvCopy(frame, resultImage);

so I figured out what went wrong in my code, it is really a silly mistake -->

I was passing the frame image immediately to the function I have done

frame = cvQueryFrame(capture);

so what solved my problem was copying the frame image into an IplImage and then it worked perfectly -->

cvCopy(frame, resultImage);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文