[OpenCV Study Notes 1] OpenCV programming Overview [workshop]

Source: Internet
Author: User

OpenCV programming Overview (Basic read/write operations for matrices, images, and videos)

Introduction to programming with OpenCV

OpenCV programming Overview

Author: Gady Agam

  • Department of Computer Science
  • January 27,200 6
  • Illinois Institute of Technology
  • URL: http://www.cs.iit.edu /~ Agam/cs512/lect-notes/opencv-intro/opencv-intro.html # SECTION00040000000000000000

Translation: chenyusiyuan

  • January 26,201 0
  • Http://blog.csdn.net/chenyusiyuan/archive/2010/01/26/5259060.aspx

Abstract: This article aims to help readers get started with OpenCV without reading lengthy reference manuals. After having mastered the following basic knowledge of OpenCV, you can refer to the relevant reference manual if necessary.

Directory

[Hide]

  • 1. Introduction

    • 1.1 1. Features of OpenCV

      • 1.1.1 (1) General description
      • 1.1.2 (2) Functions
      • 1.1.3 (3) OpenCV Module
    • 1.2 2. Useful Learning Resources

      • 1.2.1 (1) Reference Manual:
      • 1.2.2 (2) network resources:
      • 1.2.3 (3) books:
      • 1.2.4 (4) Video Processing Routine (in <opencv-root>/samples/c /):
      • 1.2.5 (5) image processing routine (in <opencv-root>/samples/c /):
    • 1.3 3. OpenCV naming rules

      • 1.3.1 (1) Function Name:
      • 1.3.2 (2) matrix data type:
      • 1.3.3 (3) image data types:
      • 1.3.4 (4) header file:
    • 1.4 4. Compilation suggestions

      • 1.4.1 (1) Linux:
      • 1.4.2 (2) Windows:
    • 1.5 5. C routine
  • 2. GUI commands

    • 2.1 1. Window Management

      • 2.1.1 (1) Create and locate a new window:
      • 2.1.2 (2) Loading images:
      • 2.1.3 (3) display image:
      • 2.1.4 (4) Close the window:
      • 2.1.5 (5) change the window size:
    • 2.2 2. Input Processing

      • 2.2.1 (1) Handling mouse events:
      • 2.2.2 (2) handle Keyboard Events:
      • 2.2.3 (3) handle slide events:
  • 3. Basic data structure of OpenCV

    • 3.1 1. Image Data Structure

      • 3.1.1 (1) IPL image:
    • 3.2 2. Matrix and vector

      • 3.2.1 (1) matrix:
      • 3.2.2 (2) general matrix:
      • 3.2.3 (3) scalar:
    • 3.3 3. Other Structure Types

      • 3.3.1 (1:
      • 3.3.2 (2) Rectangle frame size (pixel-based ):
      • 3.3.3 (3) offset and size of rectangular frames:
  • 4. Image Processing

    • 4.1 1. Image Memory Allocation and release

      • 4.1.1 (1) allocate memory to a new image:
      • 4.1.2 (2) Release images:
      • 4.1.3 (3) Copying images:
      • 4.1.4 (4) Set/obtain the region of interest ROI:
      • 4.1.5 (5) Set/get the COI channel of interest:
    • 4.2 2. Image read/write

      • 4.2.1 (1) read an image from a file:
      • 4.2.2 (2) Save the image:
    • 4.3 3. Access image pixels

      • 4.3.1 (1) Suppose you want to access the pixels in the k channel, row I, and column j.
      • 4.3.2 (2) indirect access: (generic, but inefficient, allows access to images in any format)
      • 4.3.3 (3) Direct Access: (high efficiency, but error-prone)
      • 4.3.4 (4) Direct pointer-Based Access: (simple and efficient)
      • 4.3.5 (5) direct access based on c ++ wrapper: (simpler and more efficient)
    • 4.4 4. Image Conversion

      • 4.4.1 (1) grayscale-color conversion of byte images:
      • 4.4.2 (2) color image> grayscale image:
      • 4.4.3 (3) Conversion between different color spaces:
    • 4.5 5. Drawing instructions

      • 4.5.1 (1) Draw a rectangle:
      • 4.5.2 (2) Draw a circle:
      • 4.5.3 (3) draw a line segment:
      • 4.5.4 (4) Draw a group of line segments:
      • 4.5.5 (5) Draw a group of filled polygon:
      • 4.5.6 (6) text annotation:
  • 5. Matrix Processing

    • 5.1 1. Memory Allocation and release of the matrix

      • 5.1.1 (1) Overall:
      • 5.1.2 (2) allocate memory for the new matrix:
      • 5.1.3 (3) release matrix memory:
      • 5.1.4 (4) replication matrix:
      • 5.1.5 (5) initialization matrix:
      • 5.1.6 (6) the initialization matrix is the unit matrix:
    • 5.2 2. Access Matrix Elements

      • 5.2.1 (1) assume that the second (I, j) unit of a 2D floating point matrix needs to be accessed.
      • 5.2.2 (2) indirect access:
      • 5.2.3 (3) direct access (assuming that the matrix data is aligned in 4 bytes ):
      • 5.2.4 (4) direct access (possible alignment gaps when data row alignment may have gaps ):
      • 5.2.5 (5) perform direct access to the initialized matrix:
    • 5.3 3. matrix/vector operations

      • 5.3.1 (1) calculation between matrices:
      • 5.3.2 (2) element-level operations between matrices:
      • 5.3.3 (3) vector product:
      • 5.3.4 (4) operations on a single matrix:
      • 5.3.5 (5) non-homogeneous linear equation solution:
      • 5.3.6 (6) feature values and feature vectors (the Matrix is a square matrix ):
  • 6. Video Processing

    • 6.1 1. capture a frame from the video stream

      • 6.1.1 (1) OpenCV supports capturing frames from cameras or video files (AVI format.
      • 6.1.2 (2) initialize a camera CATCHER:
      • 6.1.3 (3) initialize a video file CATCHER:
      • 6.1.4 (4) capture a frame:
      • 6.1.5 (5) release the video stream CATCHER:
    • 6.2 2. Obtain/set video stream information

      • 6.2.1 (1) obtain video stream device information:
      • 6.2.2 (2) obtain the frame chart information:
      • 6.2.3 (3) set the position of the first frame captured from the video file:
    • 6.3 3. Save video files

      • 6.3.1 (1) initialize the video Writer:
      • 6.3.2 (2) video file Persistence:
      • 6.3.3 (3) release the video Writer:

[Edit]

I. Introduction

[Edit]

1. OpenCV features

[Edit]

(1) General description

  • OpenCV is an open-source image processing function library based on C/C ++.
  • Its code has been optimized and can be used to process images in real time.
  • Good portability
  • Allows you to load, save, and collect images or videos.
  • Low-level and advanced application interfaces (APIS)
  • Provides an Intel IPP-Oriented Multi-media function library interface, which can optimize the code for your Intel CPU and improve program performance, you do not need IPP to improve performance, so version 2.0 does not provide IPP interfaces)

[Edit]

(2) Functions

  • Image Data Operations (memory allocation and release, image copying, setting, and conversion)

Image data manipulation (allocation, release, copying, setting, conversion ).

  • 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 ).

  • Matrix/vector data operations and linear algebra operations (matrix product, matrix equation solving, feature value, Singular Value Decomposition)

