Simplicity
Simplicity

Reputation: 48956

Saving a labeled image

I have used the function bwlabel to label an image as follows:

[L, num] = bwlabel(I, 8);

For the the image I'm working with, say I have got 20 labels. Thus, when you run unique(L), you will get a number list up to 20.

The issue is that when I imwrite(L), and the run unique on that new written image, I get the values of the pixels as their intensity in the original image I labeled, and not as a labeled image.

How can I imwrite that labeled image, while keeping it as a labeled image?

Upvotes: 1

Views: 1348

Answers (1)

Shai
Shai

Reputation: 114886

You can write an image as an indexed image (with a colormap)

imwrite( L, rand( 256, 3 ), 'myIndexedImage.png' ); % wrirte

To read

[L cmp] = imread( 'myIndexedImage.png' ); % and you can ignore the colormap

Alternatively, convert to uint8 and save labels as gray scales (assuming you do not have more than 256 labels)

imwrite( uint8(L), 'myLabels.png' );

To read

L = imread( 'myLabels.png' );

Be careful NOT to use any compressive format (e.g., jpg).

Upvotes: 2

Related Questions