Added "Scale view" action (only enabled in graphics view mode)
git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@24 620bd6b2-0a58-f614-fd9a-1bd335dccda9
This commit is contained in:
parent
3db8e21688
commit
495da944ca
@ -182,6 +182,7 @@ int main(int argc, char * argv[])
|
||||
else
|
||||
{
|
||||
objWidget.setKptColor(indexes_1.at(k), QColor(0,0,0,alpha));
|
||||
sceneWidget.setKptColor(indexes_2.at(k), QColor(0,0,0,alpha));
|
||||
}
|
||||
}
|
||||
QPen rectPen(color);
|
||||
@ -200,9 +201,27 @@ int main(int argc, char * argv[])
|
||||
// Wait for gui
|
||||
objWidget.setGraphicsViewMode(false);
|
||||
objWidget.setWindowTitle("Object");
|
||||
if(objWidget.image().width() <= 800)
|
||||
{
|
||||
objWidget.setGeometry(0, 0, objWidget.image().width(), objWidget.image().height());
|
||||
}
|
||||
else
|
||||
{
|
||||
objWidget.setGeometry(0, 0, 800, 600);
|
||||
objWidget.setAutoScale(false);
|
||||
}
|
||||
objWidget.show();
|
||||
sceneWidget.setGraphicsViewMode(false);
|
||||
sceneWidget.setWindowTitle("Scene");
|
||||
if(sceneWidget.image().width() <= 800)
|
||||
{
|
||||
sceneWidget.setGeometry(0, 0, sceneWidget.image().width(), sceneWidget.image().height());
|
||||
}
|
||||
else
|
||||
{
|
||||
sceneWidget.setGeometry(0, 0, 800, 600);
|
||||
sceneWidget.setAutoScale(false);
|
||||
}
|
||||
sceneWidget.show();
|
||||
int r = app.exec();
|
||||
printf("Closing...\n");
|
||||
|
||||
@ -126,6 +126,7 @@ void AddObjectDialog::setState(int state)
|
||||
ui_->cameraView->setVisible(true);
|
||||
ui_->objectView->setVisible(false);
|
||||
ui_->cameraView->setGraphicsViewMode(true);
|
||||
updateNextButton();
|
||||
}
|
||||
else if(state == kVerifySelection)
|
||||
{
|
||||
@ -156,6 +157,8 @@ void AddObjectDialog::setState(int state)
|
||||
}
|
||||
}
|
||||
ui_->objectView->setData(selectedKeypoints, cv::Mat(), cvImage_);
|
||||
ui_->objectView->setMinimumSize(roi.width, roi.height);
|
||||
ui_->objectView->update();
|
||||
cvResetImageROI(cvImage_);
|
||||
ui_->pushButton_next->setEnabled(true);
|
||||
}
|
||||
@ -268,8 +271,8 @@ CvRect AddObjectDialog::computeROI(const std::vector<cv::KeyPoint> & kpts)
|
||||
roi.y = h1;
|
||||
roi.width = x2-x1;
|
||||
roi.height = h2-h1;
|
||||
printf("ptx=%d, pty=%d\n", (int)kpts.at(i).pt.x, (int)kpts.at(i).pt.y);
|
||||
printf("x=%d, y=%d, w=%d, h=%d\n", roi.x, roi.y, roi.width, roi.height);
|
||||
//printf("ptx=%d, pty=%d\n", (int)kpts.at(i).pt.x, (int)kpts.at(i).pt.y);
|
||||
//printf("x=%d, y=%d, w=%d, h=%d\n", roi.x, roi.y, roi.width, roi.height);
|
||||
}
|
||||
|
||||
return roi;
|
||||
|
||||
@ -159,6 +159,7 @@ void MainWindow::showObject(ObjWidget * obj)
|
||||
obj->setGraphicsViewMode(false);
|
||||
QList<ObjWidget*> objs = ui_->objects_area->findChildren<ObjWidget*>();
|
||||
QVBoxLayout * vLayout = new QVBoxLayout();
|
||||
obj->setMinimumSize(obj->image().width(), obj->image().height());
|
||||
int id = Settings::getGeneral_nextObjID().toInt();
|
||||
if(obj->id() == 0)
|
||||
{
|
||||
|
||||
@ -40,9 +40,9 @@ ObjWidget::ObjWidget(QWidget * parent) :
|
||||
iplImage_(0),
|
||||
graphicsView_(0),
|
||||
id_(0),
|
||||
graphicsViewMode_(true),
|
||||
detectorType_("NA"),
|
||||
descriptorType_("NA")
|
||||
descriptorType_("NA"),
|
||||
graphicsViewInitialized_(false)
|
||||
{
|
||||
setupUi();
|
||||
}
|
||||
@ -57,9 +57,9 @@ ObjWidget::ObjWidget(int id,
|
||||
iplImage_(0),
|
||||
graphicsView_(0),
|
||||
id_(id),
|
||||
graphicsViewMode_(true),
|
||||
detectorType_(detectorType),
|
||||
descriptorType_(descriptorType)
|
||||
descriptorType_(descriptorType),
|
||||
graphicsViewInitialized_(false)
|
||||
{
|
||||
setupUi();
|
||||
this->setData(keypoints, descriptors, iplImage);
|
||||
@ -75,7 +75,7 @@ ObjWidget::~ObjWidget()
|
||||
void ObjWidget::setupUi()
|
||||
{
|
||||
graphicsView_ = new QGraphicsView(this);
|
||||
graphicsView_->setVisible(true);
|
||||
graphicsView_->setVisible(false);
|
||||
graphicsView_->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
|
||||
graphicsView_->setScene(new QGraphicsScene(graphicsView_));
|
||||
|
||||
@ -93,9 +93,13 @@ void ObjWidget::setupUi()
|
||||
_mirrorView = _menu->addAction(tr("Mirror view"));
|
||||
_mirrorView->setCheckable(true);
|
||||
_mirrorView->setChecked(false);
|
||||
_plainView = _menu->addAction(tr("Plain view"));
|
||||
_plainView->setCheckable(true);
|
||||
_plainView->setChecked(!graphicsViewMode_);
|
||||
_graphicsViewMode = _menu->addAction(tr("Graphics view"));
|
||||
_graphicsViewMode->setCheckable(true);
|
||||
_graphicsViewMode->setChecked(false);
|
||||
_autoScale = _menu->addAction(tr("Scale view"));
|
||||
_autoScale->setCheckable(true);
|
||||
_autoScale->setChecked(true);
|
||||
_autoScale->setEnabled(false);
|
||||
_menu->addSeparator();
|
||||
_saveImage = _menu->addAction(tr("Save picture..."));
|
||||
_menu->addSeparator();
|
||||
@ -121,12 +125,13 @@ void ObjWidget::setId(int id)
|
||||
|
||||
void ObjWidget::setGraphicsViewMode(bool on)
|
||||
{
|
||||
graphicsViewMode_ = on;
|
||||
_graphicsViewMode->setChecked(on);
|
||||
graphicsView_->setVisible(on);
|
||||
_autoScale->setEnabled(on);
|
||||
//update items' color
|
||||
if(on)
|
||||
{
|
||||
if(keypointItems_.size() == 0)
|
||||
if(!graphicsViewInitialized_)
|
||||
{
|
||||
this->setupGraphicsView();
|
||||
}
|
||||
@ -138,9 +143,30 @@ void ObjWidget::setGraphicsViewMode(bool on)
|
||||
}
|
||||
}
|
||||
}
|
||||
graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
|
||||
_plainView->setChecked(!on);
|
||||
this->update();
|
||||
if(_autoScale->isChecked())
|
||||
{
|
||||
graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
|
||||
}
|
||||
else
|
||||
{
|
||||
graphicsView_->resetTransform();
|
||||
}
|
||||
}
|
||||
|
||||
void ObjWidget::setAutoScale(bool autoScale)
|
||||
{
|
||||
_autoScale->setChecked(autoScale);
|
||||
if(_graphicsViewMode)
|
||||
{
|
||||
if(autoScale)
|
||||
{
|
||||
graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
|
||||
}
|
||||
else
|
||||
{
|
||||
graphicsView_->resetTransform();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ObjWidget::setData(const std::vector<cv::KeyPoint> & keypoints, const cv::Mat & descriptors, const IplImage * image)
|
||||
@ -150,6 +176,8 @@ void ObjWidget::setData(const std::vector<cv::KeyPoint> & keypoints, const cv::M
|
||||
kptColors_ = QVector<QColor>(keypoints.size(), defaultColor());
|
||||
keypointItems_.clear();
|
||||
rectItems_.clear();
|
||||
graphicsView_->scene()->clear();
|
||||
graphicsViewInitialized_ = false;
|
||||
if(iplImage_)
|
||||
{
|
||||
cvReleaseImage(&iplImage_);
|
||||
@ -167,9 +195,9 @@ void ObjWidget::setData(const std::vector<cv::KeyPoint> & keypoints, const cv::M
|
||||
cvCopy(image, iplImage_, NULL);
|
||||
|
||||
image_ = QPixmap::fromImage(Ipl2QImage(iplImage_));
|
||||
this->setMinimumSize(image_.size());
|
||||
//this->setMinimumSize(image_.size());
|
||||
}
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
this->setupGraphicsView();
|
||||
}
|
||||
@ -180,11 +208,12 @@ void ObjWidget::resetKptsColor()
|
||||
for(int i=0; i<kptColors_.size(); ++i)
|
||||
{
|
||||
kptColors_[i] = defaultColor();
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
keypointItems_[i]->setColor(this->defaultColor());
|
||||
}
|
||||
}
|
||||
qDeleteAll(rectItems_.begin(), rectItems_.end());
|
||||
rectItems_.clear();
|
||||
}
|
||||
|
||||
@ -195,7 +224,7 @@ void ObjWidget::setKptColor(unsigned int index, const QColor & color)
|
||||
kptColors_[index] = color;
|
||||
}
|
||||
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
if(index < keypointItems_.size())
|
||||
{
|
||||
@ -206,7 +235,11 @@ void ObjWidget::setKptColor(unsigned int index, const QColor & color)
|
||||
|
||||
void ObjWidget::addRect(QGraphicsRectItem * rect)
|
||||
{
|
||||
graphicsView_->scene()->addItem(rect);
|
||||
if(graphicsViewInitialized_)
|
||||
{
|
||||
graphicsView_->scene()->addItem(rect);
|
||||
}
|
||||
rect->setZValue(2);
|
||||
rectItems_.append(rect);
|
||||
}
|
||||
|
||||
@ -288,12 +321,12 @@ void ObjWidget::load(QDataStream & streamPtr)
|
||||
descriptors = cv::Mat(rows, cols, type, data.data()).clone();
|
||||
streamPtr >> image_;
|
||||
this->setData(kpts, descriptors, 0);
|
||||
this->setMinimumSize(image_.size());
|
||||
//this->setMinimumSize(image_.size());
|
||||
}
|
||||
|
||||
void ObjWidget::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
QWidget::paintEvent(event);
|
||||
}
|
||||
@ -304,10 +337,10 @@ void ObjWidget::paintEvent(QPaintEvent *event)
|
||||
//Scale
|
||||
float w = image_.width();
|
||||
float h = image_.height();
|
||||
float widthRatio = this->rect().width() / w;
|
||||
float heightRatio = this->rect().height() / h;
|
||||
float widthRatio = float(this->rect().width()) / w;
|
||||
float heightRatio = float(this->rect().height()) / h;
|
||||
float ratio = 1.0f;
|
||||
//printf("w=%f, h=%f, wR=%f, hR=%f, sW=%f, sH=%f\n", w, h, widthRatio, heightRatio, sceneRect.width(), sceneRect.height());
|
||||
//printf("w=%f, h=%f, wR=%f, hR=%f, sW=%d, sH=%d\n", w, h, widthRatio, heightRatio, this->rect().width(), this->rect().height());
|
||||
if(widthRatio < heightRatio)
|
||||
{
|
||||
ratio = widthRatio;
|
||||
@ -350,11 +383,13 @@ void ObjWidget::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
painter.drawPixmap(QPoint(0,0), image_);
|
||||
}
|
||||
|
||||
if(_showFeatures->isChecked())
|
||||
{
|
||||
drawKeypoints(&painter);
|
||||
}
|
||||
|
||||
|
||||
for(int i=0; i<rectItems_.size(); ++i)
|
||||
{
|
||||
painter.save();
|
||||
@ -363,6 +398,7 @@ void ObjWidget::paintEvent(QPaintEvent *event)
|
||||
painter.drawRect(rectItems_.at(i)->rect());
|
||||
painter.restore();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -370,7 +406,7 @@ void ObjWidget::paintEvent(QPaintEvent *event)
|
||||
void ObjWidget::resizeEvent(QResizeEvent* event)
|
||||
{
|
||||
QWidget::resizeEvent(event);
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked() && _autoScale->isChecked())
|
||||
{
|
||||
graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
|
||||
}
|
||||
@ -399,7 +435,7 @@ void ObjWidget::contextMenuEvent(QContextMenuEvent * event)
|
||||
}
|
||||
else if(action == _showFeatures || action == _showImage)
|
||||
{
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
this->updateItemsShown();
|
||||
}
|
||||
@ -411,7 +447,7 @@ void ObjWidget::contextMenuEvent(QContextMenuEvent * event)
|
||||
else if(action == _mirrorView)
|
||||
{
|
||||
graphicsView_->setTransform(QTransform().scale(this->isMirrorView()?-1.0:1.0, 1.0));
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked() && _autoScale->isChecked())
|
||||
{
|
||||
graphicsView_->fitInView(graphicsView_->sceneRect(), Qt::KeepAspectRatio);
|
||||
}
|
||||
@ -424,16 +460,20 @@ void ObjWidget::contextMenuEvent(QContextMenuEvent * event)
|
||||
{
|
||||
emit removalTriggered(this);
|
||||
}
|
||||
else if(action == _plainView)
|
||||
else if(action == _graphicsViewMode)
|
||||
{
|
||||
this->setGraphicsViewMode(!_plainView->isChecked());
|
||||
this->setGraphicsViewMode(_graphicsViewMode->isChecked());
|
||||
}
|
||||
else if(action == _autoScale)
|
||||
{
|
||||
this->setAutoScale(_autoScale->isChecked());
|
||||
}
|
||||
QWidget::contextMenuEvent(event);
|
||||
}
|
||||
|
||||
QPixmap ObjWidget::getSceneAsPixmap()
|
||||
{
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
QPixmap img(graphicsView_->sceneRect().width(), graphicsView_->sceneRect().height());
|
||||
QPainter p(&img);
|
||||
@ -472,7 +512,7 @@ void ObjWidget::drawKeypoints(QPainter * painter)
|
||||
{
|
||||
const cv::KeyPoint & r = *iter;
|
||||
float radius = 14*1.2/9.*2;//r.size*1.2/9.*2;
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
QString info = QString( "ID = %1\n"
|
||||
"Response = %2\n"
|
||||
@ -508,7 +548,7 @@ QColor ObjWidget::defaultColor() const
|
||||
std::vector<cv::KeyPoint> ObjWidget::selectedKeypoints() const
|
||||
{
|
||||
std::vector<cv::KeyPoint> selected;
|
||||
if(graphicsViewMode_)
|
||||
if(_graphicsViewMode->isChecked())
|
||||
{
|
||||
QList<QGraphicsItem*> items = graphicsView_->scene()->selectedItems();
|
||||
for(int i=0; i<items.size(); ++i)
|
||||
@ -524,7 +564,6 @@ std::vector<cv::KeyPoint> ObjWidget::selectedKeypoints() const
|
||||
|
||||
void ObjWidget::setupGraphicsView()
|
||||
{
|
||||
graphicsView_->scene()->clear();
|
||||
if(!image_.isNull())
|
||||
{
|
||||
graphicsView_->scene()->setSceneRect(image_.rect());
|
||||
@ -537,8 +576,17 @@ void ObjWidget::setupGraphicsView()
|
||||
pixmapItem->setVisible(this->isImageShown());
|
||||
this->drawKeypoints();
|
||||
|
||||
graphicsView_->fitInView(sceneRect, Qt::KeepAspectRatio);
|
||||
for(int i=0; i<rectItems_.size(); ++i)
|
||||
{
|
||||
graphicsView_->scene()->addItem(rectItems_.at(i));
|
||||
}
|
||||
|
||||
if(_autoScale->isChecked())
|
||||
{
|
||||
graphicsView_->fitInView(sceneRect, Qt::KeepAspectRatio);
|
||||
}
|
||||
}
|
||||
graphicsViewInitialized_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -42,6 +42,7 @@ public:
|
||||
void resetKptsColor();
|
||||
void setKptColor(unsigned int index, const QColor & color);
|
||||
void setGraphicsViewMode(bool on);
|
||||
void setAutoScale(bool autoScale);
|
||||
void setDeletable(bool deletable);
|
||||
void addRect(QGraphicsRectItem * rect);
|
||||
|
||||
@ -88,11 +89,11 @@ private:
|
||||
QList<KeypointItem*> keypointItems_;
|
||||
QGraphicsView * graphicsView_;
|
||||
int id_;
|
||||
bool graphicsViewMode_;
|
||||
QVector<QColor> kptColors_;
|
||||
QString detectorType_;
|
||||
QString descriptorType_;
|
||||
QList<QGraphicsRectItem*> rectItems_;
|
||||
bool graphicsViewInitialized_;
|
||||
|
||||
// menu stuff
|
||||
QString _savedFileName;
|
||||
@ -102,7 +103,8 @@ private:
|
||||
QAction * _saveImage;
|
||||
QAction * _mirrorView;
|
||||
QAction * _delete;
|
||||
QAction * _plainView;
|
||||
QAction * _graphicsViewMode;
|
||||
QAction * _autoScale;
|
||||
};
|
||||
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user