Suppose I want to read an indexed image as it is( not the 3 channel 24 bit images) I wish to read and modify the color palette of an indexed image. In opencv I haven't come across any such functions which extract the color palette of an image! Also I would like to know the datatype of the palette. i am coding in c using opencv Any help?
-
+1 Me too, keen to know on that... – Wazy Jun 21 '12 at 10:29
-
Can you post a link to an example paletted image? I made a 256 color paletted png to experiment on your question but when I load it, the data shows up as the usual 3-byte set. – KobeJohn Jun 21 '12 at 10:44
-
http://www.pictureshoster.com/files/6v97n5jzbsqcdft8z9.gif – shiladitya Jun 21 '12 at 10:52
-
this is my sample image. its a single channel 8 bit image – shiladitya Jun 21 '12 at 10:52
-
sorry for the bad link! http://shila1608.my3gb.com/mandril_color.gif .. this should work! – shiladitya Jun 21 '12 at 11:27
-
@shiladitya How did this work out? I'd like to know if you found a way to work with indexed palette images in OpenCV. – KobeJohn Jun 26 '12 at 06:19
2 Answers
Thanks for the image link. First, OpenCV doesn't support GIF (as Ameya005 linked). However there are other indexed palette image formats. It couldn't get an indexed palette image in OpenCV so unless I have missed something, I don't think you are going to be able to work with indexed palettes directly in OpenCV. It sounds like you need an alternative solution. Why do you need to work directly with the indexed palettes?
The second option below though would probably let you have the indexes in OpenCV (the palette would be lost) if that's all you need.
Here is what I've tried:
Load a paletted PNG (gets converted to 3-channel color)
import cv2
im = cv2.imread("mandril_color.png")
im.shape # returns (512, 512, 3) so it's been converted to 3-channel color
Load a paletted GIF (gets converted to grayscale)
I guess this is converting to grayscale with the index used as intensity, but I haven't verified it.
import cv2
import Image
import numpy as np
im_pil = Image.open("mandril_color.gif")
im_cv = np.asarray(im_pil)
im_cv.shape # returns (512, 512) so it's become grayscale
Create a paletted image from scratch in OpenCV
I haven't found any way to do this.

- 7,390
- 6
- 41
- 62
Well, I don't think OpenCV has any function to do this. The cvLoadImage() or imread() functions use libpng for codecs to directly read images.
Check the documentation for further information

- 31
- 1
- 5