How to convert/save a 3D watershed segmented RGB image stack to a .txt file by assigning unique integers to each 3D label

Hi everyone,
I have a stack of RGB images which have been labeled by applying 3D watershed algorithm, each grain (region) in the dataset has a unique identifier (color) associated with it. I want to store the information of each 3D labeled grain in a text (.txt) file. As after cancatenation of 2d image sections, each grain(color) takes 3D form and comprises of many voxels. To explain, consider a red color grain comprises of 5 voxels and a green color grain comprises of 4 voxels, then i want to assign a unique integer to every color grain, so i assign for example here the integer '230' to each voxel of red color grain and integer '150' to each voxel of green color grain and...... so on (different integer for different color grain), thus the frmat of the text file would be 230 230 230 230 230 150 150 150 150.......
here 230 230 230 230 230 is for five voxels of red grain and 150 150 150 150 is for four voxels of green grain. similarly, i want to apply this procedure to all the 3D grains in my image stack and save the assigned integers in a .txt file. The dimensions of my sample volume are 400x400x120. some images of my dataset are uploaded here for information http://www.sendspace.com/file/ifu50s . I am fairly new to matlab and searched several times for it solution but unfortunately failed. Can any one guide me please? Any help will be highly appreciated!
Thanks

2 Comments

I would like to help, but I don't particularly want to download a file from an untrusted source. Can you provide a small example of input/operations/expected outputs?
Note: the most-active volunteers will seldom download .rar files. They are more likely to download .zip files, but they prefer plain images posted to a system that allows them to preview what they would be downloading.

Sign in to comment.

 Accepted Answer

See my color segmentation demos http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862. Or if you're lucky, you can get by with rgb2ind to do the color segmentation. Then, once you've segmented your image into two binary images of all the red particles, and all the green particles, you can multiply the binary images by some number (the intensity that you want them to be like 230 or 150) and recombine with cat(3, r, g, b) to construct your RGB image.
If that's not what you want, then upload two simple 2D images of input and output, to illustrate what you want.
I didn't quite understand what you want since the subject doesn't exactly agree with the body of your message. Looking at your subject line, I'd recommend the function label2rgb() to convert an already-labeled image into an image where the labeled objects have a color, though you'd need to know which object number was which class (red or green).

15 Comments

Hi Image Analyst,
thank you very much for your response to my quesion.
Actually my question is, i have a stack of 120 labeled images which i got after 3D watershed segmentation and converted to RGB by using label2rgb() function of matlab, now i want to save this stack of images (3D labeled matrix) as a text image (in a single text file) which contain information of each RGB labeled 3D grain in my stack. The dimensions of my sample volume are 400x400x120. A sample 2D image of my stack can be seen here
Here you can see each grain is labeled with a different color
I hope you will understand my question now.
Thanks
Is each 2D slice labeled independently? If so, it's label may not match up with the slices above and below. That would be a major problem. Or did you label the 3D binary volume? And why on earth would you want to save some gigantic 3D volumetric image as text? Of what possible use could that be?
The slices are not labeled independently, rather i have labeled the 3D binary volume by applying 3D watershed segmentation algorithm followed by a 3D euclidean distance transformation. So each label has the same identifier as its corresponding labels in other slices.
The answer to why? is, actually i want to input this volume into Monte carlo method to simulate grain growth and our Monte carlo simulation method takes input volume as a text file which contain unique integer labels for each 3D grain. For example, if a 3D grain has '3215' as its integer label then all of its voxels will have the same integer as label just like 3215 3215 3215 3215... for all the voxels of this concerned grain and similarly for all the other 3D grains.
Thank you very much for your response and help.
So it's just a label image - which is essentially what you have already! (At least that's what the above image looks like)
i have converted the labeled images into RGB by applying matlab's label2rgb() function.
But you don't want to do that!!!!
You already have a label image - which is exactly what you want for your monte carlo sim.
Thats fine, but i do not know how to write these 3D labels to a .txt file. As i am fairly new to matlab, please guide me how to do that.
thanks
fopen / fwrite / fclose
You will need to figure out how to store the size information and what not.
If you really really need a text file because that is all your simulation program can input, then I'd use fprintf, but maybe fwrite will do it also. I just always use fprintf for text files, and save fwrite for the binary files.
Image Analyst and Sean de Wolski,
Thank you very much both of you for guidance. fprintf() works well.
Thanks
Hi, Thank you for helping me. I have saved the labels as text file thats fine for simulation purposes. However, to visualize my 3D data alongwith simulation i want to save the labeled dataset which i got after applying label2rgb() function with all the color information, just as above shared image. I tried many times but the visualization seems that all the color information is discarded.
How can i save/write the labels to text file along with their corresponding color information?
Thanks for your help
The default colormap is jet. And I think that's with 64 different colors. You can pass in a different one if you want, maybe one with more colors. So for each blob number, you can just read off it's color from jet. Jet will repeat over and over again as you get more regions. For example, with jet(64), blob1Color = blob65Color = blob129Color, = jet(1,:), etc., and blob2Color = blob66Color, =jet(2,:), etc. So you can just write out the color along with the label number in your fprintf.
I have used colorcube map to apply color to my labeled image stack, thus each 3D label(blob) has a unique color associated with it. i have used the following code to write this colorlabeled dataset to text file(please guide me is this the right code to acheive my goal, as i am new to matlab):
clc;
clear all;
n=200;
fidIn=0;
fidOut=fopen('labeled.txt', 'w');
for i=1:n
strFileName=strcat('section',num2str(i),'.tiff');
% here 'section' is the colored RGB image as i have shared above
fidIn=imread(strFileName);
[M,N]=size(fidIn);
x = repmat('%d ',1,(N-1));
fprintf(fidOut,[x,'%d\n'],fidIn);
end
fclose(fidOut);
When i visualize this resulting text file then it looks like
please guide me how to modify this code to get the actual color information also.
Thanks
Hi image analyst,
Looking for your precious advice and guidance please!
Thank you very much

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!