How to convert 3D color stack to 3D gray stack?


I have a color image stack (along the z-direction) with the size of HxWx3xZ and I want to convert to the gray image stack with the size of HxWx1x(3*Z).

Could you please help me to get this conversion using ImageJ? (code in Java is also fine)

I want to extend this to higher dimensions like HxWxZxT -> HxWx1x(ZT) and HxWx3xT -> HxWx1X(3T) and HxWx3XZxT -> HxWx1x(3ZT) and this input formats are required since my machine learning model accepts input of the format HxWx1XN i.e only grayscale images.
H = image height
W = image weight
Z = image depth (in z-direction)
T = image time series samples (in time-direction)
C = 1 for gray scale and 3 for color image
N = (mini) batch size of the machine learning model

Could you please suggest a method to go back to original image format (i.e HxWx1x(3*Z) -> HxWx3xZ)?


Hello Bobby -

Does Image > Hyperstacks > Hyperstack to Stack
do what you want?

I believe that the above will still work for you.

Image > Hyperstacks > Stack to Hyperstack... should
do it.

Thanks, mm

Thank you mountain_man for your quick reply.

Yes, ‘Hyperstack to stack’ is converting HxWxCxZxT to HxWxCx(ZT) which is an image stack and now use the function ‘stack to images’ will give us the (ZT) separate images of size HxWxC. Now use the split channel to divide each color image to grayscale images HxWxC -> C images of HxW.

But I am looking for a function definition (in Java) that can convert to direct grayscale images without splitting/saving the images. (example: HxWxCxZ to HxWx1x(C*Z))?

This is due to the limitation of the machine learning model which is trained with the input of HxWx1xN but I want to show this plugin can take any input image dimension (which is different than the existed machine learning ImageJ plugins till now) and will use this new function to convert into the machine learning model input format.

In the reverse direction, we can use ‘merge channel’ and ‘Images to stack’ and ‘image -> stack -> tools -> concatenate’ functions that can give HXWx1 of C colors to HxWxC, HxWxC of Z images to HxWxCxZ and
HxWxCxZ of T stacks to single hyperstack of HxWxCxZxT size respectively.