OpenCV can change its behavior depending on the runtime environment:
enable extra debugging output or performance tracing
modify default locations and search paths
tune some algorithms or general behavior
enable or disable workarounds, safety features and optimizations
Notes:
⭐ marks most popular variables
variables with names like this VAR_${NAME} describes family of variables, where ${NAME} should be changed to one of predefined values, e.g. VAR_TBB, VAR_OPENMP, ...
Setting environment variable in Windows
In terminal or cmd-file (bat-file):
setMY_ENV_VARIABLE=true
C:\my_app.exe
In GUI:
Go to "Settings -> System -> About"
Click on "Advanced system settings" in the right part
In new window click on the "Environment variables" button
Add an entry to the "User variables" list
Setting environment variable in Linux
In terminal or shell script:
exportMY_ENV_VARIABLE=true./my_app
or as a single command:
MY_ENV_VARIABLE=true ./my_app
Setting environment variable in Python
importosos.environ["MY_ENV_VARIABLE"]="True"# value must be a stringimportcv2# variables set after this may not have effect
@note This method may not work on all operating systems and/or Python distributions. For example, it works on Ubuntu Linux with system Python interpreter, but doesn't work on Windows 10 with the official Python package. It depends on the ability of a process to change its own environment (OpenCV uses getenv from C++ runtime to read variables).
OPENCV_LEGACY_WAITKEY
Some modules have multiple available backends, following variables allow choosing specific backend or changing default priorities in which backends will be probed (e.g. when opening a video file).
name
type
default
description
OPENCV_PARALLEL_BACKEND
string
choose specific paralel_for backend (one of TBB, ONETBB, OPENMP)
OPENCV_PARALLEL_PRIORITY_${NAME}
num
set backend priority, default is 1000
OPENCV_PARALLEL_PRIORITY_LIST
string, ,-separated
list of backends in priority order
OPENCV_UI_BACKEND
string
choose highgui backend for window rendering (one of GTK, GTK3, GTK2, QT, WIN32)
OPENCV_UI_PRIORITY_${NAME}
num
set highgui backend priority, default is 1000
OPENCV_UI_PRIORITY_LIST
string, ,-separated
list of highgui backends in priority order
OPENCV_VIDEOIO_PRIORITY_${NAME}
num
set videoio backend priority, default is 1000
OPENCV_VIDEOIO_PRIORITY_LIST
string, ,-separated
list of videoio backends in priority order
plugins
Some external dependencies can be detached into a dynamic library, which will be loaded at runtime (plugin). Following variables allow changing default search locations and naming pattern for these plugins.
name
type
default
description
OPENCV_CORE_PLUGIN_PATH
paths
directories to search for core plugins
OPENCV_CORE_PARALLEL_PLUGIN_${NAME}
string, glob
parallel_for plugin library name (glob), e.g. default for TBB is "opencv_core_parallel_tbb*.so"
OPENCV_DNN_PLUGIN_PATH
paths
directories to search for dnn plugins
OPENCV_DNN_PLUGIN_${NAME}
string, glob
parallel_for plugin library name (glob), e.g. default for TBB is "opencv_core_parallel_tbb*.so"
OPENCV_CORE_PLUGIN_PATH
paths
directories to search for highgui plugins (YES it is CORE)
OPENCV_UI_PLUGIN_${NAME}
string, glob
highgui plugin library name (glob)
OPENCV_VIDEOIO_PLUGIN_PATH
paths
directories to search for videoio plugins
OPENCV_VIDEOIO_PLUGIN_${NAME}
string, glob
videoio plugin library name (glob)
OpenCL
Note: OpenCL device specification format is <Platform>:<CPU|GPU|ACCELERATOR|nothing=GPU/CPU>:<deviceName>, e.g. AMD:GPU:
name
type
default
description
OPENCV_OPENCL_RUNTIME
filepath or disabled
path to OpenCL runtime library (e.g. OpenCL.dll, libOpenCL.so)
⭐ OPENCV_OPENCL_DEVICE
string or disabled
choose specific OpenCL device. See specification format in the note above. See more details in the Links section.
OPENCV_OPENCL_RAISE_ERROR
bool
false
raise exception if something fails during OpenCL kernel preparation and execution (Release builds only)
OPENCV_OPENCL_ABORT_ON_BUILD_ERROR
bool
false
abort if OpenCL kernel compilation failed
OPENCV_OPENCL_CACHE_ENABLE
bool
true
enable OpenCL kernel cache
OPENCV_OPENCL_CACHE_WRITE
bool
true
allow writing to the cache, otherwise cache will be read-only
OPENCV_OPENCL_CACHE_LOCK_ENABLE
bool
true
use .lock files to synchronize between multiple applications using the same OpenCL cache (may not work on network drives)
OPENCV_OPENCL_CACHE_CLEANUP
bool
true
automatically remove old entries from cache (leftovers from older OpenCL runtimes)
OPENCV_OPENCL_VALIDATE_BINARY_PROGRAMS
bool
false
validate loaded binary OpenCL kernels
OPENCV_OPENCL_DISABLE_BUFFER_RECT_OPERATIONS
bool
true (Apple), false (others)
enable workaround for non-continuos data downloads
OPENCV_OPENCL_BUILD_EXTRA_OPTIONS
string
pass extra options to OpenCL kernel compilation
OPENCV_OPENCL_ENABLE_MEM_USE_HOST_PTR
bool
true
workaround/optimization for buffer allocation
OPENCV_OPENCL_ALIGNMENT_MEM_USE_HOST_PTR
num
4
parameter for OPENCV_OPENCL_ENABLE_MEM_USE_HOST_PTR
OPENCV_OPENCL_DEVICE_MAX_WORK_GROUP_SIZE
num
0
allow to decrease maxWorkGroupSize
OPENCV_OPENCL_PROGRAM_CACHE
num
0
limit number of programs in OpenCL kernel cache
OPENCV_OPENCL_RAISE_ERROR_REUSE_ASYNC_KERNEL
bool
false
raise exception if async kernel failed
OPENCV_OPENCL_BUFFERPOOL_LIMIT
num
1 << 27 (Intel device), 0 (others)
limit memory used by buffer bool
OPENCV_OPENCL_HOST_PTR_BUFFERPOOL_LIMIT
num
same as OPENCV_OPENCL_BUFFERPOOL_LIMIT, but for HOST_PTR buffers
OPENCV_OPENCL_BUFFER_FORCE_MAPPING
bool
false
force clEnqueueMapBuffer
OPENCV_OPENCL_BUFFER_FORCE_COPYING
bool
false
force clEnqueueReadBuffer/clEnqueueWriteBuffer
OPENCV_OPENCL_FORCE
bool
false
force running OpenCL kernel even if usual conditions are not met (e.g. dst.isUMat)
OPENCV_OPENCL_PERF_CHECK_BYPASS
bool
false
force running OpenCL kernel even if usual performance-related conditions are not met (e.g. image is very small)
SVM (Shared Virtual Memory) - disabled by default
name
type
default
description
OPENCV_OPENCL_SVM_DISABLE
bool
false
disable SVM
OPENCV_OPENCL_SVM_FORCE_UMAT_USAGE
bool
false
OPENCV_OPENCL_SVM_DISABLE_UMAT_USAGE
bool
false
OPENCV_OPENCL_SVM_CAPABILITIES_MASK
num
OPENCV_OPENCL_SVM_BUFFERPOOL_LIMIT
num
same as OPENCV_OPENCL_BUFFERPOOL_LIMIT, but for SVM buffers
Note: extra FFmpeg options should be pased in form key;value|key;value|key;value, for example hwaccel;cuvid|video_codec;h264_cuvid|vsync;0 or vcodec;x264|vprofile;high|vlevel;4.0
name
type
default
description
⭐ OPENCV_FFMPEG_CAPTURE_OPTIONS
string (see note)
extra options for VideoCapture FFmpeg backend
⭐ OPENCV_FFMPEG_WRITER_OPTIONS
string (see note)
extra options for VideoWriter FFmpeg backend
OPENCV_FFMPEG_THREADS
num
set FFmpeg thread count
OPENCV_FFMPEG_DEBUG
non-null
enable logging messages from FFmpeg
OPENCV_FFMPEG_LOGLEVEL
num
set FFmpeg logging level
OPENCV_FFMPEG_DLL_DIR
dir path
directory with FFmpeg plugin (legacy)
OPENCV_FFMPEG_IS_THREAD_SAFE
bool
false
enabling this option will turn off thread safety locks in the FFmpeg backend (use only if you are sure FFmpeg is built with threading support, tested on Linux)
OPENCV_FFMPEG_READ_ATTEMPTS
num
4096
number of failed av_read_frame attempts before failing read procedure
OPENCV_FFMPEG_DECODE_ATTEMPTS
num
64
number of failed avcodec_receive_frame attempts before failing decoding procedure
OPENCV_VIDEOIO_GSTREAMER_CALL_DEINIT
bool
false
close GStreamer instance on end
OPENCV_VIDEOIO_GSTREAMER_START_MAINLOOP
bool
false
start GStreamer loop in separate thread
OPENCV_VIDEOIO_MFX_IMPL
num
set specific MFX implementation (see MFX docs for enumeration)
OPENCV_VIDEOIO_MFX_EXTRA_SURFACE_NUM
num
1
add extra surfaces to the surface pool
OPENCV_VIDEOIO_MFX_POOL_TIMEOUT
num
1
timeout for waiting for free surface from the pool (in seconds)
OPENCV_VIDEOIO_MFX_BITRATE_DIVISOR
num
300
this option allows to tune encoding bitrate (video quality/size)
OPENCV_VIDEOIO_MFX_WRITER_TIMEOUT
num
1
timeout for encoding operation (in seconds)
OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS
bool
true
allow HW-accelerated transformations (DXVA) in MediaFoundation processing graph (may slow down camera probing process)
OPENCV_DSHOW_DEBUG
non-null
enable verbose logging in the DShow backend
OPENCV_DSHOW_SAVEGRAPH_FILENAME
file path
enable processing graph tump in the DShow backend
OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED
bool
false
use (0, 1) range for properties (V4L)
OPENCV_VIDEOIO_V4L_SELECT_TIMEOUT
num
10
timeout for select call (in seconds) (V4L)
OPENCV_VIDEOCAPTURE_DEBUG
bool
false
enable debug messages for VideoCapture
OPENCV_VIDEOWRITER_DEBUG
bool
false
enable debug messages for VideoWriter
⭐ OPENCV_VIDEOIO_DEBUG
bool
false
debug messages for both VideoCapture and VideoWriter
videoio tests
name
type
default
description
OPENCV_TEST_VIDEOIO_BACKEND_REQUIRE_FFMPEG
test app will exit if no FFmpeg backend is available
OPENCV_TEST_V4L2_VIVID_DEVICE
file path
path to VIVID virtual camera device for V4L2 test (e.g. /dev/video5)
OPENCV_TEST_PERF_CAMERA_LIST
paths
cameras to use in performance test (waitAny_V4L test)
OPENCV_TEST_CAMERA_%d_FPS
num
fps to set for N-th camera (0-based index) (waitAny_V4L test)
gapi
name
type
default
description
⭐ GRAPH_DUMP_PATH
file path
dump graph (dot format)
PIPELINE_MODELS_PATH
dir path
pipeline_modeling_tool sample application uses this var