Matrix and vector manipulation and linear algebra routines (products, solvers, eigenvalues, SVD ).

  • Supports multiple dynamic data structures (linked list, queue, dataset, tree, and graph)

Various dynamic data structures (lists, queues, sets, trees, graphs ).

  • Basic Image Processing (noise reduction, edge detection, corner point detection, sampling and interpolation, color transformation, Morphological Processing, histogram, and image pyramid structure)

Basic image processing (filtering, edge detection, corner detection, sampling and interpolation, color conversion, morphological operations, histograms, image pyramids ).

  • Structural Analysis (Connected Domain/branch, contour processing, distance conversion, image moment, template matching, Hof transformation, polynomial approximation, curve fitting, elliptic fitting, Dirani triangle)

Structural analysis (connected components, contour processing, distance transform, various moments, template matching, Hough transform, polygonal approximation, line fitting, ellipse fitting, and AdaBoost triangulation ).

  • Camera Calibration (search and tracking CALIBRATION mode, parameter calibration, basic Matrix Estimation, single-response matrix estimation, and stereo visual matching)

Camera calibration (finding and tracking calibration patterns, calibration, fundamental matrix estimation, homography estimation, stereo corresponsible ).

  • Motion Analysis (optical flow, Action segmentation, and target tracking)

Motion analysis (optical flow, motion segmentation, tracking ).

  • Target Recognition (feature method and HMM Model)

Object recognition (eigen-methods, HMM ).

  • Basic GUI (display images/videos, keyboard/mouse operations, slide bars)

Basic GUI (display image/video, keyboard and mouse handling, scroll-bars ).

  • Image annotation (line, curve, polygon, text annotation)

Image labeling (line, conic, polygon, text drawing)

[Edit]

(3) OpenCV Module

  • Cv-core function library
  • Cvaux-auxiliary function library
  • Cxcore-data structure and linear algebra Library
  • Highgui-GUI function library
  • Ml-machine learning function library

