I'm using QT 5.1.0 -rc2 + OpenCV 2.4 with primesensor kinect
Here's my code written below.
My problem is when I add
kinectcapture = new camera(1);
in mainwindow.cpp line 32
the program just unexpected ternimated.
Did I miss something?
Thanks.
camera.h
#ifndef CAMERA_H
#define CAMERA_H
#include <QThread>
#include <opencv.hpp>
#include "iostream"
#include "QImage"
{
Q_OBJECT
public:
//explicit camera(QObject *parent = 0);
explicit camera(const int &data);
~camera();
void stop();
bool open_camera();
signals:
void sendImage_ball
(const QImage &img
);
void sendTrueDepthImage_ball(const cv::Mat &depthImg);
void sendDepthImage_ball(const cv::Mat &depthImg);
public slots:
protected:
void run();
private:
cv::VideoCapture capture;
cv::Mat img_cur;
bool stopped;
int datatype;
};
#ifndef CAMERA_H
#define CAMERA_H
#include <QThread>
#include <opencv.hpp>
#include "iostream"
#include "QImage"
class camera : public QThread
{
Q_OBJECT
public:
//explicit camera(QObject *parent = 0);
explicit camera(const int &data);
~camera();
void stop();
bool open_camera();
signals:
void sendImage_ball(const QImage &img);
void sendTrueDepthImage_ball(const cv::Mat &depthImg);
void sendDepthImage_ball(const cv::Mat &depthImg);
public slots:
protected:
void run();
private:
cv::VideoCapture capture;
cv::Mat img_cur;
bool stopped;
int datatype;
};
To copy to clipboard, switch view to plain text mode
camera.cpp
#include "camera.h"
#include "QDebug"
camera::camera(const int &data)
{
stopped=true;
datatype = data;
}
camera::~camera(){}
bool camera::open_camera(){
return capture.open(CV_CAP_OPENNI);
}
void camera::run(){
if(!capture.isOpened())
throw 1;
stopped=false;
cv::Mat bgrImg;
cv::Mat depthImg;
while(!stopped){
if(!capture.grab())
{
std::cout<<"could not grab img!"<<endl;
break;
}
else
if( 1 && capture.retrieve( bgrImg, CV_CAP_OPENNI_BGR_IMAGE ) ) //capture bgr image
if( 1 && capture.retrieve( depthImg, CV_CAP_OPENNI_DEPTH_MAP )) //capture depth image
{
bgrImg.copyTo(img_cur); //copy the retrieve image to img_cur to proceed getCurrentImg() function
//converting Mat to QImage & display in Qlabel. Note that resolution must be set to prevent crash.
cv::cvtColor(bgrImg, bgrImg, CV_RGB2RGBA);
QImage DisplayImage_ball
= QImage((const unsigned char*)(bgrImg.
data), bgrImg.
cols, bgrImg.
rows,
QImage::Format_RGB32);
DisplayImage_ball=DisplayImage_ball.scaled(320,240,Qt::KeepAspectRatio);
cv::cvtColor(bgrImg, bgrImg, CV_RGBA2RGB);
//***********************************************************//
emit sendImage_ball(DisplayImage_ball);
/*emit sendTrueDepthImage_ball(depthImg);
cv::Mat show_ball;
const float scaleFactor = 1.0f;
depthImg.convertTo( show_ball, CV_8UC1, scaleFactor );
emit sendDepthImage_ball(show_ball);*/
}
}
}
void camera::stop(){
stopped=true;
}
#include "camera.h"
#include "QDebug"
camera::camera(const int &data)
{
stopped=true;
datatype = data;
}
camera::~camera(){}
bool camera::open_camera(){
return capture.open(CV_CAP_OPENNI);
}
void camera::run(){
if(!capture.isOpened())
throw 1;
stopped=false;
cv::Mat bgrImg;
cv::Mat depthImg;
while(!stopped){
if(!capture.grab())
{
std::cout<<"could not grab img!"<<endl;
break;
}
else
if( 1 && capture.retrieve( bgrImg, CV_CAP_OPENNI_BGR_IMAGE ) ) //capture bgr image
if( 1 && capture.retrieve( depthImg, CV_CAP_OPENNI_DEPTH_MAP )) //capture depth image
{
bgrImg.copyTo(img_cur); //copy the retrieve image to img_cur to proceed getCurrentImg() function
//converting Mat to QImage & display in Qlabel. Note that resolution must be set to prevent crash.
cv::cvtColor(bgrImg, bgrImg, CV_RGB2RGBA);
QImage DisplayImage_ball = QImage((const unsigned char*)(bgrImg.data), bgrImg.cols, bgrImg.rows, QImage::Format_RGB32);
DisplayImage_ball=DisplayImage_ball.scaled(320,240,Qt::KeepAspectRatio);
cv::cvtColor(bgrImg, bgrImg, CV_RGBA2RGB);
//***********************************************************//
emit sendImage_ball(DisplayImage_ball);
/*emit sendTrueDepthImage_ball(depthImg);
cv::Mat show_ball;
const float scaleFactor = 1.0f;
depthImg.convertTo( show_ball, CV_8UC1, scaleFactor );
emit sendDepthImage_ball(show_ball);*/
}
}
}
void camera::stop(){
stopped=true;
}
To copy to clipboard, switch view to plain text mode
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "camera.h"
#include "QMessageBox"
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private slots:
void camera_show_update
(const QImage &img
);
void on_openKinect_clicked();
private:
Ui::MainWindow *ui;
camera *kinectcapture;
};
#endif // MAINWINDOW_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "camera.h"
#include "QMessageBox"
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void camera_show_update(const QImage &img);
void on_openKinect_clicked();
private:
Ui::MainWindow *ui;
camera *kinectcapture;
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
kinectcapture =0;
}
MainWindow::~MainWindow()
{
delete ui;
if(kinectcapture){
if(kinectcapture->isRunning()){
kinectcapture->stop();
kinectcapture->wait();
delete kinectcapture;
kinectcapture=0;
}
}
}
void MainWindow::on_openKinect_clicked()
{
if(!kinectcapture){
kinectcapture = new camera(1);
}
if(!kinectcapture->open_camera()){
delete kinectcapture;
kinectcapture=0;
return;
}
kinectcapture->start();
connect(kinectcapture,
SIGNAL(sendImage_ball
(QImage)),
this,
SLOT(camera_show_update
(QImage)));
}
void MainWindow
::camera_show_update(const QImage &img
){ ui
->camera_show
->setPixmap
(QPixmap::fromImage(img
));
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
kinectcapture =0;
}
MainWindow::~MainWindow()
{
delete ui;
if(kinectcapture){
if(kinectcapture->isRunning()){
kinectcapture->stop();
kinectcapture->wait();
delete kinectcapture;
kinectcapture=0;
}
}
}
void MainWindow::on_openKinect_clicked()
{
if(!kinectcapture){
kinectcapture = new camera(1);
}
if(!kinectcapture->open_camera()){
QMessageBox::warning(0, 0, "Can Not Open Cam");
delete kinectcapture;
kinectcapture=0;
return;
}
kinectcapture->start();
connect(kinectcapture,SIGNAL(sendImage_ball(QImage)),this,SLOT(camera_show_update(QImage)));
}
void MainWindow::camera_show_update(const QImage &img){
ui->camera_show->setPixmap(QPixmap::fromImage(img));
}
To copy to clipboard, switch view to plain text mode
Bookmarks