浏览代码

-added scaling
-fixed few bugs

gapchich 13 年之前
父节点
当前提交
9f0739deb9
共有 4 个文件被更改,包括 148 次插入60 次删除
  1. 89 25
      ImageHolder.cpp
  2. 5 5
      ImageHolder.h
  3. 51 29
      ImageLabeler.cpp
  4. 3 1
      ImageLabeler.h

+ 89 - 25
ImageHolder.cpp

@@ -55,16 +55,41 @@ ImageHolder::paintEvent(QPaintEvent *anEvent)
 		pen.setColor(QColor(Qt::black));
 		pen.setStyle(Qt::DashLine);
 		painter.setPen(pen);
-		switch(tool_) {
-		case BoundingBoxTool:
-			painter.drawRect(bounding_box_.rect);
-			break;
-		case PolygonTool:
-			painter.drawPolygon(polygon_.poly);
-			break;
-		default:
-			break;
+
+		if (BoundingBoxTool == tool_) {
+			/* scaling */
+			QRect bbox = bounding_box_.rect;
+			QPoint bboxTopLeft = bbox.topLeft() * scale_;
+			QPoint bboxBottomRight = bbox.bottomRight() * scale_;
+
+			bbox.setTopLeft(bboxTopLeft);
+			bbox.setBottomRight(bboxBottomRight);
+
+			painter.drawRect(bbox);
 		}
+		else if (PolygonTool == tool_) {
+			/* scaling */
+			QPoint point;
+			QPolygon poly = polygon_.poly;
+			for (int i = 0; i < poly.size(); i++) {
+				point.setX(poly.at(i).x());
+				point.setY(poly.at(i).y());
+				point *= scale_;
+				poly.remove(i);
+				poly.insert(i, point);
+			}
+			painter.drawPolygon(poly);
+		}
+//		switch(tool_) {
+//		case BoundingBoxTool:
+//			painter.drawRect();
+//			break;
+//		case PolygonTool:
+//			painter.drawPolygon(polygon_.poly);
+//			break;
+//		default:
+//			break;
+//		}
 
 	}
 
@@ -112,11 +137,20 @@ ImageHolder::drawBoundingBoxes(
 		aPen->setWidth(width);
 		aPen->setStyle(penStyle);
 		aPainter->setPen(*aPen);
-		aPainter->drawRect(list_bounding_box_->at(i).rect);
+
+		/* scaling */
+		QRect rect = list_bounding_box_->at(i).rect.normalized();
+		QPoint topLeft = rect.topLeft() * scale_;
+		QPoint bottomRight = rect.bottomRight() * scale_;
+
+		rect.setTopLeft(topLeft);
+		rect.setBottomRight(bottomRight);
+
+		aPainter->drawRect(rect);
 		/* drawing label ids of these boxes */
 		QString labelIDText =
 			QString("%1").arg(labelID);
-		QRect rect = list_bounding_box_->at(i).rect.normalized();
+
 
 		aPainter->drawText(
 			rect.left() + 5,
@@ -150,7 +184,10 @@ ImageHolder::drawPolygons(
 		penStyle = Qt::SolidLine;
 		int labelID = list_polygon_->at(i).label_ID_;
 
-		aPen->setColor(QColor(list_label_color_->at(labelID)));
+		if (labelID < list_label_color_->count())
+			aPen->setColor(QColor(list_label_color_->at(labelID)));
+		else
+			aPen->setColor(QColor(Qt::white));
 
 		/* checking whether labeled area is of main object or not */
 		if (labelID == *main_label_)
@@ -168,11 +205,22 @@ ImageHolder::drawPolygons(
 		aPen->setWidth(width);
 		aPen->setStyle(penStyle);
 		aPainter->setPen(*aPen);
-		aPainter->drawPolygon(list_polygon_->at(i).poly);
+
+		QPoint point;
+		QPolygon poly = list_polygon_->at(i).poly;
+		for (int j = 0; j < poly.size(); j++) {
+			point.setX(poly.at(j).x());
+			point.setY(poly.at(j).y());
+			point *= scale_;
+			poly.remove(j);
+			poly.insert(j, point);
+		}
+
+		aPainter->drawPolygon(poly);
 		/* drawing label ids of these polys */
 		QString labelIDText =
 			QString("%1").arg(labelID);
-		QRect rect = list_polygon_->at(i).poly.boundingRect();
+		QRect rect = poly.boundingRect();
 		int x = rect.center().x();
 		int y = rect.center().y();
 
@@ -262,7 +310,8 @@ ImageHolder::scaleImage(
 	double scaleFactor
 )
 {
-	QSize size = pixmap()->size();
+	//QSize size = pixmap()->size();
+	QSize size = this->size();
 
 	/* zoomin */
 	if (ZoomIn == aDirection) {
@@ -275,13 +324,17 @@ ImageHolder::scaleImage(
 		scale_ /= scaleFactor;
 	}
 
-	setPixmap(
-		pixmap()->scaled(
-			size,
-			Qt::IgnoreAspectRatio,
-			Qt::SmoothTransformation
-			)
-		);
+//	setScaledContents(true);
+	this->resize(size);
+//	setPixmap(
+//		image_->scaled(
+//			size,
+//			Qt::IgnoreAspectRatio,
+//			Qt::SmoothTransformation
+//			)
+//		);
+//	setScaledContents(false);
+
 
 
 	//resize(scaleFactor * pixmap()->size());
@@ -358,6 +411,17 @@ ImageHolder::setMainLabelNum(int *aNum)
 	main_label_ = aNum;
 }
 
+void
+ImageHolder::setImage(QPixmap *anImage)
+{
+	if (0 == anImage) {
+		return;
+		/* NOTREACHED */
+	}
+
+	image_ = anImage;
+}
+
 void
 ImageHolder::clearAll()
 {
@@ -446,7 +510,7 @@ ImageHolder::keyPressEvent(QKeyEvent *anEvent)
 void
 ImageHolder::mouseMoveEvent(QMouseEvent *anEvent)
 {
-	QPoint pos = anEvent->pos();
+	QPoint pos = anEvent->pos() / scale_;
 	if (anEvent->pos().x() < 0)
 		pos.setX(0);
 
@@ -488,7 +552,7 @@ ImageHolder::mousePressEvent(QMouseEvent *anEvent)
 	if ((anEvent->buttons() & Qt::LeftButton) ||
 		(anEvent->buttons() & Qt::RightButton))
 	{
-		prev_cursor_pos_ = anEvent->pos();
+		prev_cursor_pos_ = anEvent->pos() / scale_;
 	}
 
 	if (anEvent->buttons() & Qt::LeftButton) {
@@ -503,7 +567,7 @@ ImageHolder::mousePressEvent(QMouseEvent *anEvent)
 			NewSelection == state_ &&
 			Qt::NoModifier == keyboard_modifier_)
 		{
-			triggerPolygon(anEvent->pos(), &(polygon_.poly));
+			triggerPolygon(anEvent->pos() / scale_, &(polygon_.poly));
 		}
 
 		/* starting new selection by click */

+ 5 - 5
ImageHolder.h

@@ -57,15 +57,12 @@ protected:
 		QPen *aPen
 		);
 
-	void scaleImage(ZoomDirection aDirection, double scaleFactor);
-
 public:
 	enum Tool {
 		NoTool,
 		BoundingBoxTool,
 		PolygonTool,
-		TaggingTool,
-		EraserTool
+		TaggingTool
 	};
 
 	enum State {
@@ -81,6 +78,8 @@ public:
 	void setPolygonList(QList< Polygon > *aPolyList);
 	void setLabelColorList(QList< uint > *aLabelColorList);
 	void setMainLabelNum(int *aNum);
+	void setImage(QPixmap *anImage);
+	void scaleImage(ZoomDirection aDirection, double scaleFactor);
 	State state();
 	Tool tool();
 
@@ -102,6 +101,7 @@ private:
 	QList< BoundingBox > *list_bounding_box_;
 	QList< Polygon > *list_polygon_;
 	QList< uint > *list_label_color_;
+	QPixmap *image_;
 	int *main_label_;
 	BoundingBox bounding_box_;
 	Polygon polygon_;
@@ -115,7 +115,7 @@ private:
 	int focused_selection_;
 	Tool focused_selection_type_;
 
-	int scale_;
+	double scale_;
 };
 
 #endif /* IMAGEHOLDER_H_ */

+ 51 - 29
ImageLabeler.cpp

@@ -17,6 +17,7 @@
 #include <QGridLayout>
 #include <QPixmap>
 #include <QLabel>
+#include <QScrollArea>
 #include <QPushButton>
 #include <QButtonGroup>
 #include <QListWidget>
@@ -176,11 +177,14 @@ ImageLabeler::ImageLabeler(QWidget *aParent) :
 	frame_center_ = new QFrame(central_widget_);
 	//frame_image_->setFrameStyle(QFrame::Box | QFrame::Plain);
 	//frame_image_->setLineWidth(1);
-	frame_image_ = new QFrame(frame_center_);
+	frame_image_ = new QScrollArea(frame_center_);
 	//frame_image_->setStyleSheet("QWidget {background: #888888;}");
 	frame_image_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
 	frame_image_->setLineWidth(0);
 	frame_image_->setMidLineWidth(0);
+	frame_image_->setWidgetResizable(false);
+	frame_image_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
+	frame_image_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
 	frame_labelbox_ = new QFrame(central_widget_);
 	frame_labelbox_->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
 	frame_labelbox_->setLineWidth(0);
@@ -190,17 +194,19 @@ ImageLabeler::ImageLabeler(QWidget *aParent) :
 	image_->fill(QColor(Qt::white));
 
 	//image_holder_ = new ImageHolder(frame_image_);
-	image_holder_ = new ImageHolder(frame_image_);
+	image_holder_ = new ImageHolder;//(frame_image_);
 	image_holder_->setPixmap(*image_);
-	image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
+	//image_holder_->setStyleSheet("QLabel {background: #ffffff;}");
 	image_holder_->setAlignment(Qt::AlignVCenter | Qt::AlignHCenter);
-	image_holder_->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
+	image_holder_->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
 	image_holder_->setFocusPolicy(Qt::StrongFocus);
 	image_holder_->setFocus();
-	//image_holder_->setScaledContents(true);
+	image_holder_->setScaledContents(true);
 	image_holder_->setFrameStyle(QFrame::Box | QFrame::Plain);
 	image_holder_->setLineWidth(0);
 
+	frame_image_->setWidget(image_holder_);
+
 	list_label_ = new QListWidget(central_widget_);
 	list_label_->setContextMenuPolicy(Qt::CustomContextMenu);
 	list_areas_ = new QListWidget(central_widget_);
@@ -305,12 +311,12 @@ ImageLabeler::ImageLabeler(QWidget *aParent) :
 	layout_frame_image_->setContentsMargins(0, 0, 0, 0);
 	layout_frame_image_->addWidget(frame_image_);
 
-	frame_image_->setLayout(layout_image_widget_);
-	layout_image_widget_->setRowStretch(0, 1);
-	layout_image_widget_->setColumnStretch(0, 1);
-	layout_image_widget_->addWidget(image_holder_, 1, 1);
-	layout_image_widget_->setRowStretch(2, 1);
-	layout_image_widget_->setColumnStretch(2, 1);
+	//frame_image_->setLayout(layout_image_widget_);
+//	layout_image_widget_->setRowStretch(0, 1);
+//	layout_image_widget_->setColumnStretch(0, 1);
+//	layout_image_widget_->addWidget(image_holder_, 1, 1);
+//	layout_image_widget_->setRowStretch(2, 1);
+//	layout_image_widget_->setColumnStretch(2, 1);
 
 	//layout_frame_image_->addStretch(1);
 	layout_frame_image_->addLayout(layout_center_buttons_);
@@ -539,6 +545,7 @@ ImageLabeler::ImageLabeler(QWidget *aParent) :
 	image_holder_->setPolygonList(&list_polygon_);
 	image_holder_->setLabelColorList(&list_label_colors_);
 	image_holder_->setMainLabelNum(&main_label_);
+	image_holder_->setImage(image_);
 }
 
 ImageLabeler::~ImageLabeler()
@@ -647,9 +654,14 @@ ImageLabeler::addLabel()
 	newItem->setText(label);
 
 	QPixmap iconPix = QPixmap(20, 20);
-	iconPix.fill(Qt::white);
+	QColor color;
+	if (0 != itemNum)
+		color = Qt::white;
+	else
+		color = Qt::black;
+	iconPix.fill(color);
 	QIcon icon(iconPix);
-	list_label_colors_.append(0xffffffff);
+	list_label_colors_.append(color.rgb());
 
 	newItem->setIcon(icon);
 
@@ -734,18 +746,20 @@ ImageLabeler::removeLabel()
 		/* NOTREACHED */
 	}
 
-	QListWidgetItem *current = list_label_->currentItem();
-
-	/* because we need to keep BACKGROUND category */
-	if (0 == list_label_->row(current)) {
+	//QListWidgetItem *current = list_label_->currentItem();
+	/* we need to keep BACKGROUND category */
+	if (label_ID_ < 1) {
 		return;
 		/* NOTREACHED */
 	}
 
-	int row = list_label_->row(current);
-	list_label_->takeItem(row);
-	list_label_colors_.takeAt(row);
-	delete current;
+	if (list_label_->count() <= label_ID_ ||
+		list_label_colors_.count() <= label_ID_) {
+		return;
+		/* NOTREACHED */
+	}
+	list_label_->takeItem(label_ID_);
+	list_label_colors_.takeAt(label_ID_);
 }
 
 void
@@ -1036,7 +1050,13 @@ ImageLabeler::nextImage()
 		current_image_ = list_images_->begin();
 	else
 		current_image_++;
+
+	if ((*current_image_).isEmpty()) {
+		return;
+		/* NOTREACHED */
+	}
 	image_->load(*current_image_);
+	image_holder_->resize(image_->size());
 	image_holder_->setPixmap(*image_);
 	list_bounding_box_.clear();
 	list_polygon_.clear();
@@ -1064,6 +1084,7 @@ ImageLabeler::prevImage()
 	else
 		current_image_--;
 	image_->load(*current_image_);
+	image_holder_->resize(image_->size());
 	image_holder_->setPixmap(*image_);
 	list_bounding_box_.clear();
 	list_polygon_.clear();
@@ -1562,7 +1583,7 @@ ImageLabeler::loadImages()
 		/* NOTREACHED */
 	}
 
-	clearAll();
+	clearAllTool();
 
 	getImagesFromDir(QDir(dirName));
 
@@ -1574,7 +1595,8 @@ ImageLabeler::loadImages()
 
 	current_image_ = list_images_->begin();
 
-	image_->load(list_images_->first());
+	image_->load(*current_image_);
+	image_holder_->resize(image_->size());
 	image_holder_->setPixmap(*image_);
 
 	enableTools();
@@ -1945,7 +1967,7 @@ ImageLabeler::BBoxFromString(
 	/* getting bbox id in the list(it cannot be changed) */
 	bool ok = 0;
 	int bboxID = getNumFromString(aString, "BBox #", ";", &ok);
-	if (!ok || -1 <= bboxID) {
+	if (!ok || bboxID <= -1) {
 		qDebug() <<
 			"BBoxFromString: poly ID is corrupted";
 		return bbox;
@@ -1954,7 +1976,7 @@ ImageLabeler::BBoxFromString(
 
 	/* getting new label id */
 	int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
-	if (!ok || -1 <= labelID) {
+	if (!ok || labelID <= -1) {
 		showWarning(
 			tr("new LabelID is wrong, area can not be changed")
 				);
@@ -2074,7 +2096,7 @@ ImageLabeler::polyFromString(
 	/* getting poly id in the list(it cannot be changed) */
 	bool ok = 0;
 	int polyID = getNumFromString(aString, "Poly #", ";", &ok);
-	if (!ok || -1 <= polyID) {
+	if (!ok || polyID <= -1) {
 		qDebug() <<
 			"polyFromString: poly ID is corrupted";
 		return poly;
@@ -2083,7 +2105,7 @@ ImageLabeler::polyFromString(
 
 	/* getting new label id */
 	int labelID = getNumFromString(aString, "LabelID: ", ";", &ok);
-	if (!ok || -1 <= labelID) {
+	if (!ok || labelID <= -1) {
 		showWarning(
 			tr("new LabelID is wrong, area can not be changed")
 				);
@@ -2484,11 +2506,11 @@ ImageLabeler::wheelEvent(QWheelEvent *anEvent)
 {
 	/* zoomin */
 	if (0 < anEvent->delta()) {
-		//image_holder_->scaleImage(ZoomIn, 1.1);
+		image_holder_->scaleImage(ZoomIn, 1.1);
 	}
 	/* zoomout */
 	else if (anEvent->delta() < 0) {
-		//image_holder_->scaleImage(ZoomOut, 1.1);
+		image_holder_->scaleImage(ZoomOut, 1.1);
 	}
 }
 

+ 3 - 1
ImageLabeler.h

@@ -25,6 +25,7 @@ class QGridLayout;
 class QPushButton;
 class QPixmap;
 class QLabel;
+class QScrollArea;
 class QFrame;
 class QListWidget;
 class QListWidgetItem;
@@ -174,7 +175,8 @@ private:
 
 	/* widgets */
 	QWidget *central_widget_;
-	QFrame *frame_image_;
+	//QFrame *frame_image_;
+	QScrollArea *frame_image_;
 	QFrame *frame_center_;
 	QFrame *frame_toolbox_;
 	QFrame *frame_labelbox_;