How to install OpenCV_python to read JPEG2000 files?

asked 2019-10-28 10:05:00 -0600

tjorda01 gravatar image

updated 2019-10-28 11:08:51 -0600

Is Jasper still the recommended way to read and write JP2 files in OpenCV? I heard there were security issues with it and OpenCV is moving to OpenJPEG. Has that happened yet?

I tried to use OpenCV with Jasper by enabling it in Python code as follows:

os.environ['OPENCV_IO_ENABLE_JASPER'] = 'true'

However, when I attempt to read an ESA Sentinel-2 image like this...

img_nir = cv2.imread(nir_path, 0)

I still get this error:

cv2.error: OpenCV(4.1.2) D:\Build\OpenCV\opencv-4.1.2\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/issu... in function 'cv::initJasper'

I installed OpenCV in an anaconda Python 3.7 virtual environment on Windows 10 like this:

python -m pip install opencv_python-4.1.2-cp37-cp37m-win_amd64.whl

What's the current recommended way to read and write JP2 files in OpenCV_python?

edit retag flag offensive close merge delete

Comments

Perhaps you should set the environment variable to 'on' instead of 'true'.

mvuori gravatar imagemvuori ( 2019-10-28 16:07:36 -0600 )edit

Thanks for your reply. However, it still fails with the same error message with it set to true and 1.

os.environ['OPENCV_IO_ENABLE_JASPER'] = 'true'

and

os.environ['OPENCV_IO_ENABLE_JASPER'] = '1'

tjorda01 gravatar imagetjorda01 ( 2019-10-31 14:56:39 -0600 )edit

Not sure if this is helpful in your scenario, but I got it to work by setting the environment variable in Powershell: $Env:OPENCV_IO_ENABLE_JASPER = "True"

jasperandrew gravatar imagejasperandrew ( 2020-02-17 13:33:42 -0600 )edit

as mvuori said it needs to be set to "ON"!

benlcb785 gravatar imagebenlcb785 ( 2020-09-05 04:01:36 -0600 )edit

I believe I was having the same issue as you. As explained in this GitHub comment, I needed to set the environment variable before even importing OpenCV. For example:

from os import environ
environ["OPENCV_IO_ENABLE_JASPER"] = "true"
import cv2
jreynolds gravatar imagejreynolds ( 2020-09-05 07:11:56 -0600 )edit