SlideShare a Scribd company logo
1 of 44
Download to read offline
4th Computer Vision Workshop
February 2011
Content
 Basic Structures
 Arrays, Matrices, and Images
 Matrix and Image Operators
 Drawing Things
 Drawing Text
Basic Structures
Basic Structures (Point)
typedef struct CvPoint
{
int x;
int y;
} CvPoint;
CV_INLINE CvPoint cvPoint(int x, int y)
{
CvPoint p;
p.x = x;
p.y = y;
return p;
}
CvPoint pt1;
pt1.x = 20;
pt1.y = 50
CvPoint pt2 = cvPoint(20, 50);
cxtypes.h
Basic Structures (Point)
typedef struct CvPoint
{
int x;
int y;
} CvPoint;
cxtypes.h
typedef struct CvPoint2D32f
{
float x;
float y;
} CvPoint2D32f;
typedef struct CvPoint2D64f
{
double x;
double y;
} CvPoint2D64f;
typedef struct CvPoint3D32f
{
float x;
float y;
float z;
} CvPoint3D32f;
typedef struct CvPoint3D64f
{
double x;
double y;
double z;
} CvPoint3D64f;
Basic Structures (Size)
typedef struct CvSize
{
int width;
int height;
} CvSize;
cxtypes.h
typedef struct CvSize2D32f
{
float width;
float height;
} CvSize2D32f
typedef struct CvRect
{
int x;
int y;
int width;
int height;
} CvRect;
Basic Structures (Scalar)
typedef struct CvScalar
{
double val[4];
} CvScalar;
cxtypes.h
/* Constructor:
initializes val[0] with val0, val[1] with val1, etc.
*/
inline CvScalar cvScalar( double val0, double val1=0,
double val2=0, double val3=0 );
/* Constructor:
initializes all of val[0]...val[3] with val0123
*/
inline CvScalar cvScalarAll( double val0123 );
/* Constructor:
initializes val[0] with val0, and all of val[1]...val[3] with zeros
*/
inline CvScalar cvRealScalar( double val0 );
Arrays, Matrices, and Images
Array → Matrix → Image
cxtypes.h
Even though OpenCV is implemented in C, the structures used in OpenCV
have an object-oriented design; in effect, IplImage is derived from
CvMat, which is derived from CvArr
Array→Matrix→Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
Array → Matrix → Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
CV_<bit_depth>(S|U|F)C<number_of_channels>
CV_8UC3
CV_32FC1 32-bit floats
unsigned integer
8-bit triplets
Array → Matrix → Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
the length of a row in bytes
a pointer to a data array
Array → Matrix → Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
the height (rows) and
width (cols) of the matrix
Array → Matrix → Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
Matrix Header
Array → Matrix → Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
Some Matrix Operators:
// Create a new rows by cols matrix of type ‘type’
CvMat* cvCreateMat(int rows, int cols, int type)
// Creates only matrix header without allocating data
CvMat* cvCreateMatHeader(int rows, int cols, int type)
// allocates image, matrix or multi-dimensional array data
void cvCreateData(CvArr* arr)
// Initialize header on existing CvMat structure
CvMat* cvInitMatHeader(CvMat* mat,
int rows, int cols, int type,
void* data=NULL, int step=CV_AUTOSTEP)
// Like cvInitMatHeader() but allocates CvMat as well
CvMat cvMat(int rows, int cols, int type, void* data=NULL)
;
// Allocate a new matrix just like the matrix ‘mat’
CvMat* cvCloneMat(const cvMat* mat);
// Free the matrix ‘mat’, both header and data
void cvReleaseMat(CvMat** mat);
Array → Matrix → Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
Accessing matrix elements
Easy way:
CV_MAT_ELEM()
CV_MAT_ELEM_PTR()
CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );
int i, j;
float sum = 0.0f;
for(i=0; i<5; i++)
for(j=0; j<5; j++)
CV_MAT_ELEM(*mat, float, i, j) = (float) i + j;
for(i=0; i<5; i++)
for(j=0; j<5; j++)
sum += CV_MAT_ELEM(*mat, float, i, j);
Array → Matrix → Image
typedef struct CvMat
{
int type;
int step; //# bytes per row
int* refcount; // internal use
int hdr_refcount; // internal use
union{
uchar* ptr;
short* s;
int* i;
float* fl;
double* db;
} data;
union{
int rows;
int height;
};
union{
int cols;
int width;
};
} CvMat;
cxtypes.h
Accessing matrix elements
Easy way:
CV_MAT_ELEM()
CV_MAT_ELEM_PTR()
CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );
int i, j;
float sum = 0.0f;
for(i=0; i<5; i++)
for(j=0; j<5; j++)
*((float*) CV_MAT_ELEM_PTR(*mat, i, j)) = (float) i + j;
for(i=0; i<5; i++)
for(j=0; j<5; j++)
sum += *((float*) CV_MAT_ELEM_PTR(*mat, i, j)) ;
Array → Matrix → Image
typedef struct _IplImage {
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
char colorModel[4];
char channelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI* roi;
struct _IplImage* maskROI;
void* imageId;
struct _IplTileInfo* tileInfo;
int imageSize;
char* imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char* imageDataOrigin;
} IplImage;
Array → Matrix → Image
typedef struct _IplImage {
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
char colorModel[4];
char channelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI* roi;
struct _IplImage* maskROI;
void* imageId;
struct _IplTileInfo* tileInfo;
int imageSize;
char* imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char* imageDataOrigin;
} IplImage;
• nSize - sizeof(IplImage)
• ID - Version, always equals 0
• nChannels - Number of channels. Most OpenCV functions support 1-4 channels.
• alphaChannel - Ignored by OpenCV
• depth - Pixel depth in bits. The supported depths are:
IPL_DEPTH_8U - Unsigned 8-bit integer
IPL_DEPTH_8S - Signed 8-bit integer
IPL_DEPTH_16U - Unsigned 16-bit integer
IPL_DEPTH_16S - Signed 16-bit integer
IPL_DEPTH_32S - Signed 32-bit integer
IPL_DEPTH_32F - Single-precision floating point
IPL_DEPTH_64F - Double-precision floating point
Array → Matrix → Image
typedef struct _IplImage {
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
char colorModel[4];
char channelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI* roi;
struct _IplImage* maskROI;
void* imageId;
struct _IplTileInfo* tileInfo;
int imageSize;
char* imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char* imageDataOrigin;
} IplImage;
• colorModel - Ignored by OpenCV.
• channelSeq - Ignored by OpenCV
• dataOrder - 0 = IPL_DATA_ORDER_PIXEL
1 = IPL_DATA_ORDER_PLANE
• origin - 0 = IPL_ORIGIN_TL
1 = IPL_ORIGIN_BL
• align - OpenCV ignores this and uses widthStep instead.
• width - Image width in pixels
• height - Image height in pixels
Array → Matrix → Image
typedef struct _IplImage {
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
char colorModel[4];
char channelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI* roi;
struct _IplImage* maskROI;
void* imageId;
struct _IplTileInfo* tileInfo;
int imageSize;
char* imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char* imageDataOrigin;
} IplImage;
• roi - Region Of Interest (ROI).
If not NULL, only this image region will be processed.
• maskROI - Must be NULL in OpenCV
• imageId - Must be NULL in OpenCV
• tileInfo - Must be NULL in OpenCV
Array → Matrix → Image
typedef struct _IplImage {
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
char colorModel[4];
char channelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI* roi;
struct _IplImage* maskROI;
void* imageId;
struct _IplTileInfo* tileInfo;
int imageSize;
char* imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char* imageDataOrigin;
} IplImage;
• imageSize - Image data size in bytes.
For interleaved data, this equals
image->height*image->widthStep
• imageData - A pointer to the aligned image data
• widthStep - The size of an aligned image row, in bytes
• BorderMode - Border completion mode, ignored by OpenCV
• BorderConst - Border completion mode, ignored by OpenCV
• imageDataOrigin - A pointer to the origin of the image data
(not necessarily aligned). This is used for
image deallocation.
Accessing Image Data
typedef struct _IplImage {
int nSize;
int ID;
int nChannels;
int alphaChannel;
int depth;
char colorModel[4];
char channelSeq[4];
int dataOrder;
int origin;
int align;
int width;
int height;
struct _IplROI* roi;
struct _IplImage* maskROI;
void* imageId;
struct _IplTileInfo* tileInfo;
int imageSize;
char* imageData;
int widthStep;
int BorderMode[4];
int BorderConst[4];
char* imageDataOrigin;
} IplImage;
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
void saturate_sv( IplImage* img )
{
for( int y=0; y<img->height; y++ ) {
uchar* ptr = (uchar*) (
img->imageData + y * img->widthStep
);
for( int x=0; x<img->width; x++ ) {
ptr[3*x+1] = 255;
ptr[3*x+2] = 255;
}
}
}
int main( int argc, char** argv )
{
IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
saturate_sv(img);
cvShowImage("Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow("Example1");
}
ROI
typedef struct _IplImage {
........................
int width;
int height;
........................
struct _IplROI* roi;
........................
char* imageData;
int widthStep;
........................
} IplImage;
void cvSetImageROI( IplImage* image, CvRect rect );
void cvResetImageROI( IplImage* image );
cvSetImageROI
ROI
typedef struct _IplImage {
........................
int width;
int height;
........................
struct _IplROI* roi;
........................
char* imageData;
int widthStep;
........................
} IplImage;
void cvSetImageROI( IplImage* image, CvRect rect );
void cvResetImageROI( IplImage* image );
cvSetImageROI
cvAddS
ROI
typedef struct _IplImage {
........................
int width;
int height;
........................
struct _IplROI* roi;
........................
char* imageData;
int widthStep;
........................
} IplImage;
void cvSetImageROI( IplImage* image, CvRect rect );
void cvResetImageROI( IplImage* image );
cvSetImageROI
cvAddS
#include <cv.h>
#include <highgui.h>
// ch3_ex3_12 image_name x y width height add#
int main(int argc, char** argv)
{
IplImage* src;
cvNamedWindow("Example3_12_pre", CV_WINDOW_AUTOSIZE);
cvNamedWindow("Example3_12_post", CV_WINDOW_AUTOSIZE);
if( argc == 7 && ((src=cvLoadImage(argv[1],1)) != 0 ))
{
int x = atoi(argv[2]);
int y = atoi(argv[3]);
int width = atoi(argv[4]);
int height = atoi(argv[5]);
int add = atoi(argv[6]);
cvShowImage( "Example3_12_pre", src);
cvSetImageROI(src, cvRect(x,y,width,height));
cvAddS(src, cvScalar(add, add, add),src);
cvResetImageROI(src);
cvShowImage( "Example3_12_post",src);
cvWaitKey();
}
cvReleaseImage( &src );
cvDestroyWindow("Example3_12_pre");
cvDestroyWindow("Example3_12_post");
return 0;
}
Drawing Things
Drawing Functions
 Drawing functions work with matrices/images of
arbitrary depth
 The boundaries of the shapes can be rendered
with antialiasing
 implemented only for 8-bit images for now
 All the functions include the parameter color
that uses a rgb value (that may be constructed
with CV_RGB macro or the cvScalar function )
for color images and brightness for grayscale
images
Drawing Functions
 Clipping
 If a drawn figure is partially or completely outside the
image, the drawing functions clip it.
 sub-pixel accuracy
 many drawing functions can handle pixel coordinates
specified with sub-pixel accuracy, i.e., the coordinates
can be passed as fixed-point numbers, encoded as
integers.
 The number of fractional bits is specified by the shift
parameter and the real point coordinates are calculated
as
Point( , ) Point2f( 2 , 2 )shift shift
x y x y 
  
CV_RGB
Constructs a color value
#define CV_RGB( r, g, b ) cvScalar( (b), (g), (r) )
Line
void cvLine(
CvArr* img,
CvPoint pt1,
CvPoint pt2,
CvScalar color,
int thickness=1,
int line_type=8,
int shift=0
);
Draws a line by the Bresenham’s algorithm
Rectangle
void cvRectangle(
CvArr* img,
CvPoint pt1,
CvPoint pt2,
double color,
int thickness=1
int line_type=8,
int shift=0
);
Draws simple, thick or
filled rectangle
thickness
Thickness of lines that make up
the rectangle.
Negative values, e.g.,
CV_FILLED, make the function
to draw a filled rectangle.
Circle
void cvCircle(
CvArr* img,
CvPoint center,
int radius,
cvScalar color,
int thickness=1
int line_type=8,
int shift=0
);
Draws simple, thick or filled circle
Ellipse
void cvEllipse(
CvArr* img,
CvPoint center,
CvSize axes,
double angle,
double start_angle,
double end_angle,
cvScalar color,
int thickness=1
int line_type=8,
int shift=0
);
Draws simple or thick elliptic arc or
fills ellipse sector
EllipseBox
void cvEllipseBox(
CvArr* img,
CvBox2D box,
cvScalar color,
int thickness=1
int line_type=8,
int shift=0
);
An alternate way to draw ellipse
typedef struct {
CvPoint2D32f center;
CvSize2D32f size;
float angle;
} CvBox2D;
FillPoly
void cvFillPoly(
CvArr* img,
CvPoint** pts,
int* npts,
int contours,
cvScalar color,
int line_type=8,
int shift=0
);
Fills polygons interior
pts
Array of pointers to polygons.
npts
Array of polygon vertex counters.
contours
#contours that bind the filled region.
FillConvexPoly
void cvFillConvexPoly(
CvArr* img,
CvPoint* pts,
int npts,
cvScalar color,
int thickness=1
int line_type=8,
int shift=0
);
Fills convex polygon
pts
Array of pointers to a single polygon
npts
Polygon vertex counter
PolyLine
void cvPolyLine(
CvArr* img,
CvPoint** pts,
int* npts,
int contours,
int is_closed,
cvScalar color,
int thickness=1
int line_type=8,
int shift=0
);
Draws simple or thick polygons
pts
Array of pointers to polygons.
npts
Array of polygon vertex counters.
contours
#contours that bind the filled region.
is_closed
Indicates whether the polylines must
be drawn closed.
Drawing Text
InitFont
void cvInitFont(
CvFont* font,
int font_face,
double hscale,
double vscale,
double shear=0,
int thickness=1,
int line_type=8
);
Initializes font structure
InitFont
void cvInitFont(
CvFont* font,
int font_face,
double hscale,
double vscale,
double shear=0,
int thickness=1,
int line_type=8
);
Initializes font structure
InitFont
void cvInitFont(
CvFont* font,
int font_face,
double hscale,
double vscale,
double shear=0,
int thickness=1,
int line_type=8
);
Initializes font structure
PutText
void cvPutText(
CvArr* img,
const char* text,
CvPoint org,
const CvFont* font,
CvScalar color
);
Draws text string
GetTextSize
void cvGetTextSize(
const char* text_string,
const CvFont* font,
CvSize* text_size,
int* baseline
);
Retrieves width and height of text string

More Related Content

What's hot

What's hot (20)

Multidimensional arrays in C++
Multidimensional arrays in C++Multidimensional arrays in C++
Multidimensional arrays in C++
 
Unit4 Slides
Unit4 SlidesUnit4 Slides
Unit4 Slides
 
Boost.Interfaces
Boost.InterfacesBoost.Interfaces
Boost.Interfaces
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Pointers
PointersPointers
Pointers
 
#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started#OOP_D_ITS - 2nd - C++ Getting Started
#OOP_D_ITS - 2nd - C++ Getting Started
 
OpenGL ES 3 Reference Card
OpenGL ES 3 Reference CardOpenGL ES 3 Reference Card
OpenGL ES 3 Reference Card
 
pointers 1
pointers 1pointers 1
pointers 1
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
Smart pointers
Smart pointersSmart pointers
Smart pointers
 
Codejunk Ignitesd
Codejunk IgnitesdCodejunk Ignitesd
Codejunk Ignitesd
 
C++11 smart pointer
C++11 smart pointerC++11 smart pointer
C++11 smart pointer
 
C++ 11 Features
C++ 11 FeaturesC++ 11 Features
C++ 11 Features
 
Hooking signals and dumping the callstack
Hooking signals and dumping the callstackHooking signals and dumping the callstack
Hooking signals and dumping the callstack
 
C chap08
C chap08C chap08
C chap08
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0C++11 - A Change in Style - v2.0
C++11 - A Change in Style - v2.0
 
Array notes
Array notesArray notes
Array notes
 
Pointers [compatibility mode]
Pointers [compatibility mode]Pointers [compatibility mode]
Pointers [compatibility mode]
 

Viewers also liked

OpenCV祭り (配布用)
OpenCV祭り (配布用)OpenCV祭り (配布用)
OpenCV祭り (配布用)tomoaki0705
 
Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009Enthought, Inc.
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonTariq Rashid
 
Images and Vision in Python
Images and Vision in PythonImages and Vision in Python
Images and Vision in Pythonstreety
 
PCAP Graphs for Cybersecurity and System Tuning
PCAP Graphs for Cybersecurity and System TuningPCAP Graphs for Cybersecurity and System Tuning
PCAP Graphs for Cybersecurity and System TuningDr. Mirko Kämpf
 
Realtime Detection of DDOS attacks using Apache Spark and MLLib
Realtime Detection of DDOS attacks using Apache Spark and MLLibRealtime Detection of DDOS attacks using Apache Spark and MLLib
Realtime Detection of DDOS attacks using Apache Spark and MLLibRyan Bosshart
 
파이썬 Numpy 선형대수 이해하기
파이썬 Numpy 선형대수 이해하기파이썬 Numpy 선형대수 이해하기
파이썬 Numpy 선형대수 이해하기Yong Joon Moon
 
Real time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache SparkReal time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache SparkRahul Jain
 

Viewers also liked (12)

1st section
1st section1st section
1st section
 
OpenCV祭り (配布用)
OpenCV祭り (配布用)OpenCV祭り (配布用)
OpenCV祭り (配布用)
 
Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009Scientific Computing with Python Webinar --- May 22, 2009
Scientific Computing with Python Webinar --- May 22, 2009
 
Arrays
ArraysArrays
Arrays
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with Python
 
Images and Vision in Python
Images and Vision in PythonImages and Vision in Python
Images and Vision in Python
 
Enter The Matrix
Enter The MatrixEnter The Matrix
Enter The Matrix
 
PCAP Graphs for Cybersecurity and System Tuning
PCAP Graphs for Cybersecurity and System TuningPCAP Graphs for Cybersecurity and System Tuning
PCAP Graphs for Cybersecurity and System Tuning
 
Realtime Detection of DDOS attacks using Apache Spark and MLLib
Realtime Detection of DDOS attacks using Apache Spark and MLLibRealtime Detection of DDOS attacks using Apache Spark and MLLib
Realtime Detection of DDOS attacks using Apache Spark and MLLib
 
파이썬 Numpy 선형대수 이해하기
파이썬 Numpy 선형대수 이해하기파이썬 Numpy 선형대수 이해하기
파이썬 Numpy 선형대수 이해하기
 
Getting started with image processing using Matlab
Getting started with image processing using MatlabGetting started with image processing using Matlab
Getting started with image processing using Matlab
 
Real time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache SparkReal time Analytics with Apache Kafka and Apache Spark
Real time Analytics with Apache Kafka and Apache Spark
 

Similar to 4th Computer Vision Workshop: Basic Structures, Matrices, Images, and Operators

Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfgiriraj65
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07HUST
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Appletbackdoor
 
Some examples of the 64-bit code errors
Some examples of the 64-bit code errorsSome examples of the 64-bit code errors
Some examples of the 64-bit code errorsPVS-Studio
 
array 2 (1)_merged.pdf
array  2 (1)_merged.pdfarray  2 (1)_merged.pdf
array 2 (1)_merged.pdfPradiptaPaul7
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++Yung-Yu Chen
 
Instancing
InstancingInstancing
Instancingacbess
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab ManualAkhilaaReddy
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
TensorFlow Quantization Tour
TensorFlow Quantization TourTensorFlow Quantization Tour
TensorFlow Quantization TourKSuzukiii
 
426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer Tools426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer ToolsMark Billinghurst
 

Similar to 4th Computer Vision Workshop: Basic Structures, Matrices, Images, and Operators (20)

Using an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdfUsing an Array include ltstdiohgt include ltmpih.pdf
Using an Array include ltstdiohgt include ltmpih.pdf
 
Csphtp1 07
Csphtp1 07Csphtp1 07
Csphtp1 07
 
Client Side Programming with Applet
Client Side Programming with AppletClient Side Programming with Applet
Client Side Programming with Applet
 
Some examples of the 64-bit code errors
Some examples of the 64-bit code errorsSome examples of the 64-bit code errors
Some examples of the 64-bit code errors
 
array 2 (1)_merged.pdf
array  2 (1)_merged.pdfarray  2 (1)_merged.pdf
array 2 (1)_merged.pdf
 
SimpleArray between Python and C++
SimpleArray between Python and C++SimpleArray between Python and C++
SimpleArray between Python and C++
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Instancing
InstancingInstancing
Instancing
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
VTU DSA Lab Manual
VTU DSA Lab ManualVTU DSA Lab Manual
VTU DSA Lab Manual
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Day 1
Day 1Day 1
Day 1
 
pointers
pointerspointers
pointers
 
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizationsEgor Bogatov - .NET Core intrinsics and other micro-optimizations
Egor Bogatov - .NET Core intrinsics and other micro-optimizations
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
C# basics
C# basicsC# basics
C# basics
 
TensorFlow Quantization Tour
TensorFlow Quantization TourTensorFlow Quantization Tour
TensorFlow Quantization Tour
 
426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer Tools426 lecture 4: AR Developer Tools
426 lecture 4: AR Developer Tools
 
PostThis
PostThisPostThis
PostThis
 
Exploiting vectorization with ISPC
Exploiting vectorization with ISPCExploiting vectorization with ISPC
Exploiting vectorization with ISPC
 

4th Computer Vision Workshop: Basic Structures, Matrices, Images, and Operators

  • 1. 4th Computer Vision Workshop February 2011
  • 2. Content  Basic Structures  Arrays, Matrices, and Images  Matrix and Image Operators  Drawing Things  Drawing Text
  • 4. Basic Structures (Point) typedef struct CvPoint { int x; int y; } CvPoint; CV_INLINE CvPoint cvPoint(int x, int y) { CvPoint p; p.x = x; p.y = y; return p; } CvPoint pt1; pt1.x = 20; pt1.y = 50 CvPoint pt2 = cvPoint(20, 50); cxtypes.h
  • 5. Basic Structures (Point) typedef struct CvPoint { int x; int y; } CvPoint; cxtypes.h typedef struct CvPoint2D32f { float x; float y; } CvPoint2D32f; typedef struct CvPoint2D64f { double x; double y; } CvPoint2D64f; typedef struct CvPoint3D32f { float x; float y; float z; } CvPoint3D32f; typedef struct CvPoint3D64f { double x; double y; double z; } CvPoint3D64f;
  • 6. Basic Structures (Size) typedef struct CvSize { int width; int height; } CvSize; cxtypes.h typedef struct CvSize2D32f { float width; float height; } CvSize2D32f typedef struct CvRect { int x; int y; int width; int height; } CvRect;
  • 7. Basic Structures (Scalar) typedef struct CvScalar { double val[4]; } CvScalar; cxtypes.h /* Constructor: initializes val[0] with val0, val[1] with val1, etc. */ inline CvScalar cvScalar( double val0, double val1=0, double val2=0, double val3=0 ); /* Constructor: initializes all of val[0]...val[3] with val0123 */ inline CvScalar cvScalarAll( double val0123 ); /* Constructor: initializes val[0] with val0, and all of val[1]...val[3] with zeros */ inline CvScalar cvRealScalar( double val0 );
  • 9. Array → Matrix → Image cxtypes.h Even though OpenCV is implemented in C, the structures used in OpenCV have an object-oriented design; in effect, IplImage is derived from CvMat, which is derived from CvArr
  • 10. Array→Matrix→Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h
  • 11. Array → Matrix → Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h CV_<bit_depth>(S|U|F)C<number_of_channels> CV_8UC3 CV_32FC1 32-bit floats unsigned integer 8-bit triplets
  • 12. Array → Matrix → Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h the length of a row in bytes a pointer to a data array
  • 13. Array → Matrix → Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h the height (rows) and width (cols) of the matrix
  • 14. Array → Matrix → Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h Matrix Header
  • 15. Array → Matrix → Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h Some Matrix Operators: // Create a new rows by cols matrix of type ‘type’ CvMat* cvCreateMat(int rows, int cols, int type) // Creates only matrix header without allocating data CvMat* cvCreateMatHeader(int rows, int cols, int type) // allocates image, matrix or multi-dimensional array data void cvCreateData(CvArr* arr) // Initialize header on existing CvMat structure CvMat* cvInitMatHeader(CvMat* mat, int rows, int cols, int type, void* data=NULL, int step=CV_AUTOSTEP) // Like cvInitMatHeader() but allocates CvMat as well CvMat cvMat(int rows, int cols, int type, void* data=NULL) ; // Allocate a new matrix just like the matrix ‘mat’ CvMat* cvCloneMat(const cvMat* mat); // Free the matrix ‘mat’, both header and data void cvReleaseMat(CvMat** mat);
  • 16. Array → Matrix → Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h Accessing matrix elements Easy way: CV_MAT_ELEM() CV_MAT_ELEM_PTR() CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 ); int i, j; float sum = 0.0f; for(i=0; i<5; i++) for(j=0; j<5; j++) CV_MAT_ELEM(*mat, float, i, j) = (float) i + j; for(i=0; i<5; i++) for(j=0; j<5; j++) sum += CV_MAT_ELEM(*mat, float, i, j);
  • 17. Array → Matrix → Image typedef struct CvMat { int type; int step; //# bytes per row int* refcount; // internal use int hdr_refcount; // internal use union{ uchar* ptr; short* s; int* i; float* fl; double* db; } data; union{ int rows; int height; }; union{ int cols; int width; }; } CvMat; cxtypes.h Accessing matrix elements Easy way: CV_MAT_ELEM() CV_MAT_ELEM_PTR() CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 ); int i, j; float sum = 0.0f; for(i=0; i<5; i++) for(j=0; j<5; j++) *((float*) CV_MAT_ELEM_PTR(*mat, i, j)) = (float) i + j; for(i=0; i<5; i++) for(j=0; j<5; j++) sum += *((float*) CV_MAT_ELEM_PTR(*mat, i, j)) ;
  • 18. Array → Matrix → Image typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI* roi; struct _IplImage* maskROI; void* imageId; struct _IplTileInfo* tileInfo; int imageSize; char* imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char* imageDataOrigin; } IplImage;
  • 19. Array → Matrix → Image typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI* roi; struct _IplImage* maskROI; void* imageId; struct _IplTileInfo* tileInfo; int imageSize; char* imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char* imageDataOrigin; } IplImage; • nSize - sizeof(IplImage) • ID - Version, always equals 0 • nChannels - Number of channels. Most OpenCV functions support 1-4 channels. • alphaChannel - Ignored by OpenCV • depth - Pixel depth in bits. The supported depths are: IPL_DEPTH_8U - Unsigned 8-bit integer IPL_DEPTH_8S - Signed 8-bit integer IPL_DEPTH_16U - Unsigned 16-bit integer IPL_DEPTH_16S - Signed 16-bit integer IPL_DEPTH_32S - Signed 32-bit integer IPL_DEPTH_32F - Single-precision floating point IPL_DEPTH_64F - Double-precision floating point
  • 20. Array → Matrix → Image typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI* roi; struct _IplImage* maskROI; void* imageId; struct _IplTileInfo* tileInfo; int imageSize; char* imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char* imageDataOrigin; } IplImage; • colorModel - Ignored by OpenCV. • channelSeq - Ignored by OpenCV • dataOrder - 0 = IPL_DATA_ORDER_PIXEL 1 = IPL_DATA_ORDER_PLANE • origin - 0 = IPL_ORIGIN_TL 1 = IPL_ORIGIN_BL • align - OpenCV ignores this and uses widthStep instead. • width - Image width in pixels • height - Image height in pixels
  • 21. Array → Matrix → Image typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI* roi; struct _IplImage* maskROI; void* imageId; struct _IplTileInfo* tileInfo; int imageSize; char* imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char* imageDataOrigin; } IplImage; • roi - Region Of Interest (ROI). If not NULL, only this image region will be processed. • maskROI - Must be NULL in OpenCV • imageId - Must be NULL in OpenCV • tileInfo - Must be NULL in OpenCV
  • 22. Array → Matrix → Image typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI* roi; struct _IplImage* maskROI; void* imageId; struct _IplTileInfo* tileInfo; int imageSize; char* imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char* imageDataOrigin; } IplImage; • imageSize - Image data size in bytes. For interleaved data, this equals image->height*image->widthStep • imageData - A pointer to the aligned image data • widthStep - The size of an aligned image row, in bytes • BorderMode - Border completion mode, ignored by OpenCV • BorderConst - Border completion mode, ignored by OpenCV • imageDataOrigin - A pointer to the origin of the image data (not necessarily aligned). This is used for image deallocation.
  • 23. Accessing Image Data typedef struct _IplImage { int nSize; int ID; int nChannels; int alphaChannel; int depth; char colorModel[4]; char channelSeq[4]; int dataOrder; int origin; int align; int width; int height; struct _IplROI* roi; struct _IplImage* maskROI; void* imageId; struct _IplTileInfo* tileInfo; int imageSize; char* imageData; int widthStep; int BorderMode[4]; int BorderConst[4]; char* imageDataOrigin; } IplImage; #include <stdio.h> #include <cv.h> #include <highgui.h> void saturate_sv( IplImage* img ) { for( int y=0; y<img->height; y++ ) { uchar* ptr = (uchar*) ( img->imageData + y * img->widthStep ); for( int x=0; x<img->width; x++ ) { ptr[3*x+1] = 255; ptr[3*x+2] = 255; } } } int main( int argc, char** argv ) { IplImage* img = cvLoadImage( argv[1] ); cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE ); saturate_sv(img); cvShowImage("Example1", img ); cvWaitKey(0); cvReleaseImage( &img ); cvDestroyWindow("Example1"); }
  • 24. ROI typedef struct _IplImage { ........................ int width; int height; ........................ struct _IplROI* roi; ........................ char* imageData; int widthStep; ........................ } IplImage; void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); cvSetImageROI
  • 25. ROI typedef struct _IplImage { ........................ int width; int height; ........................ struct _IplROI* roi; ........................ char* imageData; int widthStep; ........................ } IplImage; void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); cvSetImageROI cvAddS
  • 26. ROI typedef struct _IplImage { ........................ int width; int height; ........................ struct _IplROI* roi; ........................ char* imageData; int widthStep; ........................ } IplImage; void cvSetImageROI( IplImage* image, CvRect rect ); void cvResetImageROI( IplImage* image ); cvSetImageROI cvAddS #include <cv.h> #include <highgui.h> // ch3_ex3_12 image_name x y width height add# int main(int argc, char** argv) { IplImage* src; cvNamedWindow("Example3_12_pre", CV_WINDOW_AUTOSIZE); cvNamedWindow("Example3_12_post", CV_WINDOW_AUTOSIZE); if( argc == 7 && ((src=cvLoadImage(argv[1],1)) != 0 )) { int x = atoi(argv[2]); int y = atoi(argv[3]); int width = atoi(argv[4]); int height = atoi(argv[5]); int add = atoi(argv[6]); cvShowImage( "Example3_12_pre", src); cvSetImageROI(src, cvRect(x,y,width,height)); cvAddS(src, cvScalar(add, add, add),src); cvResetImageROI(src); cvShowImage( "Example3_12_post",src); cvWaitKey(); } cvReleaseImage( &src ); cvDestroyWindow("Example3_12_pre"); cvDestroyWindow("Example3_12_post"); return 0; }
  • 28. Drawing Functions  Drawing functions work with matrices/images of arbitrary depth  The boundaries of the shapes can be rendered with antialiasing  implemented only for 8-bit images for now  All the functions include the parameter color that uses a rgb value (that may be constructed with CV_RGB macro or the cvScalar function ) for color images and brightness for grayscale images
  • 29. Drawing Functions  Clipping  If a drawn figure is partially or completely outside the image, the drawing functions clip it.  sub-pixel accuracy  many drawing functions can handle pixel coordinates specified with sub-pixel accuracy, i.e., the coordinates can be passed as fixed-point numbers, encoded as integers.  The number of fractional bits is specified by the shift parameter and the real point coordinates are calculated as Point( , ) Point2f( 2 , 2 )shift shift x y x y    
  • 30. CV_RGB Constructs a color value #define CV_RGB( r, g, b ) cvScalar( (b), (g), (r) )
  • 31. Line void cvLine( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color, int thickness=1, int line_type=8, int shift=0 ); Draws a line by the Bresenham’s algorithm
  • 32. Rectangle void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, double color, int thickness=1 int line_type=8, int shift=0 ); Draws simple, thick or filled rectangle thickness Thickness of lines that make up the rectangle. Negative values, e.g., CV_FILLED, make the function to draw a filled rectangle.
  • 33. Circle void cvCircle( CvArr* img, CvPoint center, int radius, cvScalar color, int thickness=1 int line_type=8, int shift=0 ); Draws simple, thick or filled circle
  • 34. Ellipse void cvEllipse( CvArr* img, CvPoint center, CvSize axes, double angle, double start_angle, double end_angle, cvScalar color, int thickness=1 int line_type=8, int shift=0 ); Draws simple or thick elliptic arc or fills ellipse sector
  • 35. EllipseBox void cvEllipseBox( CvArr* img, CvBox2D box, cvScalar color, int thickness=1 int line_type=8, int shift=0 ); An alternate way to draw ellipse typedef struct { CvPoint2D32f center; CvSize2D32f size; float angle; } CvBox2D;
  • 36. FillPoly void cvFillPoly( CvArr* img, CvPoint** pts, int* npts, int contours, cvScalar color, int line_type=8, int shift=0 ); Fills polygons interior pts Array of pointers to polygons. npts Array of polygon vertex counters. contours #contours that bind the filled region.
  • 37. FillConvexPoly void cvFillConvexPoly( CvArr* img, CvPoint* pts, int npts, cvScalar color, int thickness=1 int line_type=8, int shift=0 ); Fills convex polygon pts Array of pointers to a single polygon npts Polygon vertex counter
  • 38. PolyLine void cvPolyLine( CvArr* img, CvPoint** pts, int* npts, int contours, int is_closed, cvScalar color, int thickness=1 int line_type=8, int shift=0 ); Draws simple or thick polygons pts Array of pointers to polygons. npts Array of polygon vertex counters. contours #contours that bind the filled region. is_closed Indicates whether the polylines must be drawn closed.
  • 40. InitFont void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 ); Initializes font structure
  • 41. InitFont void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 ); Initializes font structure
  • 42. InitFont void cvInitFont( CvFont* font, int font_face, double hscale, double vscale, double shear=0, int thickness=1, int line_type=8 ); Initializes font structure
  • 43. PutText void cvPutText( CvArr* img, const char* text, CvPoint org, const CvFont* font, CvScalar color ); Draws text string
  • 44. GetTextSize void cvGetTextSize( const char* text_string, const CvFont* font, CvSize* text_size, int* baseline ); Retrieves width and height of text string