Pointing to a fixed directory structure

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

Pointing to a fixed directory structure

dlraby
I am new to writing Java (2 months), but find the ImageJ a very interesting
tool, and hope to write a few plugins to "quickly" test some imaging
concepts. This work requires me to compare three images. The available
examples, i have found, show the image information being taken from the
image selected via the GUI however i need my plugin to read the contents
from a fixed directory on the host PC. The image names will also be hard
coded by the camera attached to the PC. If you have a pointer to a similar
plugin or example it would be greatly appreciated.

I have tried to read 3 bitmap images from the same directory and have been
unable to set the correct path. Could you let me know from which location
the path should be set relative to (i am running Eclipse on my PC):

plugin directory path: C:\Code\Java\MyPlugin\src\test_.java
image directory path: C:\Code\Java\MyPlugin\images\xxx.bmp

I have tried using the full system path and the relative path and neither
one seems to work.

I am trying to use, to point to and open a test image:

String imagePath = IJ.getDirectory("C:\Code\Java\MyPlugin\images\test1.bmp");
dirPath = IJ.openAsString(imagePath);
ImagePlus imp2 = IJ.openImage(dirPath);

In the above code, the "slash" direction reflects that shown on the Eclipse
tool, please let me know if is need to parse this and change the "slash"
direction.

thank you, Dean
Reply | Threaded
Open this post in threaded view
|

Re: Pointing to a fixed directory structure

ctrueden
Hi Dean,

I have tried to read 3 bitmap images from the same directory and have been
> unable to set the correct path. Could you let me know from which location
> the path should be set relative to (i am running Eclipse on my PC):
>

Backslash literals in Java must be escaped with another backslash ("\\"),
because backslash is used to represent other special characters (tab is
"\t", newline is "\n", etc.).

So in your code, try:

  String imagePath =
IJ.getDirectory("C:\\Code\\Java\\MyPlugin\\images\\test1.bmp");

It would probably also work with forward slashes.

-Curtis

On Tue, Oct 13, 2009 at 1:52 PM, Dean Raby <[hidden email]> wrote:

> I am new to writing Java (2 months), but find the ImageJ a very interesting
> tool, and hope to write a few plugins to "quickly" test some imaging
> concepts. This work requires me to compare three images. The available
> examples, i have found, show the image information being taken from the
> image selected via the GUI however i need my plugin to read the contents
> from a fixed directory on the host PC. The image names will also be hard
> coded by the camera attached to the PC. If you have a pointer to a similar
> plugin or example it would be greatly appreciated.
>
> I have tried to read 3 bitmap images from the same directory and have been
> unable to set the correct path. Could you let me know from which location
> the path should be set relative to (i am running Eclipse on my PC):
>
> plugin directory path: C:\Code\Java\MyPlugin\src\test_.java
> image directory path: C:\Code\Java\MyPlugin\images\xxx.bmp
>
> I have tried using the full system path and the relative path and neither
> one seems to work.
>
> I am trying to use, to point to and open a test image:
>
> String imagePath =
> IJ.getDirectory("C:\Code\Java\MyPlugin\images\test1.bmp");
> dirPath = IJ.openAsString(imagePath);
> ImagePlus imp2 = IJ.openImage(dirPath);
>
> In the above code, the "slash" direction reflects that shown on the Eclipse
> tool, please let me know if is need to parse this and change the "slash"
> direction.
>
> thank you, Dean
>
Reply | Threaded
Open this post in threaded view
|

Re: Pointing to a fixed directory structure

David Webster
In reply to this post by dlraby
You may need to use "\\" rather than "\" as a single slash is an escape
character in both Java and C.

On Tue, Oct 13, 2009 at 11:52 AM, Dean Raby <[hidden email]> wrote:

> I am new to writing Java (2 months), but find the ImageJ a very interesting
> tool, and hope to write a few plugins to "quickly" test some imaging
> concepts. This work requires me to compare three images. The available
> examples, i have found, show the image information being taken from the
> image selected via the GUI however i need my plugin to read the contents
> from a fixed directory on the host PC. The image names will also be hard
> coded by the camera attached to the PC. If you have a pointer to a similar
> plugin or example it would be greatly appreciated.
>
> I have tried to read 3 bitmap images from the same directory and have been
> unable to set the correct path. Could you let me know from which location
> the path should be set relative to (i am running Eclipse on my PC):
>
> plugin directory path: C:\Code\Java\MyPlugin\src\test_.java
> image directory path: C:\Code\Java\MyPlugin\images\xxx.bmp
>
> I have tried using the full system path and the relative path and neither
> one seems to work.
>
> I am trying to use, to point to and open a test image:
>
> String imagePath =
> IJ.getDirectory("C:\Code\Java\MyPlugin\images\test1.bmp");
> dirPath = IJ.openAsString(imagePath);
> ImagePlus imp2 = IJ.openImage(dirPath);
>
> In the above code, the "slash" direction reflects that shown on the Eclipse
> tool, please let me know if is need to parse this and change the "slash"
> direction.
>
> thank you, Dean
>
Reply | Threaded
Open this post in threaded view
|

