30 #include <QtGui/QWheelEvent> 31 #include <QtCore/qmath.h> 33 #include <QFileDialog> 34 #include <QtCore/QDir> 36 #include <QGraphicsEffect> 37 #include <QInputDialog> 38 #include <QVBoxLayout> 39 #include <QColorDialog> 41 #include <QGraphicsRectItem> 47 #if QT_VERSION >= 0x050000 48 #include <QStandardPaths> 57 LineItem(
float x1,
float y1,
float x2,
float y2,
const QString & text = QString(), QGraphicsItem * parent = 0) :
58 QGraphicsLineItem(x1, y1, x2, y2, parent),
62 this->setAcceptHoverEvents(
true);
63 this->setFlag(QGraphicsItem::ItemIsFocusable,
true);
76 QGraphicsScene * scene = this->scene();
77 if(scene && scene->focusItem() == 0)
83 this->setPen(QPen(pen().color(),
_width+2));
85 QGraphicsLineItem::hoverEnterEvent(event);
94 QGraphicsLineItem::hoverEnterEvent(event);
100 QGraphicsLineItem::focusInEvent(event);
106 QGraphicsLineItem::focusOutEvent(event);
118 if(qGray(pen().color().rgb() > 255/2))
124 _placeHolder->setBrush(QBrush(QColor ( 255, 255, 255, 170 )));
126 QGraphicsTextItem * text =
new QGraphicsTextItem(
_placeHolder);
127 text->setDefaultTextColor(this->pen().color().rgb());
128 text->setPlainText(
_text);
140 QPen pen = this->pen();
141 this->setPen(QPen(pen.color(),
_width+2));
149 this->setPen(QPen(pen().color(),
_width));
160 QPixmap pixmap(50, 50);
162 return QIcon(pixmap);
169 _defaultBgColor(Qt::black),
170 _defaultFeatureColor(Qt::yellow),
171 _defaultMatchingFeatureColor(Qt::magenta),
172 _defaultMatchingLineColor(Qt::cyan),
176 #if QT_VERSION >= 0x050000 177 _savedFileName = QStandardPaths::writableLocation(QStandardPaths::PicturesLocation);
190 _graphicsView->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
194 this->setLayout(
new QVBoxLayout(
this));
196 this->layout()->setContentsMargins(0,0,0,0);
198 _menu =
new QMenu(tr(
""),
this);
237 QActionGroup * group =
new QActionGroup(
this);
241 QMenu * colorMap =
_menu->addMenu(
"Depth color map");
254 group =
new QActionGroup(
this);
273 settings.beginGroup(group);
280 settings.setValue(
"alpha", this->
getAlpha());
299 settings.beginGroup(group);
301 this->
setImageShown(settings.value(
"image_shown", this->isImageShown()).toBool());
302 this->
setImageDepthShown(settings.value(
"depth_shown", this->isImageDepthShown()).toBool());
303 this->
setFeaturesShown(settings.value(
"features_shown", this->isFeaturesShown()).toBool());
304 this->
setFeaturesSize(settings.value(
"features_size", this->getFeaturesSize()).toInt());
305 this->
setLinesShown(settings.value(
"lines_shown", this->isLinesShown()).toBool());
306 this->
setAlpha(settings.value(
"alpha", this->getAlpha()).toInt());
308 this->
setDefaultFeatureColor(settings.value(
"feature_color", this->getDefaultFeatureColor()).value<QColor>());
311 this->
setGraphicsViewMode(settings.value(
"graphics_view", this->isGraphicsViewMode()).toBool());
312 this->
setGraphicsViewScaled(settings.value(
"graphics_view_scale", this->isGraphicsViewScaled()).toBool());
314 int colorMap = settings.value(
"colormap", 2).toInt();
404 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
453 for(
int i=0; i<
_lines.size(); ++i)
472 float scale, offsetX, offsetY;
486 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
494 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
595 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
597 if(QColor(iter.value()->pen().color().rgb()) == previousColor)
601 iter.value()->setPen(QPen(c));
602 iter.value()->setBrush(QBrush(c));
614 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
616 if(QColor(iter.value()->pen().color().rgb()) == previousColor)
620 iter.value()->setPen(QPen(c));
621 iter.value()->setBrush(QBrush(c));
625 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
627 if(QColor((*iter)->pen().color().rgb()) == previousColor)
631 (*iter)->setPen(QPen(c));
643 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
645 if(QColor(iter.value()->pen().color().rgb()) == previousColor)
649 iter.value()->setPen(QPen(c));
650 iter.value()->setBrush(QBrush(c));
654 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
656 if(QColor((*iter)->pen().color().rgb()) == previousColor)
660 (*iter)->setPen(QPen(c));
689 float widthRatio = float(targetRect.width()) / w;
690 float heightRatio = float(targetRect.height()) / h;
693 if(widthRatio < heightRatio)
707 if(w < targetRect.width())
709 offsetX = (targetRect.width() - w)/2.0
f;
711 if(h < targetRect.height())
713 offsetY = (targetRect.height() - h)/2.0
f;
728 QWidget::paintEvent(event);
735 float ratio, offsetX, offsetY;
737 QPainter painter(
this);
742 painter.drawRect(event->rect());
745 painter.translate(offsetX, offsetY);
746 painter.scale(ratio, ratio);
752 painter.setOpacity(0.5);
757 painter.drawPixmap(QPoint(0,0),
_image);
768 for(QMultiMap<int, rtabmap::KeypointItem *>::iterator iter =
_features.begin(); iter !=
_features.end(); ++iter)
770 QColor color = iter.value()->pen().color();
772 painter.setPen(color);
773 painter.setBrush(color);
774 painter.drawEllipse(iter.value()->rect());
781 for(QList<QGraphicsLineItem*>::iterator iter =
_lines.begin(); iter !=
_lines.end(); ++iter)
783 QColor color = (*iter)->pen().color();
785 painter.setPen(color);
786 painter.drawLine((*iter)->line());
796 QWidget::resizeEvent(event);
821 QAction * action =
_menu->exec(e->globalPos());
827 QString extensions =
"*.png *.xpm *.jpg";
830 extensions +=
" *.pdf";
833 extensions +=
" *.svg";
835 text = QFileDialog::getSaveFileName(
this, tr(
"Save figure to ..."),
_savedFileName, extensions);
839 if(QFileInfo(text).suffix() ==
"")
846 if(QFileInfo(text).suffix().compare(
"pdf") == 0)
848 QPrinter printer(QPrinter::HighResolution);
849 printer.setOrientation(QPrinter::Portrait);
850 printer.setOutputFileName( text );
851 QPainter p(&printer);
853 double xscale = printer.pageRect().width()/double(
_graphicsView->sceneRect().width());
854 double yscale = printer.pageRect().height()/double(
_graphicsView->sceneRect().height());
855 double scale = qMin(xscale, yscale);
856 p.scale(scale, scale);
870 this->render(&p, QPoint(),
_graphicsView->sceneRect().toRect());
899 color = QColorDialog::getColor(color,
this);
956 int value = QInputDialog::getInt(
this, tr(
"Set features and lines transparency"), tr(
"alpha (0-255)"),
_alpha, 0, 255, 10, &ok);
966 int value = QInputDialog::getInt(
this, tr(
"Set features size"), tr(
"Size (0 means actual keypoint size)"),
_featuresSize, 0, 999, 1, &ok);
987 QGraphicsOpacityEffect * effect =
new QGraphicsOpacityEffect();
988 effect->setOpacity(0.5);
1008 if (this->
sceneRect().isValid() && !depth.empty() && int(this->
sceneRect().height()) % depth.rows == 0 && int(this->
sceneRect().width()) % depth.cols == 0)
1010 UDEBUG(
"depth=%dx%d sceneRect=%fx%f", depth.cols, depth.rows, this->sceneRect().width(), this->
sceneRect().height());
1011 xRatio = float(depth.cols)/float(this->
sceneRect().width());
1012 yRatio = float(depth.rows)/float(this->
sceneRect().height());
1013 UDEBUG(
"xRatio=%f yRatio=%f", xRatio, yRatio);
1016 for(std::multimap<int, cv::KeyPoint>::const_iterator iter = refWords.begin(); iter != refWords.end(); ++iter )
1018 if (xRatio > 0 && yRatio > 0)
1020 addFeature(iter->first, iter->second,
util2d::getDepth(depth, iter->second.pt.x*xRatio, iter->second.pt.y*yRatio,
false), color);
1024 addFeature(iter->first, iter->second, 0, color);
1040 if (this->
sceneRect().isValid() && !depth.empty() && int(this->
sceneRect().height()) % depth.rows == 0 && int(this->
sceneRect().width()) % depth.cols == 0)
1042 UDEBUG(
"depth=%dx%d sceneRect=%fx%f", depth.cols, depth.rows, this->sceneRect().width(), this->
sceneRect().height());
1043 xRatio = float(depth.cols) / float(this->
sceneRect().width());
1044 yRatio = float(depth.rows) / float(this->
sceneRect().height());
1045 UDEBUG(
"xRatio=%f yRatio=%f", xRatio, yRatio);
1048 for(
unsigned int i = 0; i< features.size(); ++i )
1050 if(xRatio > 0 && yRatio > 0)
1088 item->setPen(QPen(color));
1101 _image = QPixmap::fromImage(image);
1116 if(image.rect().isValid())
1138 if(
_image.width() > 0 &&
1172 QList<KeypointItem*> items =
_features.values(
id);
1175 for(
int i=0; i<items.size(); ++i)
1177 items[i]->setColor(color);
1182 UWARN(
"Not found feature %d",
id);
1194 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
1196 iter.value()->setColor(color);
1207 UASSERT(alpha >=0 && alpha <= 255);
1209 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
1211 QColor c = iter.value()->pen().color();
1213 iter.value()->setPen(QPen(c));
1214 iter.value()->setBrush(QBrush(c));
1217 for(QList<QGraphicsLineItem*>::iterator iter=
_lines.begin(); iter!=
_lines.end(); ++iter)
1219 QColor c = (*iter)->pen().color();
1221 (*iter)->setPen(QPen(c));
1233 for(QMultiMap<int, KeypointItem*>::iterator iter=
_features.begin(); iter!=
_features.end(); ++iter)
1235 const cv::KeyPoint & kpt = iter.value()->keypoint();
1238 size = kpt.size==0?3:kpt.size;
1241 iter.value()->setRect(kpt.pt.x-sizef/2.0f, kpt.pt.y-sizef/2.0f, sizef, sizef);
void setDefaultFeatureColor(const QColor &color)
void addLine(float x1, float y1, float x2, float y2, QColor color, const QString &text=QString())
QMultiMap< int, rtabmap::KeypointItem * > _features
bool isImageDepthShown() const
QAction * _showImageDepth
const QColor & getDefaultMatchingFeatureColor() const
bool isLinesShown() const
void setDefaultMatchingLineColor(const QColor &color)
void setFeaturesColor(QColor color)
virtual void hoverEnterEvent(QGraphicsSceneHoverEvent *event)
bool isImageShown() const
bool isGraphicsViewScaledToHeight() const
void setFeatureColor(int id, QColor color)
QAction * _setFeaturesSize
void setLinesShown(bool shown)
void computeScaleOffsets(const QRect &targetRect, float &scale, float &offsetX, float &offsetY) const
QList< QGraphicsLineItem * > _lines
bool isGraphicsViewScaled() const
GLM_FUNC_DECL genType e()
void setFeaturesSize(int size)
QAction * _graphicsViewScaledToHeight
void setImage(const QImage &image)
LineItem(float x1, float y1, float x2, float y2, const QString &text=QString(), QGraphicsItem *parent=0)
QAction * _colorMapRedToBlue
const QColor & getDefaultBackgroundColor() const
QAction * _setMatchingFeatureColor
#define UASSERT(condition)
QGraphicsView * _graphicsView
QAction * _graphicsViewMode
void loadSettings(QSettings &settings, const QString &group="")
void setSceneRect(const QRectF &rect)
void setImageShown(bool shown)
void setBackgroundColor(const QColor &color)
QGraphicsRectItem * _placeHolder
QGraphicsPixmapItem * _imageItem
QImage uCvMat2QImage(const cv::Mat &image, bool isBgr=true, uCvQtDepthColorMap colorMap=uCvQtDepthWhiteToBlack)
void setGraphicsViewScaledToHeight(bool scaled)
void setDefaultMatchingFeatureColor(const QColor &color)
QAction * _colorMapWhiteToBlack
QColor _defaultMatchingFeatureColor
void setImageDepth(const cv::Mat &imageDepth)
void setFeaturesShown(bool shown)
QColor _defaultFeatureColor
virtual void hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
virtual void focusOutEvent(QFocusEvent *event)
virtual QSize sizeHint() const
ImageView(QWidget *parent=0)
void addFeature(int id, const cv::KeyPoint &kpt, float depth, QColor color)
QAction * _setMatchingLineColor
bool isGraphicsViewMode() const
void setGraphicsViewMode(bool on)
void setColor(const QColor &color)
const QColor & getBackgroundColor() const
virtual void focusInEvent(QFocusEvent *event)
const QColor & getDefaultFeatureColor() const
void setGraphicsViewScaled(bool scaled)
QAction * _colorMapBlueToRed
virtual void resizeEvent(QResizeEvent *event)
const QColor & getDefaultMatchingLineColor() const
QAction * _setFeatureColor
QColor _defaultMatchingLineColor
virtual void paintEvent(QPaintEvent *event)
void sceneRectChanged(const QRectF &rect)
QIcon createIcon(const QColor &color)
ULogger class and convenient macros.
QAction * _colorMapBlackToWhite
bool isFeaturesShown() const
float RTABMAP_EXP getDepth(const cv::Mat &depthImage, float x, float y, bool smoothing, float depthErrorRatio=0.02f, bool estWithNeighborsIfNull=false)
virtual void contextMenuEvent(QContextMenuEvent *e)
QAction * _graphicsViewNoScaling
void setFeatures(const std::multimap< int, cv::KeyPoint > &refWords, const cv::Mat &depth=cv::Mat(), const QColor &color=Qt::yellow)
uCvQtDepthColorMap getDepthColorMap() const
int getFeaturesSize() const
void setDefaultBackgroundColor(const QColor &color)
void saveSettings(QSettings &settings, const QString &group="") const
QAction * _graphicsViewScaled
void setImageDepthShown(bool shown)
QGraphicsPixmapItem * _imageDepthItem