Skip to content
Search

OpenCV

lastmod 2025-05-28created 2014-10-03258 edits

OpenCV Modules

  • Main modules
    • core
    • imgproc
    • imgcodecs
    • videoio
    • highgui
    • video
    • calib3d
    • features2d
    • objdetect
    • ml
    • flann
    • photo
    • stitching
    • cudaarithm
    • cudabgsegm
    • cudacodec
    • cudafeatures2d
    • cudafilters
    • cudaimgproc
    • cudalegacy
    • cudaobjdetect
    • cudaoptflow
    • cudastereo
    • cudawarping
    • cudev
    • shape
    • superres
    • videostab
    • viz
  • Extra modules
    • aruco
    • bgsegm
    • bioinspired
    • ccalib
    • cvv
    • datasets
    • dnn
    • dpm
    • face
    • fuzzy
    • hdf
    • line_descriptor
    • matlab
    • optflow
    • plot
    • reg
    • rgbd
    • saliency
    • sfm
    • stereo
    • structured_light
    • surface_matching
    • text
    • tracking
    • xfeatures2d
    • ximgproc
    • xobjdetect
    • xphoto
  • Other

OpenCV (Open Computer Vision) is an open-source computer vision C library. It was originally developed by Intel. It can be used on many platforms such as Windows and Linux. It is a library focused on real-time image processing. It supports Intel Performance Primitives (IPP), which can give a speedup when used on Intel CPUs.

Category

Main modules

The following can be found on the OpenCV: OpenCV modules page.

Extra modules

  • alphamat - Alpha Matting
  • aruco - ArUco Marker Detection
  • barcode - Barcode detecting and decoding methods
  • bgsegm - Improved Background-Foreground Segmentation Methods
  • bioinspired - Biologically inspired vision models and derivated tools
  • ccalib - Custom Calibration Pattern for 3D reconstruction
  • cudaarithm - Operations on Matrices
  • cudabgsegm - Background Segmentation
  • cudacodec - Video Encoding/Decoding
  • cudafeatures2d - Feature Detection and Description
  • cudafilters - Image Filtering
  • cudaimgproc - Image Processing
  • cudalegacy - Legacy support
  • cudaobjdetect - Object Detection
  • cudaoptflow - Optical Flow
  • cudastereo - Stereo Correspondence
  • cudawarping - Image Warping
  • cudev - Device layer
  • cvv - GUI for Interactive Visual Debugging of Computer Vision Programs
  • datasets - Framework for working with different datasets
  • dnn_objdetect - DNN used for object detection
  • dnn_superres - DNN used for super resolution
  • dpm - Deformable Part-based Models
  • face - Face Analysis
  • freetype - Drawing UTF-8 strings with freetype/harfbuzz
  • fuzzy - Image processing based on fuzzy mathematics
  • hdf - Hierarchical Data Format I/O routines
  • hfs - Hierarchical Feature Selection for Efficient Image Segmentation
  • img_hash - The module brings implementations of different image hashing algorithms.
  • intensity_transform - The module brings implementations of intensity transformation algorithms to adjust image contrast.
  • julia - Julia bindings for OpenCV
  • line_descriptor - Binary descriptors for lines extracted from an image
  • mcc - Macbeth Chart module
  • optflow - Optical Flow Algorithms
  • ovis - OGRE 3D Visualiser
  • phase_unwrapping - Phase Unwrapping API
  • plot - Plot function for Mat data
  • quality - Image Quality Analysis (IQA) API
  • rapid - silhouette based 3D object tracking
  • reg - Image Registration
  • rgbd - RGB-Depth Processing
  • saliency - Saliency API
  • sfm - Structure From Motion
  • shape - Shape Distance and Matching
  • stereo - Stereo Correspondance Algorithms
  • structured_light - Structured Light API
  • superres - Super Resolution
  • surface_matching - Surface Matching
  • text - Scene Text Detection and Recognition
  • tracking - Tracking API
  • videostab - Video Stabilization
  • viz - 3D Visualizer
  • wechat_qrcode - WeChat QR code detector for detecting and parsing QR code.
  • xfeatures2d - Extra 2D Features Framework
  • ximgproc - Extended Image Processing
  • xobjdetect - Extended object detection
  • xphoto - Additional photo processing algorithms

Example

Libraries

  • cvui
  • ImageZMQ
  • imageflow - High-performance image manipulation for web servers.
  • OpenVINO - OpenVINO is an open-source toolkit for optimizing and deploying deep learning models. It provides boosted deep learning performance for vision, audio, and language models from popular frameworks like TensorFlow, PyTorch, and more. - https://docs.openvino.ai/latest/home.html

Graph-based GUI

CMake options

The CMake command-line options are as follows.

-D WITH_CUDA=OFF
Do not use CUDA.
-D ENABLE_PRECOMPILED_HEADERS=OFF
Do not use PCH.
-D WITH_LAPACK=OFF
Do not use BLAS's LAPACK.
libopencv_core.so.3.2.0: undefined reference to 'dpotrf_' #7970

Build tests, samples and applications

There are two kinds of tests: accuracy (opencv_test_*) and performance (opencv_perf_*). Tests and applications are enabled by default. Examples are not being built by default and should be enabled explicitly.

Corresponding cmake options:

cmake \
 -DBUILD_TESTS=ON \
 -DBUILD_PERF_TESTS=ON \
 -DBUILD_EXAMPLES=ON \
 -DBUILD_opencv_apps=ON \
 ../opencv

CMake default options