Re: Pointing to a fixed directory structure

dlraby
In reply to this post by ctrueden
Thank you both, the ("\\") fixed the path and the short test plugin
generates dialog windows at points through the code and also displays the
path to the test1.bmp. However, i have been stumped by my inability to read
and display the image to verify that i have successfully imported the image
into my function. Hopefully you can help me again.

my simple test code is:

public class FileGrabTest_ implements PlugIn {

    private String title;

    ImagePlus ip1;
    private String imagePath1 = "";
    private String imagePath2 = "";
    private String imagePath3 = "";


    public void run(String arg) {
            IJ.showMessage(title, "Running the first Test");

            // line 29 in error report
            test_file_grab();
        }

    public void test_file_grab() {

         String imagePath1 = IJ.getDirectory("C:\\Users\\draby\\Documents");

         imagePath2 = imagePath1+"test1.bmp";
         imagePath3 = IJ.openAsString(imagePath2);

         ImagePlus rgbBackGround = IJ.openImage(imagePath3);

         IJ.showMessage(title, imagePath2);

         IJ.showMessage(title, "Running the second Test");

         // extract image information and pixel data
         // line 47 in error report
         ImageProcessor bg_ip = rgbBackGround.getProcessor();
         int[] bg_pixels = (int[]) bg_ip.getPixels();

         rgbBackGround.show();
         rgbBackGround.updateAndDraw();
    }
}

and the error message is:
    java.lang.NullPointerException
    at HandDet_.test_file_grab(HandDet_.java:47)
    at HandDet_.run(HandDet_.java:29)
    at ij.IJ.runUserPlugIn(IJ.java:179)
    at ij.IJ.runPlugIn(IJ.java:146)
    at ij.Executer.runCommand(Executer.java:122)
    at ij.Executer.run(Executer.java:59)
    at java.lang.Thread.run(Unknown Source)

I am sorry to bother you with what is probably trivial but i have confused
myself over the past 8 hours to the point where i cant see the "plug for the
wires". Any further assistance would be greatly appreciated.
Thank you, Dean


On Tue, Oct 13, 2009 at 12:24 PM, Curtis Rueden <[hidden email]> wrote:

> Hi Dean,
>
> I have tried to read 3 bitmap images from the same directory and have been
> > unable to set the correct path. Could you let me know from which location
> > the path should be set relative to (i am running Eclipse on my PC):
> >
>
> Backslash literals in Java must be escaped with another backslash ("\\"),
> because backslash is used to represent other special characters (tab is
> "\t", newline is "\n", etc.).
>
> So in your code, try:
>
>  String imagePath =
> IJ.getDirectory("C:\\Code\\Java\\MyPlugin\\images\\test1.bmp");
>
> It would probably also work with forward slashes.
>
> -Curtis
>
> On Tue, Oct 13, 2009 at 1:52 PM, Dean Raby <[hidden email]> wrote:
>
> > I am new to writing Java (2 months), but find the ImageJ a very
> interesting
> > tool, and hope to write a few plugins to "quickly" test some imaging
> > concepts. This work requires me to compare three images. The available
> > examples, i have found, show the image information being taken from the
> > image selected via the GUI however i need my plugin to read the contents
> > from a fixed directory on the host PC. The image names will also be hard
> > coded by the camera attached to the PC. If you have a pointer to a
> similar
> > plugin or example it would be greatly appreciated.
> >
> > I have tried to read 3 bitmap images from the same directory and have
> been
> > unable to set the correct path. Could you let me know from which location
> > the path should be set relative to (i am running Eclipse on my PC):
> >
> > plugin directory path: C:\Code\Java\MyPlugin\src\test_.java
> > image directory path: C:\Code\Java\MyPlugin\images\xxx.bmp
> >
> > I have tried using the full system path and the relative path and neither
> > one seems to work.
> >
> > I am trying to use, to point to and open a test image:
> >
> > String imagePath =
> > IJ.getDirectory("C:\Code\Java\MyPlugin\images\test1.bmp");
> > dirPath = IJ.openAsString(imagePath);
> > ImagePlus imp2 = IJ.openImage(dirPath);
> >
> > In the above code, the "slash" direction reflects that shown on the
> Eclipse
> > tool, please let me know if is need to parse this and change the "slash"
> > direction.
> >
> > thank you, Dean
> >
>
Reply | Threaded
Open this post in threaded view
|

Re: Pointing to a fixed directory structure

David Webster
In reply to this post by dlraby
I copied your code with mods to read my own test image. I was able to find
some problems.

First, by the way you use it,

imagePath3 = IJ.openAsString(imagePath2);

seems intended to return a full path file spec. But I think it actually
returns the file pointed to by imagePath2 as a ASCII string. So, I just
commented this out and used

ImagePlus rgbBackGround = IJ.openImage(imagePath2);

Then, the statement

int[] bg_pixels = (int[]) bg_ip.getPixels();

got an error relating to casting a byte type to an int type (I think). I'm
still not sure about this, but as the statement isn't needed to do the

rgbBackGround.show();

I commented it out and then the thing ran OK.

I will check so more as time allows.

David Webster
Reply | Threaded
Open this post in threaded view
|

