Displaying jp2 Images Using Python's OpenCV

Asked 2 years ago, Updated 2 years ago, 104 views

Purpose and Current State
I want to display the jp2 image on OpenCV, but I get an error.
It seems that you can read things like jpeg.
I don't know the cause, so please let me know.

environment
ubuntu uses python3.

source code

importos
import cv2
os.environ ["OPENCV_IO_ENABLE_JASPER"] = "True"

# pass
path_b4=('T53SLV_20190504T014701_B04_10m.jp2')

# load the original image
img=cv2.imread(path_b4,cv2.IMREAD_COLOR)

# Displayed in window
cv2.imshow ("MAP", img)

# exit processing
cv2.waitKey(0)
cv2.destroyAllWindows()

error message

imread_('T53SLV_20190504T014701_B04_10m.jp2'):can't read header:OpenCV(4.2.0)/io/opencv/modules/imgcodecs/src/grfmt_jpeg2000.cpp:196:error:(-215:Assertion file'Depth==16th== department|

Traceback (most recent call last):
  File "chap5.py", line 15, in <module>
    cv2.imshow ("MAP", img)
cv2.error:OpenCV(4.2.0)/io/opencv/modules/highgui/src/window.cpp:376:error:(-215:Assertion failed)size.width>0&size.height>0 in function 'imshow'

Tried
I tried using jpeg and png files in the above program and the image was displayed.
In another way, the image was opened, so the jp2 image itself should be fine.
Below is the code that allowed you to open jp2.

importos
import matplotlib.pyplot asplt
import numpy as np
from PIL import Image

# absolute pass
path_b4=('T53SLV_20190504T014701_B04_10m.jp2')

#jp2 Loading Images
im=Image.open(path_b4)

# Convert images to arrays
im_list=np.array(im)

#contour drawing
plt.imshow(im_list,cmap="jet")
plt.colorbar() 
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

Supplementary Information
I need an image of jp2, so I would like to know how to do it without format conversion.
The jp2 image is the size of (10980, 10980).

python python3 opencv

2022-09-30 19:29

1 Answers

correct and add first:

After that, I searched continuously and found that OpenCV documentation shows that differences in OS environment are more likely to be affected, and Unix-like open-source OSes require additional codecs.
Matimread(const string & filename, int flags=1)

  • JPEG2000 files-*.jp2 (see Note 2)

Note 2: OpenCV can always read JPEGs, PNGs, and TIFFs because OpenCV uses standard image codecs (libjpeg, libpng, libtiff and libjasper) by default in Windows and MacOSX environments.MacOSX also offers options for using native MacOSX image loaders. However, please note that color management built into MacOSX now allows the pixel values of images to vary slightly when using these native image loaders.

In Linux, BSD, and other Unix open source OS environments, OpNCV looks for the image codec provided by the OS. To get support for the codec, install related packages (for example, "libjpeg-dev") or turn on OPENCV_BUILD_3RDPARTY_LIBBS in CMake.

The last boldface above is cited by

Also for reference, opencv-python only calls C++ code
Bug in python version of imwrite w/16bit JPEG2000#384

There is no "python version" of OpenCV, Python just calls the C++ code.Open issue to the OpenCV repository, this repository contains only a build toolchain and I cannot fix OpenCV bugs here.

https://github.com/opencv/opencv/issues

Also, it says that OpenCV is now moving to OpenJPEG instead of jasper
Bug in imwrite w/16bit JPEG2000#18263

BTW, OpenCV is migrating on OpenJPEG decoder/encoder (instead of Jasper).

Maybe the following is part of the action?But it doesn't seem to be the essence

Isn't it simply the opposite order of setting environment variables and importing cv2? (In addition, it may be a problem with the number of OpenCV versions?)
Running Python 3.8.6 and OpenCV 4.4.0.46 in the order of the source of the question:

import cv2
os.environ ["OPENCV_IO_ENABLE_JASPER"] = "True"

This error occurred.

[WARN:0]global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-71670poj\opencv\modules\src\grfmt_jpeg2000.cpp(103)cv::initJasperimgcodecs:Jasperimcodecs:Jasper is available.JSPECDECDISCable is 2000
Traceback (most recent call last):
  File "jp2.py", line 9, in <module>
    img=cv2.imread(path_b4,cv2.IMREAD_COLOR)
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-71670poj\opencv\modules\imgcodecs\src\grfmt_jpeg2000.cpp:104: error: (-213:The function/feature is not implemented) imgcodecs: Jasper (JPEG-2000) codec is disabled. You can enable it via 'OPENCV_IO_ENABLE_JASPER' option. Refer for details and cautions here: https://github.com/opencv/opencv/issues/14058 in function 'cv::initJasper'

If you change the order and do it here, it will appear.

 os.environ ["OPENCV_IO_ENABLE_JASPER"] = "True"
import cv2

The data is here
Transleaf Digital Archive|Download|JuGeMuJPEG2000...

JPEG2000 image sample Edo Old Map

I used .


2022-09-30 19:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.