How do I remove slices from a stack, then put it back at the correct location

Hi all,

Hope you’re going well, with all this shit!

So here is what I want to do : I have several time-lapse stacks with random blurry acquisitions. I need to register these movies (with multistackreg). Registration can not be done properly with the blurry images, so I wrote a macro to locate these blurry images (based on standard deviation or kurtosis). First thing I tried was to locate, delete these frames and replace it with black frames, in order not to lose the temporal information (because I do tracking on these movies). The issue is that registration can not be done properly with the black frames too. So what I want now is to locate, delete, replace with black frames and then, remove these black frames to do the registration, then put it back in the stacks. But I have troubles to save these black frames with the correct label so that I can put them back automatically at the good location in the stack after. I tried to add labels before removing the slices, then put back the back frames at the end of the movie and “sort by label”, but it doesn’t work.

I will paste my code here and it would be very nice if someone cold help me to achieve this.

Thank you all!

file1 = getDirectory(“input”);
list1 = getFileList(file1);
n1 = lengthOf(list1);

file2 = getDirectory(“output”);
list2 = getFileList(file2);

sdevThr=12.0;
setBatchMode(false);

for (j = 0; j < n1; j++) {
name = list1[j];
open(file1 + list1[j]);

run(“Duplicate…”, “title=temp duplicate”);
run(“8-bit”);
i=1;
do {
setSlice(i);
List.setMeasurements;
sd=List.getValue(“StdDev”);
if (sd<sdevThr) run(“Delete Slice”) ; if (sd<sdevThr) run(“Add Slice”) ; else i++;
} while (i<=nSlices);
setBatchMode(false);
setSlice(1);
saveAs(“tiff”, file2 + name);
close();
close();
}

Hi Azael,

Can’t really help you with your code but have you considered putting an image that is not black and contains your registration markers?

If you could upload a movie, we could have a look. If the registration markers don’t move too much between frames, you could try a median intensity projection, for example?

M.

Hi MatthieuV,

Thank you for your answer. Indeed I thought about replacing, for instance, the blurry slices with the following ones, but still, the problem is that I have several movies and several slices to replace in each one, but at random locations. So I need to know, after replacement, where exactly are all the replaced slices in order not to biais the tracking I’ll do after.

But your suggestion make me think I could try something : to replace the blurry slice with the following one, in order to keep registration markers, then do the registration and save the “transformation file”, then apply this registration file on the same movie but with the black slice instead, so that when I’ll do the tracking I will know where are the gaps.

I’ll come back to confirm that it works :wink:

Still, if someone knows a good way to automatically save duplicated slices with the correct label so that I can put them back into the movie automatically, at the good location, would be very nice!

:slight_smile:

How about using an array to note which slices are original and save the array as a text file along the image stack? The array can be as simple as 0 for not-changed or 1 for changed, or involved as a number that indicates where you got the stand-in slice from, with a checksum to verify that the text file indeed belongs to the stack with the same name…

Hi eljonco,

Indeed, it is something like that I was looking for, just don’t know how to generate a file that automatically write which are the original slices/removed slices and that could be used after the registration to insert news slices where they have been removed (again, automatically). Another trick could be, as suggested by Matthieu, to replace with the following slice, but by adding something on the slice (like a big blight square…), so that after registration I can detect automatically the replaced slices because their intensity data would be different from the other thanks to the bright square…

The text file could hold the slice numbers that you deleted. Suppose your stack has 1,2,3,4,5,6,7,8 as slices and you need tot delete slices 1,2,5 and 7. The stack then contains 3,4,6,8. A text file containing “1,2,5,7” would ask for insertion of a black slice (or an interpolated one for that matter) twice before the first slice (actually, before the first, then before the second slice) of the current stack, then your stack is 1,2,3,4,6,8. Then insert a slice before the fifth, your stack becomes 1,2,3,4,5,6,8 and finally insert before the seventh slice, so you have 1,2,3,4,5,6,7,8 again. See the functions list on how to read and write text files.

Ok, I think your method is the more elegant way to achieve this. But since I am a bit lazy these times and can not take the risk to spent to much time understanding how to use theses trays, and since the second method I had (duplicating then replacing with the following slice) is not simple because I have to specify every times the window to select, I found a much more simpler method that should work : when a blurry slice is detected, I simply – go to the previous slice – copy – go to next slice – past. I will save the registration file generated with this movie and then apply it on the movie with the black slices.

Thanks you a lot for your attention, and it makes me realize I really need to learn how to write text files and use the informations they contain to manipulate a stack :wink: