def mouseReleaseEvent(self, event: QtWidgets.QGraphicsSceneMouseEvent) -> None: if not event.button() & QtCore.Qt.LeftButton: return modes = list(self.modifierModes(event.modifiers())) pixel = self.pixelSize() array = polygonf_to_array(self.poly) # Get start and end points of area x1, x2 = np.amin(array[:, 0]), np.amax(array[:, 0]) y1, y2 = np.amin(array[:, 1]), np.amax(array[:, 1]) # Bound to image area x1, y1 = max(x1, 0.0), max(y1, 0.0) x2 = min(x2, self.rect.width() - pixel.width() / 2.0) y2 = min(y2, self.rect.height() - pixel.height() / 2.0) # Generate pixel centers xs = np.arange(x1, x2, pixel.width()) + pixel.width() / 2.0 ys = np.arange(y1, y2, pixel.height()) + pixel.height() / 2.0 X, Y = np.meshgrid(xs, ys) pixels = np.stack((X.flat, Y.flat), axis=1) # Get mask of selected area mask = np.zeros(self.image_shape, dtype=bool) polymask = polygonf_contains_points(self.poly, pixels).reshape(ys.size, xs.size) # Insert ix, iy = int(x1 / pixel.width()), int(y1 / pixel.height()) mask[iy:iy + ys.size, ix:ix + xs.size] = polymask # self.poly.append(self.poly.first()) self.poly.clear() self.prepareGeometryChange() self.selectionChanged.emit(mask, modes)
def mousePressEvent(self, event: QGraphicsSceneMouseEvent): if event.button() == Qt.LeftButton and \ event.modifiers() & Qt.ControlModifier: tabIndex = self.tabWidget.currentIndex() tabName = self.tabWidget.tabText(tabIndex) rowIndex = self.tabWidget.getCurrentTableModel().rowCount( QModelIndex()) label = self.comboBox.currentText() box = QRectF(event.buttonDownScenePos(Qt.LeftButton), QSizeF(1, 1)) color = self.tabWidget.color_map(tabIndex)[label] rect = self.addBox(tabIndex, tabName, rowIndex, self.page, label, box, color) rect.handleSelected = 4 self.signalHandler.boxCreated.emit(rect) super().mousePressEvent(event)
def mouseReleaseEvent(self, event: QtWidgets.QGraphicsSceneMouseEvent) -> None: if not event.button() & QtCore.Qt.LeftButton: return modes = list(self.modifierModes(event.modifiers())) px, py = ( self.rect.width() / self.image_shape[1], self.rect.height() / self.image_shape[0], ) # pixel size x1, y1, x2, y2 = self._rect.normalized().getCoords() x1 = np.round(x1 / px).astype(int) x2 = np.round(x2 / px).astype(int) y1 = np.round(y1 / py).astype(int) y2 = np.round(y2 / py).astype(int) mask = np.zeros(self.image_shape, dtype=bool) mask[y1:y2, x1:x2] = True self._rect = QtCore.QRectF() self.prepareGeometryChange() self.selectionChanged.emit(mask, modes)