torch / image

An Image toolbox for Torch.
Other
208 stars 141 forks source link

Can't read pixel value precisely #202

Closed haqishen closed 7 years ago

haqishen commented 7 years ago

Hello, I'm working on semantic segmentation on PASCAL VOC dataset with Torch7+LUA5.2. I'm trying to load segmentation annotations, which have PNG format and only 1 channel. In a segmentationClass annotation, if a value of pixel is 1, that indicates this pixel belongs to class #1 , and a value of pixel is 20 indicates it belongs to class #20, etc. But when I use image.load(path, 1, 'byte') I found out that the values of pixels getting kinds of chaos, none of the pixel falls into the range [1,20] though there are only 20 classes in PASCAL VOC. And if I try image.load(path, 'double') and do :mul(255), It results the same.

I've tried to change the PNG into JPEG with 3channels(repeated the origin channel by 3 times use PIL Image with python) and load it. with image.load(path, 3, 'byte'). I'm shocked that there are many pixels with value 1,2,3,4,5.....19,20, although there are only 1 class of object in the image.

Actually, I also have tried to save the loaded PNG as a new PNG file without any change, it becomes a new image though it's object got a same shape with the origin one.

Is there any idea?

fmassa commented 7 years ago

This happens because the images from Pascal VOC Segmentation are saved with a color palette. Standard PNG loader from torch will load the images and apply the color palette, which will make the [0-20] range became RGB images.

When I faced this issue, I hacked around the loader in these lines to load PNG images without applying the color palette (sorry don't have the exact change anymore).

Another solution would be to load the images in another language without applying the color palette, and then save the images as grayscale images (it look almost as if it was all black) or save them in another format like .mat or .h5.

haqishen commented 7 years ago

Thanks, fmassa. I've found a lib can deal with this problem: https://github.com/LuaDist2/indexedpng