[Edit]

2. Useful Learning Resources

[Edit]

(1) Reference Manual:

  • <Opencv-root>/docs/index.htm (in your OpenCV installation directory <opencv-root>)

[Edit]

(2) network resources:

  • Http://www.intel.com/technology/computing/opencv/
  • Download: http://sourceforge.net/projects/opencvlibrary/

[Edit]

(3) books:

  • Open Source Computer Vision Library

By Gary R. Bradski, Vadim Pisarevsky, and Jean-Yves Bouguet, Springer, 1st ed. (June, 2006). chenyusiyuan: additional books

  • Learning OpenCV-Computer Vision with the OpenCV Library

By Gary Bradski & Adrian Kaehler, O 'Reilly Media, 1 st ed. (September, 2008 ).

  • OpenCV tutorial-Basics

Author: Liu Rui Yu Shiqi, Beijing University of Aeronautics and Astronautics Press, published on: 200706

[Edit]

(4) Video Processing Routine (in <opencv-root>/samples/c /):

  • Color tracking: camshiftdemo
  • Point Tracking: lkdemo
  • Action segmentation: motempl
  • Edge Detection: laplace

[Edit]

(5) image processing routine (in <opencv-root>/samples/c /):

  • Edge Detection: edge
  • Image segmentation: pyramid_segmentation
  • Morphology: morphology
  • Histogram: demhist
  • Distance Transformation: distrans
  • Elliptic fitting: fitellipse

[Edit]

3. OpenCV naming rules

[Edit]

(1) Function Name:

CvActionTargetMod (...)

Action = core functionality (e.g. set, create)
Target = target image area (e.g. contour, polygon)
Mod = (optional) optional modifiers (e.g. argument type)

[Edit]

(2) matrix data type:

CV _ <bit_depth> (S | U | F) C <number_of_channels>

S = signed integer
U = unsigned integer
F = floating point type

E. g.: CV_8UC1 refers to an 8-bit unsigned integer single-channel matrix,
CV_32FC2 refers to a 32-bit floating-point dual-channel matrix.

[Edit]

(3) image data types:

IPL_DEPTH _ <bit_depth> (S | U | F)

E. g.: The pixel data of the IPL_DEPTH_8U image is an 8-bit unsigned integer.
IPL_DEPTH_32F the pixel data of the image is 32-bit floating point.

[Edit]

(4) header file:

# Include <cv. h>
# Include <cvaux. h>
# Include # Include <ml. h>
# Include <cxcore. h> // This header file is not required.

[Edit]

4. Compilation suggestions

[Edit]

(1) Linux:

G ++ hello-world.cpp-o hello-world \
-I/usr/local/include/opencv-L/usr/local/lib \
-Lm-lcv-lhighgui-lcvaux

[Edit]

(2) Windows:

Set the path of the OpenCV-related files in the 'options' and 'project' of Visual Studio.

[Edit]

5. C routine

//////////////////////////////////////// ////////////////////////////////
//
// Hello-world.cpp
//
// This program reads an image from the file, returns the reversed color, and then displays it.
//
//////////////////////////////////////// ////////////////////////////////
# Include <stdlib. h>
# Include <stdio. h>
# Include <math. h>
# Include <cv. h>
# Include  
Int main (int argc, char * argv [])
{
IplImage * img = 0;
Int height, width, step, channels;
Uchar * data;
Int I, j, k;
 
If (argc <2 ){
Printf ("Usage: main <image-file-name> \ n \ 7 ");
Exit (0 );
}
 
// Load an image
Img = cvLoadImage (argv [1]);
If (! Img ){
Printf ("cocould not load image file: % s \ n", argv [1]);
Exit (0 );
}
 
// Get the image data
Height = img-> height;
Width = img-> width;
Step = img-> widthStep;
Channels = img-> nChannels;
Data = (uchar *) img-> imageData;
Printf ("Processing a % dx % d image with % d channels \ n", height, width, channels );
 
// Create a window
CvNamedWindow ("mainWin", CV_WINDOW_AUTOSIZE );
CvMoveWindow ("mainWin", 100,100 );
 
// Invert the image
// Equivalent to cvNot (img );
For (I = 0; I Data [I * step + j * channels + k] = 255-data [I * step + j * channels + k];
 
// Show the image
CvShowImage ("mainWin", img );
 
// Wait for a key
CvWaitKey (0 );
 
// Release the image
CvReleaseImage (& img );
Return 0;
}

[Edit]

Ii. GUI commands

[Edit]

1. Window Management

[Edit]

(1) Create and locate a new window:

CvNamedWindow ("win1", CV_WINDOW_AUTOSIZE );
CvMoveWindow ("win1", 100,100); // offset from the UL corner of the screen

[Edit]

(2) Loading images:

IplImage * img = 0;
Img = cvLoadImage (fileName );
If (! Img) printf ("cocould not load image file: % s \ n", fileName );

[Edit]

(3) display image:

CvShowImage ("win1", img );

This function can display byte/floating point images in color or gray scale. The pixel value range of a byte image is [0-255], and that of a floating point image is [0-1]. Color images are stored in BGR (blue-red-green) sequence.

[Edit]

(4) Close the window:

CvDestroyWindow ("win1 ");

[Edit]

(5) change the window size:

CvResizeWindow ("win1", 100,100); // new width/heigh in pixels

[Edit]

2. Input Processing

[Edit]

(1) Handling mouse events:

  • 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 down with CTRL pressed \ n ");
Break;
 
Case CV_EVENT_LBUTTONUP:
Printf ("Left button up \ n ");
Break;
}
}

