0

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.

fahd
  • 141
  • 7

1 Answers1

1

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.

Herbie
  • 143
  • 5
  • Thank you, it is working. I even removed run, exit commands, it still works - where they really important? How ever I still want to be able to call this function anywhere just like you called 'makePoint()' in your macro command, How can we make that possible? Do we have to define the function everytime? – fahd Jan 05 '23 at 12:39
  • Thanks for introducing me to function makePoint() - Hoever it works only if I command run("Draw", "Slice" ) followed by it or atleast run("Draw"). Where as if I use my function drawLine(x,y,x,y) it does not need anything else to follow it. Why is it so? – fahd Jan 05 '23 at 12:57
  • As I've commneted, run("To Selection"); is for better visibility only. Using exit(); is good coding practice. – Herbie Jan 05 '23 at 13:48
  • Macro-code functions are not made for being called like you want it to. Use a macro that can be stored and called by other macros. – Herbie Jan 05 '23 at 13:51
  • makePoint( xPos, yPos ); creates a point-selection that needs to be filled by run("Draw", "slice");. Of course you may use instead drawLine( xPos, yPos, xPos, yPos);. – Herbie Jan 05 '23 at 13:53
  • So if I need to have a function(or I call in macro with arguments), I need to define it every time in the macro code I write. And there is no way of installing functions like can be done for macro, so it acts like buit-in. Thank you for the clarification! – fahd Jan 05 '23 at 14:02
  • Instead of using functions that always belong to a macro, use a macro that performs what the function should do. You may then call this stored macro from any other macro. That's the way to go with macros... – Herbie Jan 05 '23 at 14:11
  • but problem is I need the argument/inputs to specify 'where', each time - which I believe cannot be put into a macro that can be called quickly – fahd Jan 05 '23 at 14:27
  • Of course you can, using the runMacro(name, arg) macro function. Please make sure that you are familiar with the basics of ImageJ-macro coding. – Herbie Jan 05 '23 at 14:34
  • Thank you! I am beginner. Was looking only at 'funtions' sections of official documentation with the assumption my solution is only there, and I missed even this basic information. Looking forward to going through the whole documentation on 'Macro language', thanks again! – fahd Jan 05 '23 at 15:12