Login  Register

Trouble with active windows within a loop

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Trouble with active windows within a loop

moxed
Scripting newbie here, having trouble calling windows to the front in a for
loop. As a brief example, I'm trying to write a script (jython) that will
loop through an array of images and convert them to 8-bit and also set a
particular threshold.

I can't seem to get the convert to 8bit command to work on all images in the
array. It looks like only the last image is acted upon.  

I think I need to be able to call an image to the front, using the image
title or ID, but I can't figure out how to do that. Any advice would be
great.
(In my real code there are a number of more manipulations, and also more
than 2 files being processed)

    import os
    from java.io import File
    from ij import IJ, Macro, ImagePlus, WindowManager
    from ij.process import ImageStatistics as IS, ImageProcessor
    from ij.measure import Measurements
    from ij.process import ImageConverter


    wt_path = "C:/wt.tif"
    dic_path = "C:/dic.tif"

    wt = IJ.openImage(wt_path)
    dic = IJ.openImage(dic_path)

    imp = [wt, dic]

    for img in imp:
        IJ.getImage()
    IJ.run("8-bit")
    if img == dic:
    maxThreshold = wt.getStatistics(Measurements.MIN_MAX).max
    IJ.setThreshold(0.50*maxThreshold, maxThreshold)
    print img, "mxt=", maxThreshold
    else:
    maxThreshold = img.getStatistics(Measurements.MIN_MAX).max
    IJ.setThreshold(0.20*maxThreshold, maxThreshold)
    print img, "mxt=", maxThreshold


    print end

The threshold function works perfect, but I can't for the life of me get the
8 bit to run.  Only the last image in the array `dic` is converted.

I've also tried:
    for img in imp:
        ImageConverter.convertToGray8(img)

But that throws an error:
> TypeError: convertToGray8(): self arg can't be coerced to
> ij.process.ImageConverter


I can't figure out how to call a specific image either by window title or by
image ID.



--
Sent from: http://imagej.1557.x6.nabble.com/

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

Re: Trouble with active windows within a loop

Herbie
Good day Samantha,

did you try using the ImageJ macro recorder?

For Java and JavaScript it gives me:

IJ.run(imp, "8-bit", "");

Please note that this is has been cross-posted to the ImageJ-Forum.

Regards

Herbie

:::::::::::::::::::::::::::::::::::
Am 04.04.18 um 22:42 schrieb moxed:

> Scripting newbie here, having trouble calling windows to the front in a for
> loop. As a brief example, I'm trying to write a script (jython) that will
> loop through an array of images and convert them to 8-bit and also set a
> particular threshold.
>
> I can't seem to get the convert to 8bit command to work on all images in the
> array. It looks like only the last image is acted upon.
>
> I think I need to be able to call an image to the front, using the image
> title or ID, but I can't figure out how to do that. Any advice would be
> great.
> (In my real code there are a number of more manipulations, and also more
> than 2 files being processed)
>
>      import os
>      from java.io import File
>      from ij import IJ, Macro, ImagePlus, WindowManager
>      from ij.process import ImageStatistics as IS, ImageProcessor
>      from ij.measure import Measurements
>      from ij.process import ImageConverter
>
>
>      wt_path = "C:/wt.tif"
>      dic_path = "C:/dic.tif"
>
>      wt = IJ.openImage(wt_path)
>      dic = IJ.openImage(dic_path)
>
>      imp = [wt, dic]
>
>      for img in imp:
>          IJ.getImage()
>       IJ.run("8-bit")
>       if img == dic:
>       maxThreshold = wt.getStatistics(Measurements.MIN_MAX).max
>       IJ.setThreshold(0.50*maxThreshold, maxThreshold)
>       print img, "mxt=", maxThreshold
>       else:
>       maxThreshold = img.getStatistics(Measurements.MIN_MAX).max
>       IJ.setThreshold(0.20*maxThreshold, maxThreshold)
>       print img, "mxt=", maxThreshold
>
>
>      print end
>
> The threshold function works perfect, but I can't for the life of me get the
> 8 bit to run.  Only the last image in the array `dic` is converted.
>
> I've also tried:
>      for img in imp:
> ImageConverter.convertToGray8(img)
>
> But that throws an error:
>> TypeError: convertToGray8(): self arg can't be coerced to
>> ij.process.ImageConverter
>
>
> I can't figure out how to call a specific image either by window title or by
> image ID.
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> 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
| More
Print post
Permalink

Re: Trouble with active windows within a loop

Michael Schmid
In reply to this post by moxed
Hi Samantha,

IJ.getImage() returns the current image, but you don't use it and you
don't want to work on it anyhow, rather on the current image 'img' of
the loop.

IJ.run("8-bit") works on the current foreground image, which is always
the same.
You can try
   IJ.run(img, "8-bit", "");
to run the "8-bit" command on the current image 'img'.
(The last argument would be any options from a dialog; the "8-bit"
command has none, so it is an empty String)

Michael
________________________________________________________________

On 04/04/2018 22:42, moxed wrote:

> Scripting newbie here, having trouble calling windows to the front in a for
> loop. As a brief example, I'm trying to write a script (jython) that will
> loop through an array of images and convert them to 8-bit and also set a
> particular threshold.
>
> I can't seem to get the convert to 8bit command to work on all images in the
> array. It looks like only the last image is acted upon.
>
> I think I need to be able to call an image to the front, using the image
> title or ID, but I can't figure out how to do that. Any advice would be
> great.
> (In my real code there are a number of more manipulations, and also more
> than 2 files being processed)
>
>      import os
>      from java.io import File
>      from ij import IJ, Macro, ImagePlus, WindowManager
>      from ij.process import ImageStatistics as IS, ImageProcessor
>      from ij.measure import Measurements
>      from ij.process import ImageConverter
>
>
>      wt_path = "C:/wt.tif"
>      dic_path = "C:/dic.tif"
>
>      wt = IJ.openImage(wt_path)
>      dic = IJ.openImage(dic_path)
>
>      imp = [wt, dic]
>
>      for img in imp:
>          IJ.getImage()
>       IJ.run("8-bit")
>       if img == dic:
>       maxThreshold = wt.getStatistics(Measurements.MIN_MAX).max
>       IJ.setThreshold(0.50*maxThreshold, maxThreshold)
>       print img, "mxt=", maxThreshold
>       else:
>       maxThreshold = img.getStatistics(Measurements.MIN_MAX).max
>       IJ.setThreshold(0.20*maxThreshold, maxThreshold)
>       print img, "mxt=", maxThreshold
>
>
>      print end
>
> The threshold function works perfect, but I can't for the life of me get the
> 8 bit to run.  Only the last image in the array `dic` is converted.
>
> I've also tried:
>      for img in imp:
> ImageConverter.convertToGray8(img)
>
> But that throws an error:
>> TypeError: convertToGray8(): self arg can't be coerced to
>> ij.process.ImageConverter
>
>
> I can't figure out how to call a specific image either by window title or by
> image ID.
>
>
>
> --
> Sent from: http://imagej.1557.x6.nabble.com/
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

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