How to divide a picture into several parts

Hello to all,

I work on images presenting porosities and I would like to divide an image into several zones to analyze them separately. Is it possible on Fiji to create a network of rectangles which will allow to divide one picture?


I saw that is possible to consolidate several images into the only one and I found the tool "Montage to stack " but theses tools do not seem to be really adapted to what I look for.

Thank you for your help

@Efiji

@ctrueden had answered a similar question on the listserv awhile back… He presented this macro that will divide up an image into equal parts chosen by the user - I just updated it a bit using Script Parameters:

#@ int(label="How many divisions (e.g., 2 means quarters)?") n

// This macro chops an image into NxN tiles, where N is the number
// of divisions chosen by the user.

id = getImageID(); 
title = getTitle(); 
getLocationAndSize(locX, locY, sizeW, sizeH); 
width = getWidth(); 
height = getHeight(); 
tileWidth = width / n; 
tileHeight = height / n; 
for (y = 0; y < n; y++) { 
offsetY = y * height / n; 
 for (x = 0; x < n; x++) { 
offsetX = x * width / n; 
selectImage(id); 
 call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY); 
tileTitle = title + " [" + x + "," + y + "]"; 
 run("Duplicate...", "title=" + tileTitle); 
makeRectangle(offsetX, offsetY, tileWidth, tileHeight); 
 run("Crop"); 
} 
} 
selectImage(id); 
close(); 

You can adapt this script as you see fit. Here are some helpful links on Scripting in ImageJ/Fiji:

Hope this helps!

eta :slight_smile:

Too - if you want to then merge those images into a stack at the end… after the close(); call - just add this line: run("Images to Stack", "name=Stack title=[] use"); and all those tiles will be merged into a single stack. :slight_smile:

3 Likes

Thank you for theses tricks ! I think that they are going to help me :wink:

1 Like

Hello,

Thank you for sharing the script, it works well. However do you know if it applies to stacks as well? I’ve been trying to run it on a stack of images but it only ever gives me the first image and the rest of the images in the stack just disappear.

Thanks!

@Harristotle

You’ll have to modify the script to get things working on a stack (working slice-by-slice) - it will complicate things for sure. So what exactly is it you wish to do?

We just have a stack of the same images with different markers that we want to split up into a few sections so that the cells can be labelled by different people. If it doesn’t work directly on the stack with the above function I could always just do it on each individual image in the stack since we have that and then stack it together at the end to get me my divisions with every channel per stack. Not sure if that makes sense or is clear.

Thanks

1 Like

@Harristotle

It’s not really clear - sorry. I suppose - you can convert your Stack to Images and then process each individual image. How you stack it together in the end - who goes with whom - that I don’t know. But you should be able to do it. Follow those helpful links above to modify the script. If you need specific help on how to carry something out - post code and a clear/concise example - and we can help you with more specific details.

Hi all, thank you for uploading this. It’s been very helpful so far. One problem I have is that all of the windows generated from the macro have the same title. Could someone please help in guiding me to how I can label them as separate files (e.g. Image_1, Image_2, Image_3, etc…)? I would love to to this in the macro instead of doing it manually :slight_smile:

Hi
@kkulinski

run("Boats");
id = getImageID(); 
title = getTitle(); 
n=2;
getLocationAndSize(locX, locY, sizeW, sizeH); 
width = getWidth(); 
height = getHeight(); 
tileWidth = width / n; 
tileHeight = height / n; 
i=0;
for (y = 0; y < n; y++) { 
offsetY = y * height / n; 
 for (x = 0; x < n; x++) { 
offsetX = x * width / n; 
selectImage(id); 
 call("ij.gui.ImageWindow.setNextLocation", locX + offsetX, locY + offsetY); 
tileTitle = title + " [" + x + "," + y + "]"; 
 run("Duplicate...", "title=" + tileTitle); 
makeRectangle(offsetX, offsetY, tileWidth, tileHeight); 
 run("Crop");
rename("boat_"+i);
i++;
} 
} 
selectImage(id); 
close(); 

Great, thank you! It’s working just fine now.

Hi all, I had a similar question (I wanted to divide a STACK into several parts - not just the image and I wanted to make sure everything was labelled uniquely in a logical manner). I modified scripts uploaded by @ctrueden and Matthew above: I have called the Macro “ChopStack”. I really have no idea what I am doing and am shocked it worked for me. I hope this will be helpful to someone. Now I just have to figure out how to recreate my two channel stacks for each part.

n = getNumber(“How many divisions (e.g., 2 means quarters)?”, 2);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
Stack.setPosition(1, 1, 1);
getDimensions(w, h, channels, slices, frames);
Stack.getPosition(channel, slice, frame);
print("Position (before): “+channel+”, “+slice+”, "+frame);
Stack.setPosition(channels, slices, frames);
Stack.getPosition(channel, slice, frame);
print(“Position (after): “+channel+”, “+slice+”, “+frame);
Stack.setPosition(1, 1, 1);
for (a=1; a<=channels; a++) {
for (b=1; b<=slices; b++) {
Stack.setChannel(a);
wait(50);
Stack.setSlice(b);
wait(50);
width = getWidth();
height = getHeight();
tileWidth = width / n;
tileHeight = height / n;
id = getImageID();
title = getTitle();
getLocationAndSize(locX, locY, sizeW, sizeH);
i=1;
for (y = 0; y < n; y++) {
offsetY = y * height / n;
for (x = 0; x < n; x++) {
offsetX = x * width / n;
selectImage(id);
call(“ij.gui.ImageWindow.setNextLocation”, locX + offsetX, locY + offsetY);
tileTitle = “[” + x + “,” + y + “]” + title;
run(“Duplicate…”, “title=” + tileTitle);
makeRectangle(offsetX, offsetY, tileWidth, tileHeight);
run(“Crop”);
rename(a+”_”+b+tileTitle);
i++;
}
}
selectImage(id);
}
}

1 Like