Read labeled image with Images.jl

Hi.
I have made segmentation labeling of images with the following tool (Labelme repo). It allows to export labels as a PNG image with every pixel as a single byte and it could be possible to load it properly with Python’s PIL and NumPy libraries (https://github.com/wkentaro/labelme/tree/main/examples/tutorial#how-to-load-label-png-file).

I’ve tried to load these PNG images with the load function but it loads the image as a matrix of RGB pixels.
What parameters could be passed to the load function to get the image loaded as a matrix of bytes?

Hello and welcome.
Have you tried Images.jl function: rawview(image)

1 Like

Untested, but RGB seems to be the means of choice.

1 Like

If we’re loading it as an RGB, it’s presumably a problem with Labelme: it should be saving the images as grayscale, not RGB.

Our image-loading libraries would load a single-channel, single-byte image as Gray{N0f8}, but you can get back to integers with rawview(channelview(img)).

2 Likes

Unfortunately, rawview is not working.

Also, I analyzed the image and found that they used 8bit format, but PNG is paletted - so if there any function in Images that allow to read palette from PNG file?

Since it’s an RGB, try rawview(channelview(Gray.(img))).

See the documentation at Home · JuliaImages if you’re still confused.

2 Likes

Also, yes, PNGFiles (which you get if ImageIO is handling your I/O) should be able to handle paletted files. https://github.com/JuliaIO/PNGFiles.jl/blob/b32b97f039f68172fe75c6084c34fa0240689a52/src/io.jl#L18-L20

2 Likes

Thanks, @tim.holy .
Usage of PNGFiles helps me.

Example of solution

using PNGFiles
img = PNGFiles.load("label.png"; expand_paletted=true) # expand_paletted=true is crucial
# img.index - contains matrix with labels
# unique(img.index) - contains all label values