imagej / ImageJ

Public domain software for processing and analyzing scientific images
http://imagej.org
Other
576 stars 227 forks source link

Incorrect point count returned for counter 0 for PointRoi when points are added interactively #205

Closed melikovk closed 1 year ago

melikovk commented 1 year ago

I get the incorrect number of points returned by getCount(0) when points are added interactively. Everything works fine when points are added with addPoint method or for other counters. See Python examples below.

from ij import WindowManager
from ij.gui import PointRoi
from time import sleep

roi = PointRoi()
imp = WindowManager.getCurrentImage()
imp.setRoi(roi)
counter = 0
roi.setCounter(counter)
print(roi.getCount(counter), len(roi.getContainedPoints()))
for i in range(3):
    roi.addPoint(i*20,i*20)
    print(roi.getCount(counter), len(roi.getContainedPoints())) 
for i in range(5):
    sleep(1)
    print(roi.getCount(counter), len(roi.getContainedPoints()))

outputs

(0, 0)
(1, 1)
(2, 2)
(3, 3)
(5, 4)
(6, 5)
(7, 6)
(8, 7)
(8, 7)

Above I was adding points interactively during the second for loop. When counter value is changed to any other value (5 for the output below) everything works fine.

(0, 0)
(1, 1)
(2, 2)
(3, 3)
(4, 4)
(5, 5)
(6, 6)
(6, 6)
(7, 7)

Thank you for your help

scuniff commented 1 year ago

I can re-produce this issue with multi-stack Images, like:

File->Open Samples->MRI- Stack File->Open Samples->Mitosis (5D Stack) File->Open Samples->FluorescentCells.tif

Other non Stack images do not seem to have the issue.

See attached screen recordings.

Do you see the same on your Imagej installment?

Screen recording showing problem with Image Stack: https://user-images.githubusercontent.com/40019379/229549943-5fb6f921-8cff-42ee-a06c-72647e007aa3.mp4

Screen recording showing no problem with non stack Image: https://user-images.githubusercontent.com/40019379/229550218-12a309da-91a9-4c5d-9dbf-9c1dab047469.mp4

melikovk commented 1 year ago

Yes, the issue is with multi-stack images only. I did not realize it during the initial submission, but I see the same as you on my ImageJ installation.

scuniff commented 1 year ago

Here are some observations I’ve made.

The summary is that the array counts[0] is initialized to 1 and not 0, thus the count starts off 1 more than it should after a point has been added.

Details……

When the python code calls getCount(0), the Java method PointRoi.getCount(int counter) as shown below is executed.

When no points have been added yet, variable counters is null and variable nPoints is returned. This scenario returns the correct number.

After 1 point has been added manually to the image and getCount(0) is called, variable counters is not null and variable counts[0] is returned. Variable counts[0] always has a value 1 more than the number of points on the image, thus the issue seen.

/** Returns the count associated with the specified counter index.
 * @see #getLastCounter
 * @see <a href="http://wsr.imagej.net/macros/js/PointProperties.js">PointProperties.js</a>
 */
public int getCount(int counter) {
    if (counter==0 && counters==null)
        return nPoints;
    else
        return counts[counter];
}

From what I see in the Eclipse debugger, counts[0] is initialized to 1 and not 0, thus starting off the point count 1 more than it should.

When the python code “roi = PointRoi()” is called, the constructor below is called. After the line “this(0.0, 0.0)” is executed, the variable counts[0](as used by in the code above) is initialized to 1. After the line “deletePoint(0) is executed, counts[0] is still 1. Thus, the count is already 1 more than it should be.

public PointRoi() {
    this(0.0, 0.0);
    deletePoint(0);
}

See attached screen recording that may make it clearer in what I'm seeing.

I might be able to dig in more closely in a couple of weeks.

https://github.com/imagej/ImageJ/assets/40019379/eab4cd34-0e46-41b4-bcb1-5291c672c1eb

scuniff commented 1 year ago

Attached is a screen recording showing the PointRoi() constructor being called and showing the variable counts[0] getting initialized to 1.

I'm think counts[0[ should be initialized to 0.

https://github.com/imagej/ImageJ/assets/40019379/6f898019-b5b1-4f69-8fe2-5aeebd21faa7

melikovk commented 1 year ago

Thank you for exploring this issue. Do you understand why it appears only on multi-stack images as you have discovered before? I also noticed that it appears only in a multi-point mode.

rasband commented 1 year ago

This bug is fixed in the ImageJ 1.54f23 daily build. The commit is at https://github.com/imagej/ImageJ/commit/044bd650dcc4fb9943ee3c39980e6610c813317a.