Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV reads an image as a 3-channel image while PIL reads the same image as a 1-channel image

I'm trying to figure out the NYU-v2 for semantic segmentation dataset in this Repository. I'm having a hard time trying to understand how the image labels have been stored.

For example, given the following image :

enter image description here

The corresponding label image is:

enter image description here

Now if I open the label image in OpenCV it reads this image as an RGB image:(wrongly, though. Even tried -1 as the second argument in imread)

import cv2
import numpy as np

img = img = cv2.imread(labelImage)

img.shape
 >>> (468, 625, 3)

np.unque(img)
 >>> array([  0,  64, 128, 192, 224], dtype=uint8)

Whereas, if I open this same file in PIL:


from PIL import Image
import numpy as np


img = np.array(Image.open(labelImage))

img.shape
 >>> (468, 625)

np.unique(i)
 >>> array([  0,   2,   4,  13,  25,  39, 255], dtype=uint8)

Now the result that PIL shows is correct, and np.unique confirms this as each image in the NYU dataset has labels ranging from 0-39(255 is ignored), where each object is given a label in the 2-d space.

Now the real question. How is the author storing the images such that they show up as color images in my OS's image viewer and OpenCV, but show up as single-channel images in PIL?

like image 251
Rafay Khan Avatar asked Dec 23 '22 19:12

Rafay Khan


1 Answers

It is stored as a palette (indexed) image. See here. OpenCV doesn't really like palette images and just converts to RGB.

If I dump it with ImageMagick in Terminal, you can see the palette (under Colormap) and the 7 colours under Histogram:

identify -verbose YourImage.png

Sample Output

