1

I have this PNG image that shows a spectrum of greyscale, but the largest pixel value is over 49,000. The smallest non-zero value is 6,825:

enter image description here

import cv2
import os
import numpy as np

img_path = os.path.expanduser('~/test.png')
img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
img_array = np.array(img)

print(np.min(img_array[img_array > 0])) #6825
print(np.max(img_array)) #49350

How come the image shows a spectrum when the values are (0, 6825...49350) and appears to be normalised? How come the values can exceed 255?

John M.
  • 2,642
  • 7
  • 26
  • 55
  • 1
    16 bit grayscale, often used for depth maps – Micka Sep 17 '19 at 18:10
  • 1
    The [docs](https://docs.opencv.org/2.4/modules/imgproc/doc/miscellaneous_transformations.html#cvtcolor) say images with [cvtype CV_16U](https://stackoverflow.com/q/13428689/190597) have a range of 0 .. 65535 (= 2**16 - 1). – unutbu Sep 17 '19 at 18:15
  • But if it was opened using `cv2.imread(img_path)`, the maximum value is 255. The default flag is `IMREAD_COLOR` which I don't think converts to 8 bit grayscale. – John M. Sep 17 '19 at 18:15
  • 1
    imread by default converts to 3 channel each 8 bit. There is a flag for unchanged as you've seen yourself – Micka Sep 17 '19 at 18:24
  • 1
    @Micka Thanks a lot – John M. Sep 17 '19 at 18:26

0 Answers0