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();
270  rect_ = QRect();
271  if(!image.isNull())
272  {
273  pixmap_ = QPixmap::fromImage(image);
274  rect_ = pixmap_.rect();
275  }
276  label_->setVisible(image.isNull());
277 }
278 void ObjWidget::updateData(const std::vector<cv::KeyPoint> & keypoints, const QMultiMap<int, int> & words)
279 {
281  kptColors_ = QVector<QColor>((int)keypoints.size(), defaultColor(0));
282  keypointItems_.clear();
283  rectItems_.clear();
284  this->updateWords(words);
285  graphicsView_->scene()->clear();
286  graphicsViewInitialized_ = false;
287  mouseCurrentPos_ = mousePressedPos_; // this will reset roi selection
288 
289  //this->setMinimumSize(image_.size());
290 
291  if(graphicsViewMode_->isChecked())
292  {
293  this->setupGraphicsView();
294  }
295  else
296  {
297  this->update();
298  }
299 }
300 
301 void ObjWidget::updateWords(const QMultiMap<int,int> & words)
302 {
303  words_.clear();
304  for(QMultiMap<int,int>::const_iterator iter=words.begin(); iter!=words.end(); ++iter)
305  {
306  words_.insert(iter.value(), iter.key());
307  }
308  for(int i=0; i<kptColors_.size(); ++i)
309  {
310  kptColors_[i] = defaultColor(words_.size()?words_.value(i,-1):0);
311  if(keypointItems_.size() == kptColors_.size())
312  {
313  keypointItems_[i]->setWordID(words_.value(i,-1));
314  keypointItems_[i]->setColor(defaultColor(words_.size()?keypointItems_[i]->wordID():0));
315  }
316  }
317 }
318 
320 {
321  for(int i=0; i<kptColors_.size(); ++i)
322  {
323  if(keypointItems_.size() == kptColors_.size())
324  {
325  kptColors_[i] = defaultColor(keypointItems_[i]->wordID());
326  keypointItems_[i]->setColor(this->defaultColor(keypointItems_[i]->wordID()));
327  }
328  else
329  {
330  kptColors_[i] = defaultColor(words_.value(i,-1));
331  }
332  }
333  qDeleteAll(rectItems_.begin(), rectItems_.end());
334  rectItems_.clear();
335 }
336 
338 {
339  words_.clear();
340  for(int i=0; i<keypointItems_.size(); ++i)
341  {
342  keypointItems_[i]->setWordID(-1);
343  }
344 }
345 
346 void ObjWidget::setKptColor(int index, const QColor & color)
347 {
348  if(index < kptColors_.size())
349  {
350  kptColors_[index] = color;
351  }
352  else
353  {
354  UWARN("PROBLEM index =%d > size=%d\n", index, kptColors_.size());
355  }
356 
357  if(graphicsViewMode_->isChecked())
358  {
359  if(index < keypointItems_.size())
360  {
361  QColor c = color;
362  c.setAlpha(alpha_);
363  keypointItems_.at(index)->setColor(c);
364  }
365  }
366 }
367 
368 void ObjWidget::setKptWordID(int index, int wordID)
369 {
370  words_.insert(index, wordID);
371  if(index < keypointItems_.size())
372  {
373  keypointItems_.at(index)->setWordID(wordID);
374  }
375 }
376 
377 void ObjWidget::addRect(QGraphicsRectItem * rect)
378 {
380  {
381  graphicsView_->scene()->addItem(rect);
382  }
383  rect->setZValue(1);
384  QPen pen = rect->pen();
385  QColor color = pen.color();
386  color.setAlpha(alpha_);
387  pen.setColor(color);
388  rect->setPen(pen);
389  rectItems_.append(rect);
390 }
391 
392 QList<QGraphicsItem*> ObjWidget::selectedItems() const
393 {
394  return graphicsView_->scene()->selectedItems();
395 }
396 
398 {
399  return showImage_->isChecked();
400 }
401 
403 {
404  return showFeatures_->isChecked();
405 }
406 
408 {
409  return sizedFeatures_->isChecked();
410 }
411 
413 {
414  return mirrorView_->isChecked();
415 }
416 
417 void ObjWidget::setDeletable(bool deletable)
418 {
419  delete_->setEnabled(deletable);
420 }
421 
422 void ObjWidget::setImageShown(bool shown)
423 {
424  showImage_->setChecked(shown);
425  if(graphicsViewMode_->isChecked())
426  {
427  this->updateItemsShown();
428  }
429  else
430  {
431  this->update();
432  }
433 }
434 
436 {
437  showFeatures_->setChecked(shown);
438  if(graphicsViewMode_->isChecked())
439  {
440  this->updateItemsShown();
441  }
442  else
443  {
444  this->update();
445  }
446 }
447 
448 void ObjWidget::computeScaleOffsets(float & scale, float & offsetX, float & offsetY)
449 {
450  scale = 1.0f;
451  offsetX = 0.0f;
452  offsetY = 0.0f;
453 
454  if(!rect_.isNull())
455  {
456  float w = rect_.width();
457  float h = rect_.height();
458  float widthRatio = float(this->rect().width()) / w;
459  float heightRatio = float(this->rect().height()) / h;
460 
461  //printf("w=%f, h=%f, wR=%f, hR=%f, sW=%d, sH=%d\n", w, h, widthRatio, heightRatio, this->rect().width(), this->rect().height());
462  if(widthRatio < heightRatio)
463  {
464  scale = widthRatio;
465  }
466  else
467  {
468  scale = heightRatio;
469  }
470 
471  //printf("ratio=%f\n",ratio);
472 
473  w *= scale;
474  h *= scale;
475 
476  if(w < this->rect().width())
477  {
478  offsetX = (this->rect().width() - w)/2.0f;
479  }
480  if(h < this->rect().height())
481  {
482  offsetY = (this->rect().height() - h)/2.0f;
483  }
484  //printf("offsetX=%f, offsetY=%f\n",offsetX, offsetY);
485  }
486 }
487 
488 void ObjWidget::paintEvent(QPaintEvent *event)
489 {
490  if(graphicsViewMode_->isChecked())
491  {
492  QWidget::paintEvent(event);
493  }
494  else
495  {
496  if(!rect_.isNull())
497  {
498  //Scale
499  float ratio, offsetX, offsetY;
500  this->computeScaleOffsets(ratio, offsetX, offsetY);
501  QPainter painter(this);
502 
503  if(mirrorView_->isChecked())
504  {
505  painter.translate(offsetX+rect_.width()*ratio, offsetY);
506  painter.scale(-ratio, ratio);
507  }
508  else
509  {
510  painter.translate(offsetX, offsetY);
511  painter.scale(ratio, ratio);
512  }
513 
514  if(!pixmap_.isNull() && showImage_->isChecked())
515  {
516  painter.drawPixmap(QPoint(0,0), pixmap_);
517  }
518 
519  if(showFeatures_->isChecked())
520  {
521  drawKeypoints(&painter);
522  }
523 
524  for(int i=0; i<rectItems_.size(); ++i)
525  {
526  painter.save();
527  painter.setTransform(rectItems_.at(i)->transform(), true);
528  painter.setPen(rectItems_.at(i)->pen());
529  painter.drawRect(rectItems_.at(i)->rect());
530  painter.restore();
531  }
532 
534  {
535  painter.save();
536  int left, top, right, bottom;
541  if(mirrorView_->isChecked())
542  {
543  int l = left;
544  left = qAbs(right - rect_.width());
545  right = qAbs(l - rect_.width());
546  }
547  painter.setPen(Qt::NoPen);
548  painter.setBrush(QBrush(QColor(0,0,0,100)));
549  painter.drawRect(0, 0, rect_.width(), top);
550  painter.drawRect(0, top, left, bottom-top);
551  painter.drawRect(right, top, rect_.width()-right, bottom-top);
552  painter.drawRect(0, bottom, rect_.width(), rect_.height()-bottom);
553  painter.restore();
554  }
555  }
556  }
557 }
558 
559 void ObjWidget::resizeEvent(QResizeEvent* event)
560 {
561  QWidget::resizeEvent(event);
562  if(graphicsViewMode_->isChecked() && autoScale_->isChecked())
563  {
564  graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
565  }
566 }
567 
568 void ObjWidget::mousePressEvent(QMouseEvent * event)
569 {
570  float scale, offsetX, offsetY;
571  this->computeScaleOffsets(scale, offsetX, offsetY);
572  mousePressedPos_.setX((event->pos().x()-offsetX)/scale);
573  mousePressedPos_.setY((event->pos().y()-offsetY)/scale);
575  this->update();
576  QWidget::mousePressEvent(event);
577 }
578 
579 void ObjWidget::mouseMoveEvent(QMouseEvent * event)
580 {
581  float scale, offsetX, offsetY;
582  this->computeScaleOffsets(scale, offsetX, offsetY);
583  mouseCurrentPos_.setX((event->pos().x()-offsetX)/scale);
584  mouseCurrentPos_.setY((event->pos().y()-offsetY)/scale);
585  this->update();
586  QWidget::mouseMoveEvent(event);
587 }
588 
589 void ObjWidget::mouseReleaseEvent(QMouseEvent * event)
590 {
591  if(!rect_.isNull())
592  {
593  int left,top,bottom,right;
594 
599 
600  if(mirrorView_->isChecked())
601  {
602  int l = left;
603  left = qAbs(right - rect_.width());
604  right = qAbs(l - rect_.width());
605  }
606 
607  Q_EMIT roiChanged(cv::Rect(left, top, right-left, bottom-top));
608  }
609  QWidget::mouseReleaseEvent(event);
610 }
611 
612 void ObjWidget::contextMenuEvent(QContextMenuEvent * event)
613 {
614  QAction * action = menu_->exec(event->globalPos());
615  if(action == saveImage_)
616  {
617  QString text;
618  if(savedFileName_.isEmpty())
619  {
621  }
622  text = QFileDialog::getSaveFileName(this, tr("Save figure to ..."), savedFileName_, "*.png *.xpm *.jpg *.pdf");
623  if(!text.isEmpty())
624  {
625  if(!text.endsWith(".png") && !text.endsWith(".xpm") && !text.endsWith(".jpg") && !text.endsWith(".pdf"))
626  {
627  text.append(".png");//default
628  }
629  savedFileName_ = text;
630  getSceneAsPixmap().save(text);
631  }
632  }
633  else if(action == showFeatures_ || action == showImage_)
634  {
635  if(graphicsViewMode_->isChecked())
636  {
637  this->updateItemsShown();
638  }
639  else
640  {
641  this->update();
642  }
643  }
644  else if(action == mirrorView_)
645  {
646  this->setMirrorView(mirrorView_->isChecked());
647  }
648  else if(action == delete_)
649  {
650  Q_EMIT removalTriggered(this);
651  }
652  else if(action == graphicsViewMode_)
653  {
654  this->setGraphicsViewMode(graphicsViewMode_->isChecked());
655  }
656  else if(action == autoScale_)
657  {
658  this->setAutoScale(autoScale_->isChecked());
659  }
660  else if(action == sizedFeatures_)
661  {
662  this->setSizedFeatures(sizedFeatures_->isChecked());
663  }
664  else if(action == setColor_)
665  {
666  QColor color = QColorDialog::getColor(color_, this);
667  if(color.isValid())
668  {
669  for(int i=0; i<kptColors_.size(); ++i)
670  {
671  if(kptColors_[i] == color_)
672  {
673  kptColors_[i] = color;
674  if(graphicsViewMode_->isChecked())
675  {
676  keypointItems_[i]->setColor(color);
677  }
678  }
679  }
680  for(int i=0; i<rectItems_.size(); ++i)
681  {
682  if(rectItems_[i]->pen().color() == color_)
683  {
684  QPen p = rectItems_[i]->pen();
685  p.setColor(color);
686  rectItems_[i]->setPen(p);
687  }
688  }
689  color_ = color;
690 
691  }
692  }
693  else if(action == setAlpha_)
694  {
695  bool ok;
696  int newAlpha = QInputDialog::getInt(this, tr("Set alpha"), tr("Alpha:"), alpha_, 0, 255, 5, &ok);
697  if(ok)
698  {
699  this->setAlpha(newAlpha);
700  }
701  }
702 }
703 
705 {
706  if(graphicsViewMode_->isChecked())
707  {
708  QPixmap img(graphicsView_->sceneRect().width(), graphicsView_->sceneRect().height());
709  QPainter p(&img);
710  graphicsView_->scene()->render(&p, graphicsView_->sceneRect(), graphicsView_->sceneRect());
711  return img;
712  }
713  else
714  {
715  return QPixmap::grabWidget(this);
716  }
717 }
718 
720 {
721  QList<QGraphicsItem*> items = graphicsView_->scene()->items();
722  for(int i=0; i<items.size(); ++i)
723  {
724  if(qgraphicsitem_cast<KeypointItem*>(items.at(i)))
725  {
726  items.at(i)->setVisible(showFeatures_->isChecked());
727  }
728  else if(qgraphicsitem_cast<QGraphicsPixmapItem*>(items.at(i)))
729  {
730  items.at(i)->setVisible(showImage_->isChecked());
731  }
732  }
733 }
734 
735 void ObjWidget::drawKeypoints(QPainter * painter)
736 {
737  QList<KeypointItem *> items;
738  KeypointItem * item = 0;
739 
740  int i = 0;
741  for(std::vector<cv::KeyPoint>::const_iterator iter = keypoints_.begin(); iter != keypoints_.end(); ++iter, ++i )
742  {
743  const cv::KeyPoint & r = *iter;
744  float size = 14;
745  if(r.size>14.0f && sizedFeatures_->isChecked())
746  {
747  size = r.size;
748  }
749  float radius = size*1.2f/9.0f*2.0f;
750  QColor color(kptColors_.at(i).red(), kptColors_.at(i).green(), kptColors_.at(i).blue(), alpha_);
751  if(graphicsViewMode_->isChecked())
752  {
753  // YELLOW = NEW and multiple times
754  item = new KeypointItem(i, r.pt.x-radius, r.pt.y-radius, radius*2, r, words_.value(i, -1), color);
755  item->setVisible(this->isFeaturesShown());
756  item->setZValue(2);
757  graphicsView_->scene()->addItem(item);
758  item->setColor(defaultColor(item->wordID()));
759  kptColors_[i] = defaultColor(item->wordID());
760  keypointItems_.append(item);
761  }
762 
763  if(painter)
764  {
765  painter->save();
766  painter->setPen(color);
767  painter->setBrush(color);
768  painter->drawEllipse(r.pt.x-radius, r.pt.y-radius, radius*2, radius*2);
769  painter->restore();
770  }
771  }
772 }
773 
774 QColor ObjWidget::defaultColor(int id) const
775 {
776  QColor color(id >= 0 ? Qt::yellow : Qt::white);
777  color.setAlpha(alpha_);
778  return color;
779 }
780 
781 std::vector<cv::KeyPoint> ObjWidget::selectedKeypoints() const
782 {
783  std::vector<cv::KeyPoint> selected;
784  if(graphicsViewMode_->isChecked())
785  {
786  QList<QGraphicsItem*> items = graphicsView_->scene()->selectedItems();
787  for(int i=0; i<items.size(); ++i)
788  {
789  if(qgraphicsitem_cast<KeypointItem*>(items.at(i)))
790  {
791  selected.push_back(keypoints_.at(((KeypointItem*)items.at(i))->id()));
792  }
793  }
794  }
795  return selected;
796 }
797 
799 {
800  if(!rect_.isNull())
801  {
802  graphicsView_->setVisible(true);
803  graphicsView_->scene()->setSceneRect(rect_);
804  QList<KeypointItem*> items;
805 
806  QRectF sceneRect = graphicsView_->sceneRect();
807 
808  QGraphicsPixmapItem * pixmapItem = graphicsView_->scene()->addPixmap(pixmap_);
809  pixmapItem->setVisible(this->isImageShown());
810  this->drawKeypoints();
811 
812  for(int i=0; i<rectItems_.size(); ++i)
813  {
814  graphicsView_->scene()->addItem(rectItems_.at(i));
815  }
816 
817  if(autoScale_->isChecked())
818  {
819  graphicsView_->fitInView(sceneRect, Qt::KeepAspectRatio);
820  }
822  }
823  else
824  {
825  graphicsView_->setVisible(false);
826  }
827 }
828 
829 } // namespace find_object
830 
std::vector< cv::KeyPoint > selectedKeypoints() const
Definition: ObjWidget.cpp:781
QVector< QColor > kptColors_
Definition: ObjWidget.h:123
void setSizedFeatures(bool on)
Definition: ObjWidget.cpp:196
virtual void paintEvent(QPaintEvent *event)
Definition: ObjWidget.cpp:488
bool isImageShown() const
Definition: ObjWidget.cpp:397
bool isSizedFeatures() const
Definition: ObjWidget.cpp:407
void setImageShown(bool shown)
Definition: ObjWidget.cpp:422
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:278
std::vector< cv::KeyPoint > keypoints_
Definition: ObjWidget.h:117
void setColor(const QColor &color)
virtual void mouseMoveEvent(QMouseEvent *event)
Definition: ObjWidget.cpp:579
void drawKeypoints(QPainter *painter=0)
Definition: ObjWidget.cpp:735
QAction * showFeatures_
Definition: ObjWidget.h:134
void updateWords(const QMultiMap< int, int > &words)
Definition: ObjWidget.cpp:301
bool isMirrorView() const
Definition: ObjWidget.cpp:412
void update(const std::string &key, const XmlRpc::XmlRpcValue &v)
QGraphicsView * graphicsView_
Definition: ObjWidget.h:122
QList< KeypointItem * > keypointItems_
Definition: ObjWidget.h:121
void setDeletable(bool deletable)
Definition: ObjWidget.cpp:417
void computeScaleOffsets(float &scale, float &offsetX, float &offsetY)
Definition: ObjWidget.cpp:448
void setKptColor(int index, const QColor &color)
Definition: ObjWidget.cpp:346
virtual void mouseReleaseEvent(QMouseEvent *event)
Definition: ObjWidget.cpp:589
QAction * graphicsViewMode_
Definition: ObjWidget.h:138
void setAutoScale(bool autoScale)
Definition: ObjWidget.cpp:179
ROSCPP_DECL bool ok()
void addRect(QGraphicsRectItem *rect)
Definition: ObjWidget.cpp:377
virtual void mousePressEvent(QMouseEvent *event)
Definition: ObjWidget.cpp:568
void setKptWordID(int index, int wordId)
Definition: ObjWidget.cpp:368
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:74
QPixmap getSceneAsPixmap()
Definition: ObjWidget.cpp:704
virtual void contextMenuEvent(QContextMenuEvent *event)
Definition: ObjWidget.cpp:612
ULogger class and convenient macros.
#define UWARN(...)
void setGraphicsViewMode(bool on)
Definition: ObjWidget.cpp:146
void removalTriggered(find_object::ObjWidget *)
const QColor & color() const
Definition: ObjWidget.h:80
const QMap< int, int > & words() const
Definition: ObjWidget.h:82
bool isFeaturesShown() const
Definition: ObjWidget.cpp:402
const std::vector< cv::KeyPoint > keypoints() const
Definition: ObjWidget.h:81
void setFeaturesShown(bool shown)
Definition: ObjWidget.cpp:435
QColor defaultColor(int id) const
Definition: ObjWidget.cpp:774
QMap< int, int > words_
Definition: ObjWidget.h:118
virtual void resizeEvent(QResizeEvent *event)
Definition: ObjWidget.cpp:559
QList< QGraphicsRectItem * > rectItems_
Definition: ObjWidget.h:124
QList< QGraphicsItem * > selectedItems() const
Definition: ObjWidget.cpp:392
void roiChanged(const cv::Rect &)


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Mon Dec 12 2022 03:20:09