SEM Colouriser

File

Scanning electron microscope images look amazing but the simple greyscale look can get dull. This macro provides a method for automatically recolouring an SEM image, based on the image structure, to give it some colour.

SEM_Colouriser.ijm

Dialog.create("Greyscale gradient colouriser");
    Dialog.addNumber("Smoothing:", 4, 0, 5, "px");
    Dialog.addNumber("Hue power factor:", 0.5, 3, 5, "");
Dialog.show();
    smoothing=Dialog.getNumber();
    rpow=Dialog.getNumber();

src=getImageID();
w=getWidth();
h=getHeight();

newImage(getTitle()+"_colourised", "RGB Black", w, h, 1);
res=getImageID();

setBatchMode(true);

selectImage(src);
run("Duplicate...", "title=tmp");
tmp=getImageID();
run("32-bit");
run("Select All");
run("Copy");
run("Gaussian Blur...", "sigma="+smoothing+" slice");
source=newArray(getWidth()*getHeight());

newImage("tmp2", "RGB Black", w, h, 1);
tmp2=getImageID();
run("HSB Stack");
run("32-bit");
setSlice(3);
setPasteMode("Copy");
run("Paste");

for (x=1; x<w-1; x++) {
    showProgress(x/w);
    showStatus("Colouriser...");
    slicedata=newArray(h);
    for (y=1; y<h-1; y++) {
        selectImage(tmp);
        xs=0;
        ys=0;
        ss=0;
        for (a=0; a<3; a++) {
            for (b=0; b<3; b++) {
                v=getPixel((x-1+a), (y-1+b));
                xs=xs+(a-1)*v;
                ys=ys+(b-1)*v;
                ss=ss+v;
            }
        }
        xa=xs/ss;
        ya=ys/ss;
        angle=atan2(xa, ya);
        hue=255*angle/(3.1415926*2)+128;
        sat=pow(abs(xa)+abs(ya), rpow);
        selectImage(tmp2);
        setSlice(1);
        setPixel(x, y, hue);
        setSlice(2);
        setPixel(x, y, sat);
    }
}
selectImage(tmp2);
setSlice(2);
run("Select All");
getRawStatistics(area, mean, min, max);
run("Multiply...", "value="+255/max+" slice");
run("8-bit");
setSlice(3);
run("Select All");
getRawStatistics(area, mean, min, max);
run("Subtract...", "value="+min+" slice");
run("Multiply...", "value="+255/(max-min)+" slice");
run("8-bit");
run("RGB Color");

selectImage(tmp);
close();
selectImage(tmp2);
run("Select All");
run("Copy");
close();
selectImage(res);
run("Paste");

ImageJ Macros Gallery