Archivo para la categoría Linux

Playing with Qt and OpenCv

In this project I’m going to capture the image from a USB camera, using openCV, and showing all in a QT window.

Let’s started:

The first thing that we have to do, once we have created the project, is to edit the *.pro file, and add our OpenCv paths.

QT       += core gui
 TARGET = untitled
 TEMPLATE = app
 INCLUDEPATH += /home/opencv_source/trunk/opencv/include/opencv
 LIBS += -L/home/opencv_source/trunk/opencv/release/lib -lml -lcvaux -lhighgui -lcv -lcxcore
 SOURCES += capture.cpp\
 main.cpp\
 mainwindow.cpp

HEADERS  +=  capture.h\
 mainwindow.h

FORMS    += mainwindow.ui

I had a problem,  “undefined recerence to vtable” ,that I could fix placing on top capture.h and capture.cpp files.

Once we have ready our environment I’ll explain the content of the files:

– capture: Is the most important file, this class runs in other thread that the GUI, and all that it does is emitting a signal when has a new IplImage from the cam.

Create a new Thread in QT is so easy, all that we have to do is derivate from QThread and implement the virtual method void run()

class Captura : public QThread
 {
 Q_OBJECT              //We need this macro for use Signals mechanism
 public:
 void run();
 signals:
 void newImgAvailable(QImage*);
 };

void Capture::run()

{
 CvCapture * cap = cvCaptureFromCAM(0);
 if(cap == NULL)
 {
 cout << "There were troubles .. " << endl;
 }
 else
 {
 cout << "Ok" << endl;
 IplImage* imgDisplay;
 while(1){
 imgDisplay = cvQueryFrame(cap);
 QImage* image = IplImage2QImage(imgDisplay);
 emit newImgAvailable(image);
 QThread::msleep(10);
 }
 }
 }
<pre>

– In mainwindow we are going to create the instance of capture, start it, and connect with our own slots wich will set the image in a QLabel using setPixMap property.

The header:

class MainWindow : public QMainWindow
 {
 Q_OBJECT

public:
 explicit MainWindow(QWidget *parent = 0);
 ~MainWindow();

public slots:
 void getImg(QImage*);
 private:
 Ui::MainWindow *ui;
 };

and the cpp:

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent),  ui(new Ui::MainWindow)
 {
 ui->setupUi(this);
 Capture* cap = new Capture();               //Create the Thread wich deals with opencv
 connect(cap, SIGNAL(newImgAvailable(QImage*)),this, SLOT(getImg(QImage*)));
 cap->start();  //starting the thread...
 }

void MainWindow::getImg(QImage* img)   //This code will update the image
 {
 ui->label->setPixmap(QPixmap::fromImage(*img));
 delete img;
 }

To avoid memory leaks we have to bear in mind that getImg has to delete the QImage.

Important: We mustn’t release the IplImage* in the loop, because this  is made internally and it would be a mistake

Anuncios

1 comentario

Sistema de detección de clases de objetos utilizando modelos de partes deformables

En este proyecto se ha implementado un detector de clases genéricas de objetos utilizando una aproximación piramidal

y detectores de gradientes orientados (HOG).

Está desarrollado en C++ y hace uso de modelos ya entrenados existentes en workspaces de MATLAB.

A continuación unas imágenes del detector funcionando y un video.

Deja un comentario

Protegido: Visor de consumo de memoria

Este contenido está protegido por contraseña. Para verlo introduce tu contraseña a continuación:

Deja un comentario