save RGB image as a single layer/channel

19 views (last 30 days)
I have an RGB image which is an n x n x 3 image, corresponding to the Red Green and Blue layers/channels respectively.
Using imshow(rgbimage) I can see the image I want to see, but when I use imwrite(im2uint16('pathtofile.tiff')) to save it as a tiff file, when I open it in ImageJ it displays the channels separately. I have to go into image>RGB color to "merge" the channels.
How can I save it from MATLAB directly as a 2D color tiff image? Is it as simple as just averaging each r,g,b element?

Accepted Answer

Image Analyst
Image Analyst on 3 Jan 2021
Edited: Image Analyst on 3 Jan 2021
You need to pass an image variable to im2uint16, not a filename string. If the array is uint16 it will leave it alone. If the image is uint8 in the range 0 to 255, it will multiply the values by 257 (yes, 257, not 256 - just try it).
This works fine:
rgbImage = imread('peppers.png');
imshow(rgbImage);
image16 = im2uint16(rgbImage);
imwrite(image16, 'delete me.tif');
It opens in ImageJ as an RGB image though it does have a scrollbar for each color channel for some reason. Not sure why - might be just some kind of ImageJ quirk.
  1 Comment
AbioEngineer
AbioEngineer on 3 Jan 2021
Yes, I meant to pass the variable, and have the full filename as the file name. Yes, that is exactly the issue. Iguess imageJ opens all rgb tiff images like that. It's weird because I have a tiff stack of 5 RGB tiff images that ImageJ can open as 5 layers in a stack, instead of 15. So I'm not sure anyone can answer why imageJ opens a single rgb tif as a 3-layer image, but a multi-layer rgb tif stack with each layer as a composite RGB layer.

Sign in to comment.

More Answers (0)

Categories

Find more on Denoising and Compression in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!