數位影像處理
數位影像處理
Appendix 2
Introduction to OpenCV
Speaker: 黃世勳
1
Contents
Introduction
Image Displaying
Image Copying
OpenCV Library
Introduction
Overview
OpenCV is an open source computer vision library
It implements thousands of high-level functions for
computer vision and image processing.
Each function name in OpenCV starts with “cv” :
cvCreateImage, cvSobel, cvAdd, …
It includes many high-level datatypes, such as, Sets,
Trees, Graphs, and Matrices. . .
Introduction
Why OpenCV?
Computer vision market is large and still
continues to grow.
There is no standard APIs like OpenGL for
graphics.
• Research Code
• Very expensive commercial toolkits
• Specialized solution bundled with hardware
Standard library would simplify development of
new applications and solutions much easier.
4/60
Introduction
5/60
Introduction
Library Design
Initial Goal: build high-level and ready-to-use
components, such as gesture recognition.
But, the computer vision problem is always
complex and hard to be solved directly.
The design is to split complex problem into
building block which is a function in OpenCV
6/60
Introduction
No free libraries could do this.
We have to do it ourselves.
7/60
Introduction
8/60
Introduction
OpenCV Installation
Download the OpenCV 2.1 from website
• http://sourceforge.net/projects/opencvlibrary/
Execute the file OpenCV-2.1.0-win32-vs2008.exe
to install the OpenCv 2.1
VC2008 Installation
Download the VC2008 from website
Install the VC2008 on your computer
Introduction
Project Creation
10/60
Introduction
Project Creation
11/60
Introduction
Environment Setup
Include the header files: C:\Program
Files\OpenCV2.1\include\opencv
Introduction
Environment Setup
Include the header files: C:\Program
Files\OpenCV2.1\include\opencv
13/60
Introduction
Environment Setup
Include the library files: C:\Program
Files\OpenCV2.1\lib
14/60
Introduction
Environment Setup
Input the library files to project: cv210.lib
highgui210.lib cxcore210.lib cvaux210.lib
15/60
Introduction
Environment Setup
Input the library files to project: cv210.lib
highgui210.lib cxcore210.lib cvaux210.lib
16/60
Image Displaying
Application
Load the well-known lena.jpg from the file
Display the loaded image on the window
Save the loaded image as result.jpg
Image Displaying
Lena Application: Sample Code
#include "cv.h"
#include "highgui.h "
int _tmain(int argc, _TCHAR* argv[]){
………..
return 0;
}/* End of main body */
Image Displaying
Lena Application: Sample Code
int _tmain(int argc, _TCHAR* argv[]){
IplImage* pImage1=NULL;
pImage1 = cvLoadImage("lena.jpg");
cvNamedWindow("Lena", 1);
cvShowImage("Lena", pImage1);
cvWaitKey(0);
if(pImage1 != NULL)
cvSaveImage(“result.jpg”, pImage1);
return 0;
}/* End of main body */
Image Copying
IplImage Data Structure
typedef struct _IplImage {
int nChannels; /* support 1,2,3 or 4 channels */
int depth; /* pixel depth in bits*/
int width; /* image width in pixels */
int height; /* image height in pixels */
char *imageData; /* pointer to aligned image data */
} IplImage;
Image Copying
IplImage Data Structure
nDepths: data type of the channel
imageData: one-dimensional array
IPL_DEPTH_8U,
IPL_DEPTH_8S,
IPL_DEPTH_16S,
IPL_DEPTH_32S,
IPL_DEPTH_32F
IPL_DEPTH_64F
Image Copying
Image Creating: cvCreateImage();
IplImage* pImage2 = NULL;
pImage2 = cvCreateImage(cvSize(512, 512), IPL_DEPTH_8U, 1);
Image Copying
#include "cv.h"
#include "highgui.h "
int _tmain(int argc, _TCHAR* argv[]){
………..
unsigned char* ptr1 = (unsigned char*) pImage1->imageData;
unsigned char* ptr2 = (unsigned char*) pImage2->imageData;
………..
return 0;
}/* End of main body */
23/60
Image Copying
24/60
Image Copying
Image Data Indexing
offset: i+ j*width R offset: 3*(i+ j*width) + 0
G offset: 3*(i+ j*width) + 1
B offset: 3*(i+ j*width)+2
25/60
Image Copying
unsigned char* ptr1 = (unsigned char*) pImage1->imageData;
unsigned char* ptr2 = (unsigned char*) pImage2->imageData;
int width = pImage1->width;
int height= pImage1->height;
for(int h=0; h < height; h++)
for(int w=0; w < width; w++){
int sum = 0;
sum += ptr1[3 * (w + h * width) + 0];
sum += ptr1[3 * (w + h * width) + 1];
sum += ptr1[3 * (w + h * width) + 2];
ptr2[w + h * width] = sum / 3;
}/* End of for-loop */
Image Copying
OpenCV Library
Description
There are three main OpenCV libraries
• cxcore library
• cv library
• highgui library
The detailed documentation can be found through
the website.
• <installed dir>\docs\index.htm
There are many available samples codes for
testing
OpenCV Library
cxcore Library
Basic Data/Support Linear Algebra Structures
• CvPoint :2D point with integer coordinates
• CvPoint2D32f:2D point with floating-point coordinates
• CvMat :Multi-channel matrix
• IplImage: IPL image header
Operations
• IplImage* cvCreateImage( CvSize size, int depth, int channels );
• IplImage* cvCloneImage( const IplImage* image );
• void cvSetImageROI( IplImage* image, CvRect rect );
OpenCV Library
cv Library
It includes many useful algorithms
• Image Processing
• Structural Analysis
• Motion Analysis Algorithm
Algorithms
• void cvCanny : for edge-detection
• void cvGoodFeaturesToTrack: determine strong corners on
image
• void cvCvtColor :Converts image from one color space to
another
OpenCV Library
Edge-detection Segmentation
Histogram
OpenCV Library
Highgui Library
It includes operations for I/O manipulation
• Simple GUI
• Loading and Saving Images
• Video I/O
CvCapture* capture = 0;
capture = cvCaptureFromCAM() / cvCaptureFromAVI()
IplImage* frame = 0;
frame = cvQueryFrame(capture );
OpenCV Library
Useful Links
OpenCV for Linux : basic tutorial
• http://easynews.dl.sourceforge.net/sourceforge/opencvlibra
ry/ippocv.pdf
Overview of all the capabilities of OpenCV
• http://www.intel.com/research/mrl/research/opencv/overvie
w.htm
Advanced
35
Multimedia Lab.