multimedia programming 02: play with images departments of digital contents sang il park
TRANSCRIPT
Announcement
• Homepage is now open!http://dasan.sejong.ac.kr/~sipark/class2007/mm
Outline
• OpenCV• How to start• Your first OpenCV code: HelloCV• Basic Functions• Pixel• Draw Lines or whatever you want• Conclusion
What is OpenCV?
• OpenCV– Stands for Open Source Computer Vision
Library– Includes 500 functions implementing
algorithms:• Computer vision• Image processing• General Purpose numeric algorithms
– Portable and efficient (C/C++)– Absolutely free!
Who did make this?
• Being developed at Intel since 1999• Available on Windows, Linux and MacOSX.• Extensively used in many companies and
research centers
• Home page: www.intel.com/technology/computing/opencv/
OpenCV
• OpenCV– Intended Applications
• Computer Human Interaction (HCI)• Object Identification• Segmentation and Recognition• Face Recognition• Gesture Recognition• Motion Tracking• Motion Understanding• And so on.
Where you can find this
• http://sourceforge.net/projects/opencvlibrary• http://sourceforge.net/project/showfiles.php?
group_id=22870&package_id=16937
OpenCV structure
CXCOREbasic structures and algoritms,XML support, drawing functions
CVImage processing
and vision algorithms HighGUIGUI, Image and Video I/O
We will mostlyfocus on these twoin this presentation
Make a new project
• Start Visual Studio 6.0 or Visual Studio .Net
• Make a new project– Win32 Console Application Program
• Compile and run it! (Ctrl + F5)
Initial Setup
• Directories:– Includes:
• "C:\Program Files\OpenCV\cv\include" "C:\Program Files\OpenCV\cxcore\include" "C:\Program Files\OpenCV\otherlibs\highgui“
– Libraries:• "C:\Program Files\OpenCV\lib“
• Library files you need:– cv.lib cxcore.lib highgui.lib
Initial Setup
• Include Directories (toolsoptions):• "C:\Program Files\OpenCV\cv\include"
"C:\Program Files\OpenCV\cxcore\include" "C:\Program Files\OpenCV\otherlibs\highgui“
HelloCV
• Type this: #include "stdafx.h"#include <cv.h>#include <cxcore.h>#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[]){
IplImage * img;img = cvLoadImage("d:\\test.jpg");
cvNamedWindow("HelloCV");cvShowImage("HelloCV", img);
cvWaitKey();
cvDestroyWindow("HelloCV");cvReleaseImage(&img);
return 0;}
http://dasan.sejong.ac.kr/~sipark/class2007/mm/code/hellocv.cpp
HelloCV
#include "stdafx.h"#include <cv.h>#include <cxcore.h>#include <highgui.h>
int _tmain(int argc, _TCHAR* argv[]){
IplImage * img;img = cvLoadImage("d:\\test.jpg");
cvNamedWindow("HelloCV");cvShowImage("HelloCV", img);
cvWaitKey();
cvDestroyWindow("HelloCV");cvReleaseImage(&img);
return 0;}
http://dasan.sejong.ac.kr/~sipark/class2007/mm/code/hellocv.cpp
• IplImage
• cvLoadImage• cvReleaseImage
• cvNamedWindow
• cvShowImage• cvDestroyWindo
w
• cvWaitKey
Image structure
• IplImage (Image Processing Library)• typedef struct _IplImage {
int nSize; /* size of iplImage struct */int ID; /* image header version */int nChannels;int alphaChannel;int depth; /* pixel depth in bits */char colorModel[4];char channelSeq[4];int dataOrder;int origin;int align; /* 4- or 8-byte align */int width;int height;struct _IplROI *roi; /* pointer to ROI if any */struct _IplImage *maskROI; /*pointer to mask ROI if any */void *imageId; /* use of the application */struct _IplTileInfo *tileInfo; /* contains information on tiling*/int imageSize; /* useful size in bytes */char *imageData; /* pointer to aligned image */int widthStep; /* size of aligned line in bytes */int BorderMode[4]; /* the top, bottom, left, and right border mode */int BorderConst[4]; /* constants for the top, bottom,left, and right border */char *imageDataOrigin; /* ptr to full, nonaligned image */
} IplImage;•
Image I/O IplImage* cvLoadImage(image_path,
colorness_flag);loads image from file, converts to color or grayscle, if need, and returns it (or returns NULL).
image format is determined by the file contents.#define CV_LOAD_IMAGE_COLOR 1 #define CV_LOAD_IMAGE_GRAYSCALE 0 #define CV_LOAD_IMAGE_UNCHANGED -1 DEFAULT
cvSaveImage(image_path, image);saves image to file, image format is determined from extension.
cvReleaseImage(image_path, image);releases memory
BMP, JPEG, PNG, TIFF, PPM/PGM formats are supported.IplImage* img = cvLoadImage(“picture.jpeg”,-1);if( img ) cvSaveImage( “picture.bmp”, img );
Windows
• cvNamedWindow(window_name, fixed_size_flag);creates window accessed by its name. Window handles repaint, resize events. Its position is remembered in registry:cvNamedWindow(“ViewA”);cvMoveWindow(“ViewA”,300,100);cvDestroyWindow(“ViewA”);…
• cvShowImage(window_name, image);copies the image to window buffer, then repaints it when necessary. {8u|16s|32s|32f}{C1|3|4} are supported.
only the whole window contents can be modified. Dynamic updates of parts of the window are done using operations on images, drawing functions etc.
• cvDestroyWindow(window_name);deletes the window with the given name
User Input
• int cvWaitKey( int delay=0 )waits for a pressed key. After waiting for the given delay, it proceeds. Zero delay means waiting forever until user input. – Delay in milliseconds.
• Good for animating something
• Example) swapping between two images
What else HighGUI can do?• “Smart” windows• Image I/O, rendering• Processing keyboard and other events, timeouts• Trackbars• Mouse callbacks• Video I/O
Pixel Accessing
• Image Coordinate System
• Color Value: CvScalar– A container for 1-,2-,3- or 4-tuples of numbers – typedef struct CvScalar { double val[4]; } CvScalar;
• Example) CvScalar s;s.val[0] = 200; (Blue)s.val[1] = 11; (Green)s.val[2] = 123; (Red)
x
y (0,0)(8,2)
Pixel Accessing
• CvScalar cvGet2D (image, y, x)Gets color value at (x,y). – Example) CvScalar s;
s = cvGet2D(img, 30, 40);
• void cvSet2D (image, y, x, CvScalar)Sets color value at (x,y).– Example) CvScalar s = cvScalar(100,0,0);
cvSet2D(img, 30, 40, s);OR
cvSet2D(img, 30, 40, cvScalar(100,0,0));
ORcvSet2D(img, 30, 40,
CV_RGB(0,0,100));
Create an empty canvas
• IplImage* cvCreateImage( CvSize size, int depth, int channels ); – Depth: color depth: IPL_DEPTH_8U – Channels: 1,2,3,4 (1=grey, 3=color)– Example)
IplImage * img; img = cvCreateImage(cvSize(200,100), IPL_DEPTH_8U,3);
• cvSet(image, CvScalar)– Fill canvas with a color– Example)
cvSet(img, CV_RGB(255,255,255));
So far what you’ve learned:
• IplImage• cvLoadImage (file_name)• cvCreateImage (size, depth, channels)• cvSaveImage (file_name, image)• cvReleaseImage (image)• cvNamedWindow (window_name)• cvShowImage (window_name, image)• cvDestroyWindow (window_name)• cvWaitKey (delay)• cvGet2D (image, y, x)• cvSet2D (image, y, x, cvScalar)
Put everything together!int _tmain(int argc, _TCHAR* argv[]){
IplImage * img;img = cvLoadImage("d:\\test.jpg");
cvNamedWindow("HelloCV");cvShowImage("HelloCV", img);cvWaitKey();
int x,y;for(x=0; x<100; x++)
for(y=0; y<100; y++){
CvScalar s = cvGet2D(img, y,x);s.val[0] +=50; s.val[1] +=50; s.val[2] +=50;cvSet2D(img,y,x,s);
}
cvShowImage("HelloCV", img);cvWaitKey();
cvDestroyWindow("HelloCV");cvReleaseImage(&img);
return 0;}
HelloCV2.cpp
A Simple Function: Line Drawing• Horizontal line drawing:
– void DrawHLine(IplImage * img, int y, int st, int ed, CvScalar color)
image
Do more!
• Design a function that draws a rectangle– void DrawRectangle
(IplImage * img, int x, int y, int w, int h, CvScalar color)
– Don’t forget to make sure a pixel is inside the image
• 0 <= max_x <= img->width• 0 <= max_y <= img->height
• If done, try to draw 50 random boxes with random colors!
(x,y) w
h
For those who want more
• OpenCV Wiki-pages: http://opencvlibrary.sourceforge.net
• For HighGUI:– http://opencvlibrary.sourceforge.net/HighGui
• For cxcore:– http://opencvlibrary.sourceforge.net/CxCore
• Supplied documentation: OpenCV/docs/index.htm, faq.htm
• A Korean Community: http://www.opencv.co.kr/
Program Assignment #1
• Long long times ago in Russian Empire– Sergei Mikhailovich Prokudin-Gorskii
(1863-1944)– A man who pursuing
a colorful future in 1907
http://www.loc.gov/exhibits/empire/
Program Assignment #1
• 4 Images can be found in the class homepage:– http://dasan.sejong.ac.kr/~sipark/class2007/
mm/
• Make a team of two students• Due date: 9/11 before the class
– Submit a written report and email the codes and a execution file
• Type the filename in the cmd window, then generate a color image!
• Grading Policy:– Making a color image: 60– Automatic alignment: 40