Re: Pointing to a fixed directory structure

David Webster
In reply to this post by dlraby
OK, I checked. I input an 8-bit image so

int[] bg_pixels = (int[]) bg_ip.getPixels();

wasn't correct. When I changed to

 byte[] bg_pixels = (byte[]) bg_ip.getPixels();

What surprises me isn't not being able to upcast to an int.


On Tue, Oct 13, 2009 at 5:35 PM, dlraby <[hidden email]> wrote:

> Thank you both, the ("\\") fixed the path and the short test plugin
> generates dialog windows at points through the code and also displays the
> path to the test1.bmp. However, i have been stumped by my inability to read
> and display the image to verify that i have successfully imported the image
> into my function. Hopefully you can help me again.
>
> my simple test code is:
>
> public class FileGrabTest_ implements PlugIn {
>
>    private String title;
>
>    ImagePlus ip1;
>    private String imagePath1 = "";
>    private String imagePath2 = "";
>    private String imagePath3 = "";
>
>
>    public void run(String arg) {
>            IJ.showMessage(title, "Running the first Test");
>
>            // line 29 in error report
>            test_file_grab();
>        }
>
>    public void test_file_grab() {
>
>         String imagePath1 = IJ.getDirectory("C:\\Users\\draby\\Documents");
>
>         imagePath2 = imagePath1+"test1.bmp";
>         imagePath3 = IJ.openAsString(imagePath2);
>
>         ImagePlus rgbBackGround = IJ.openImage(imagePath3);
>
>         IJ.showMessage(title, imagePath2);
>
>         IJ.showMessage(title, "Running the second Test");
>
>         // extract image information and pixel data
>         // line 47 in error report
>         ImageProcessor bg_ip = rgbBackGround.getProcessor();
>         int[] bg_pixels = (int[]) bg_ip.getPixels();
>
>         rgbBackGround.show();
>         rgbBackGround.updateAndDraw();
>    }
> }
>
> and the error message is:
>    java.lang.NullPointerException
>    at HandDet_.test_file_grab(HandDet_.java:47)
>    at HandDet_.run(HandDet_.java:29)
>    at ij.IJ.runUserPlugIn(IJ.java:179)
>    at ij.IJ.runPlugIn(IJ.java:146)
>    at ij.Executer.runCommand(Executer.java:122)
>    at ij.Executer.run(Executer.java:59)
>    at java.lang.Thread.run(Unknown Source)
>
> I am sorry to bother you with what is probably trivial but i have confused
> myself over the past 8 hours to the point where i cant see the "plug for
> the
> wires". Any further assistance would be greatly appreciated.
> Thank you, Dean
>
>
> On Tue, Oct 13, 2009 at 12:24 PM, Curtis Rueden <[hidden email]> wrote:
>
> > Hi Dean,
> >
> > I have tried to read 3 bitmap images from the same directory and have
> been
> > > unable to set the correct path. Could you let me know from which
> location
> > > the path should be set relative to (i am running Eclipse on my PC):
> > >
> >
> > Backslash literals in Java must be escaped with another backslash ("\\"),
> > because backslash is used to represent other special characters (tab is
> > "\t", newline is "\n", etc.).
> >
> > So in your code, try:
> >
> >  String imagePath =
> > IJ.getDirectory("C:\\Code\\Java\\MyPlugin\\images\\test1.bmp");
> >
> > It would probably also work with forward slashes.
> >
> > -Curtis
> >
> > On Tue, Oct 13, 2009 at 1:52 PM, Dean Raby <[hidden email]> wrote:
> >
> > > I am new to writing Java (2 months), but find the ImageJ a very
> > interesting
> > > tool, and hope to write a few plugins to "quickly" test some imaging
> > > concepts. This work requires me to compare three images. The available
> > > examples, i have found, show the image information being taken from the
> > > image selected via the GUI however i need my plugin to read the
> contents
> > > from a fixed directory on the host PC. The image names will also be
> hard
> > > coded by the camera attached to the PC. If you have a pointer to a
> > similar
> > > plugin or example it would be greatly appreciated.
> > >
> > > I have tried to read 3 bitmap images from the same directory and have
> > been
> > > unable to set the correct path. Could you let me know from which
> location
> > > the path should be set relative to (i am running Eclipse on my PC):
> > >
> > > plugin directory path: C:\Code\Java\MyPlugin\src\test_.java
> > > image directory path: C:\Code\Java\MyPlugin\images\xxx.bmp
> > >
> > > I have tried using the full system path and the relative path and
> neither
> > > one seems to work.
> > >
> > > I am trying to use, to point to and open a test image:
> > >
> > > String imagePath =
> > > IJ.getDirectory("C:\Code\Java\MyPlugin\images\test1.bmp");
> > > dirPath = IJ.openAsString(imagePath);
> > > ImagePlus imp2 = IJ.openImage(dirPath);
> > >
> > > In the above code, the "slash" direction reflects that shown on the
> > Eclipse
> > > tool, please let me know if is need to parse this and change the
> "slash"
> > > direction.
> > >
> > > thank you, Dean
> > >
> >
>