cv_image_view.cpp
Go to the documentation of this file.
00001 
00004 /*****************************************************************************
00005 ** Includes
00006 *****************************************************************************/
00007 
00008 #include <opencv2/opencv.hpp>
00009 #include <QImage>
00010 #include <QMetaType>
00011 #include <QPixmap>
00012 #include "../../include/qglv/opencv.hpp"
00013 #include "../../include/qglv/widgets/cv_image_view.hpp"
00014 
00015 /*****************************************************************************
00016 ** Macros
00017 *****************************************************************************/
00018 
00019 Q_DECLARE_METATYPE(cv::Mat);
00020 
00021 /*****************************************************************************
00022 ** Namespaces
00023 *****************************************************************************/
00024 
00025 namespace qglv {
00026 
00027 /*****************************************************************************
00028 ** Implementation
00029 *****************************************************************************/
00030 
00031 CvImageView::CvImageView(QWidget *parent)
00032 : QGraphicsView(parent)
00033 , scene(nullptr)
00034 {
00035   qRegisterMetaType<cv::Mat>("OpencvMat");
00036 
00037   // no need for scrollbars - we always scale
00038   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00039   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
00040 
00041   // set the scene
00042   scene = new QGraphicsScene(this);
00043   setScene(scene);
00044 
00045 //  cv::Mat image;
00046 //  image = cv::imread("/home/snorri/images/slam.png", CV_LOAD_IMAGE_COLOR);
00047 //  showImage(image);
00048 }
00049 
00050 /*****************************************************************************
00051  ** Events
00052  *****************************************************************************/
00053 
00054 void CvImageView::showEvent(QShowEvent *) {
00055   fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
00056 }
00057 
00058 void CvImageView::resizeEvent(QResizeEvent* event)
00059 {
00060   QGraphicsView::resizeEvent(event);
00061   fitInView(scene->sceneRect(),Qt::KeepAspectRatio);
00062 }
00063 
00064 /*****************************************************************************
00065  ** Custom Methods
00066  *****************************************************************************/
00067 
00068 void CvImageView::show404Image() {
00069   QPixmap pixmap(":/images/never.jpg");
00070   scene->clear();
00071   scene->addPixmap(pixmap);
00072   fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
00073 }
00074 
00075 void CvImageView::showImage(cv::Mat image) {
00076   if ( image.empty() ) {
00077     show404Image();
00078   } else {
00079     std::pair<cv::Mat, QPixmap> pair = qglv::opencv::matToQPixmap(image);
00080     image_internal_storage = pair.first;
00081     scene->clear();
00082     scene->addPixmap(pair.second);
00083     fitInView(scene->itemsBoundingRect(), Qt::KeepAspectRatio);
00084   }
00085 }
00086 
00087 } // namespace qglv


qglv_opencv
Author(s): Daniel Stonier
autogenerated on Sat Jun 18 2016 08:19:28