0% found this document useful (0 votes)
54 views

Opencv Tutorial: A Brief Guide To Memory Management (And Other Miscellaneous Functions) 02 December 2005

This document provides a summary of memory management and miscellaneous functions in OpenCV. It discusses how OpenCV represents images using structures like IplImage and CvMat. It explains how to create, access, and destroy these objects and extract matrix regions from images. The document also introduces dynamic arrays like CvSeq which are stored in CvMemStorage, and stresses the importance of freeing all allocated memory.

Uploaded by

Hamza Sebai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Opencv Tutorial: A Brief Guide To Memory Management (And Other Miscellaneous Functions) 02 December 2005

This document provides a summary of memory management and miscellaneous functions in OpenCV. It discusses how OpenCV represents images using structures like IplImage and CvMat. It explains how to create, access, and destroy these objects and extract matrix regions from images. The document also introduces dynamic arrays like CvSeq which are stored in CvMemStorage, and stresses the importance of freeing all allocated memory.

Uploaded by

Hamza Sebai
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 8

OpenCV Tutorial

Part IV
A Brief Guide to Memory Management
(and other Miscellaneous Functions)

02 December 2005

Gavin S Page [email protected]


Introduction
Why is Managing OpenCV Objects Important?

•Video, 30 frames per second


•Each frame is an image A Top-Level OpenCV Type
•Images are arrays of pixels
•A 640x480 image is 307,200 pixels CvArr* is a function parameter for
•These must be represented in memory several OpenCV functions which accept
•How much memory does your machine have? arrays of more than one type. These
are often IplImage*, CvMat*, or CvSeq*.

How Does OpenCV Represent Images?

IplImage: Structure from Intel Image Processing Library


In addition to representing the image data IplImage holds
utilizes a subset of IPL data useful in IP/CV:
•nSize : Size of Image
•nChannels : Number of Image Channels (1-4)
•width, height
•ROI : Region of Interest (used in Tutorial II)
• and others (see documentation)…

Gavin S Page [email protected] 2


Handling IplImage
Creating an IplImage When allocating IplImage
•cvCreateImage( CvSize size, int depth, int channels ); in a loop be sure to
•header = cvCreateImageHeader(size,depth,channels); deallocate in the loop as
cvCreateData(header); well
•cvCloneImage( const IplImage* image );
•cvLoadImage( const char* filename, int iscolor=1 );
•The first two functions are useful for creating a blank Destroying an IplImage
image of the specified parameters. A possible use is in
functions that require a pointer to a result. •cvReleaseImage( IplImage** image );
•The clone function performs an exact copy of the •cvReleaseData( *image );
IplImage* parameter. cvReleaseImageHeader( image );
•The load function loads an image from a file. • cvReleaseImage will work for the 3 single
step creation functions
•The
An image header is initialized using cvReleaseData/cvReleaseImageHeader
cvInitImageHeader. combination is used when there is separate
data and header information

Gavin S Page [email protected] 3


Utilizing IplImage
The IplImage structure makes it possible to target
specific regions of an object for processing. This Setting the Region of Interest (ROI)
reduces overhead caused by working on the whole
image. The selection can occur at both the channel and •cvSetImageROI( IplImage* image, CvRect
the region. rect );
•cvResetImageROI( IplImage* image );
•cvGetImageROI( const IplImage* image );

Setting the ROI of the image allows the user


Setting the Channel of Interest (COI) to select a rectangular region of the image
to work with. This is useful after localizing
•cvSetImageCOI( IplImage* image, int coi ); objects for extraction and further
•cvGetImageCOI( const IplImage* image ); processing. While the region is set the rest
of the image will be ignored. Meaning any
Setting the channel of the image allows the
operation directed on the image will act on
user to work with a particular layer of the
only the region (including cvShowImage).
image. i.e. The ‘R’ layer of an RGB image or
the ‘V’ layer in the HSV format.
NOTE: Not all OpenCV functions support The CvRect function is used to specify the
this. region in cvSetImageROI.
cvRect( int x, int y, int width, int height );

Gavin S Page [email protected] 4


Other Static Array Types

CvMat

•cvCreateMat( int rows, int cols, int type );


•mat = cvCreateMatHeader( rows, cols, type );
OpenCV also has built in
cvCreateData( mat ); functions for mult-dimensional
•cvCloneMat( const CvMat* mat );
arrays (CvMatND) and sparse
•cvReleaseMat( CvMat** mat );
arrays (CvSparseMat).

OpenCV uses the CvMat* as its general purpose matrix


structure. It is managed in an equivalent style to
IplImage*

Specifying the type of a CvMat is done using the


syntax
CV_<bit_depth>(S|U|F)C<number_of_channels>
i.e. CV_8UC1 for an 8-bit single channel unsigned
Gavin S Page [email protected] 5
Getting Matrix Information
From an IplImage
In order for a matrix to be useful it must be populated
with data. OpenCV makes it possible to fill a matrix with
data from an IplImage.

Extracting Matrix Region

CvRect rect = cvRect(0, 0, 500, 600 );


CvMat* mt = cvCreateMat(500,600, CV_8UC1);
CvMat* sRect = cvGetSubRect(grayImage,mt,rect);
The actual parameters of the cvGetSubRect function are
( const CvArr* arr, CvMat* submat, CvRect rect ) .
This snippet illustrates how to copy matrix header
information from the IplImage. The function does make
use of ROI so this will be useful in specifying a target
region.

Gavin S Page [email protected] 6


Dynamic Arrays
CvSeq CvMemStorage is a
low-level structure used
•cvCreateSeq( int seq_flags, int header_size, int to store dynamic data
elem_size, CvMemStorage* storage ); objects.

CvMemStorage

• cvCreateMemStorage( int block_size=0 );


•cvClearMemStorage( CvMemStorage* storage )

OpenCV uses the CvSeq* to as its own representation


for growable 1-d arrays. It is similar to IplImage* with The sequence is released
regard to the fact that it is a structure with multiple fields by clearing the associated
which are representative of the data content. This
includes a pointer to CvMemStorage which actually CvMemStorage structure.
holds the sequence.

Gavin S Page [email protected] 7


Final Message
As with any C++ program it is
important to destroy all memory
that has been allocated.

Gavin S Page [email protected] 8

You might also like