X, y: The 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

  • Register the event handler:

MouseParam = 5;
CvSetMouseCallback ("win1", mouseHandler, & mouseParam );

[Edit]

(2) handle Keyboard Events:

  • In fact, there is no special event handler for keyboard input.
  • Detect keyboard input at intervals (applicable to loop body ):

Int key;
Key = cvWaitKey (10); // wait 10 ms for input

  • Abort program waiting for keyboard input:

Int key;
Key = cvWaitKey (0); // wait indefinitely for input

  • Keyboard input loop handler:

While (1 ){
Key = cvWaitKey (10 );
If (key = 27) break;
 
Switch (key ){
Case 'H ':
...
Break;
Case 'I ':
...
Break;
}
}

[Edit]

(3) handle slide events:

  • Define a slider handler:

Void trackbarHandler (int pos)
{
Printf ("Trackbar position: % d \ n", pos );
}

  • Register the event handler:

Int trackbarVal = 25;
Int maxVal = 100;
CvCreateTrackbar ("bar1", "win1", & trackbarVal, maxVal, trackbarHandler );

  • Obtain the current slider position:

Int pos = cvGetTrackbarPos ("bar1", "win1 ");

  • Set the slider position:

CvSetTrackbarPos ("bar1", "win1", 25 );

[Edit]

III. Basic data structure of OpenCV

The structure elements of various data structures in OpenCV 1.1, 1.2, or 2.0 are adjusted. The following is a reference only)

[Edit]

1. Image Data Structure

[Edit]

(1) IPL image:

IplImage
| -- Int nChannels; // Number of color channels (1, 2, 3, 4)
| -- Int depth; // bit depth of the pixel:
| // IPL_DEPTH_8U, IPL_DEPTH_8S,
| // IPL_DEPTH_16U, IPL_DEPTH_16S,
| // IPL_DEPTH_32S, IPL_DEPTH_32F,
| // IPL_DEPTH_64F
| -- Int width; // image width (in pixels)
| -- Int height; // Image height
| -- Char * imageData; // Image Data Pointer
| // Note that color images store data in BGR order
| -- Int dataOrder; // 0-the values of different channels of pixels are staggered to form a single pixel plane.
| // 1-arrange all pixels in the same channel value to form several channel planes, and then arrange the planes.
| // CvCreateImage: only images with staggered pixels can be created.
| -- Int origin; // 0-the pixel origin is in the upper left corner,
| // 1-The pixel origin is in the lower left corner (Windows bitmaps style)
| -- Int widthStep; // number of bytes between the points in the same column of the adjacent row
| -- Int imageSize; // The image size (in bytes) = height * widthStep
| -- Struct _ IplROI * roi; // region of interest for the image (ROI). If the ROI is not empty
| // The process is limited to the ROI Area.
| -- Char * imageDataOrigin; // data origin pointer when image data is not aligned
| // (Image memory needs to be re-allocated correctly)
| // (Needed for correct image deallocation)
| -- Int align; // line alignment of the image data: 4 or 8 byte alignment
| // This option is not available in OpenCV and is replaced by widthStep.
| -- Char colorModel [4]; // color model-ignore this option in OpenCV

[Edit]

2. Matrix and vector

[Edit]

(1) matrix:

CvMat // 2D matrix
| -- Int type; // element type (uchar, short, int, float, double) and flag
| -- Int step; // The number of bytes in the whole row.
| -- Int rows, cols; // rows and columns
| -- Int height, width; // matrix height and width, corresponding to rows and cols
| -- Union data;
| -- Uchar * ptr; // data pointer for an unsigned char matrix
| -- Short * s; // data pointer for a short matrix
| -- In

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.