functionuser-defined-functionsimagejimagej-macro

Can't run function in ImageJ


I am a beginner with imageJ. I wanted to start using scripting tools, I started with writing a simple function as shown below, to mark a single pixel as white on the image already loaded into imageJ

function MarkPixel(x,y) {
   setColor("white");
   setLineWidth(1);
   drawLine(x,y,x,y);
}

and I saved this as a file named 'MarkPixel.ijm' in the directory 'imageJ/macros'

Now I run imageJ, opened an image, start macro->interactive interpreter and run my function MarkPixel(0,0). But I get the error that says Statement cannot begin with ')'.Then instead of interactive, I started a new macro window and entered my function there and tried running it. But still I get an error but different one this time, which says

Error:      Undefined identifier in line 1:
<MarkPixel> ( 0 , 0 ) ;

Why this is not working for me? I feel like I will have to compile this function at some point, I have been reading through the documentation but couldn't find anything clearly yet.


Solution

  • Here is an ImageJ-macro that should do what you want:

    newImage("Test", "8-bit black", 256, 256, 1);
    point2white( 128, 128 ); // call the function
    run("To Selection"); // for better visibility only
    exit();
    //
    function point2white( xPos, yPos ) {
       setForegroundColor(255, 255, 255);
       makePoint( xPos, yPos );
       run("Draw", "slice");
    }
    

    Paste the above macro code to an empty macro window (Plugins >> New >> Macro) and run it.