如何用Python读取JPG2000?

How to read JPG2000 with Python?

我用 matplotlib 阅读了几张 JP2 (JPEG200) 图像,得到了大数字的 numpy 数组,超过 40000。

阅读代码:

img_blue =mpimg.imread('B02.jp2')
img_green =mpimg.imread('B03.jp2')
img_red =mpimg.imread('B04.jp2')

数据为:

[[12290 12694 13034 ...,  1968  2078  2118]
 [12174 12374 12696 ...,  1998  2068  2134]
 [12422 12522 12512 ...,  1990  1972  1990]
 ..., 
 [ 4268  4276  4064 ...,     0     0     0]
 [ 4174  4114  3938 ...,     0     0     0]
 [ 3954  4036  3906 ...,     

这个数据是什么意思?

是否意味着JP2可以包含更大的动态范围?如何将其转换为普通图像?只是正常化?什么是分母?

文件示例在这里:http://sentinel-s2-l1c.s3.amazonaws.com/tiles/37/U/DB/2017/5/10/0/B02.jp2

Question: Does it mean JP2 can contain larger dynamic range?

Yes, it does:

JPEG 2000 supports any bit depth, such as 16- and 32-bit floating point pixel images, and any color space.


Question: How can I convert it to normal image? Just normalize? What is denominator?

必须通过色调映射将 HDR 图像映射到 8 位图像,这通常是一种非线性映射。不同的色调映射曲线会产生不同的结果。您可以使用 OpenCV 执行此操作,如 OpenCV tutorial on HDR image processing:

所示
# Play with the gamma value to arrive at a result that you like
tonemap = cv2.createTonemapDurand(gamma=2.2)
tonemapped_img = tonemap.process(img.copy())
img_8bit = numpy.clip(tonemapped_img*255, 0, 255).astype('uint8')

我只是把22.5除以B02,B03,B04再合并。

看到了这个页面 https://knowledge.safe.com/articles/43742/making-rgb-images-with-sentinel-data.html

我的密码是

b_r = im_b_04/22.5;
b_g = im_b_03/22.5;
b_b = im_b_02/22.5;

RGB_gt = numpy.zeros([len(b_r), len(b_r[0]), 3], np.uint8)
RGB_gt[:, :, 0] = b_r;
RGB_gt[:, :, 1] = b_g;
RGB_gt[:, :, 2] = b_b;

有效。

import rasterio

ds = rasterio.open(path_to_jp2_image)
ds.read(1)

参见:can't open jp2 (from sentinel) with python rasterio,gdal