Issue about ImagePlus

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Issue about ImagePlus

CARL Philippe (LBP)
Dear all,
Happy new year for everybody (hoping that it will be better than the last one)!
Empirically I found an issue that I'm not really able to understand.
So the following code is working as expected (i.e. the imp1 gets closed as expected):
        public class My_Plugin1 implements PlugIn {
                ImagePlus imp1, imp2, imp3;
                public void run(String arg) {
                        imp1 = IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
                        imp1.show();
                        imp2 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default", "Grayscale");
                        imp3 = SubHyperstackMaker.makeSubhyperstack(imp2, "1", "1-3", "1-19");
                        imp3.show();
                        imp1.close();
                        imp2.close();
                }
        }
But with the following code:
        public class My_Plugin2 implements PlugIn {
                ImagePlus imp1, imp3;
                public void run(String arg) {
                        imp1 = IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
                        imp1.show();
                        imp1 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default", "Grayscale");
                        imp3 = SubHyperstackMaker.makeSubhyperstack(imp1, "1", "1-3", "1-19");
                        imp3.show();
                        imp1.close();
                }
        }
the imp1 window isn't closed.
Is this because the HyperStackConverter.toHyperStack method is creating a static ImagePlus and not a ImagePlus?
I thank you very much in advance for your lighting on this.
My best regards,
Philippe

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Issue about ImagePlus

Fred Damen
Greetings Philippe,

Even though Java considers pointers evil and hides them from you, object
variables are essentially what C++ would call smart pointers. A smart
pointer is an middleman object that functions as a pointer and keeps a
reference count, so that when the count goes to zero the actual referred
object is deleted.
So in case one imp1,
   first points to the ImagePlus that you open from a website,
   then imp1.show increased the ref count on website ImagePlus,
   then imp1.close decreased the ref count on website ImagePlus,
   then existing run's scope decreased the ref count on website ImagePlus
to zero and thus allowing garbage collection to do its job.

In case two imp1,
   first points to the ImagePlus that you open from a website,
   then imp1.show increased the ref count on website ImagePlus,
   then imp1 = HyperStack... causes imp1 to drop its reference to the
website ImagePlus from run's scope, decreasing ref count by one,
   then imp1.close is probably a noop as HyperStack... ImagePlus has not
been show(en),
   then exiting run's scope decreased the ref count on HyperStack...
ImagePlus to zero, thus allowing garbage collection to do its job.
   N.B. website ImagePlus still has a ref count of one due to it being
show(en) and never having close called on it.

Not sure what you mean by "HyperStackConverter.toHyperStack method is
creating a static ImagePlus and not a ImagePlus" as there is no "static"
involved.

Enjoy,

Fred

On Sun, January 10, 2021 1:12 pm, CARL Philippe (LBP) wrote:

