2012-02-20 63 views
0

我附上我的代碼的某些部分,的Qt + OpenCV的視頻不顯示Qt的貴,但確實在OpenCV中顯示應用

#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中)代碼正常工作時沒有問題

任何人都可以知道交易是什麼?

+0

檢查繪圖事件是否正在調用,數據到qt_img – 2012-02-20 17:23:19

+0

是的,它被稱爲是因爲當我立即通過從相機幀圖像到QT幀它顯示在屏幕上..我怎麼可能嘗試添加隨機數據? – user573014 2012-02-21 08:28:21

回答

0

所以我想通了什麼差錯在我的代碼,它確實是一個愚蠢的錯誤 - >

我立刻傳遞幀圖像,以我做

frame = cvQueryFrame(capture); 

等什麼功能解決了我的問題是將幀圖像複製到IplImage中,然後它完美運行 - >

cvCopy(frame, resultImage);