Skip to content

Commit

Permalink
fix roi adding and deleting for rectangles
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosuc3m committed Dec 3, 2024
1 parent 5aa5275 commit 14c7174
Showing 1 changed file with 26 additions and 22 deletions.
48 changes: 26 additions & 22 deletions src/main/java/ai/nets/samj/ij/ui/Consumer.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,12 +242,13 @@ public <T extends RealType<T> & NativeType<T>> RandomAccessibleInterval<T> getFo
@Override
public List<Rectangle> getRectRoisOnFocusImage() {
Roi roi = IJ.getImage().getRoi();
//List<Rectangle> list = getRectRoisFromRoiManager();
List<Rectangle> list = new ArrayList<Rectangle>();
List<Rectangle> 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;
}
Expand All @@ -256,7 +257,6 @@ public List<Rectangle> getRectRoisOnFocusImage() {
public List<int[]> getPointRoisOnFocusImage() {
Roi roi = IJ.getImage().getRoi();
List<int[]> list = getPointRoisFromRoiManager();
//List<int[]> list = new ArrayList<int[]>();
if (roi == null)
return list;
if (roi.getType() != Roi.POINT)
Expand Down Expand Up @@ -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;
}

}
}

Expand All @@ -347,7 +348,10 @@ private List<Rectangle> 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;
}

Expand Down

0 comments on commit 14c7174

Please sign in to comment.