Image: YourImage.png
  Format: PNG (Portable Network Graphics)
  Mime type: image/png
  Class: PseudoClass
  Geometry: 625x468+0+0
  Units: Undefined
  Colorspace: sRGB
  Type: Palette
  Base type: Undefined
  Endianess: Undefined
  Depth: 8-bit
  Channel depth:
    Red: 8-bit
    Green: 8-bit
    Blue: 8-bit
  Channel statistics:
    Pixels: 292500
    Red:
      min: 0  (0)
      max: 224 (0.878431)
      mean: 64.0019 (0.250988)
      standard deviation: 87.3558 (0.342572)
      kurtosis: -1.23408
      skewness: 0.749894
      entropy: 0.746999
    Green:
      min: 0  (0)
      max: 224 (0.878431)
      mean: 37.0024 (0.145107)
      standard deviation: 68.3573 (0.268068)
      kurtosis: 1.38689
      skewness: 1.64292
      entropy: 0.594363
    Blue:
      min: 0  (0)
      max: 192 (0.752941)
      mean: 61.0934 (0.239582)
      standard deviation: 80.7328 (0.316599)
      kurtosis: -1.34383
      skewness: 0.676587
      entropy: 0.842137
  Image statistics:
    Overall:
      min: 0  (0)
      max: 224 (0.878431)
      mean: 54.0326 (0.211892)
      standard deviation: 78.8153 (0.30908)
      kurtosis: -0.752325
      skewness: 0.974955
      entropy: 0.727833
  Colors: 7
  Histogram:
    163911: (  0,  0,  0) #000000 black
     16626: (  0,  0,128) #000080 navy
      5261: (  0,128,  0) #008000 green
     37681: (128,128,192) #8080C0 srgb(128,128,192)
     36210: (192,  0,128) #C00080 srgb(192,0,128)
     12644: (192, 64,  0) #C04000 srgb(192,64,0)
     20167: (224,224,192) #E0E0C0 srgb(224,224,192)
  Colormap entries: 256
  Colormap:
         0: (  0,  0,  0,255) #000000FF black
         1: (128,  0,  0,255) #800000FF maroon
         2: (  0,128,  0,255) #008000FF green
         3: (128,128,  0,255) #808000FF olive
         4: (  0,  0,128,255) #000080FF navy
         5: (128,  0,128,255) #800080FF purple
         6: (  0,128,128,255) #008080FF teal
         7: (128,128,128,255) #808080FF fractal
         8: ( 64,  0,  0,255) #400000FF srgba(64,0,0,1)
         9: (192,  0,  0,255) #C00000FF srgba(192,0,0,1)
        10: ( 64,128,  0,255) #408000FF srgba(64,128,0,1)
        11: (192,128,  0,255) #C08000FF srgba(192,128,0,1)
        12: ( 64,  0,128,255) #400080FF srgba(64,0,128,1)
        13: (192,  0,128,255) #C00080FF srgba(192,0,128,1)
        14: ( 64,128,128,255) #408080FF srgba(64,128,128,1)
        15: (192,128,128,255) #C08080FF srgba(192,128,128,1)
        16: (  0, 64,  0,255) #004000FF srgba(0,64,0,1)
        17: (128, 64,  0,255) #804000FF srgba(128,64,0,1)
        18: (  0,192,  0,255) #00C000FF srgba(0,192,0,1)
        19: (128,192,  0,255) #80C000FF srgba(128,192,0,1)
        20: (  0, 64,128,255) #004080FF srgba(0,64,128,1)
        21: (128, 64,128,255) #804080FF srgba(128,64,128,1)
        22: (  0,192,128,255) #00C080FF srgba(0,192,128,1)
        23: (128,192,128,255) #80C080FF srgba(128,192,128,1)
        24: ( 64, 64,  0,255) #404000FF srgba(64,64,0,1)
        25: (192, 64,  0,255) #C04000FF srgba(192,64,0,1)
        26: ( 64,192,  0,255) #40C000FF srgba(64,192,0,1)
        27: (192,192,  0,255) #C0C000FF srgba(192,192,0,1)
        28: ( 64, 64,128,255) #404080FF srgba(64,64,128,1)
        29: (192, 64,128,255) #C04080FF srgba(192,64,128,1)
        30: ( 64,192,128,255) #40C080FF srgba(64,192,128,1)
        31: (192,192,128,255) #C0C080FF srgba(192,192,128,1)
        32: (  0,  0, 64,255) #000040FF srgba(0,0,64,1)
        33: (128,  0, 64,255) #800040FF srgba(128,0,64,1)
        34: (  0,128, 64,255) #008040FF srgba(0,128,64,1)
        35: (128,128, 64,255) #808040FF srgba(128,128,64,1)
        36: (  0,  0,192,255) #0000C0FF srgba(0,0,192,1)
        37: (128,  0,192,255) #8000C0FF srgba(128,0,192,1)
        38: (  0,128,192,255) #0080C0FF srgba(0,128,192,1)
        39: (128,128,192,255) #8080C0FF srgba(128,128,192,1)
        40: ( 64,  0, 64,255) #400040FF srgba(64,0,64,1)
        41: (192,  0, 64,255) #C00040FF srgba(192,0,64,1)
        42: ( 64,128, 64,255) #408040FF srgba(64,128,64,1)
        43: (192,128, 64,255) #C08040FF srgba(192,128,64,1)
        44: ( 64,  0,192,255) #4000C0FF srgba(64,0,192,1)
        45: (192,  0,192,255) #C000C0FF srgba(192,0,192,1)
        46: ( 64,128,192,255) #4080C0FF srgba(64,128,192,1)
        47: (192,128,192,255) #C080C0FF srgba(192,128,192,1)
        48: (  0, 64, 64,255) #004040FF srgba(0,64,64,1)
        49: (128, 64, 64,255) #804040FF srgba(128,64,64,1)
        50: (  0,192, 64,255) #00C040FF srgba(0,192,64,1)
        51: (128,192, 64,255) #80C040FF srgba(128,192,64,1)
        52: (  0, 64,192,255) #0040C0FF srgba(0,64,192,1)
        53: (128, 64,192,255) #8040C0FF srgba(128,64,192,1)
        54: (  0,192,192,255) #00C0C0FF srgba(0,192,192,1)
        55: (128,192,192,255) #80C0C0FF srgba(128,192,192,1)
        56: ( 64, 64, 64,255) #404040FF grey25
        57: (192, 64, 64,255) #C04040FF srgba(192,64,64,1)
        58: ( 64,192, 64,255) #40C040FF srgba(64,192,64,1)
        59: (192,192, 64,255) #C0C040FF srgba(192,192,64,1)
        60: ( 64, 64,192,255) #4040C0FF srgba(64,64,192,1)
        61: (192, 64,192,255) #C040C0FF srgba(192,64,192,1)
        62: ( 64,192,192,255) #40C0C0FF srgba(64,192,192,1)
        63: (192,192,192,255) #C0C0C0FF silver
        64: ( 32,  0,  0,255) #200000FF srgba(32,0,0,1)
        65: (160,  0,  0,255) #A00000FF srgba(160,0,0,1)
        66: ( 32,128,  0,255) #208000FF srgba(32,128,0,1)
        67: (160,128,  0,255) #A08000FF srgba(160,128,0,1)
        68: ( 32,  0,128,255) #200080FF srgba(32,0,128,1)
        69: (160,  0,128,255) #A00080FF srgba(160,0,128,1)
        70: ( 32,128,128,255) #208080FF srgba(32,128,128,1)
        71: (160,128,128,255) #A08080FF srgba(160,128,128,1)
        72: ( 96,  0,  0,255) #600000FF srgba(96,0,0,1)
        73: (224,  0,  0,255) #E00000FF srgba(224,0,0,1)
        74: ( 96,128,  0,255) #608000FF srgba(96,128,0,1)
        75: (224,128,  0,255) #E08000FF srgba(224,128,0,1)
        76: ( 96,  0,128,255) #600080FF srgba(96,0,128,1)
        77: (224,  0,128,255) #E00080FF srgba(224,0,128,1)
        78: ( 96,128,128,255) #608080FF srgba(96,128,128,1)
        79: (224,128,128,255) #E08080FF srgba(224,128,128,1)
        80: ( 32, 64,  0,255) #204000FF srgba(32,64,0,1)
        81: (160, 64,  0,255) #A04000FF srgba(160,64,0,1)
        82: ( 32,192,  0,255) #20C000FF srgba(32,192,0,1)
        83: (160,192,  0,255) #A0C000FF srgba(160,192,0,1)
        84: ( 32, 64,128,255) #204080FF srgba(32,64,128,1)
        85: (160, 64,128,255) #A04080FF srgba(160,64,128,1)
        86: ( 32,192,128,255) #20C080FF srgba(32,192,128,1)
        87: (160,192,128,255) #A0C080FF srgba(160,192,128,1)
        88: ( 96, 64,  0,255) #604000FF srgba(96,64,0,1)
        89: (224, 64,  0,255) #E04000FF srgba(224,64,0,1)
        90: ( 96,192,  0,255) #60C000FF srgba(96,192,0,1)
        91: (224,192,  0,255) #E0C000FF srgba(224,192,0,1)
        92: ( 96, 64,128,255) #604080FF srgba(96,64,128,1)
        93: (224, 64,128,255) #E04080FF srgba(224,64,128,1)
        94: ( 96,192,128,255) #60C080FF srgba(96,192,128,1)
        95: (224,192,128,255) #E0C080FF srgba(224,192,128,1)
        96: ( 32,  0, 64,255) #200040FF srgba(32,0,64,1)
        97: (160,  0, 64,255) #A00040FF srgba(160,0,64,1)
        98: ( 32,128, 64,255) #208040FF srgba(32,128,64,1)
        99: (160,128, 64,255) #A08040FF srgba(160,128,64,1)
       100: ( 32,  0,192,255) #2000C0FF srgba(32,0,192,1)
       101: (160,  0,192,255) #A000C0FF srgba(160,0,192,1)
       102: ( 32,128,192,255) #2080C0FF srgba(32,128,192,1)
       103: (160,128,192,255) #A080C0FF srgba(160,128,192,1)
       104: ( 96,  0, 64,255) #600040FF srgba(96,0,64,1)
       105: (224,  0, 64,255) #E00040FF srgba(224,0,64,1)
       106: ( 96,128, 64,255) #608040FF srgba(96,128,64,1)
       107: (224,128, 64,255) #E08040FF srgba(224,128,64,1)
       108: ( 96,  0,192,255) #6000C0FF srgba(96,0,192,1)
       109: (224,  0,192,255) #E000C0FF srgba(224,0,192,1)
       110: ( 96,128,192,255) #6080C0FF srgba(96,128,192,1)
       111: (224,128,192,255) #E080C0FF srgba(224,128,192,1)
       112: ( 32, 64, 64,255) #204040FF srgba(32,64,64,1)
       113: (160, 64, 64,255) #A04040FF srgba(160,64,64,1)
       114: ( 32,192, 64,255) #20C040FF srgba(32,192,64,1)
       115: (160,192, 64,255) #A0C040FF srgba(160,192,64,1)
       116: ( 32, 64,192,255) #2040C0FF srgba(32,64,192,1)
       117: (160, 64,192,255) #A040C0FF srgba(160,64,192,1)
       118: ( 32,192,192,255) #20C0C0FF srgba(32,192,192,1)
       119: (160,192,192,255) #A0C0C0FF srgba(160,192,192,1)
       120: ( 96, 64, 64,255) #604040FF srgba(96,64,64,1)
       121: (224, 64, 64,255) #E04040FF srgba(224,64,64,1)
       122: ( 96,192, 64,255) #60C040FF srgba(96,192,64,1)
       123: (224,192, 64,255) #E0C040FF srgba(224,192,64,1)
       124: ( 96, 64,192,255) #6040C0FF srgba(96,64,192,1)
       125: (224, 64,192,255) #E040C0FF srgba(224,64,192,1)
       126: ( 96,192,192,255) #60C0C0FF srgba(96,192,192,1)
       127: (224,192,192,255) #E0C0C0FF srgba(224,192,192,1)
       128: (  0, 32,  0,255) #002000FF srgba(0,32,0,1)
       129: (128, 32,  0,255) #802000FF srgba(128,32,0,1)
       130: (  0,160,  0,255) #00A000FF srgba(0,160,0,1)
       131: (128,160,  0,255) #80A000FF srgba(128,160,0,1)
       132: (  0, 32,128,255) #002080FF srgba(0,32,128,1)
       133: (128, 32,128,255) #802080FF srgba(128,32,128,1)
       134: (  0,160,128,255) #00A080FF srgba(0,160,128,1)
       135: (128,160,128,255) #80A080FF srgba(128,160,128,1)
       136: ( 64, 32,  0,255) #402000FF srgba(64,32,0,1)
       137: (192, 32,  0,255) #C02000FF srgba(192,32,0,1)
       138: ( 64,160,  0,255) #40A000FF srgba(64,160,0,1)
       139: (192,160,  0,255) #C0A000FF srgba(192,160,0,1)
       140: ( 64, 32,128,255) #402080FF srgba(64,32,128,1)
       141: (192, 32,128,255) #C02080FF srgba(192,32,128,1)
       142: ( 64,160,128,255) #40A080FF srgba(64,160,128,1)
       143: (192,160,128,255) #C0A080FF srgba(192,160,128,1)
       144: (  0, 96,  0,255) #006000FF srgba(0,96,0,1)
       145: (128, 96,  0,255) #806000FF srgba(128,96,0,1)
       146: (  0,224,  0,255) #00E000FF srgba(0,224,0,1)
       147: (128,224,  0,255) #80E000FF srgba(128,224,0,1)
       148: (  0, 96,128,255) #006080FF srgba(0,96,128,1)
       149: (128, 96,128,255) #806080FF srgba(128,96,128,1)
       150: (  0,224,128,255) #00E080FF srgba(0,224,128,1)
       151: (128,224,128,255) #80E080FF srgba(128,224,128,1)
       152: ( 64, 96,  0,255) #406000FF srgba(64,96,0,1)
       153: (192, 96,  0,255) #C06000FF srgba(192,96,0,1)
       154: ( 64,224,  0,255) #40E000FF srgba(64,224,0,1)
       155: (192,224,  0,255) #C0E000FF srgba(192,224,0,1)
       156: ( 64, 96,128,255) #406080FF srgba(64,96,128,1)
       157: (192, 96,128,255) #C06080FF srgba(192,96,128,1)
       158: ( 64,224,128,255) #40E080FF srgba(64,224,128,1)
       159: (192,224,128,255) #C0E080FF srgba(192,224,128,1)
       160: (  0, 32, 64,255) #002040FF srgba(0,32,64,1)
       161: (128, 32, 64,255) #802040FF srgba(128,32,64,1)
       162: (  0,160, 64,255) #00A040FF srgba(0,160,64,1)
       163: (128,160, 64,255) #80A040FF srgba(128,160,64,1)
       164: (  0, 32,192,255) #0020C0FF srgba(0,32,192,1)
       165: (128, 32,192,255) #8020C0FF srgba(128,32,192,1)
       166: (  0,160,192,255) #00A0C0FF srgba(0,160,192,1)
       167: (128,160,192,255) #80A0C0FF srgba(128,160,192,1)
       168: ( 64, 32, 64,255) #402040FF srgba(64,32,64,1)
       169: (192, 32, 64,255) #C02040FF srgba(192,32,64,1)
       170: ( 64,160, 64,255) #40A040FF srgba(64,160,64,1)
       171: (192,160, 64,255) #C0A040FF srgba(192,160,64,1)
       172: ( 64, 32,192,255) #4020C0FF srgba(64,32,192,1)
       173: (192, 32,192,255) #C020C0FF srgba(192,32,192,1)
       174: ( 64,160,192,255) #40A0C0FF srgba(64,160,192,1)
       175: (192,160,192,255) #C0A0C0FF srgba(192,160,192,1)
       176: (  0, 96, 64,255) #006040FF srgba(0,96,64,1)
       177: (128, 96, 64,255) #806040FF srgba(128,96,64,1)
       178: (  0,224, 64,255) #00E040FF srgba(0,224,64,1)
       179: (128,224, 64,255) #80E040FF srgba(128,224,64,1)
       180: (  0, 96,192,255) #0060C0FF srgba(0,96,192,1)
       181: (128, 96,192,255) #8060C0FF srgba(128,96,192,1)
       182: (  0,224,192,255) #00E0C0FF srgba(0,224,192,1)
       183: (128,224,192,255) #80E0C0FF srgba(128,224,192,1)
       184: ( 64, 96, 64,255) #406040FF srgba(64,96,64,1)
       185: (192, 96, 64,255) #C06040FF srgba(192,96,64,1)
       186: ( 64,224, 64,255) #40E040FF srgba(64,224,64,1)
       187: (192,224, 64,255) #C0E040FF srgba(192,224,64,1)
       188: ( 64, 96,192,255) #4060C0FF srgba(64,96,192,1)
       189: (192, 96,192,255) #C060C0FF srgba(192,96,192,1)
       190: ( 64,224,192,255) #40E0C0FF srgba(64,224,192,1)
       191: (192,224,192,255) #C0E0C0FF srgba(192,224,192,1)
       192: ( 32, 32,  0,255) #202000FF srgba(32,32,0,1)
       193: (160, 32,  0,255) #A02000FF srgba(160,32,0,1)
       194: ( 32,160,  0,255) #20A000FF srgba(32,160,0,1)
       195: (160,160,  0,255) #A0A000FF srgba(160,160,0,1)
       196: ( 32, 32,128,255) #202080FF srgba(32,32,128,1)
       197: (160, 32,128,255) #A02080FF srgba(160,32,128,1)
       198: ( 32,160,128,255) #20A080FF srgba(32,160,128,1)
       199: (160,160,128,255) #A0A080FF srgba(160,160,128,1)
       200: ( 96, 32,  0,255) #602000FF srgba(96,32,0,1)
       201: (224, 32,  0,255) #E02000FF srgba(224,32,0,1)
       202: ( 96,160,  0,255) #60A000FF srgba(96,160,0,1)
       203: (224,160,  0,255) #E0A000FF srgba(224,160,0,1)
       204: ( 96, 32,128,255) #602080FF srgba(96,32,128,1)
       205: (224, 32,128,255) #E02080FF srgba(224,32,128,1)
       206: ( 96,160,128,255) #60A080FF srgba(96,160,128,1)
       207: (224,160,128,255) #E0A080FF srgba(224,160,128,1)
       208: ( 32, 96,  0,255) #206000FF srgba(32,96,0,1)
       209: (160, 96,  0,255) #A06000FF srgba(160,96,0,1)
       210: ( 32,224,  0,255) #20E000FF srgba(32,224,0,1)
       211: (160,224,  0,255) #A0E000FF srgba(160,224,0,1)
       212: ( 32, 96,128,255) #206080FF srgba(32,96,128,1)
       213: (160, 96,128,255) #A06080FF srgba(160,96,128,1)
       214: ( 32,224,128,255) #20E080FF srgba(32,224,128,1)
       215: (160,224,128,255) #A0E080FF srgba(160,224,128,1)
       216: ( 96, 96,  0,255) #606000FF srgba(96,96,0,1)
       217: (224, 96,  0,255) #E06000FF srgba(224,96,0,1)
       218: ( 96,224,  0,255) #60E000FF srgba(96,224,0,1)
       219: (224,224,  0,255) #E0E000FF srgba(224,224,0,1)
       220: ( 96, 96,128,255) #606080FF srgba(96,96,128,1)
       221: (224, 96,128,255) #E06080FF srgba(224,96,128,1)
       222: ( 96,224,128,255) #60E080FF srgba(96,224,128,1)
       223: (224,224,128,255) #E0E080FF srgba(224,224,128,1)
       224: ( 32, 32, 64,255) #202040FF srgba(32,32,64,1)
       225: (160, 32, 64,255) #A02040FF srgba(160,32,64,1)
       226: ( 32,160, 64,255) #20A040FF srgba(32,160,64,1)
       227: (160,160, 64,255) #A0A040FF srgba(160,160,64,1)
       228: ( 32, 32,192,255) #2020C0FF srgba(32,32,192,1)
       229: (160, 32,192,255) #A020C0FF srgba(160,32,192,1)
       230: ( 32,160,192,255) #20A0C0FF srgba(32,160,192,1)
       231: (160,160,192,255) #A0A0C0FF srgba(160,160,192,1)
       232: ( 96, 32, 64,255) #602040FF srgba(96,32,64,1)
       233: (224, 32, 64,255) #E02040FF srgba(224,32,64,1)
       234: ( 96,160, 64,255) #60A040FF srgba(96,160,64,1)
       235: (224,160, 64,255) #E0A040FF srgba(224,160,64,1)
       236: ( 96, 32,192,255) #6020C0FF srgba(96,32,192,1)
       237: (224, 32,192,255) #E020C0FF srgba(224,32,192,1)
       238: ( 96,160,192,255) #60A0C0FF srgba(96,160,192,1)
       239: (224,160,192,255) #E0A0C0FF srgba(224,160,192,1)
       240: ( 32, 96, 64,255) #206040FF srgba(32,96,64,1)
       241: (160, 96, 64,255) #A06040FF srgba(160,96,64,1)
       242: ( 32,224, 64,255) #20E040FF srgba(32,224,64,1)
       243: (160,224, 64,255) #A0E040FF srgba(160,224,64,1)
       244: ( 32, 96,192,255) #2060C0FF srgba(32,96,192,1)
       245: (160, 96,192,255) #A060C0FF srgba(160,96,192,1)
       246: ( 32,224,192,255) #20E0C0FF srgba(32,224,192,1)
       247: (160,224,192,255) #A0E0C0FF srgba(160,224,192,1)
       248: ( 96, 96, 64,255) #606040FF srgba(96,96,64,1)
       249: (224, 96, 64,255) #E06040FF srgba(224,96,64,1)
       250: ( 96,224, 64,255) #60E040FF srgba(96,224,64,1)
       251: (224,224, 64,255) #E0E040FF srgba(224,224,64,1)
       252: ( 96, 96,192,255) #6060C0FF srgba(96,96,192,1)
       253: (224, 96,192,255) #E060C0FF srgba(224,96,192,1)
       254: ( 96,224,192,255) #60E0C0FF srgba(96,224,192,1)
       255: (224,224,192,255) #E0E0C0FF srgba(224,224,192,1)
  Rendering intent: Perceptual
  Gamma: 0.454545
  Chromaticity:
    red primary: (0.64,0.33)
    green primary: (0.3,0.6)
    blue primary: (0.15,0.06)
    white point: (0.3127,0.329)
  Matte color: grey74
  Background color: white
  Border color: srgb(223,223,223)
  Transparent color: none
  Interlace: None
  Intensity: Undefined
  Compose: Over
  Page geometry: 625x468+0+0
  Dispose: Undefined
  Iterations: 0
  Compression: Zip
  Orientation: Undefined
  Properties:
    date:create: 2020-01-21T11:05:35+00:00
    date:modify: 2020-01-21T11:05:35+00:00
    png:IHDR.bit-depth-orig: 8
    png:IHDR.bit_depth: 8
    png:IHDR.color-type-orig: 3
    png:IHDR.color_type: 3 (Indexed)
    png:IHDR.interlace_method: 0 (Not interlaced)
    png:IHDR.width,height: 625, 468
    png:PLTE.number_colors: 256
    png:sRGB: intent=0 (Perceptual Intent)
    signature: 12c7d4ddd28f23d431ea875c618757f305577bf8aec476ec418728c161d3de8e
  Artifacts:
    verbose: true
  Tainted: False
  Filesize: 6497B
  Number pixels: 292500
  Pixels per second: 54.4078MP
  User time: 0.000u
  Elapsed time: 0:01.005
  Version: ImageMagick 7.0.9-6 Q16 x86_64 2019-11-27 https://imagemagick.org

