From 9cc964b09e2b6076ad759485022dfdc48a22d9e1 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Sat, 27 Apr 2013 16:54:54 +0200 Subject: Avoid crashes in video frame --- ftnoir_tracker_ht/ftnoir_tracker_ht.cpp | 2 +- ftnoir_tracker_ht/video_widget.cpp | 17 ++++++++-- ftnoir_tracker_ht/video_widget.h | 1 - ftnoir_tracker_pt/ftnoir_tracker_pt.cpp | 2 +- ftnoir_tracker_pt/video_widget.cpp | 59 ++++++++++++++++++--------------- ftnoir_tracker_pt/video_widget.h | 4 +-- 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/ftnoir_tracker_ht/ftnoir_tracker_ht.cpp b/ftnoir_tracker_ht/ftnoir_tracker_ht.cpp index d88699a9..c1755862 100644 --- a/ftnoir_tracker_ht/ftnoir_tracker_ht.cpp +++ b/ftnoir_tracker_ht/ftnoir_tracker_ht.cpp @@ -207,7 +207,7 @@ void Tracker::StartTracker(QFrame* videoframe) #else subprocess.start(QCoreApplication::applicationDirPath() + "/tracker-ht/headtracker-ftnoir"); #endif - connect(&timer, SIGNAL(timeout()), this, SLOT(paint_widget()), Qt::QueuedConnection); + connect(&timer, SIGNAL(timeout()), this, SLOT(paint_widget())); timer.start(40); } diff --git a/ftnoir_tracker_ht/video_widget.cpp b/ftnoir_tracker_ht/video_widget.cpp index 1504822d..2fa272bd 100644 --- a/ftnoir_tracker_ht/video_widget.cpp +++ b/ftnoir_tracker_ht/video_widget.cpp @@ -14,9 +14,20 @@ using namespace std; void VideoWidget::update_image(unsigned char *frame, int width, int height) { QMutexLocker((QMutex*)&mtx); - QImage qframe = QImage(frame, width, height, 3 * width, QImage::Format_RGB888).rgbSwapped(); - if (qframe.size() == size() || (qframe.width() <= this->width() && qframe.height() <= this->height())) - qframe = qframe; + QImage qframe = QImage(width, height, QImage::Format_RGB888); + uchar* data = qframe.bits(); + const int pitch = qframe.bytesPerLine(); + const int cn = 3; + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + const int pos = 3 * (y*width + x); + data[y * pitch + x * 3 + 0] = frame[pos + 2]; + data[y * pitch + x * 3 + 1] = frame[pos + 1]; + data[y * pitch + x * 3 + 2] = frame[pos + 0]; + } + if (qframe.size() == size() || (qframe.width() <= this->width() && qframe.height() <= this->height())) { + } else qframe = qframe.scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation); pixmap = QPixmap::fromImage(qframe); diff --git a/ftnoir_tracker_ht/video_widget.h b/ftnoir_tracker_ht/video_widget.h index 9d8ab489..7cccc9fb 100644 --- a/ftnoir_tracker_ht/video_widget.h +++ b/ftnoir_tracker_ht/video_widget.h @@ -9,7 +9,6 @@ #define VIDEOWIDGET_H #include -#include #include #include #include diff --git a/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp b/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp index 09e758c7..f18671e2 100644 --- a/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp +++ b/ftnoir_tracker_pt/ftnoir_tracker_pt.cpp @@ -177,7 +177,7 @@ void Tracker::StartTracker(QFrame* videoframe) videoframe->setLayout(layout); video_widget->resize(VIDEO_FRAME_WIDTH, VIDEO_FRAME_HEIGHT); videoframe->show(); - connect(&timer, SIGNAL(timeout()), this, SLOT(paint_widget()), Qt::QueuedConnection); + connect(&timer, SIGNAL(timeout()), this, SLOT(paint_widget())); timer.start(40); camera.start(); start(); diff --git a/ftnoir_tracker_pt/video_widget.cpp b/ftnoir_tracker_pt/video_widget.cpp index 7ca1892a..e6d1e24c 100644 --- a/ftnoir_tracker_pt/video_widget.cpp +++ b/ftnoir_tracker_pt/video_widget.cpp @@ -12,39 +12,44 @@ using namespace cv; using namespace std; -void VideoWidget::update_image(Mat frame, std::auto_ptr< vector > points) +void VideoWidget::update_image(Mat frame, std::auto_ptr< vector >) { QMutexLocker((QMutex*)&mtx); - this->frame = frame; - this->points = points; - - QImage qframe; - // convert to QImage - if (frame.channels() == 3) - qframe = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, frame.cols * 3, QImage::Format_RGB888).rgbSwapped(); - else if (frame.channels() == 1) - qframe = QImage((const unsigned char*)(frame.data), frame.cols, frame.rows, frame.cols, QImage::Format_Indexed8).convertToFormat(QImage::Format_RGB888); - if (qframe.size() == size() || (qframe.width() <= width() && qframe.height() <= height())) + if (frame.channels() != 3 && frame.channels() != 1) + return; + + int width = frame.cols, height = frame.rows; + unsigned char* src = frame.data; + + QImage qframe(width, height, QImage::Format_RGB888); + if (frame.channels() == 3) { - ;;; + uchar* data = qframe.bits(); + const int pitch = qframe.bytesPerLine(); + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + const int pos = 3 * (y*width + x); + data[y * pitch + x * 3 + 0] = src[pos + 2]; + data[y * pitch + x * 3 + 1] = src[pos + 1]; + data[y * pitch + x * 3 + 2] = src[pos + 0]; + } + } else { + uchar* data = qframe.bits(); + const int pitch = qframe.bytesPerLine(); + for (int y = 0; y < height; y++) + for (int x = 0; x < width; x++) + { + const int pos = (y*width + x); + data[y * pitch + x * 3 + 0] = src[pos]; + data[y * pitch + x * 3 + 1] = src[pos]; + data[y * pitch + x * 3 + 2] = src[pos]; + } + } + if (qframe.size() == size() || (qframe.width() <= this->width() && qframe.height() <= this->height())) { } else qframe = qframe.scaled(size(), Qt::IgnoreAspectRatio, Qt::FastTransformation); - QPainter painter(&qframe); - painter.setPen(Qt::red); - painter.setBrush(Qt::red); - if (points.get() != NULL) { - const int crosshair_radius = 10; - for (vector::iterator iter = points->begin(); - iter != points->end(); - ++iter) - { - int x = (*iter)[0]; - int y = (*iter)[1]; - painter.drawLine(QLine(x-crosshair_radius, y, x+crosshair_radius, y)); - painter.drawLine(QLine(x, y-crosshair_radius, x, y+crosshair_radius)); - } - } pixmap = QPixmap::fromImage(qframe); } diff --git a/ftnoir_tracker_pt/video_widget.h b/ftnoir_tracker_pt/video_widget.h index eb63a15e..57be8bff 100644 --- a/ftnoir_tracker_pt/video_widget.h +++ b/ftnoir_tracker_pt/video_widget.h @@ -26,7 +26,7 @@ class VideoWidget : public QWidget public: VideoWidget(QWidget *parent) : QWidget(parent), mtx() { } - void update_image(cv::Mat frame, std::auto_ptr< std::vector > points); + void update_image(cv::Mat frame, std::auto_ptr< std::vector >); protected slots: void paintEvent( QPaintEvent* e ) { QMutexLocker((QMutex*)&mtx); @@ -35,9 +35,7 @@ protected slots: } private: - cv::Mat frame; QMutex mtx; - std::auto_ptr< std::vector > points; QPixmap pixmap; }; -- cgit v1.2.3