> Dear all,
> Happy new year for everybody (hoping that it will be better than the last
> one)!
> Empirically I found an issue that I'm not really able to understand.
> So the following code is working as expected (i.e. the imp1 gets closed as
> expected):
> public class My_Plugin1 implements PlugIn {
> ImagePlus imp1, imp2, imp3;
> public void run(String arg) {
> imp1 =
> IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp2 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
> "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp2, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> imp2.close();
> }
> }
> But with the following code:
> public class My_Plugin2 implements PlugIn {
> ImagePlus imp1, imp3;
> public void run(String arg) {
> imp1 =
> IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp1 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
> "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp1, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> }
> }
> the imp1 window isn't closed.
> Is this because the HyperStackConverter.toHyperStack method is creating a
> static ImagePlus and not a ImagePlus?
> I thank you very much in advance for your lighting on this.
> My best regards,
> Philippe
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Issue about ImagePlus

CARL Philippe (LBP)
Dear Fred,
I'm not sure to agree with your explanations, especially when looking at the output of the following code:
        public void run(String arg) {
                ImagePlus imp = IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
                IJ.log("" + imp);
                imp.close();
                IJ.log("" + imp);
                imp.show();
                IJ.log("" + imp);
// imp = HyperStackConverter.toHyperStack(imp, 2, 3, 19, "default", "Grayscale");
// IJ.log("" + imp);
                imp.close();
                IJ.log("" + imp);
        }
As for the static ImagePlus question, if you look at the HyperStackConverter API under the following link:
https://imagej.nih.gov/ij/developer/api/ij/plugin/HyperStackConverter.html
you will see that the toHyperStack method is outputing a static ImagePlus and not an ImagePlus like for example the getImagePlus method of the ImageWindow API:
https://imagej.nih.gov/ij/developer/api/ij/gui/ImageWindow.html
My best regards,
Philippe

----- Mail original -----
De: "Fred Damen" <[hidden email]>
À: "imagej" <[hidden email]>
Envoyé: Dimanche 10 Janvier 2021 23:45:19
Objet: Re: Issue about ImagePlus

Greetings Philippe,

Even though Java considers pointers evil and hides them from you, object
variables are essentially what C++ would call smart pointers. A smart
pointer is an middleman object that functions as a pointer and keeps a
reference count, so that when the count goes to zero the actual referred
object is deleted.
So in case one imp1,
   first points to the ImagePlus that you open from a website,
   then imp1.show increased the ref count on website ImagePlus,
   then imp1.close decreased the ref count on website ImagePlus,
   then existing run's scope decreased the ref count on website ImagePlus
to zero and thus allowing garbage collection to do its job.

In case two imp1,
   first points to the ImagePlus that you open from a website,
   then imp1.show increased the ref count on website ImagePlus,
   then imp1 = HyperStack... causes imp1 to drop its reference to the
website ImagePlus from run's scope, decreasing ref count by one,
   then imp1.close is probably a noop as HyperStack... ImagePlus has not
been show(en),
   then exiting run's scope decreased the ref count on HyperStack...
ImagePlus to zero, thus allowing garbage collection to do its job.
   N.B. website ImagePlus still has a ref count of one due to it being
show(en) and never having close called on it.

Not sure what you mean by "HyperStackConverter.toHyperStack method is
creating a static ImagePlus and not a ImagePlus" as there is no "static"
involved.

Enjoy,

Fred

On Sun, January 10, 2021 1:12 pm, CARL Philippe (LBP) wrote:

> Dear all,
> Happy new year for everybody (hoping that it will be better than the last
> one)!
> Empirically I found an issue that I'm not really able to understand.
> So the following code is working as expected (i.e. the imp1 gets closed as
> expected):
> public class My_Plugin1 implements PlugIn {
> ImagePlus imp1, imp2, imp3;
> public void run(String arg) {
> imp1 =
> IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp2 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
> "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp2, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> imp2.close();
> }
> }
> But with the following code:
> public class My_Plugin2 implements PlugIn {
> ImagePlus imp1, imp3;
> public void run(String arg) {
> imp1 =
> IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp1 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
> "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp1, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> }
> }
> the imp1 window isn't closed.
> Is this because the HyperStackConverter.toHyperStack method is creating a
> static ImagePlus and not a ImagePlus?
> I thank you very much in advance for your lighting on this.
> My best regards,
> Philippe
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Issue about ImagePlus

Wayne Rasband-2
In reply to this post by CARL Philippe (LBP)
> On Jan 10, 2021, at 2:12 PM, CARL Philippe (LBP) <[hidden email]> wrote:
>
> Dear all,
> Happy new year for everybody (hoping that it will be better than the last one)!
> Empirically I found an issue that I'm not really able to understand.

Hi Philippe,
The window is not closed in the second example because the ‘imp1’ variable points to a different ImagePlus, a non-displayed hyperstack version of the original image. It is the HyperStackConverter.toHyperStack() method that is static, not the ImagePlus it returns.

-waye

> So the following code is working as expected (i.e. the imp1 gets closed as expected):
> public class My_Plugin1 implements PlugIn {
> ImagePlus imp1, imp2, imp3;
> public void run(String arg) {
> imp1 = IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp2 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default", "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp2, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> imp2.close();
> }
> }
> But with the following code:
> public class My_Plugin2 implements PlugIn {
> ImagePlus imp1, imp3;
> public void run(String arg) {
> imp1 = IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
> imp1.show();
> imp1 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default", "Grayscale");
> imp3 = SubHyperstackMaker.makeSubhyperstack(imp1, "1", "1-3", "1-19");
> imp3.show();
> imp1.close();
> }
> }
> the imp1 window isn't closed.
> Is this because the HyperStackConverter.toHyperStack method is creating a static ImagePlus and not a ImagePlus?
> I thank you very much in advance for your lighting on this.
> My best regards,
> Philippe

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html
Reply | Threaded
Open this post in threaded view
|

Re: Issue about ImagePlus

Fred Damen
In reply to this post by CARL Philippe (LBP)
Greetings Philippe,

In my attempt to be precise I seem have to over complicated the issue, see
Wayne's eloquent explanation.

I am not sure what relevance you intended to show with this example as you
did not provide the output. Albeit, imp points to the same ImagePlus
Object throughout your code snippet.

Enjoy,

Fred

On Sun, January 10, 2021 6:07 pm, CARL Philippe (LBP) wrote:

> Dear Fred,
> I'm not sure to agree with your explanations, especially when looking at
> the output of the following code:
> public void run(String arg) {
> ImagePlus imp =
> IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
> IJ.log("" + imp);
> imp.close();
> IJ.log("" + imp);
> imp.show();
> IJ.log("" + imp);
> // imp = HyperStackConverter.toHyperStack(imp, 2, 3, 19, "default",
> "Grayscale");
> // IJ.log("" + imp);
> imp.close();
> IJ.log("" + imp);
> }
> As for the static ImagePlus question, if you look at the
> HyperStackConverter API under the following link:
> https://imagej.nih.gov/ij/developer/api/ij/plugin/HyperStackConverter.html
> you will see that the toHyperStack method is outputing a static ImagePlus
> and not an ImagePlus like for example the getImagePlus method of the
> ImageWindow API:
> https://imagej.nih.gov/ij/developer/api/ij/gui/ImageWindow.html
> My best regards,
> Philippe
>
> ----- Mail original -----
> De: "Fred Damen" <[hidden email]>
> À: "imagej" <[hidden email]>
> Envoyé: Dimanche 10 Janvier 2021 23:45:19
> Objet: Re: Issue about ImagePlus
>
> Greetings Philippe,
>
> Even though Java considers pointers evil and hides them from you, object
> variables are essentially what C++ would call smart pointers. A smart
> pointer is an middleman object that functions as a pointer and keeps a
> reference count, so that when the count goes to zero the actual referred
> object is deleted.
> So in case one imp1,
>    first points to the ImagePlus that you open from a website,
>    then imp1.show increased the ref count on website ImagePlus,
>    then imp1.close decreased the ref count on website ImagePlus,
>    then existing run's scope decreased the ref count on website ImagePlus
> to zero and thus allowing garbage collection to do its job.
>
> In case two imp1,
>    first points to the ImagePlus that you open from a website,
>    then imp1.show increased the ref count on website ImagePlus,
>    then imp1 = HyperStack... causes imp1 to drop its reference to the
> website ImagePlus from run's scope, decreasing ref count by one,
>    then imp1.close is probably a noop as HyperStack... ImagePlus has not
> been show(en),
>    then exiting run's scope decreased the ref count on HyperStack...
> ImagePlus to zero, thus allowing garbage collection to do its job.
>    N.B. website ImagePlus still has a ref count of one due to it being
> show(en) and never having close called on it.
>
> Not sure what you mean by "HyperStackConverter.toHyperStack method is
> creating a static ImagePlus and not a ImagePlus" as there is no "static"
> involved.
>
> Enjoy,
>
> Fred
>
> On Sun, January 10, 2021 1:12 pm, CARL Philippe (LBP) wrote:
>> Dear all,
>> Happy new year for everybody (hoping that it will be better than the
>> last
>> one)!
>> Empirically I found an issue that I'm not really able to understand.
>> So the following code is working as expected (i.e. the imp1 gets closed
>> as
>> expected):
>> public class My_Plugin1 implements PlugIn {
>> ImagePlus imp1, imp2, imp3;
>> public void run(String arg) {
>> imp1 =
>> IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
>> imp1.show();
>> imp2 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
>> "Grayscale");
>> imp3 = SubHyperstackMaker.makeSubhyperstack(imp2, "1", "1-3",
>> "1-19");
>> imp3.show();
>> imp1.close();
>> imp2.close();
>> }
>> }
>> But with the following code:
>> public class My_Plugin2 implements PlugIn {
>> ImagePlus imp1, imp3;
>> public void run(String arg) {
>> imp1 =
>> IJ.openImage("https://imagej.net/images/bat-cochlea-volume.zip");
>> imp1.show();
>> imp1 = HyperStackConverter.toHyperStack(imp1, 2, 3, 19, "default",
>> "Grayscale");
>> imp3 = SubHyperstackMaker.makeSubhyperstack(imp1, "1", "1-3",
>> "1-19");
>> imp3.show();
>> imp1.close();
>> }
>> }
>> the imp1 window isn't closed.
>> Is this because the HyperStackConverter.toHyperStack method is creating
>> a
>> static ImagePlus and not a ImagePlus?
>> I thank you very much in advance for your lighting on this.
>> My best regards,
>> Philippe
>>
>> --
>> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>>
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html