Introduction to OpenCV programming (basic read and write operations of matrix/image/Video)

Source: Internet
Author: User
Tags data structures manual

PS. Due to the limited length of the CSDN blog post, some of the content in this article has been cut off.
On the wiki of OpenCV Chinese website, there is a more readable and complete version, welcome to browse.

OpenCV Wiki: Introduction to OpenCV programming (basic read and write operations of matrix/image/Video)

Introduction to programming with OpenCV
Introduction to OPENCV Programming


Gady Agam
Department of Computer Science
January 27, 2006
Illinois Institute of Technology

--url:http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#section00040000000000000000

Translation: Chenyusiyuan
--January 26, 2010
--Http://blog.csdn.net/chenyusiyuan

Summary:
This article is intended to help readers get started OPENCV without having to read lengthy reference manuals. After mastering the following basic knowledge of OPENCV, consult the relevant reference manual if necessary.

Directory
A Brief introduction
o OpenCV Features
o Useful Learning Resources
O Naming rules
O Compilation Recommendations
o a routine

Two GUI commands
o Window Management
o Input Processing

Basic data structure of three OPENCV
o Image Data structure
o matrices and vectors
o Other data structures

Quad image processing
o Memory allocation and release
o Read and write images
o Accessing image pixels
o Image Conversion
o Drawing Instructions

Five-matrix processing
o Memory allocation and release
o Access matrix elements
o Matrix/vector operations

Six video processing
o Frame capture of video
o reading and setting of frame information
o Save video files

=================================================
First, Introduction
1, the characteristics of OPENCV
(1) General description
o OpenCV is a library of open-source image processing functions based on C + + language
o its code is optimized for real-time processing of images
O with good portability
o General operation for image/video loading, storage and capture
o Low-level and advanced Application Interface (API)
O provides an interface to the Intel IPP efficient multimedia library to improve program performance for your Intel CPU optimized code: OpenCV version 2.0 code has been significantly optimized to improve performance without IPP, so the IPP interface is no longer available in version 2.0

(2) function
o Image data manipulation (memory allocation and release, image copying, setting, and conversion)
Image Data manipulation (allocation, release, copying, setting, conversion).
o image/Video input/output (support for file or camera input, image/video file output)
Image and video I/O (file and camera based input, image/video file output).
o Matrix/vector data manipulation and linear algebra operations (matrix product, matrix equation solution, eigenvalue, singular value decomposition)
Matrix and vector manipulation and linear algebra routines (products, solvers, eigenvalues, SVD).
O supports a variety of dynamic data structures (linked lists, queues, datasets, trees, graphs)
Various Dynamic Data Structures (lists, queues, sets, trees, graphs).
o Basic image processing (denoising, edge detection, corner detection, sampling and interpolation, color transformation, morphological processing, histogram, image pyramid structure)
Basic image processing (filtering, edge detection, corner detection, sampling and interpolation, color conversion, Morphol Ogical operations, histograms, image pyramids).
o Structural analysis (connected domain/branch, contour processing, distance conversion, image moment, template matching, Hough transform, polynomial approximation, curve fitting, ellipse fitting, Delaunay triangulation)
Structural Analysis (connected components, contour processing, distance transform, various moments, template matching, Hou GH transform, polygonal approximation, line fitting, ellipse fitting, Delaunay triangulation).
o Camera calibration (search and track calibration mode, parameter calibration, basic matrix estimation, single-response matrix estimation, stereo visual matching)
Camera Calibration (finding and tracking calibration patterns, calibration, fundamental matrix estimation, homography Esti Mation, stereo correspondence).
O Motion Analysis (optical flow, motion segmentation, target tracking)
Motion Analysis (optical flow, motion segmentation, tracking).
O Target Recognition (feature method, hmm model) object recognition (Eigen-methods, hmm).
O Basic GUI (display image/video, keyboard/mouse operation, slider)
Basic GUI (Display image/video, keyboard and mouse handling, scroll-bars).
o Image callout (line, curve, polygon, text callout)
Image Labeling (line, conic, polygon, text drawing)

(3) OPENCV module
o cv– core Function library
o cvaux– Auxiliary function library
o cxcore– data structures and linear algebra libraries
o Highgui–gui Function Library
o ml– machine learning function Library

