From f4b1b64ae6df039f2209e2aa8e62ca1a3c0d253c Mon Sep 17 00:00:00 2001 From: "Tux3 / Mlkj / !Lev.uXFMLA" Date: Fri, 29 Aug 2014 13:40:37 +0200 Subject: [PATCH] Switch self cam view to OpenCV --- qtox.pro | 2 +- widget/camera.cpp | 15 ++++++++++++--- widget/camera.h | 6 +++++- widget/selfcamview.cpp | 22 +++++++++++++++++++--- 4 files changed, 37 insertions(+), 8 deletions(-) diff --git a/qtox.pro b/qtox.pro index 1d0a5591d..46ad30c9e 100644 --- a/qtox.pro +++ b/qtox.pro @@ -45,7 +45,7 @@ win32 { } macx { LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lsodium -lvpx -framework OpenAL } else { - LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lsodium -lvpx -lopenal + LIBS += -L$$PWD/libs/lib/ -ltoxcore -ltoxav -lsodium -lvpx -lopenal -lopencv_core -lopencv_highgui -lopencv_imgproc } #### Static linux build diff --git a/widget/camera.cpp b/widget/camera.cpp index 7b5270bab..c1841f398 100644 --- a/widget/camera.cpp +++ b/widget/camera.cpp @@ -20,6 +20,8 @@ #include #include +using namespace cv; + static inline void fromYCbCrToRGB( uint8_t Y, uint8_t Cb, uint8_t Cr, uint8_t& R, uint8_t& G, uint8_t& B) @@ -75,7 +77,7 @@ void Camera::suscribe() if (refcount <= 0) { refcount = 1; - camera->start(); + cap.open(0); } else refcount++; @@ -87,16 +89,23 @@ void Camera::unsuscribe() if (refcount <= 0) { - camera->stop(); + cap.release(); refcount = 0; } } -QVideoFrame Camera::getLastFrame() +QVideoFrame Camera::getLastVideoFrame() { return lastFrame; } +Mat Camera::getLastFrame() +{ + Mat frame; + cap >> frame; + return frame; +} + bool Camera::start(const QVideoSurfaceFormat &format) { if(supportedFormats.contains(format.pixelFormat())) diff --git a/widget/camera.h b/widget/camera.h index dd085b61b..321778e69 100644 --- a/widget/camera.h +++ b/widget/camera.h @@ -21,6 +21,7 @@ #include #include #include "vpx/vpx_image.h" +#include "opencv2/opencv.hpp" /** * This class is a wrapper to share a camera's captured video frames @@ -39,7 +40,8 @@ public: Camera(); void suscribe(); ///< Call this once before trying to get frames void unsuscribe(); ///< Call this once when you don't need frames anymore - QVideoFrame getLastFrame(); ///< Get the last captured frame + cv::Mat getLastFrame(); ///< Get the last captured frame + QVideoFrame getLastVideoFrame(); ///< Get the last captured frame QImage getLastImage(); ///< Convert the last frame to a QImage (can be expensive !) vpx_image getLastVPXImage(); ///< Convert the last frame to a vpx_image (can be expensive !) bool isFormatSupported(const QVideoSurfaceFormat & format) const; @@ -58,6 +60,8 @@ private: QVideoFrame lastFrame; int frameFormat; QList supportedFormats; + + cv::VideoCapture cap; }; #endif // CAMERA_H diff --git a/widget/selfcamview.cpp b/widget/selfcamview.cpp index d38b773dd..3a453deff 100644 --- a/widget/selfcamview.cpp +++ b/widget/selfcamview.cpp @@ -24,6 +24,8 @@ #include "videosurface.h" #include "widget.h" +using namespace cv; + SelfCamView::SelfCamView(Camera* Cam, QWidget* parent) : QWidget(parent), displayLabel{new QLabel}, mainLayout{new QHBoxLayout()}, cam(Cam) @@ -60,8 +62,22 @@ void SelfCamView::showEvent(QShowEvent* event) event->accept(); } -void SelfCamView::updateDisplay() -{ - displayLabel->setPixmap(QPixmap::fromImage(cam->getLastImage())); +QImage Mat2QImage(const cv::Mat3b &src) { + QImage dest(src.cols, src.rows, QImage::Format_ARGB32); + for (int y = 0; y < src.rows; ++y) { + const cv::Vec3b *srcrow = src[y]; + QRgb *destrow = (QRgb*)dest.scanLine(y); + for (int x = 0; x < src.cols; ++x) { + destrow[x] = qRgba(srcrow[x][2], srcrow[x][1], srcrow[x][0], 255); + } + } + return dest; +} + +void SelfCamView::updateDisplay() +{ + Mat frame = cam->getLastFrame(); + + displayLabel->setPixmap(QPixmap::fromImage(Mat2QImage(frame))); }