Opencv Study Notes (35) -- use QT for camera reading

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.