How to read JPG2000 using Python?
I read some JP2 (JPEG200) images with matplotlib and got numpy arrays with large numbers over 40,000.
Reading code:
img_blue =mpimg.imread('B02.jp2')
img_green =mpimg.imread('B03.jp2')
img_red =mpimg.imread('B04.jp2')
Data:
[[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 ...,
What does this data mean?
Does this mean JP2 can contain more dynamic range? How can I convert it to a regular image? Just normalize? What does the denominator mean?
An example file is here: http://sentinel-s2-l1c.s3.amazonaws.com/tiles/37/U/DB/2017/5/10/0/B02.jp2
source to share
Q: Does this mean JP2 can contain a higher dynamic range?
JPEG 2000 supports any bit depth, such as 16- and 32-bit floating point pixel images, and any color space.
Q: How do I convert it to a regular image? Just normalize? What does the denominator mean?
An HDR image must be mapped to an 8-bit image using tonemapping, which is, in general, non-linear mapping. Different tone shaping curves will give different results. You can do it with OpenCV as shown in the OpenCV HDR Image Processing Tutorial :
# 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')
source to share
I just divide 22.5 by B02, B03, B04 and combine them.
saw this page https://knowledge.safe.com/articles/43742/making-rgb-images-with-sentinel-data.html
my code
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;
it works.
source to share