ObjWidget.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2011-2014, Mathieu Labbe - IntRoLab - Universite de Sherbrooke
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007     * Redistributions of source code must retain the above copyright
00008       notice, this list of conditions and the following disclaimer.
00009     * Redistributions in binary form must reproduce the above copyright
00010       notice, this list of conditions and the following disclaimer in the
00011       documentation and/or other materials provided with the distribution.
00012     * Neither the name of the Universite de Sherbrooke nor the
00013       names of its contributors may be used to endorse or promote products
00014       derived from this software without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
00020 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 */
00027 
00028 #include "find_object/Settings.h"
00029 #include "find_object/utilite/ULogger.h"
00030 #include "find_object/ObjWidget.h"
00031 #include "find_object/QtOpenCV.h"
00032 
00033 #include "KeypointItem.h"
00034 
00035 #include <opencv2/highgui/highgui.hpp>
00036 
00037 #include <QtGui/QWidget>
00038 #include <QtGui/QContextMenuEvent>
00039 #include <QtGui/QMenu>
00040 #include <QtGui/QMenu>
00041 #include <QtGui/QFileDialog>
00042 #include <QtGui/QAction>
00043 #include <QtGui/QGraphicsView>
00044 #include <QtGui/QGraphicsScene>
00045 #include <QtGui/QVBoxLayout>
00046 #include <QtGui/QGraphicsRectItem>
00047 #include <QtGui/QInputDialog>
00048 #include <QtGui/QPen>
00049 #include <QtGui/QLabel>
00050 #include <QtGui/QColorDialog>
00051 
00052 #include <QtCore/QDir>
00053 
00054 #include <stdio.h>
00055 
00056 namespace find_object {
00057 
00058 ObjWidget::ObjWidget(QWidget * parent) :
00059         QWidget(parent),
00060         id_(0),
00061         graphicsView_(0),
00062         graphicsViewInitialized_(false),
00063         alpha_(100),
00064         color_(Qt::red)
00065 {
00066         setupUi();
00067 }
00068 ObjWidget::ObjWidget(int id, const std::vector<cv::KeyPoint> & keypoints, const QImage & image, QWidget * parent) :
00069         QWidget(parent),
00070         id_(id),
00071         graphicsView_(0),
00072         graphicsViewInitialized_(false),
00073         alpha_(100),
00074         color_(QColor((Qt::GlobalColor)((id % 11 + 7)==Qt::yellow?Qt::gray:(id % 11 + 7))))
00075 {
00076         setupUi();
00077         this->setData(keypoints, image);
00078 }
00079 ObjWidget::~ObjWidget()
00080 {
00081 }
00082 
00083 void ObjWidget::setupUi()
00084 {
00085         graphicsView_ = new QGraphicsView(this);
00086         graphicsView_->setVisible(false);
00087         graphicsView_->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
00088         graphicsView_->setScene(new QGraphicsScene(graphicsView_));
00089 
00090         label_ = new QLabel();
00091         label_->setAlignment(Qt::AlignCenter);
00092         label_->setTextInteractionFlags(Qt::TextSelectableByMouse);
00093 
00094         this->setLayout(new QVBoxLayout(this));
00095         this->layout()->addWidget(graphicsView_);
00096         this->layout()->addWidget(label_);
00097         this->layout()->setContentsMargins(0,0,0,0);
00098 
00099         menu_ = new QMenu(tr(""), this);
00100         showImage_ = menu_->addAction(tr("Show image"));
00101         showImage_->setCheckable(true);
00102         showImage_->setChecked(true);
00103         showFeatures_ = menu_->addAction(tr("Show features"));
00104         showFeatures_->setCheckable(true);
00105         showFeatures_->setChecked(true);
00106         mirrorView_ = menu_->addAction(tr("Mirror view"));
00107         mirrorView_->setCheckable(true);
00108         mirrorView_->setChecked(false);
00109         graphicsViewMode_ = menu_->addAction(tr("Graphics view"));
00110         graphicsViewMode_->setCheckable(true);
00111         graphicsViewMode_->setChecked(false);
00112         autoScale_ = menu_->addAction(tr("Scale view"));
00113         autoScale_->setCheckable(true);
00114         autoScale_->setChecked(true);
00115         autoScale_->setEnabled(false);
00116         sizedFeatures_ = menu_->addAction(tr("Sized features"));
00117         sizedFeatures_->setCheckable(true);
00118         sizedFeatures_->setChecked(false);
00119         menu_->addSeparator();
00120         setColor_ = menu_->addAction(tr("Set color..."));
00121         setAlpha_ = menu_->addAction(tr("Set alpha..."));
00122         menu_->addSeparator();
00123         saveImage_ = menu_->addAction(tr("Save picture..."));
00124         menu_->addSeparator();
00125         delete_ = menu_->addAction(tr("Delete"));
00126         delete_->setEnabled(false);
00127 
00128         this->setId(id_);
00129 
00130         graphicsView_->setRubberBandSelectionMode(Qt::ContainsItemShape);
00131         graphicsView_->setDragMode(QGraphicsView::RubberBandDrag);
00132 
00133         connect(graphicsView_->scene(), SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
00134 }
00135 
00136 void ObjWidget::setId(int id)
00137 {
00138         color_ = QColor((Qt::GlobalColor)((id % 11 + 7)==Qt::yellow?Qt::gray:(id % 11 + 7)));
00139         id_=id;
00140         if(id_)
00141         {
00142                 savedFileName_ = QString("object_%1.png").arg(id_);
00143         }
00144 }
00145 
00146 void ObjWidget::setGraphicsViewMode(bool on)
00147 {
00148         graphicsViewMode_->setChecked(on);
00149         graphicsView_->setVisible(on && graphicsView_->scene()->items().size());
00150         autoScale_->setEnabled(on);
00151         //update items' color
00152         if(on)
00153         {
00154                 if(!graphicsViewInitialized_)
00155                 {
00156                         this->setupGraphicsView();
00157                 }
00158                 else
00159                 {
00160                         for(int i=0; i<keypointItems_.size(); ++i)
00161                         {
00162                                 QColor color = kptColors_.at(i);
00163                                 color.setAlpha(alpha_);
00164                                 keypointItems_[i]->setColor(color);
00165                         }
00166                 }
00167         }
00168         if(autoScale_->isChecked())
00169         {
00170                 graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
00171         }
00172         else
00173         {
00174                 graphicsView_->resetTransform();
00175                 graphicsView_->setTransform(QTransform().scale(this->isMirrorView()?-1.0:1.0, 1.0));
00176         }
00177 }
00178 
00179 void ObjWidget::setAutoScale(bool autoScale)
00180 {
00181         autoScale_->setChecked(autoScale);
00182         if(graphicsViewMode_)
00183         {
00184                 if(autoScale)
00185                 {
00186                         graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
00187                 }
00188                 else
00189                 {
00190                         graphicsView_->resetTransform();
00191                         graphicsView_->setTransform(QTransform().scale(this->isMirrorView()?-1.0:1.0, 1.0));
00192                 }
00193         }
00194 }
00195 
00196 void ObjWidget::setSizedFeatures(bool on)
00197 {
00198         sizedFeatures_->setChecked(on);
00199         if(graphicsViewInitialized_)
00200         {
00201                 for(unsigned int i=0; i<(unsigned int)keypointItems_.size() && i<keypoints_.size(); ++i)
00202                 {
00203                         float size = 14;
00204                         if(on && keypoints_[i].size>14.0f)
00205                         {
00206                                 size = keypoints_[i].size;
00207                         }
00208                         float radius = size*1.2f/9.0f*2.0f;
00209                         keypointItems_.at(i)->setRect(keypoints_[i].pt.x-radius, keypoints_[i].pt.y-radius, radius*2, radius*2);
00210                 }
00211         }
00212         if(!graphicsViewMode_->isChecked())
00213         {
00214                 this->update();
00215         }
00216 }
00217 
00218 void ObjWidget::setMirrorView(bool on)
00219 {
00220         mirrorView_->setChecked(on);
00221         graphicsView_->setTransform(QTransform().scale(this->isMirrorView()?-1.0:1.0, 1.0));
00222         if(graphicsViewMode_->isChecked() && autoScale_->isChecked())
00223         {
00224                 graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
00225         }
00226         else if(!graphicsViewMode_->isChecked())
00227         {
00228                 this->update();
00229         }
00230 }
00231 
00232 void ObjWidget::setAlpha(int alpha)
00233 {
00234         if(alpha>=0 && alpha<=255)
00235         {
00236                 alpha_ = alpha;
00237                 if(graphicsViewInitialized_)
00238                 {
00239                         for(int i=0; i<keypointItems_.size() && i<kptColors_.size(); ++i)
00240                         {
00241                                 QColor color = kptColors_.at(i);
00242                                 color.setAlpha(alpha_);
00243                                 keypointItems_.at(i)->setColor(color);
00244                         }
00245                 }
00246                 for(int i=0; i<rectItems_.size(); ++i)
00247                 {
00248                         QPen pen = rectItems_.at(i)->pen();
00249                         QColor color = pen.color();
00250                         color.setAlpha(alpha_);
00251                         pen.setColor(color);
00252                         rectItems_.at(i)->setPen(pen);
00253                 }
00254 
00255                 if(!graphicsViewMode_->isChecked())
00256                 {
00257                         this->update();
00258                 }
00259         }
00260 }
00261 
00262 void ObjWidget::setTextLabel(const QString & text)
00263 {
00264         label_->setText(text);
00265 }
00266 
00267 void ObjWidget::setData(const std::vector<cv::KeyPoint> & keypoints, const QImage & image)
00268 {
00269         keypoints_ = keypoints;
00270         kptColors_ = QVector<QColor>((int)keypoints.size(), defaultColor());
00271         keypointItems_.clear();
00272         rectItems_.clear();
00273         graphicsView_->scene()->clear();
00274         graphicsViewInitialized_ = false;
00275         mouseCurrentPos_ = mousePressedPos_; // this will reset roi selection
00276 
00277         pixmap_ = QPixmap::fromImage(image);
00278         //this->setMinimumSize(image_.size());
00279 
00280         if(graphicsViewMode_->isChecked())
00281         {
00282                 this->setupGraphicsView();
00283         }
00284         label_->setVisible(image.isNull());
00285 }
00286 
00287 void ObjWidget::resetKptsColor()
00288 {
00289         for(int i=0; i<kptColors_.size(); ++i)
00290         {
00291                 kptColors_[i] = defaultColor();
00292                 if(graphicsViewMode_->isChecked())
00293                 {
00294                         keypointItems_[i]->setColor(this->defaultColor());
00295                 }
00296         }
00297         qDeleteAll(rectItems_.begin(), rectItems_.end());
00298         rectItems_.clear();
00299 }
00300 
00301 void ObjWidget::setKptColor(int index, const QColor & color)
00302 {
00303         if(index < kptColors_.size())
00304         {
00305                 kptColors_[index] = color;
00306         }
00307         else
00308         {
00309                 UWARN("PROBLEM index =%d > size=%d\n", index, kptColors_.size());
00310         }
00311 
00312         if(graphicsViewMode_->isChecked())
00313         {
00314                 if(index < keypointItems_.size())
00315                 {
00316                         QColor c = color;
00317                         c.setAlpha(alpha_);
00318                         keypointItems_.at(index)->setColor(c);
00319                 }
00320         }
00321 }
00322 
00323 void ObjWidget::addRect(QGraphicsRectItem * rect)
00324 {
00325         if(graphicsViewInitialized_)
00326         {
00327                 graphicsView_->scene()->addItem(rect);
00328         }
00329         rect->setZValue(1);
00330         QPen pen = rect->pen();
00331         QColor color = pen.color();
00332         color.setAlpha(alpha_);
00333         pen.setColor(color);
00334         rect->setPen(pen);
00335         rectItems_.append(rect);
00336 }
00337 
00338 QList<QGraphicsItem*> ObjWidget::selectedItems() const
00339 {
00340         return graphicsView_->scene()->selectedItems();
00341 }
00342 
00343 bool ObjWidget::isImageShown() const
00344 {
00345         return showImage_->isChecked();
00346 }
00347 
00348 bool ObjWidget::isFeaturesShown() const
00349 {
00350         return showFeatures_->isChecked();
00351 }
00352 
00353 bool ObjWidget::isSizedFeatures() const
00354 {
00355         return sizedFeatures_->isChecked();
00356 }
00357 
00358 bool ObjWidget::isMirrorView() const
00359 {
00360         return mirrorView_->isChecked();
00361 }
00362 
00363 void ObjWidget::setDeletable(bool deletable)
00364 {
00365         delete_->setEnabled(deletable);
00366 }
00367 
00368 void ObjWidget::setImageShown(bool shown)
00369 {
00370         showImage_->setChecked(shown);
00371         if(graphicsViewMode_->isChecked())
00372         {
00373                 this->updateItemsShown();
00374         }
00375         else
00376         {
00377                 this->update();
00378         }
00379 }
00380 
00381 void ObjWidget::setFeaturesShown(bool shown)
00382 {
00383         showFeatures_->setChecked(shown);
00384         if(graphicsViewMode_->isChecked())
00385         {
00386                 this->updateItemsShown();
00387         }
00388         else
00389         {
00390                 this->update();
00391         }
00392 }
00393 
00394 void ObjWidget::computeScaleOffsets(float & scale, float & offsetX, float & offsetY)
00395 {
00396         scale = 1.0f;
00397         offsetX = 0.0f;
00398         offsetY = 0.0f;
00399 
00400         if(!pixmap_.isNull())
00401         {
00402                 float w = pixmap_.width();
00403                 float h = pixmap_.height();
00404                 float widthRatio = float(this->rect().width()) / w;
00405                 float heightRatio = float(this->rect().height()) / h;
00406 
00407                 //printf("w=%f, h=%f, wR=%f, hR=%f, sW=%d, sH=%d\n", w, h, widthRatio, heightRatio, this->rect().width(), this->rect().height());
00408                 if(widthRatio < heightRatio)
00409                 {
00410                         scale = widthRatio;
00411                 }
00412                 else
00413                 {
00414                         scale = heightRatio;
00415                 }
00416 
00417                 //printf("ratio=%f\n",ratio);
00418 
00419                 w *= scale;
00420                 h *= scale;
00421 
00422                 if(w < this->rect().width())
00423                 {
00424                         offsetX = (this->rect().width() - w)/2.0f;
00425                 }
00426                 if(h < this->rect().height())
00427                 {
00428                         offsetY = (this->rect().height() - h)/2.0f;
00429                 }
00430                 //printf("offsetX=%f, offsetY=%f\n",offsetX, offsetY);
00431         }
00432 }
00433 
00434 void ObjWidget::paintEvent(QPaintEvent *event)
00435 {
00436         if(graphicsViewMode_->isChecked())
00437         {
00438                 QWidget::paintEvent(event);
00439         }
00440         else
00441         {
00442                 if(!pixmap_.isNull())
00443                 {
00444                         //Scale
00445                         float ratio, offsetX, offsetY;
00446                         this->computeScaleOffsets(ratio, offsetX, offsetY);
00447                         QPainter painter(this);
00448 
00449                         if(mirrorView_->isChecked())
00450                         {
00451                                 painter.translate(offsetX+pixmap_.width()*ratio, offsetY);
00452                                 painter.scale(-ratio, ratio);
00453                         }
00454                         else
00455                         {
00456                                 painter.translate(offsetX, offsetY);
00457                                 painter.scale(ratio, ratio);
00458                         }
00459 
00460                         if(showImage_->isChecked())
00461                         {
00462                                 painter.drawPixmap(QPoint(0,0), pixmap_);
00463                         }
00464 
00465                         if(showFeatures_->isChecked())
00466                         {
00467                                 drawKeypoints(&painter);
00468                         }
00469 
00470                         for(int i=0; i<rectItems_.size(); ++i)
00471                         {
00472                                 painter.save();
00473                                 painter.setTransform(rectItems_.at(i)->transform(), true);
00474                                 painter.setPen(rectItems_.at(i)->pen());
00475                                 painter.drawRect(rectItems_.at(i)->rect());
00476                                 painter.restore();
00477                         }
00478 
00479                         if(mouseCurrentPos_ != mousePressedPos_)
00480                         {
00481                                 painter.save();
00482                                 int left, top, right, bottom;
00483                                 left = mousePressedPos_.x() < mouseCurrentPos_.x() ? mousePressedPos_.x():mouseCurrentPos_.x();
00484                                 top = mousePressedPos_.y() < mouseCurrentPos_.y() ? mousePressedPos_.y():mouseCurrentPos_.y();
00485                                 right = mousePressedPos_.x() > mouseCurrentPos_.x() ? mousePressedPos_.x():mouseCurrentPos_.x();
00486                                 bottom = mousePressedPos_.y() > mouseCurrentPos_.y() ? mousePressedPos_.y():mouseCurrentPos_.y();
00487                                 if(mirrorView_->isChecked())
00488                                 {
00489                                         int l = left;
00490                                         left = qAbs(right - pixmap_.width());
00491                                         right = qAbs(l - pixmap_.width());
00492                                 }
00493                                 painter.setPen(Qt::NoPen);
00494                                 painter.setBrush(QBrush(QColor(0,0,0,100)));
00495                                 painter.drawRect(0, 0, pixmap_.width(), top);
00496                                 painter.drawRect(0, top, left, bottom-top);
00497                                 painter.drawRect(right, top, pixmap_.width()-right, bottom-top);
00498                                 painter.drawRect(0, bottom, pixmap_.width(), pixmap_.height()-bottom);
00499                                 painter.restore();
00500                         }
00501                 }
00502         }
00503 }
00504 
00505 void ObjWidget::resizeEvent(QResizeEvent* event)
00506 {
00507         QWidget::resizeEvent(event);
00508         if(graphicsViewMode_->isChecked() && autoScale_->isChecked())
00509         {
00510                 graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
00511         }
00512 }
00513 
00514 void ObjWidget::mousePressEvent(QMouseEvent * event)
00515 {
00516         float scale, offsetX, offsetY;
00517         this->computeScaleOffsets(scale, offsetX, offsetY);
00518         mousePressedPos_.setX((event->pos().x()-offsetX)/scale);
00519         mousePressedPos_.setY((event->pos().y()-offsetY)/scale);
00520         mouseCurrentPos_ = mousePressedPos_;
00521         this->update();
00522         QWidget::mousePressEvent(event);
00523 }
00524 
00525 void ObjWidget::mouseMoveEvent(QMouseEvent * event)
00526 {
00527         float scale, offsetX, offsetY;
00528         this->computeScaleOffsets(scale, offsetX, offsetY);
00529         mouseCurrentPos_.setX((event->pos().x()-offsetX)/scale);
00530         mouseCurrentPos_.setY((event->pos().y()-offsetY)/scale);
00531         this->update();
00532         QWidget::mouseMoveEvent(event);
00533 }
00534 
00535 void ObjWidget::mouseReleaseEvent(QMouseEvent * event)
00536 {
00537         if(!pixmap_.isNull())
00538         {
00539                 int left,top,bottom,right;
00540 
00541                 left = mousePressedPos_.x() < mouseCurrentPos_.x() ? mousePressedPos_.x():mouseCurrentPos_.x();
00542                 top = mousePressedPos_.y() < mouseCurrentPos_.y() ? mousePressedPos_.y():mouseCurrentPos_.y();
00543                 right = mousePressedPos_.x() > mouseCurrentPos_.x() ? mousePressedPos_.x():mouseCurrentPos_.x();
00544                 bottom = mousePressedPos_.y() > mouseCurrentPos_.y() ? mousePressedPos_.y():mouseCurrentPos_.y();
00545 
00546                 if(mirrorView_->isChecked())
00547                 {
00548                         int l = left;
00549                         left = qAbs(right - pixmap_.width());
00550                         right = qAbs(l - pixmap_.width());
00551                 }
00552 
00553                 Q_EMIT roiChanged(cv::Rect(left, top, right-left, bottom-top));
00554         }
00555         QWidget::mouseReleaseEvent(event);
00556 }
00557 
00558 void ObjWidget::contextMenuEvent(QContextMenuEvent * event)
00559 {
00560         QAction * action = menu_->exec(event->globalPos());
00561         if(action == saveImage_)
00562         {
00563                 QString text;
00564                 if(savedFileName_.isEmpty())
00565                 {
00566                         savedFileName_=Settings::workingDirectory()+"/figure.png";
00567                 }
00568                 text = QFileDialog::getSaveFileName(this, tr("Save figure to ..."), savedFileName_, "*.png *.xpm *.jpg *.pdf");
00569                 if(!text.isEmpty())
00570                 {
00571                         if(!text.endsWith(".png") && !text.endsWith(".xpm") && !text.endsWith(".jpg") && !text.endsWith(".pdf"))
00572                         {
00573                                 text.append(".png");//default
00574                         }
00575                         savedFileName_ = text;
00576                         getSceneAsPixmap().save(text);
00577                 }
00578         }
00579         else if(action == showFeatures_ || action == showImage_)
00580         {
00581                 if(graphicsViewMode_->isChecked())
00582                 {
00583                         this->updateItemsShown();
00584                 }
00585                 else
00586                 {
00587                         this->update();
00588                 }
00589         }
00590         else if(action == mirrorView_)
00591         {
00592                 this->setMirrorView(mirrorView_->isChecked());
00593         }
00594         else if(action == delete_)
00595         {
00596                 Q_EMIT removalTriggered(this);
00597         }
00598         else if(action == graphicsViewMode_)
00599         {
00600                 this->setGraphicsViewMode(graphicsViewMode_->isChecked());
00601         }
00602         else if(action == autoScale_)
00603         {
00604                 this->setAutoScale(autoScale_->isChecked());
00605         }
00606         else if(action == sizedFeatures_)
00607         {
00608                 this->setSizedFeatures(sizedFeatures_->isChecked());
00609         }
00610         else if(action == setColor_)
00611         {
00612                 QColor color = QColorDialog::getColor(color_, this);
00613                 if(color.isValid())
00614                 {
00615                         for(int i=0; i<kptColors_.size(); ++i)
00616                         {
00617                                 if(kptColors_[i] == color_)
00618                                 {
00619                                         kptColors_[i] = color;
00620                                         if(graphicsViewMode_->isChecked())
00621                                         {
00622                                                 keypointItems_[i]->setColor(color);
00623                                         }
00624                                 }
00625                         }
00626                         for(int i=0; i<rectItems_.size(); ++i)
00627                         {
00628                                 if(rectItems_[i]->pen().color() == color_)
00629                                 {
00630                                         QPen p = rectItems_[i]->pen();
00631                                         p.setColor(color);
00632                                         rectItems_[i]->setPen(p);
00633                                 }
00634                         }
00635                         color_ = color;
00636 
00637                 }
00638         }
00639         else if(action == setAlpha_)
00640         {
00641                 bool ok;
00642                 int newAlpha = QInputDialog::getInt(this, tr("Set alpha"), tr("Alpha:"), alpha_, 0, 255, 5, &ok);
00643                 if(ok)
00644                 {
00645                         this->setAlpha(newAlpha);
00646                 }
00647         }
00648 }
00649 
00650 QPixmap ObjWidget::getSceneAsPixmap()
00651 {
00652         if(graphicsViewMode_->isChecked())
00653         {
00654                 QPixmap img(graphicsView_->sceneRect().width(), graphicsView_->sceneRect().height());
00655                 QPainter p(&img);
00656                 graphicsView_->scene()->render(&p, graphicsView_->sceneRect(), graphicsView_->sceneRect());
00657                 return img;
00658         }
00659         else
00660         {
00661                 return QPixmap::grabWidget(this);
00662         }
00663 }
00664 
00665 void ObjWidget::updateItemsShown()
00666 {
00667         QList<QGraphicsItem*> items = graphicsView_->scene()->items();
00668         for(int i=0; i<items.size(); ++i)
00669         {
00670                 if(qgraphicsitem_cast<KeypointItem*>(items.at(i)))
00671                 {
00672                         items.at(i)->setVisible(showFeatures_->isChecked());
00673                 }
00674                 else if(qgraphicsitem_cast<QGraphicsPixmapItem*>(items.at(i)))
00675                 {
00676                         items.at(i)->setVisible(showImage_->isChecked());
00677                 }
00678         }
00679 }
00680 
00681 void ObjWidget::drawKeypoints(QPainter * painter)
00682 {
00683         QList<KeypointItem *> items;
00684         KeypointItem * item = 0;
00685 
00686         int i = 0;
00687         for(std::vector<cv::KeyPoint>::const_iterator iter = keypoints_.begin(); iter != keypoints_.end(); ++iter, ++i )
00688         {
00689                 const cv::KeyPoint & r = *iter;
00690                 float size = 14;
00691                 if(r.size>14.0f && sizedFeatures_->isChecked())
00692                 {
00693                         size = r.size;
00694                 }
00695                 float radius = size*1.2f/9.0f*2.0f;
00696                 QColor color(kptColors_.at(i).red(), kptColors_.at(i).green(), kptColors_.at(i).blue(), alpha_);
00697                 if(graphicsViewMode_->isChecked())
00698                 {
00699                         QString info = QString( "ID = %1\n"
00700                                                                         "Response = %2\n"
00701                                                                         "Angle = %3\n"
00702                                                                         "X = %4\n"
00703                                                                         "Y = %5\n"
00704                                                                         "Size = %6").arg(i+1).arg(r.response).arg(r.angle).arg(r.pt.x).arg(r.pt.y).arg(r.size);
00705                         // YELLOW = NEW and multiple times
00706                         item = new KeypointItem(i+1, r.pt.x-radius, r.pt.y-radius, radius*2, info, color);
00707                         item->setVisible(this->isFeaturesShown());
00708                         item->setZValue(2);
00709                         graphicsView_->scene()->addItem(item);
00710                         keypointItems_.append(item);
00711                 }
00712 
00713                 if(painter)
00714                 {
00715                         painter->save();
00716                         painter->setPen(color);
00717                         painter->setBrush(color);
00718                         painter->drawEllipse(r.pt.x-radius, r.pt.y-radius, radius*2, radius*2);
00719                         painter->restore();
00720                 }
00721         }
00722 }
00723 
00724 QColor ObjWidget::defaultColor() const
00725 {
00726         QColor color(Qt::yellow);
00727         color.setAlpha(alpha_);
00728         return color;
00729 }
00730 
00731 std::vector<cv::KeyPoint> ObjWidget::selectedKeypoints() const
00732 {
00733         std::vector<cv::KeyPoint> selected;
00734         if(graphicsViewMode_->isChecked())
00735         {
00736                 QList<QGraphicsItem*> items = graphicsView_->scene()->selectedItems();
00737                 for(int i=0; i<items.size(); ++i)
00738                 {
00739                         if(qgraphicsitem_cast<KeypointItem*>(items.at(i)))
00740                         {
00741                                 selected.push_back(keypoints_.at(((KeypointItem*)items.at(i))->id()-1)); // ids start at 1
00742                         }
00743                 }
00744         }
00745         return selected;
00746 }
00747 
00748 void ObjWidget::setupGraphicsView()
00749 {
00750         if(!pixmap_.isNull())
00751         {
00752                 graphicsView_->setVisible(true);
00753                 graphicsView_->scene()->setSceneRect(pixmap_.rect());
00754                 QList<KeypointItem*> items;
00755 
00756                 QRectF sceneRect = graphicsView_->sceneRect();
00757 
00758                 QGraphicsPixmapItem * pixmapItem = graphicsView_->scene()->addPixmap(pixmap_);
00759                 pixmapItem->setVisible(this->isImageShown());
00760                 this->drawKeypoints();
00761 
00762                 for(int i=0; i<rectItems_.size(); ++i)
00763                 {
00764                         graphicsView_->scene()->addItem(rectItems_.at(i));
00765                 }
00766 
00767                 if(autoScale_->isChecked())
00768                 {
00769                         graphicsView_->fitInView(sceneRect, Qt::KeepAspectRatio);
00770                 }
00771                 graphicsViewInitialized_ = true;
00772         }
00773         else
00774         {
00775                 graphicsView_->setVisible(false);
00776         }
00777 }
00778 
00779 } // namespace find_object
00780 


find_object_2d
Author(s): Mathieu Labbe
autogenerated on Fri Feb 12 2016 01:18:17