Skip to content
Snippets Groups Projects
GUI_draft_live.py 40.8 KiB
Newer Older
  • Learn to ignore specific revisions
  • Christian's avatar
    Christian committed
                self._advanced_widget.hide()
    
    Christian's avatar
    Christian committed
            self.adjustSize()
    
    
        def open_circle_editor(self):
    
    Christian's avatar
    Christian committed
            """ Replace central widget with circle editor. """
    
            if not self._last_loaded_pixmap:
                print("No image loaded yet! Cannot open circle editor.")
                return
    
            old_widget = self.takeCentralWidget()
            self._old_central_widget = old_widget
    
    
    Christian's avatar
    Christian committed
            init_radius = self._circle_calibrated_radius
    
            editor = CircleEditorWidget(
                pixmap=self._last_loaded_pixmap,
                init_radius=init_radius,
                done_callback=self._on_circle_editor_done
            )
            self._editor = editor
            self.setCentralWidget(editor)
    
        def _on_circle_editor_done(self, final_radius):
    
    Christian's avatar
    Christian committed
            self._circle_calibrated_radius = final_radius
    
            print(f"Circle Editor done. Radius = {final_radius}")
    
    
    Christian's avatar
    Christian committed
            if self._last_loaded_file_path:
                cost_img = compute_cost_image(
                    self._last_loaded_file_path,
                    self._circle_calibrated_radius,
                    clip_limit=self._current_clip_limit
                )
                self.image_view.cost_image_original = cost_img
                self.image_view.cost_image = cost_img.copy()
                self.image_view._apply_all_guide_points_to_cost()
                self.image_view._rebuild_full_path()
                self._update_advanced_images()
    
    
            editor_widget = self.takeCentralWidget()
            if editor_widget is not None:
                editor_widget.setParent(None)
    
            if self._old_central_widget is not None:
                self.setCentralWidget(self._old_central_widget)
                self._old_central_widget = None
    
            if self._editor is not None:
                self._editor.deleteLater()
                self._editor = None
    
    
        def toggle_rainbow(self):
            self.image_view.toggle_rainbow()
    
    
        def load_image(self):
            options = QFileDialog.Options()
            file_path, _ = QFileDialog.getOpenFileName(
                self, "Open Image", "",
                "Images (*.png *.jpg *.jpeg *.bmp *.tif)",
                options=options
            )
            if file_path:
                self.image_view.load_image(file_path)
    
    Christian's avatar
    Christian committed
    
                cost_img = compute_cost_image(
                    file_path,
                    self._circle_calibrated_radius,
                    clip_limit=self._current_clip_limit
                )
    
                self.image_view.cost_image_original = cost_img
                self.image_view.cost_image = cost_img.copy()
    
    
                pm = QPixmap(file_path)
                if not pm.isNull():
                    self._last_loaded_pixmap = pm
    
    
    Christian's avatar
    Christian committed
                self._last_loaded_file_path = file_path
                self._update_advanced_images()
    
        def update_contrast(self, clip_limit):
            self._current_clip_limit = clip_limit
            if self._last_loaded_file_path:
                cost_img = compute_cost_image(
                    self._last_loaded_file_path,
                    self._circle_calibrated_radius,
                    clip_limit=clip_limit
                )
                self.image_view.cost_image_original = cost_img
                self.image_view.cost_image = cost_img.copy()
                self.image_view._apply_all_guide_points_to_cost()
                self.image_view._rebuild_full_path()
    
            self._update_advanced_images()
    
        def _update_advanced_images(self):
            if not self._last_loaded_pixmap:
                return
            pm_np = self._qpixmap_to_gray_float(self._last_loaded_pixmap)
            contrasted_blurred = preprocess_image(
                pm_np,
                sigma=3,
                clip_limit=self._current_clip_limit
            )
            cost_img_np = self.image_view.cost_image
            self._advanced_widget.update_displays(contrasted_blurred, cost_img_np)
    
        def _qpixmap_to_gray_float(self, qpix):
            img = qpix.toImage()
            img = img.convertToFormat(QImage.Format_ARGB32)
            ptr = img.bits()
            ptr.setsize(img.byteCount())
            arr = np.frombuffer(ptr, np.uint8).reshape((img.height(), img.width(), 4))
            rgb = arr[..., :3].astype(np.float32)
            gray = rgb.mean(axis=2) / 255.0
            return gray
    
    
    Christian's avatar
    Christian committed
        def export_path(self):
            full_xy = self.image_view.get_full_path_xy()
            if not full_xy:
                print("No path to export.")
    
                return
    
            options = QFileDialog.Options()
            file_path, _ = QFileDialog.getSaveFileName(
    
    Christian's avatar
    Christian committed
                self, "Export Path", "",
    
                "NumPy Files (*.npy);;All Files (*)",
                options=options
            )
            if file_path:
    
    Christian's avatar
    Christian committed
                arr = np.array(full_xy)
                np.save(file_path, arr)
                print(f"Exported path with {len(arr)} points to {file_path}")
    
    
        def clear_points(self):
            self.image_view.clear_guide_points()
    
        def closeEvent(self, event):
            super().closeEvent(event)
    
    
    def main():
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()
        sys.exit(app.exec_())
    
    
    if __name__ == "__main__":
        main()