-D CMAKE_BUILD_TYPE=RELEASE
Release build.
-D CMAKE_INSTALL_PREFIX=/usr/local
Sets the installation directory prefix.

How to install

You can install it with yum and the like, but you can also install it directly from the homepage.

When installing it yourself, errors may occur where the library path and the like are not recognized properly.

./a.out: error while loading shared libraries: libopencv_core.so.2.4: cannot open shared object file: No such file or directory

In this case, double-check that LD_LIBRARY_PATH and the like are set correctly on that PC.

When using the highgui module, you need to install libgtk2.0-dev and pkg-config. Also, when using FFmpeg, you may need to use the -fPIC option.

Required build dependencies : Ubuntu

sudo apt-get install cmake
sudo apt-get install gcc g++

to support python2:

sudo apt-get install python-dev python-numpy

to support python3:

sudo apt-get install python3-dev python3-numpy

Next we need GTK support for GUI features, Camera support (v4l), Media Support (ffmpeg, gstreamer) etc.

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install libgstreamer-plugins-base1.0-dev libgstreamer1.0-dev

to support gtk2:

sudo apt-get install libgtk2.0-dev

to support gtk3:

sudo apt-get install libgtk-3-dev

Optional Dependencies

Above dependencies are sufficient to install OpenCV in your Ubuntu machine. But depending upon your requirements, you may need some extra dependencies. A list of such optional dependencies are given below. You can either leave it or install it, your call :)

OpenCV comes with supporting files for image formats like PNG, JPEG, JPEG2000, TIFF, WebP etc. But it may be a little old. If you want to get latest libraries, you can install development files for system libraries of these formats.

sudo apt-get install libpng-dev
sudo apt-get install libjpeg-dev
sudo apt-get install libopenexr-dev
sudo apt-get install libtiff-dev
sudo apt-get install libwebp-dev

Installation in Linux

# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
unzip opencv.zip
# Create build directory
mkdir -p build && cd build
# Configure
cmake ../opencv-4.x
# Build
cmake --build .

Things to know when starting with OpenCV

If you call cvCreateImage, you must call cdReleaseImage.
That is how you prevent memory leaks.
OpenCV's color image order is B,G,R, that is, Blue, Green, Red.
We usually think in R,G,B, and MATLAB images are also in R,G,B order.
In an image, the bottom left is (1.1)
That is, there are cases where it is the origin and cases where the top left is (1,1). The origin coordinates may differ between processing in MATLAB, processing in openCV, and the Image(jpg, BMP) we usually use. Check this before saying the result image came out upside down.
To fix an upside-down image, use cvFlip(IplImage, NULL, 0);.
If the resulting image is tilted by 90 degrees
This usually happens when columns and rows (width and height) are computed the other way around.
Many OpenCV functions are single channel functions, that is, they can only handle gray images.
So for a color image, split it into 3 gray images, apply the function you want, and merge them back.
The usual error message says something about depth. depth is the depth of the channels: 1 is a gray image, 3 is a color image, and 4 is color + alpha channel.

How to use CUDA

First, it must be compiled with the option to use CUDA.

import cv2
import numpy as np

# Numpy 배열 생성
image = np.zeros((512, 512, 3), dtype=np.uint8)

# Numpy 배열을 GPU 메모리로 복사
d_image = cv2.cuda_GpuMat()
d_image.upload(image)

Perform operations using functions that run on the GPU. For example, to convert an image to grayscale on the GPU, you can do the following.

## 1채널로 변환
gray_image = cv2.cuda.cvtColor(d_image, cv2.COLOR_BGR2GRAY)

When the operations are done, you need to copy the result back to host memory.

# 결과를 호스트 메모리로 복사
result = gray_image.download()

Example

Mersenne Twister random number generation

Example of generating random numbers with Mersenne Twister (MT19937):

cv::RNG_MT19937 rng;
for (int i = 0; i < hulls.size(); ++i) {
    cv::drawContours(image, hulls, i, cv::Scalar(rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255)), CV_FILLED);
}

Collision check

dist = cv2.pointPolygonTest(cnt,(50,50),True)

Version check

With OpenCV 2.4.x: You can use CV_VERSION or CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION from a C/C++ simple program.

#include <stdio.h>
#include <cv.h>

int main(void)
{
    printf("%s\r\n", CV_VERSION);
    printf("%u.%u.%u\r\n", CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
}

Documentations

Welcome to OpenCV-Python Tutorials’s documentation!
https://opencv-python-tutroals.readthedocs.io/en/latest/
Opencv-python-tutroals_2019-03-07.zip (2019-03-07)
[Recommended] OpenCV-Python Study documentation!
https://opencv-python.readthedocs.io/en/latest/index.html
OpenCV-Python_Study_documentation_2018-11-21.zip (2018-11-21)
OpenCV Korean manual 01 (Unknown user's)
Opencv_kor_manual_01.pdf
OpenCV Korean manual 02 (Unknown user's)
Opencv_kor_manual_02.pdf

See also

Favorite site

OpenCV 3.0

OpenCV Study: OpenCV modules

Guide

Tutorials

Example

Streaming

References


  1. OpenCV_TIP_-_Image_Processing_Glossary.pdf ↩

  2. Multimedia_Systems_-_lossy_compression_techniques.zip ↩

  3. Microsoft_software_-_based_image_processing_library_OpenCV_2.pdf ↩

  4. Opencv_iplimage_struct.pdf ↩

  5. Opencv_basic_struct_and_function.pdf ↩

  6. Opencv_cvGetQuadrangleSubPix_for_image_rotation.pdf ↩

author Zer0 sitemap