ObjWidget.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the name of the Universite de Sherbrooke nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #include "find_object/Settings.h"
30 #include "find_object/ObjWidget.h"
31 #include "find_object/QtOpenCV.h"
32 
33 #include "KeypointItem.h"
34 
35 #include <opencv2/highgui/highgui.hpp>
36 
37 #include <QWidget>
38 #include <QContextMenuEvent>
39 #include <QMenu>
40 #include <QFileDialog>
41 #include <QAction>
42 #include <QGraphicsView>
43 #include <QGraphicsScene>
44 #include <QVBoxLayout>
45 #include <QGraphicsRectItem>
46 #include <QInputDialog>
47 #include <QPen>
48 #include <QLabel>
49 #include <QColorDialog>
50 
51 #include <QtCore/QDir>
52 
53 #include <stdio.h>
54 
55 namespace find_object {
56 
57 ObjWidget::ObjWidget(QWidget * parent) :
58  QWidget(parent),
59  id_(0),
60  graphicsView_(0),
61  graphicsViewInitialized_(false),
62  alpha_(100),
63  color_(Qt::red)
64 {
65  setupUi();
66 }
67 ObjWidget::ObjWidget(int id, const std::vector<cv::KeyPoint> & keypoints, const QMultiMap<int,int> & words, const QImage & image, QWidget * parent) :
68  QWidget(parent),
69  id_(id),
70  graphicsView_(0),
72  alpha_(100),
73  color_(QColor((Qt::GlobalColor)((id % 10 + 7)==Qt::yellow?Qt::darkYellow:(id % 10 + 7))))
74 {
75  setupUi();
76  this->updateImage(image);
77  this->updateData(keypoints, words);
78 }
80 {
81 }
82 
84 {
85  graphicsView_ = new QGraphicsView(this);
86  graphicsView_->setVisible(false);
87  graphicsView_->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
88  graphicsView_->setScene(new QGraphicsScene(graphicsView_));
89 
90  label_ = new QLabel();
91  label_->setAlignment(Qt::AlignCenter);
92  label_->setTextInteractionFlags(Qt::TextSelectableByMouse);
93 
94  this->setLayout(new QVBoxLayout(this));
95  this->layout()->addWidget(graphicsView_);
96  this->layout()->addWidget(label_);
97  this->layout()->setContentsMargins(0,0,0,0);
98 
99  menu_ = new QMenu(tr(""), this);
100  showImage_ = menu_->addAction(tr("Show image"));
101  showImage_->setCheckable(true);
102  showImage_->setChecked(true);
103  showFeatures_ = menu_->addAction(tr("Show features"));
104  showFeatures_->setCheckable(true);
105  showFeatures_->setChecked(true);
106  mirrorView_ = menu_->addAction(tr("Mirror view"));
107  mirrorView_->setCheckable(true);
108  mirrorView_->setChecked(false);
109  graphicsViewMode_ = menu_->addAction(tr("Graphics view"));
110  graphicsViewMode_->setCheckable(true);
111  graphicsViewMode_->setChecked(false);
112  autoScale_ = menu_->addAction(tr("Scale view"));
113  autoScale_->setCheckable(true);
114  autoScale_->setChecked(true);
115  autoScale_->setEnabled(false);
116  sizedFeatures_ = menu_->addAction(tr("Sized features"));
117  sizedFeatures_->setCheckable(true);
118  sizedFeatures_->setChecked(false);
119  menu_->addSeparator();
120  setColor_ = menu_->addAction(tr("Set color..."));
121  setAlpha_ = menu_->addAction(tr("Set alpha..."));
122  menu_->addSeparator();
123  saveImage_ = menu_->addAction(tr("Save picture..."));
124  menu_->addSeparator();
125  delete_ = menu_->addAction(tr("Delete"));
126  delete_->setEnabled(false);
127 
128  this->setId(id_);
129 
130  graphicsView_->setRubberBandSelectionMode(Qt::ContainsItemShape);
131  graphicsView_->setDragMode(QGraphicsView::RubberBandDrag);
132 
133  connect(graphicsView_->scene(), SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
134 }
135 
136 void ObjWidget::setId(int id)
137 {
138  color_ = QColor((Qt::GlobalColor)((id % 10 + 7)==Qt::yellow?Qt::darkYellow:(id % 10 + 7)));
139  id_=id;
140  if(id_)
141  {
142  savedFileName_ = QString("object_%1.png").arg(id_);
143  }
144 }
145 
147 {
148  graphicsViewMode_->setChecked(on);
149  graphicsView_->setVisible(on && graphicsView_->scene()->items().size());
150  autoScale_->setEnabled(on);
151  //update items' color
152  if(on)
153  {
155  {
156  this->setupGraphicsView();
157  }
158  else
159  {
160  for(int i=0; i<keypointItems_.size(); ++i)
161  {
162  QColor color = kptColors_.at(i);
163  color.setAlpha(alpha_);
164  keypointItems_[i]->setColor(color);
165  }
166  }
167  }
168  if(autoScale_->isChecked())
169  {
170  graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
171  }
172  else
173  {
174  graphicsView_->resetTransform();
175  graphicsView_->setTransform(QTransform().scale(this->isMirrorView()?-1.0:1.0, 1.0));
176  }
177 }
178 
179 void ObjWidget::setAutoScale(bool autoScale)
180 {
181  autoScale_->setChecked(autoScale);
183  {
184  if(autoScale)
185  {
186  graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
187  }
188  else
189  {
190  graphicsView_->resetTransform();
191  graphicsView_->setTransform(QTransform().scale(this->isMirrorView()?-1.0:1.0, 1.0));
192  }
193  }
194 }
195 
197 {
198  sizedFeatures_->setChecked(on);
200  {
201  for(unsigned int i=0; i<(unsigned int)keypointItems_.size() && i<keypoints_.size(); ++i)
202  {
203  float size = 14;
204  if(on && keypoints_[i].size>14.0f)
205  {
206  size = keypoints_[i].size;
207  }
208  float radius = size*1.2f/9.0f*2.0f;
209  keypointItems_.at(i)->setRect(keypoints_[i].pt.x-radius, keypoints_[i].pt.y-radius, radius*2, radius*2);
210  }
211  }
212  if(!graphicsViewMode_->isChecked())
213  {
214  this->update();
215  }
216 }
217 
219 {
220  mirrorView_->setChecked(on);
221  graphicsView_->setTransform(QTransform().scale(this->isMirrorView()?-1.0:1.0, 1.0));
222  if(graphicsViewMode_->isChecked() && autoScale_->isChecked())
223  {
224  graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
225  }
226  else if(!graphicsViewMode_->isChecked())
227  {
228  this->update();
229  }
230 }
231 
232 void ObjWidget::setAlpha(int alpha)
233 {
234  if(alpha>=0 && alpha<=255)
235  {
236  alpha_ = alpha;
238  {
239  for(int i=0; i<keypointItems_.size() && i<kptColors_.size(); ++i)
240  {
241  QColor color = kptColors_.at(i);
242  color.setAlpha(alpha_);
243  keypointItems_.at(i)->setColor(color);
244  }
245  }
246  for(int i=0; i<rectItems_.size(); ++i)
247  {
248  QPen pen = rectItems_.at(i)->pen();
249  QColor color = pen.color();
250  color.setAlpha(alpha_);
251  pen.setColor(color);
252  rectItems_.at(i)->setPen(pen);
253  }
254 
255  if(!graphicsViewMode_->isChecked())
256  {
257  this->update();
258  }
259  }
260 }
261 
262 void ObjWidget::setTextLabel(const QString & text)
263 {
264  label_->setText(text);
265 }
266 
267 void ObjWidget::updateImage(const QImage & image)
268 {
269  pixmap_ = QPixmap::fromImage(image);
270  rect_ = pixmap_.rect();
271  label_->setVisible(image.isNull());
272 }
273 void ObjWidget::updateData(const std::vector<cv::KeyPoint> & keypoints, const QMultiMap<int, int> & words)
274 {
276  kptColors_ = QVector<QColor>((int)keypoints.size(), defaultColor(0));
277  keypointItems_.clear();
278  rectItems_.clear();
279  this->updateWords(words);
280  graphicsView_->scene()->clear();
281  graphicsViewInitialized_ = false;
282  mouseCurrentPos_ = mousePressedPos_; // this will reset roi selection
283 
284  //this->setMinimumSize(image_.size());
285 
286  if(graphicsViewMode_->isChecked())
287  {
288  this->setupGraphicsView();
289  }
290  else
291  {
292  this->update();
293  }
294 }
295 
296 void ObjWidget::updateWords(const QMultiMap<int,int> & words)
297 {
298  words_.clear();
299  for(QMultiMap<int,int>::const_iterator iter=words.begin(); iter!=words.end(); ++iter)
300  {
301  words_.insert(iter.value(), iter.key());
302  }
303  for(int i=0; i<kptColors_.size(); ++i)
304  {
305  kptColors_[i] = defaultColor(words_.size()?words_.value(i,-1):0);
306  if(keypointItems_.size() == kptColors_.size())
307  {
308  keypointItems_[i]->setWordID(words_.value(i,-1));
309  keypointItems_[i]->setColor(defaultColor(words_.size()?keypointItems_[i]->wordID():0));
310  }
311  }
312 }
313 
315 {
316  for(int i=0; i<kptColors_.size(); ++i)
317  {
318  if(keypointItems_.size() == kptColors_.size())
319  {
320  kptColors_[i] = defaultColor(keypointItems_[i]->wordID());
321  keypointItems_[i]->setColor(this->defaultColor(keypointItems_[i]->wordID()));
322  }
323  else
324  {
325  kptColors_[i] = defaultColor(words_.value(i,-1));
326  }
327  }
328  qDeleteAll(rectItems_.begin(), rectItems_.end());
329  rectItems_.clear();
330 }
331 
333 {
334  words_.clear();
335  for(int i=0; i<keypointItems_.size(); ++i)
336  {
337  keypointItems_[i]->setWordID(-1);
338  }
339 }
340 
341 void ObjWidget::setKptColor(int index, const QColor & color)
342 {
343  if(index < kptColors_.size())
344  {
345  kptColors_[index] = color;
346  }
347  else
348  {
349  UWARN("PROBLEM index =%d > size=%d\n", index, kptColors_.size());
350  }
351 
352  if(graphicsViewMode_->isChecked())
353  {
354  if(index < keypointItems_.size())
355  {
356  QColor c = color;
357  c.setAlpha(alpha_);
358  keypointItems_.at(index)->setColor(c);
359  }
360  }
361 }
362 
363 void ObjWidget::setKptWordID(int index, int wordID)
364 {
365  words_.insert(index, wordID);
366  if(index < keypointItems_.size())
367  {
368  keypointItems_.at(index)->setWordID(wordID);
369  }
370 }
371 
372 void ObjWidget::addRect(QGraphicsRectItem * rect)
373 {
375  {
376  graphicsView_->scene()->addItem(rect);
377  }
378  rect->setZValue(1);
379  QPen pen = rect->pen();
380  QColor color = pen.color();
381  color.setAlpha(alpha_);
382  pen.setColor(color);
383  rect->setPen(pen);
384  rectItems_.append(rect);
385 }
386 
387 QList<QGraphicsItem*> ObjWidget::selectedItems() const
388 {
389  return graphicsView_->scene()->selectedItems();
390 }
391 
393 {
394  return showImage_->isChecked();
395 }
396 
398 {
399  return showFeatures_->isChecked();
400 }
401 
403 {
404  return sizedFeatures_->isChecked();
405 }
406 
408 {
409  return mirrorView_->isChecked();
410 }
411 
412 void ObjWidget::setDeletable(bool deletable)
413 {
414  delete_->setEnabled(deletable);
415 }
416 
417 void ObjWidget::setImageShown(bool shown)
418 {
419  showImage_->setChecked(shown);
420  if(graphicsViewMode_->isChecked())
421  {
422  this->updateItemsShown();
423  }
424  else
425  {
426  this->update();
427  }
428 }
429 
431 {
432  showFeatures_->setChecked(shown);
433  if(graphicsViewMode_->isChecked())
434  {
435  this->updateItemsShown();
436  }
437  else
438  {
439  this->update();
440  }
441 }
442 
443 void ObjWidget::computeScaleOffsets(float & scale, float & offsetX, float & offsetY)
444 {
445  scale = 1.0f;
446  offsetX = 0.0f;
447  offsetY = 0.0f;
448 
449  if(!rect_.isNull())
450  {
451  float w = rect_.width();
452  float h = rect_.height();
453  float widthRatio = float(this->rect().width()) / w;
454  float heightRatio = float(this->rect().height()) / h;
455 
456  //printf("w=%f, h=%f, wR=%f, hR=%f, sW=%d, sH=%d\n", w, h, widthRatio, heightRatio, this->rect().width(), this->rect().height());
457  if(widthRatio < heightRatio)
458  {
459  scale = widthRatio;
460  }
461  else
462  {
463  scale = heightRatio;
464  }
465 
466  //printf("ratio=%f\n",ratio);
467 
468  w *= scale;
469  h *= scale;
470 
471  if(w < this->rect().width())
472  {
473  offsetX = (this->rect().width() - w)/2.0f;
474  }
475  if(h < this->rect().height())
476  {
477  offsetY = (this->rect().height() - h)/2.0f;
478  }
479  //printf("offsetX=%f, offsetY=%f\n",offsetX, offsetY);
480  }
481 }
482 
483 void ObjWidget::paintEvent(QPaintEvent *event)
484 {
485  if(graphicsViewMode_->isChecked())
486  {
487  QWidget::paintEvent(event);
488  }
489  else
490  {
491  if(!rect_.isNull())
492  {
493  //Scale
494  float ratio, offsetX, offsetY;
495  this->computeScaleOffsets(ratio, offsetX, offsetY);
496  QPainter painter(this);
497 
498  if(mirrorView_->isChecked())
499  {
500  painter.translate(offsetX+rect_.width()*ratio, offsetY);
501  painter.scale(-ratio, ratio);
502  }
503  else
504  {
505  painter.translate(offsetX, offsetY);
506  painter.scale(ratio, ratio);
507  }
508 
509  if(!pixmap_.isNull() && showImage_->isChecked())
510  {
511  painter.drawPixmap(QPoint(0,0), pixmap_);
512  }
513 
514  if(showFeatures_->isChecked())
515  {
516  drawKeypoints(&painter);
517  }
518 
519  for(int i=0; i<rectItems_.size(); ++i)
520  {
521  painter.save();
522  painter.setTransform(rectItems_.at(i)->transform(), true);
523  painter.setPen(rectItems_.at(i)->pen());
524  painter.drawRect(rectItems_.at(i)->rect());
525  painter.restore();
526  }
527 
529  {
530  painter.save();
531  int left, top, right, bottom;
536  if(mirrorView_->isChecked())
537  {
538  int l = left;
539  left = qAbs(right - rect_.width());
540  right = qAbs(l - rect_.width());
541  }
542  painter.setPen(Qt::NoPen);
543  painter.setBrush(QBrush(QColor(0,0,0,100)));
544  painter.drawRect(0, 0, rect_.width(), top);
545  painter.drawRect(0, top, left, bottom-top);
546  painter.drawRect(right, top, rect_.width()-right, bottom-top);
547  painter.drawRect(0, bottom, rect_.width(), rect_.height()-bottom);
548  painter.restore();
549  }
550  }
551  }
552 }
553 
554 void ObjWidget::resizeEvent(QResizeEvent* event)
555 {
556  QWidget::resizeEvent(event);
557  if(graphicsViewMode_->isChecked() && autoScale_->isChecked())
558  {
559  graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
560  }
561 }
562 
563 void ObjWidget::mousePressEvent(QMouseEvent * event)
564 {
565  float scale, offsetX, offsetY;
566  this->computeScaleOffsets(scale, offsetX, offsetY);
567  mousePressedPos_.setX((event->pos().x()-offsetX)/scale);
568  mousePressedPos_.setY((event->pos().y()-offsetY)/scale);
570  this->update();
571  QWidget::mousePressEvent(event);
572 }
573 
574 void ObjWidget::mouseMoveEvent(QMouseEvent * event)
575 {
576  float scale, offsetX, offsetY;
577  this->computeScaleOffsets(scale, offsetX, offsetY);
578  mouseCurrentPos_.setX((event->pos().x()-offsetX)/scale);
579  mouseCurrentPos_.setY((event->pos().y()-offsetY)/scale);
580  this->update();
581  QWidget::mouseMoveEvent(event);
582 }
583 
584 void ObjWidget::mouseReleaseEvent(QMouseEvent * event)
585 {
586  if(!rect_.isNull())
587  {
588  int left,top,bottom,right;
589 
594 
595  if(mirrorView_->isChecked())
596  {
597  int l = left;
598  left = qAbs(right - rect_.width());
599  right = qAbs(l - rect_.width());
600  }
601 
602  Q_EMIT roiChanged(cv::Rect(left, top, right-left, bottom-top));
603  }
604  QWidget::mouseReleaseEvent(event);
605 }
606 
607 void ObjWidget::contextMenuEvent(QContextMenuEvent * event)
608 {
609  QAction * action = menu_->exec(event->globalPos());
610  if(action == saveImage_)
611  {
612  QString text;
613  if(savedFileName_.isEmpty())
614  {
616  }
617  text = QFileDialog::getSaveFileName(this, tr("Save figure to ..."), savedFileName_, "*.png *.xpm *.jpg *.pdf");
618  if(!text.isEmpty())
619  {
620  if(!text.endsWith(".png") && !text.endsWith(".xpm") && !text.endsWith(".jpg") && !text.endsWith(".pdf"))
621  {
622  text.append(".png");//default
623  }
624  savedFileName_ = text;
625  getSceneAsPixmap().save(text);
626  }
627  }
628  else if(action == showFeatures_ || action == showImage_)
629  {
630  if(graphicsViewMode_->isChecked())
631  {
632  this->updateItemsShown();
633  }
634  else
635  {
636  this->update();
637  }
638  }
639  else if(action == mirrorView_)
640  {
641  this->setMirrorView(mirrorView_->isChecked());
642  }
643  else if(action == delete_)
644  {
645  Q_EMIT removalTriggered(this);
646  }
647  else if(action == graphicsViewMode_)
648  {
649  this->setGraphicsViewMode(graphicsViewMode_->isChecked());
650  }
651  else if(action == autoScale_)
652  {
653  this->setAutoScale(autoScale_->isChecked());
654  }
655  else if(action == sizedFeatures_)
656  {
657  this->setSizedFeatures(sizedFeatures_->isChecked());
658  }
659  else if(action == setColor_)
660  {
661  QColor color = QColorDialog::getColor(color_, this);
662  if(color.isValid())
663  {
664  for(int i=0; i<kptColors_.size(); ++i)
665  {
666  if(kptColors_[i] == color_)
667  {
668  kptColors_[i] = color;
669  if(graphicsViewMode_->isChecked())
670  {
671  keypointItems_[i]->setColor(color);
672  }
673  }
674  }
675  for(int i=0; i<rectItems_.size(); ++i)
676  {
677  if(rectItems_[i]->pen().color() == color_)
678  {
679  QPen p = rectItems_[i]->pen();
680  p.setColor(color);
681  rectItems_[i]->setPen(p);
682  }
683  }
684  color_ = color;
685 
686  }
687  }
688  else if(action == setAlpha_)
689  {
690  bool ok;
691  int newAlpha = QInputDialog::getInt(this, tr("Set alpha"), tr("Alpha:"), alpha_, 0, 255, 5, &ok);
692  if(ok)
693  {
694  this->setAlpha(newAlpha);
695  }
696  }
697 }
698 
700 {
701  if(graphicsViewMode_->isChecked())
702  {
703  QPixmap img(graphicsView_->sceneRect().width(), graphicsView_->sceneRect().height());
704  QPainter p(&img);
705  graphicsView_->scene()->render(&p, graphicsView_->sceneRect(), graphicsView_->sceneRect());
706  return img;
707  }
708  else
709  {
710  return QPixmap::grabWidget(this);
711  }
712 }
713 
715 {
716  QList<QGraphicsItem*> items = graphicsView_->scene()->items();
717  for(int i=0; i<items.size(); ++i)
718  {
719  if(qgraphicsitem_cast<KeypointItem*>(items.at(i)))
720  {
721  items.at(i)->setVisible(showFeatures_->isChecked());
722  }
723  else if(qgraphicsitem_cast<QGraphicsPixmapItem*>(items.at(i)))
724  {
725  items.at(i)->setVisible(showImage_->isChecked());
726  }
727  }
728 }
729 
730 void ObjWidget::drawKeypoints(QPainter * painter)
731 {
732  QList<KeypointItem *> items;
733  KeypointItem * item = 0;
734 
735  int i = 0;
736  for(std::vector<cv::KeyPoint>::const_iterator iter = keypoints_.begin(); iter != keypoints_.end(); ++iter, ++i )
737  {
738  const cv::KeyPoint & r = *iter;
739  float size = 14;
740  if(r.size>14.0f && sizedFeatures_->isChecked())
741  {
742  size = r.size;
743  }
744  float radius = size*1.2f/9.0f*2.0f;
745  QColor color(kptColors_.at(i).red(), kptColors_.at(i).green(), kptColors_.at(i).blue(), alpha_);
746  if(graphicsViewMode_->isChecked())
747  {
748  // YELLOW = NEW and multiple times
749  item = new KeypointItem(i, r.pt.x-radius, r.pt.y-radius, radius*2, r, words_.value(i, -1), color);
750  item->setVisible(this->isFeaturesShown());
751  item->setZValue(2);
752  graphicsView_->scene()->addItem(item);
753  item->setColor(defaultColor(item->wordID()));
754  kptColors_[i] = defaultColor(item->wordID());
755  keypointItems_.append(item);
756  }
757 
758  if(painter)
759  {
760  painter->save();
761  painter->setPen(color);
762  painter->setBrush(color);
763  painter->drawEllipse(r.pt.x-radius, r.pt.y-radius, radius*2, radius*2);
764  painter->restore();
765  }
766  }
767 }
768 
769 QColor ObjWidget::defaultColor(int id) const
770 {
771  QColor color(id >= 0 ? Qt::yellow : Qt::white);
772  color.setAlpha(alpha_);
773  return color;
774 }
775 
776 std::vector<cv::KeyPoint> ObjWidget::selectedKeypoints() const
777 {
778  std::vector<cv::KeyPoint> selected;
779  if(graphicsViewMode_->isChecked())
780  {
781  QList<QGraphicsItem*> items = graphicsView_->scene()->selectedItems();
782  for(int i=0; i<items.size(); ++i)
783  {
784  if(qgraphicsitem_cast<KeypointItem*>(items.at(i)))
785  {
786  selected.push_back(keypoints_.at(((KeypointItem*)items.at(i))->id()));
787  }
788  }
789  }
790  return selected;
791 }
792 
794 {
795  if(!rect_.isNull())
796  {
797  graphicsView_->setVisible(true);
798  graphicsView_->scene()->setSceneRect(rect_);
799  QList<KeypointItem*> items;
800 
801  QRectF sceneRect = graphicsView_->sceneRect();
802 
803  QGraphicsPixmapItem * pixmapItem = graphicsView_->scene()->addPixmap(pixmap_);
804  pixmapItem->setVisible(this->isImageShown());
805  this->drawKeypoints();
806 
807  for(int i=0; i<rectItems_.size(); ++i)
808  {
809  graphicsView_->scene()->addItem(rectItems_.at(i));
810  }
811 
812  if(autoScale_->isChecked())
813  {
814  graphicsView_->fitInView(sceneRect, Qt::KeepAspectRatio);
815  }
817  }
818  else
819  {
820  graphicsView_->setVisible(false);
821  }
822 }
823 
824 } // namespace find_object
825 
QVector< QColor > kptColors_
Definition: ObjWidget.h:123
const QColor & color() const
Definition: ObjWidget.h:80
void setSizedFeatures(bool on)
Definition: ObjWidget.cpp:196
virtual void paintEvent(QPaintEvent *event)
Definition: ObjWidget.cpp:483
void setImageShown(bool shown)
Definition: ObjWidget.cpp:417
void updateImage(const QImage &image)
Definition: ObjWidget.cpp:267
f
QAction * sizedFeatures_
Definition: ObjWidget.h:140
void setAlpha(int alpha)
Definition: ObjWidget.cpp:232
void updateData(const std::vector< cv::KeyPoint > &keypoints, const QMultiMap< int, int > &words=QMultiMap< int, int >())
Definition: ObjWidget.cpp:273
bool isMirrorView() const
Definition: ObjWidget.cpp:407
std::vector< cv::KeyPoint > keypoints_
Definition: ObjWidget.h:117
void setColor(const QColor &color)
std::vector< cv::KeyPoint > selectedKeypoints() const
Definition: ObjWidget.cpp:776
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: ObjWidget.cpp:574
const std::vector< cv::KeyPoint > keypoints() const
Definition: ObjWidget.h:81
void drawKeypoints(QPainter *painter=0)
Definition: ObjWidget.cpp:730
QAction * showFeatures_
Definition: ObjWidget.h:134
void updateWords(const QMultiMap< int, int > &words)
Definition: ObjWidget.cpp:296
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
QGraphicsView * graphicsView_
Definition: ObjWidget.h:122
QList< KeypointItem * > keypointItems_
Definition: ObjWidget.h:121
bool isFeaturesShown() const
Definition: ObjWidget.cpp:397
void setDeletable(bool deletable)
Definition: ObjWidget.cpp:412
void computeScaleOffsets(float &scale, float &offsetX, float &offsetY)
Definition: ObjWidget.cpp:443
void setKptColor(int index, const QColor &color)
Definition: ObjWidget.cpp:341
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: ObjWidget.cpp:584
QAction * graphicsViewMode_
Definition: ObjWidget.h:138
void setAutoScale(bool autoScale)
Definition: ObjWidget.cpp:179
void addRect(QGraphicsRectItem *rect)
Definition: ObjWidget.cpp:372
virtual void mousePressEvent(QMouseEvent *event)
Definition: ObjWidget.cpp:563
void setKptWordID(int index, int wordId)
Definition: ObjWidget.cpp:363
QList< QGraphicsItem * > selectedItems() const
Definition: ObjWidget.cpp:387
void setTextLabel(const QString &text)
Definition: ObjWidget.cpp:262
ObjWidget(QWidget *parent=0)
Definition: ObjWidget.cpp:57
action
void setMirrorView(bool on)
Definition: ObjWidget.cpp:218
static QString workingDirectory()
Definition: Settings.cpp:70
TFSIMD_FORCE_INLINE const tfScalar & w() const
QPixmap getSceneAsPixmap()
Definition: ObjWidget.cpp:699
virtual void contextMenuEvent(QContextMenuEvent *event)
Definition: ObjWidget.cpp:607
int id() const
Definition: ObjWidget.h:79
const QMap< int, int > & words() const
Definition: ObjWidget.h:82
ULogger class and convenient macros.
#define UWARN(...)
void setGraphicsViewMode(bool on)
Definition: ObjWidget.cpp:146
void removalTriggered(find_object::ObjWidget *)
void setFeaturesShown(bool shown)
Definition: ObjWidget.cpp:430
QMap< int, int > words_
Definition: ObjWidget.h:118
virtual void resizeEvent(QResizeEvent *event)
Definition: ObjWidget.cpp:554
QList< QGraphicsRectItem * > rectItems_
Definition: ObjWidget.h:124
void roiChanged(const cv::Rect &)
bool isSizedFeatures() const
Definition: ObjWidget.cpp:402
QColor defaultColor(int id) const
Definition: ObjWidget.cpp:769
bool isImageShown() const
Definition: ObjWidget.cpp:392


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Jun 10 2019 13:21:31