The graphic interface design of QT + opencv was introduced before.
I will introduce my development environment qt4.7.4 + opencv2.3.1 + vs2008. In fact, it is very simple to declare the following variables in my qmainwindow subclass:
public:camCapture(QWidget *parent = 0, Qt::WFlags flags = 0);~camCapture();protected:void paintEvent(QPaintEvent * e);private:Ui::camCaptureClass ui;cv::Mat frame;cv::VideoCapture capture;QImage *image;QTimer *timer;private slots:void nextFrame();
The paintevent function is overloaded to update the drawing and add the following to its definition:
Void camcapture: paintevent (qpaintevent * E) {// update the image qpainter painter (this); painter. drawimage (qpoint (0, 12), * image );}
Add the following initialization code to the constructor of camcapture:
// Initialize the qimage and frame. Enable the timer capture. Open (-1); If (capture. isopened () {capture> frame; If (! Frame. empty () {CV: cvtcolor (frame, frame, cv_bgr2rgb); CV: Flip (frame, frame, 1); image = new qimage (const unsigned char *) (frame. data), frame. cols, frame. rows, qimage: format_rgb888); timer = new qtimer (this); timer-> setinterval (30); Connect (timer, signal (timeout (), this, slot (nextframe (); timer-> Start ();}}
Release the timer and image variables in the destructor.
The nextframe function updates data:
// Update data capture> frame; If (! Frame. Empty () {CV: cvtcolor (frame, frame, cv_bgr2rgb); CV: Flip (frame, frame, 1); this-> Update ();}
Here I remembered that when I was reading images, I changed the parameters cv_bgr2rgb and format_rgb888 to cv_bgr2rgba and format_rgb32. However, this test found that these parameters are only valid for the images I tested, this parameter can be used for videos such as cv_bgr2rgb and format_rgb888.
Another bad thing is that I did not find any wizard when I added the nextframe function or the heavy-duty paintevent function. It may be because my development environment vs is not strong enough to support the QT project, you may need to use qtcreator in the future. I am a real newbie in QT. If I have experience, I can tell you how to find the wizard for adding QT events in the vs environment. Thank you very much.
Welcome to download http://download.csdn.net/detail/yang_xian521/3882970