From 14c717482488dd6df8ab972630009eda7ffb3f6f Mon Sep 17 00:00:00 2001 From: carlosuc3m <100329787@alumnos.uc3m.es> Date: Tue, 3 Dec 2024 15:12:32 +0100 Subject: [PATCH] fix roi adding and deleting for rectangles --- .../java/ai/nets/samj/ij/ui/Consumer.java | 48 ++++++++++--------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/ai/nets/samj/ij/ui/Consumer.java b/src/main/java/ai/nets/samj/ij/ui/Consumer.java index 3cbece3..9afa6c3 100644 --- a/src/main/java/ai/nets/samj/ij/ui/Consumer.java +++ b/src/main/java/ai/nets/samj/ij/ui/Consumer.java @@ -242,12 +242,13 @@ public & NativeType> RandomAccessibleInterval getFo @Override public List getRectRoisOnFocusImage() { Roi roi = IJ.getImage().getRoi(); - //List list = getRectRoisFromRoiManager(); - List list = new ArrayList(); + List list = getRectRoisFromRoiManager(); if (roi == null) return list; if (roi.getType() != Roi.RECTANGLE) return list; + if (list.stream().anyMatch(a -> a.equals(roi.getBounds()))) + return list; list.add(roi.getBounds()); return list; } @@ -256,7 +257,6 @@ public List getRectRoisOnFocusImage() { public List getPointRoisOnFocusImage() { Roi roi = IJ.getImage().getRoi(); List list = getPointRoisFromRoiManager(); - //List list = new ArrayList(); if (roi == null) return list; if (roi.getType() != Roi.POINT) @@ -316,28 +316,29 @@ public void deletePointRoi(int[] pp) { @Override public void deleteRectRoi(Rectangle rect) { - Roi roi = IJ.getImage().getRoi(); - if (roi.getType() == Roi.RECTANGLE) { - if (roi.getBounds() == rect) { - activeImage.deleteRoi(); - return; - } - } Roi[] roiManagerRois = RoiManager.getInstance().getRoisAsArray(); - for (int i = 0; i < roiManagerRois.length; i ++) { - Roi managerRoi = roiManagerRois[i]; - if (managerRoi.getType() != Roi.RECTANGLE) - continue; - if (roi.getBounds() == rect) { - try { - RoiManagerPrivateViolator.deleteRoiAtPosition(RoiManager.getInstance(), i); - } catch (NoSuchFieldException | SecurityException | NoSuchMethodException | IllegalAccessException - | IllegalArgumentException | InvocationTargetException e) { - e.printStackTrace(); + if (roiManagerRois != null) { + for (int i = 0; i < roiManagerRois.length; i ++) { + Roi managerRoi = roiManagerRois[i]; + if (managerRoi.getType() != Roi.RECTANGLE) + continue; + if (managerRoi.getBounds().equals(rect)) { + try { + RoiManagerPrivateViolator.deleteRoiAtPosition(RoiManager.getInstance(), i); + } catch (NoSuchFieldException | SecurityException | NoSuchMethodException | IllegalAccessException + | IllegalArgumentException | InvocationTargetException e) { + e.printStackTrace(); + } + return; } + } + } + Roi roi = IJ.getImage().getRoi(); + if (roi != null && roi.getType() == Roi.RECTANGLE) { + if (roi.getBounds().equals(rect)) { + activeImage.deleteRoi(); return; } - } } @@ -347,7 +348,10 @@ private List getRectRoisFromRoiManager() { if (rois.length == 0) return list; list = Arrays.stream(rois).filter(rr -> rr.getType() == Roi.RECTANGLE) - .map(rr -> rr.getBounds()).collect(Collectors.toList()); + .map(rr -> { + rr.setImage(activeImage); + return rr.getBounds(); + }).collect(Collectors.toList()); return list; }