NumPy convert 8-bit to 16/32-bit image

前端 未结 2 1495
我在风中等你
我在风中等你 2021-01-05 14:27

I am using OpenCV 2 to do some images manipulations in YCbCr color space. For the moment I can detect some noise due to the conversion RGB -> YCbCr and then YCbCr -> RGB, bu

相关标签:
2条回答
  • 2021-01-05 14:58

    Thanks to @moarningsun, problem resolved:

    i = cv2.imread(imgNameIn, cv2.CV_LOAD_IMAGE_COLOR) # Need to be sure to have a 8-bit input
    img = np.array(i, dtype=np.uint16) # This line only change the type, not values
    img *= 256 # Now we get the good values in 16 bit format
    
    0 讨论(0)
  • 2021-01-05 15:01

    The accepted answer is not accurate. A 16-bit image has 65536 intensity levels (2^16) hence, values ranging from 0 to 65535.

    If one wants to obtain a 16-bit image from an image represented as an array of float ranging from 0 to 1, one has to multiply every coefficient of this array by 65535.

    Also, it is good practice to cast the type of your end result as the very last step of the operations you perform. This is mainly for two reasons: - If you perform divisions or multiplications by float, the result will return a float and you will need to change the type again. - In general (in the mathematical sense of the term), a transformation from float to integer can introduce errors. Casting the type at the very end of the operations prevents error propagation.

    0 讨论(0)
提交回复
热议问题