If ImageMagick is too big a dependency for you, you can also use the much smaller pngcheck like this:

pngcheck -vp YourImage.png

Sample Output

File: YourImage.png (6497 bytes)
  chunk IHDR at offset 0x0000c, length 13
    625 x 468 image, 8-bit palette, non-interlaced
  chunk PLTE at offset 0x00025, length 768: 256 palette entries
      0:  (  0,  0,  0) = (0x00,0x00,0x00)
      1:  (128,  0,  0) = (0x80,0x00,0x00)
      2:  (  0,128,  0) = (0x00,0x80,0x00)
      3:  (128,128,  0) = (0x80,0x80,0x00)
      4:  (  0,  0,128) = (0x00,0x00,0x80)
      5:  (128,  0,128) = (0x80,0x00,0x80)
      6:  (  0,128,128) = (0x00,0x80,0x80)
      7:  (128,128,128) = (0x80,0x80,0x80)
      8:  ( 64,  0,  0) = (0x40,0x00,0x00)
      9:  (192,  0,  0) = (0xc0,0x00,0x00)
     10:  ( 64,128,  0) = (0x40,0x80,0x00)
     11:  (192,128,  0) = (0xc0,0x80,0x00)
     12:  ( 64,  0,128) = (0x40,0x00,0x80)
     13:  (192,  0,128) = (0xc0,0x00,0x80)
     14:  ( 64,128,128) = (0x40,0x80,0x80)
     15:  (192,128,128) = (0xc0,0x80,0x80)
     16:  (  0, 64,  0) = (0x00,0x40,0x00)
     17:  (128, 64,  0) = (0x80,0x40,0x00)
     18:  (  0,192,  0) = (0x00,0xc0,0x00)
     19:  (128,192,  0) = (0x80,0xc0,0x00)
     20:  (  0, 64,128) = (0x00,0x40,0x80)
     21:  (128, 64,128) = (0x80,0x40,0x80)
     22:  (  0,192,128) = (0x00,0xc0,0x80)
     23:  (128,192,128) = (0x80,0xc0,0x80)
     24:  ( 64, 64,  0) = (0x40,0x40,0x00)
     25:  (192, 64,  0) = (0xc0,0x40,0x00)
    ...
    ...
    251:  (224,224, 64) = (0xe0,0xe0,0x40)
    252:  ( 96, 96,192) = (0x60,0x60,0xc0)
    253:  (224, 96,192) = (0xe0,0x60,0xc0)
    254:  ( 96,224,192) = (0x60,0xe0,0xc0)
    255:  (224,224,192) = (0xe0,0xe0,0xc0)
  chunk IDAT at offset 0x00331, length 5660
    zlib: deflated, 32K window, default compression
  chunk IEND at offset 0x01959, length 0
No errors detected in p.png (4 chunks, 97.8% compression).

I see you want to write your results out with the same palette as you started with. I don't believe OpenCV can do that, but you could write your results as a normal, unpalettised PNG with OpenCV and then remap that to your original palette with ImageMagick in Terminal.

So, we can make a 7x1 pixel image of the colours in your palette like this:

magick -size 1x1 xc:black xc:navy xc:green xc:'#8080C0' xc:'#C00080' xc:'#C04000' xc:'#E0E0C0' +append palette.png

That looks like this when enlarged:

enter image description here

You can now make any results images you like with OpenCV, but convert (i.e. "remap") them to that original palette like this:

magick OpenCVresult.png +dither -remap palette.png result.png

If your ImageMagick is v6, use convert in place of magick.

like image 65
Mark Setchell Avatar answered Dec 29 '22 11:12

Mark Setchell