2. Useful Learning Resources
(1) Reference manual:
O/docs/index.htm (in your OPENCV installation directory)
? Network resources:
O Official website: http://www.intel.com/technology/computing/opencv/
o Software Download: http://sourceforge.net/projects/opencvlibrary/
(2) Books:
o Open Source computer Vision Library
by Gary R. Bradski, Vadim Pisarevsky, and Jean-yves Bouguet, Springer, 1st ed. (June, 2006).
Chenyusiyuan: Supplement the following books
O Learning opencv-computer Vision with the OpenCV Library
by Gary Bradski & Adrian Kaehler, O ' Reilly Media, 1st ed. (September, 2008).
o OpenCV Tutorial--Basic article
Author: Liu Rui Ushchi, Beijing University of Aeronautics and Astronautics Press, published date: 200706
(3) Video processing routines (in/samples/c/):
O Color Tracker: Camshiftdemo
O-point tracking: Lkdemo
o Action split: Motempl
O Edge Detection: Laplace
(4) Image processing routines (in/samples/c/):
O Edge detection: Edge
o Image segmentation: pyramid_segmentation
O Morphology: morphology
O Histogram: demhist
O Distance Transform: Distrans
o Ellipse fitting: fitellipse

3. OpenCV naming rules
(1) Function name:

    Cvactiontargetmod (...) 

    Action = Core function (functionality) (e.g. set, create) target
    = target image area (e.g. contour, polygon)
    Mo D    = (optional) adjustment language (optional modifiers) (e.g. argument type)


(2) Matrix data type:

    Cv_<bit_depth> (s| u| f) c<number_of_channels> 

    S = Symbolic integer
    U = unsigned integer
    F = float type 

    e.g.: CV_8UC1 is a 8-bit unsigned integer single-channel matrix, 
          cv_ 32FC2 refers to a 32-bit floating-point dual-channel matrix.


(3) Image data type:

    Ipl_depth_<bit_depth> (s| u| F) 

    e.g.: ipl_depth_8u image pixel data is 8-bit unsigned integer.
          ipl_depth_32f image pixel data is a 32-bit floating point type.


(4) header file:

    #include <cv.h>
    #include <cvaux.h>
    #include 

4. Compiling suggestions
(1) Linux:

g++ hello-world.cpp-o hello-world/
    -i/usr/local/include/opencv-l/usr/local/lib  /
    -lm-lcv-lhighgui -lcvaux


(2) Windows:
Set the path to the OpenCV related files in Visual Studio ' options ' and ' projects '.

5, C routines

//Hello-world.cpp////The program reads an image from a file, 
Turn the color back on and then show it. #include <stdlib.h> #include <
  stdio.h> #include <math.h> #include <cv.h> #include  


=================================================
Second, GUI instructions
1. Window Management
(1) Create and locate a new window:

  Cvnamedwindow ("Win1", cv_window_autosize); 
  Cvmovewindow ("Win1", 100, 100); Offset from the UL corner of the


(2) Load Image:

  iplimage* img=0; 
  Img=cvloadimage (fileName);
  if (!img) printf ("Could Not load image file:%s/n", fileName);


(3) Display Image:

Cvshowimage ("Win1", IMG);


This function can display a colored or grayscale byte/float image. BYTE-type image has a range of pixel values [0-255], and a floating-point image has a range of pixel values [0-1]. The three-color elements of a color image are stored sequentially in BGR (blue-red-green).
(4) Close the window:

Cvdestroywindow ("Win1");


(5) Change the window size:

Cvresizewindow ("Win1", 100,100); New Width/heigh in pixels

2. Input processing
(1) Handling Mouse events:
o Define a mouse handler:

void Mousehandler (int event, int x, int y, int flags, void* param)
  {
    switch (event) {case
      Cv_event_lbuttondown :
        if (Flags & Cv_event_flag_ctrlkey) 
          printf ("left button off with CTRL pressed/n");
        break; 

      Case Cv_event_lbuttonup:
        printf ("left button up/n");
        break;
    }
  }


X, y: pixel coordinates relative to the upper-left corner

Event:cv_event_lbuttondown, Cv_event_rbuttondown, Cv_event_mbuttondown,
Cv_event_lbuttonup, Cv_event_rbuttonup, Cv_event_mbuttonup,
CV_EVENT_LBUTTONDBLCLK, CV_EVENT_RBUTTONDBLCLK, CV_EVENT_MBUTTONDBLCLK,
Cv_event_mousemove:

Flags:cv_event_flag_ctrlkey, Cv_event_flag_shiftkey, Cv_event_flag_altkey,
Cv_event_flag_lbutton, Cv_event_flag_rbutton, Cv_event_flag_mbutton
o Register the event handler:

mouseparam=5;
  Cvsetmousecallback ("Win1", Mousehandler,&mouseparam);


(2) Handling keyboard events:
o There is actually no special event handler for keyboard input.
o Detect keyboard input at a certain interval (for loop body):

int key;
  Key=cvwaitkey (10); Wait 10ms for input


o Abort program waits for keyboard input:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.