Image generation - Donut with a Gaussian cross-section

imagej
macro

#1

Hello,

I am new to ImageJ and I do not find the explanation to the following issue. I would like to draw a “donut” with a Gaussian radial cross-section. I have written the following macro, but instead of getting the expected donut, I do get a saturated disc. Can anyone please help me spot my mistake?

ScreenSize=0.2;        //in m
ScreenDistance=0.2;    //in m
AngleIn=0.1;        //in rad

Amplitude=255;
FRD=1;

ImageResolution=300;            //in dpi
m2px=ImageResolution/0.0254        //from m to px
ImageSize = ScreenSize*m2px;    //in px

r0=ScreenDistance*tan(AngleIn)*m2px;    //in px

start = getTime();

xcenter = ImageSize/2;
ycenter = ImageSize/2;

newImage("FRD", "GRAY8", ImageSize, ImageSize, 1);
for (y=0; y<ImageSize; y++) {
    for (x=0; x<ImageSize; x++) {
        dx = x - xcenter; 
        dy = y - ycenter;
        r = sqrt(dx*dx + dy*dy);
        v = Amplitude * exp((-(r-r0)^2)/(2*FRD^2));
        putPixel(x, y, v);
    }     
    //if (y%250==0)
    updateDisplay();
}
showStatus(round((ImageSize*ImageSize)/((getTime()-start)/1000)) + " pixels/sec");

Any help would be greatly appreciated!

Best regards
Mat


#2

Hi Mat,

You could also use the Process > Math > Macro… command to draw such donuts, e.g.

newImage("Untitled", "8-bit black", 256, 256, 1);
run("Macro...", "code=v=255*exp(-((d-50)*(d-50)/w)/2)");

Sincerely,
Jerome.


#3

Thank you Jerome!
It turns out that ImageJ does not what I expect with the “A^2” notation. If I write “A*A” then my previous case also works.
What does ImageJ does then with the “^” operator?
Best regards
Mat


#4

Hi,
The ^ operator is bitwise XOR. For powers, use the pow(base, exponent) function.
Jerome.


#5

Hi Jerome,

Thanks so much for your code above. I need to manipulate and I’m having trouble understanding why your code works. What are d and w and where/how are they defined?

Thanks,
Shellie


#6

They’re explained in the interactive version of the command (Process > Math > Macro…), see also the user guide:

  • d: distance from center
  • w: image width