Use opencv in QT creator to collect camera Information

Source: Internet
Author: User

Previously, opencv was compiled on QT creator. Therefore, the camera information needs to be collected as required. Therefore, I collected some information on the Internet, followed the instructions of the gourd painting, and finally successfully collected the information.

Open qtcreator and create a widget project.

Put two labels on the interface to display the data and photos collected by the camera.

The source code in widget. H is as follows:

# Ifndef widget_h

#define WIDGET_H
#include <QWidget>
#include <QImage>
# Include <qtimer> // set the data collection Interval
#include 
#include <cv.h>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
private slots:
Void opencamara (); // camera when enabled
Void readfarme (); // read the current frame information
Void closecamara (); // disable the camera
Void takingpictures (); // photograph
private:
    Ui::Widget *ui;
   QTimer *timer;
    QImage *imag;
Cvcapture * Cam; // The structure of the video, which is used as a parameter of the video retrieval function.
Iplimage * frame; // apply for iplimage type pointer, that is, apply for memory space to store each frame of Image
};

#endif // WIDGET_H

 

The source files in widget. cpp are as follows:

# Include "widget. H"

#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    cam=NULL;
    timer=new QTimer(this);
    imag=new QImage();
    connect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));
    connect(ui->open,SIGNAL(clicked()),this,SLOT(openCamara()));
    connect(ui->pic,SIGNAL(clicked()),this,SLOT(takingPictures()));
    connect(ui->close,SIGNAL(clicked()),this,SLOT(closeCamara()));
}
Widget::~Widget()
{
    delete ui;
}
void Widget::openCamara()
{
    cam=cvCreateCameraCapture(0);
    timer->start(33);
}
void Widget::readFarme()
{
    frame=cvQueryFrame(cam);
  //  QImage imag((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888);
    QImage imag=QImage((const uchar*)frame->imageData,frame->width,frame->height,QImage::Format_RGB888).rgbSwapped();
    ui->label->setPixmap(QPixmap::fromImage(imag));
}
void Widget::closeCamara()
{
    timer->stop();
Cvreleasecapture (& cam); // releases the memory;
}

void Widget::takingPictures()
{
   frame = cvQueryFrame(cam);
   QImage imag((const uchar*)frame->imageData, frame->width, frame->height, QImage::Format_RGB888);
   ui->label_2->setPixmap(QPixmap::fromImage(imag));
}

Okay, all the code is OK.

(The general idea is: Click the open camera button to enable the camera and set a timer to capture the frames captured by the camera at intervals and convert them to qimage, display on the qlabel control at the end)

 

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.