108
OPEN CV 1

C3 OPEN CV

Embed Size (px)

Citation preview

  • OPEN CV*

  • GII THIUOpenCV l vit tt ca Open Source Computer Vision , l th vin x l nh m ngun m hon ton min ph ca Intel. OpenCV l mt th vin m gm cc hm c xy dng phc v cho vic x l th gic my thi gian thc (Real time computer vision). Cc thut ton x l nh thng thng ln cao cp u c ti u ha bi cc nh pht trin th vin thnh cc hm n gin v rt d s dng. Cc version: 1.0, 1.1, 2.0, 2.1, 2.2Thng 10 nm 2009, OpenCV ra phin bn th 2. OpenCV 2.0 bao gm nhng thay i ln vo giao din C++ Visual Studio 6.0 , nhm d dng hn, nhiu kiu-mu an ton, cc chc nng mi, v thc thi tt hn v hiu sut (c bit l trn h thng a li ngy cng ph bin).Hin nay c bn Open CV 2.2.0 dng vi Visual Studio 2010 trn trang web http://sourceforge.net/projects/opencvlibrary/http://www.intel.com/research /mrl/research/opencv/ *

  • GII THIUNu dng C# ta download thm Emgu CV 2.2.0 l wrapper ca opencvhttp://sourceforge.net/projects/emgucv/files/

    Hoc dng Opencv1.0 kt hp opencv.net http://downloads.sourceforge.net/opencvlibrary/OpenCV_1.0.exe?modtime=1161287502&big_mirror=1http://opencvdotnet.googlecode.com/files/opencvdotnet-0.7-setup.exeCi visual c# 2008 express edition*

  • GII THIUTnh ti thi im hin ti (2014) OpenCV bao gm hn 2500 thut ton gii thut ng dng trn hn 28 lnh vc ca x l nh, vi nhng u im sau:ng thi h tr rt nhiu h iu hnh nh: Window, Linux, v MacOS X. Android, IOSVit trn nn tng C/C++, Python, Java. Ngoi ra n cn c th vit trn nn C# hay VB vi vic dng cc th vin h tr truy xut OpenCV.Lun lun cp nht lin tc nhng gii thut mi Ti liu quan trng hc Open CV l quyn Learning OpenCV ca Garry Bradski v Adrian Kaehler, nh xut bn ORelly Media Inc, 2008http://opencv.willowgarage.com/documentation/cpp/index.html

    *

  • Nhng im c trng

    Image and Video I/ONhng giao din ny s gip bn c c d liu nh t file hoc trc tip t video. Bn cng c th to cc file nh v video vi giao din nyTh gic my v cc thut ton x l nh ( General computer-vision and image-processing algorithms (mid and low level APIs))S dng nhng giao din ny, bn c th thc hnh vi rt nhiu chun th gic my m khng cn phi c m ngun ca chng. Modul th gic my cp caoOpenCV gm mt vi p dng cp cao. Thm vo nhn dng mt, d tm, theo di. N bao gm lung th gic ( s dng camera di ng xc nh cu trc 3D), kch c camera v m thanh ni.*

  • Nhng im c trng

    AI and machine-learningCc ng dng ca th gic my thng yu cu my mc phi hc ( machine learning) hoc cc hnh thc tr tu nhn to khc. Mt vi trong s chng l c sn trong gi OpenCVLy mu nh v php bin i N thng rt tt cho qu trnh x l mt nhm phn t nh nh l mt n v. OpenCV bao gm ly tch ra, ly mu ngu nhin, phc ch, xoay nh, lm cong nh ( warping), thay i hiu ng ca nh.Cch thc to v phn tch nh nh phnnh nh phn thng xuyn c dng trong cc h thng kim tra c khuyt im hnh dng hoc cc b phn quan trng. S biu din nh cng rt thun tin khi chng ta bit r vt th cn bt.*

  • Nhng im c trng

    Cc phng php tnh ton thng tin 3D Nhng hm ny rt c ch khi cn sp xp v xc nh mt khi lp th (with a stereo rig) hoc vi khng gian nhn phc tp (multiple views) t mt camera ring.Cc php ton cho x l nh, th gic my v biu din nhOpenCV s dng cc php ton ph bin nh: i s hc, thng k v tnh ton hnh hc haNhng giao din ny gip bn vit ch v v trn hnh nh. Thm vo nhng chc nng ny c s dng nhiu trong ghi nhn v nh du. V d nu bn vit mt chng trnh cn nhn dng nhiu i tng th n s rt c ch cho to nhn nh (label image) vi kch thc v v tr.*

  • Nhng im c trng

    Phng thc GUI Giao din ngi dng (Graphic user Interface)OpenCV bao gm ca s giao din ca chnh bn thn n. Trong khi nhng giao din ny c so snh gii hn vi kh nng c th thc hin trong mi mi trng. Chng cung cp nhng mi trng API a phng tin v n gin hin th hnh nh, cho php ngi dng nhp d liu thng qua chut, bn phm v iu khin qu trnh.Cu trc d liu v gii thutVi nhng giao din ny bn c th gi li, tm kim, lu v cch danh mc iu khin, cc tuyn tp(cng nh cc tp hp lnh c gi ), ha v s nhnh mt cch hiu qu.*

  • CI T OPENCVDownload phn mm Open CV 2.1 a ch http://SourceForge.net/projects/opencvlibraryCi t phn mmHng dn OpenCV Reference Manual hn 1000 trang nm th mc /opencv/doc/opencv.pdf, l th mc ci OpenCVC th lp trnh x l nh dng OpenCV vi VC++ trong Visual Studio 6.0 hay Visual Studio t 2005 tr iC th lp trnh vi C# bng cch dng thm phn mm bc wrapper EmgucvVi VS 6.0 ta dng opencv1.0, chp cc file *.dll trong th mc opencv\bin vo th mc windows\system32.*

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0Ci OpenCV 1.0 vo th mc C:\Program Files\OpenCV

    *

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0Khai bo ng dn My Computer bm chut phi --> Properties -System Properties --> Advanced --> Environment Variables --> User variable --> highlight PATH --> Edit.Thm C:\Program Files\OpenCV\bin vo PATH. Cc ng dn cch nhau bng du chm phyNu m VC th ng li VC cp nht path

    *

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0Vo th mc tools- option-directories thm cc ng dn*

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0*

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0*

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0To mt project, v d compvi, loi MFCAppWizard(exe) loi dialog based. Trong file compviDlg.h thm cc dng #include "cv.h"#include "highgui.h"#include "math.h"#include #include #include #include #include Vo ca s Project settings*

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0hp thoi Project Settings xut hin, chn tab C/C++. Trn tab ny, t Category tr xung, chn Preprocessor. Sau , ta thm cc ng dn sau vo hp hp text: Additional Include DirectoriesC:\Program Files\OpenCV\cv\includeC:\Program Files\OpenCV\cxcore\includeC:\Program Files\OpenCV\cvaux\includeC:\Program Files\OpenCV\otherlibs\cvcam\includeC:\Program Files\OpenCV\otherlibs\highguiCh : Cc ng dn trn ph thuc vo vic OpenCV c ci u, y OpenCV c ci C:\Program Files\. Nu OpenCV c ci ng dn khc th cc ng dn trn cng s thay i theo.

    *

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0Ta s nhn thy trc tip cc ng dn trn c thm vo Project Options bn di:

    *

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0Tip theo, cng trong hp thoi Project Settings, chng ta chuyn sang chn tab Link. Trn tab ny, trong Category tr xung, chn General. Trong hp text Object/Library modules, ta nhp cc thu vin sau vo v nhn Enter kt thc:cv.libcxcore.libhighgui.libcvcam.libcxts.libtrs.lib

    *

  • CCH S DNG OPENCV1.0 TRONG VISUAL C++ 6.0Trong BOOL CCompviDlg::OnInitDialog() thm // TODO: Add extra initialization here//Load image into imgIplImage* img = cvLoadImage("SDC16361.jpg");//Create a window with name "Vidu coban" type autosizecvNamedWindow("Vi du coban", CV_WINDOW_AUTOSIZE);//Show the image into that windowcvShowImage("Vidu coban", img);//Wait for a key pressedcvWaitKey(0);//Then release image and destroy windowcvReleaseImage(&img);cvDestroyWindow("Vidu coban");SDC16361.jpg l file nh trong th mc CompviSave AllBuild, Debug , nh s xut hin, Ta ci t thnh cng UpenCV

    *

  • CCH S DNG OPENCV2.1 TRONG VISUAL C++ 6.0

    To mt Project c tn bt k (kiu Dialog Based), chng hn tn l SHAPEDETECTIONTrong file SHAPEDETECTIONDlg.h, trong phn nh ngha Include, trc phn nh ngha lp C SHAPEDETECTIONDlg.cpp, chng ta thm cc header files sau y:#include "cv.h"#include "highgui.h"#include "math.h"#include #include #include #include #include Tip theo, t Project->Settings, hp thoi Project Settings xut hin, chn tab C/C++. Trn tab ny, t Category tr xung, chn Preprocessor. Sau , ta thm cc ng dn vo hp text: Additional Include Directories ch ng cc file header

    *

  • CCH S DNG OPENCV2.1TRONG VISUAL C++ 6.0c:\opencv2.1\include\opencv\c:\opencv2.1\lib\Tip theo, cng trong hp thoi Project Settings, chng ta chuyn sang chn tab Link. Trn tab ny, trog Category tr xung, chn General. Trong hp text Object/Library modules, ta nhp cc thu vin sau vo v nhn Enter kt thc:cv210.libcxcore210.libhighgui210.lib*

  • *

  • OPENCV V VS6.0*

  • CCH S DNG OPENCV TRONG VISUAL STUDIO C++2008To ProjectVo Tools Option Projects and Solutions VC++ directories, b sung cc ng dn tr n cc file *.h, *.lib

    *

  • CCH S DNG OPENCV TRONG VISUAL STUDIO C++2008*

  • CCH S DNG OPENCV TRONG VISUAL STUDIO C++2008*

  • CCH S DNG OPENCV 2.1 TRONG VISUAL STUDIO C++2008To project, v d first try loi win32 console application, vit chng trnh

    *// first try.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "cv.h"#include "highgui.h"int main( int argc, char** argv ) {IplImage* img = cvLoadImage( "anh.jpg" );cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );cvShowImage( "Example1", img );cvWaitKey(0);cvReleaseImage( &img );cvDestroyWindow( "Example1" );}

  • CCH S DNG OPENCV TRONG VISUAL STUDIO C++2008Vo Project- Properties -Configuration properties -Linker- Input -Additional Dependencies, khai bo cc file lib*

  • CCH S DNG OPENCV 2.1 TRONG VISUAL STUDIO C++2008Build project to file exe Hoc vo Debug- Start Debgging*

  • OPENCV2.2 V VS2010Ta cng lm cc bc trn Vo File New Project- Visual C++ Win32- Win32 Console Application to Project vidu1 *

  • OPENCV V VS2010Vo Project- vidu1 Properties VC++Directories khai bo cc ng dn *

  • OPENCV V VS2010*

  • OPENCV V VS2010*C:\opencv2.2\includeC:\\opencv2.2\include\opencvC:\\opencv2.2\include\opencv2C:\opencv2.2\lib

  • OPENCV V VS2010*

  • OPENCV V VS2010*

  • OPENCV V VS2010*

  • OPENCV V VS2010*Vo Linker Input Additional Dependencies

  • OPENCV V VS2010*

  • Chp cc tn file lib voopencv_calib3d220.lib opencv_calib3d220d.lib opencv_contrib220.lib opencv_contrib220d.lib opencv_core220.lib opencv_core220d.lib opencv_features2d220.lib opencv_features2d220d.lib opencv_ffmpeg220.lib opencv_ffmpeg220d.lib opencv_flann220.lib opencv_flann220d.lib opencv_gpu220.lib opencv_gpu220d.lib opencv_highgui220.lib opencv_highgui220d.lib opencv_imgproc220.lib opencv_imgproc220d.lib opencv_legacy220.lib opencv_legacy220d.lib opencv_ml220.lib opencv_ml220d.lib opencv_objdetect220.lib opencv_objdetect220d.lib opencv_ts220.lib opencv_video220.lib opencv_video220d.lib

    *

  • *

  • OPENCV V VS2010*

  • OPENCV V VS2010*

  • OPENCV V VS2010Chp file sau vo chng trnh// vidu1.cpp : #include "stdafx.h"#include "cv.h"#include "highgui.h"int main( int argc, char** argv ) {IplImage* img = cvLoadImage( c:/anh.jpg" );cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );cvShowImage( "Example1", img );cvSaveImage( c:/anh2.png,img); //ct di dng khccvWaitKey(0);cvReleaseImage( &img );cvDestroyWindow( "Example1" );}*

  • VC++#include"opencv2/highgui/highgui.hpp" #include using namespacecv; using namespacestd; intmain(intargc, const char**argv ) { Mat img = imread("MyPic.JPG", CV_LOAD_IMAGE_UNCHANGED); //read the image data in the file "MyPic.JPG" and store it in 'img if(img.empty())//checkwhetherthe image is loaded or not { cout
  • OPENCV V VS2010Nu mun cu hnh trn p dng cho mi project x l nh v sau , khng cn phi cu hnh li , ta m menu View- Other Windows- Property Manager Debug/Win32 bm chut kp vo dng Microsoft.Cpp.Win32.user Sau thc hin li cc bc trn. Sau lu cu hnh li . Khi ta to Project mi, VC++ s bin dch thnh cng cc lnh ca OpenCV*

  • *

  • *

  • OPENCV BN MIHin c opencv 2.4.8 build sn cho VS2010,2011,2012*

  • S DNG OPENCV 2.1 V VC 2008-2010 WINFORM APPLICATIONTo Project mang tn , v d opcvwinform kiu windows forms application

    *

  • *

  • S DNG OPENCV 2.1 V VC 2008-2010 WINFORM APPLICATIONVo Properties , dng General- Common Language Runtime Support chn Common Language Runtime Support (/clr)*

  • S DNG OPENCV 2.1 V VC 2008-2010 WINFORM APPLICATIONKhai bo cc ng dn cho include, lib nh trong win32 consoleTo button EXIT*

  • S DNG OPENCV 2.1 V VC 2008-2010 WINFORM APPLICATIONBm chut kp vo nt EXIT, m ra code* Di dng #pragma once thm on code sau

  • S DNG OPENCV 2.1 V VC 2008-2010 WINFORM APPLICATION#include #include #ifdef _DEBUG //Debug#pragma comment(lib,"cv210d.lib") #pragma comment(lib,"cxcore210d.lib") #pragma comment(lib,"cvaux210d.lib") #pragma comment(lib,"highgui210d.lib")#else //Release#pragma comment(lib,"cv210.lib") #pragma comment(lib,"cxcore210.lib") #pragma comment(lib,"cvaux210.lib") #pragma comment(lib,"highgui210.lib")#endif*

  • Nu dng opencv2.2xx tr ln (v d 2.4.8) ta dng#include #include #ifdef _DEBUG //Debug #pragma comment(lib,opencv_core248d.lib") #pragma comment(lib,opencv_highgui248d.lib")#else //Release #pragma comment(lib,"opencv_core248.lib") #pragma comment(lib,"opencv_highgui248.lib")#endif

    *

  • Trong VC++ Dir khai bo includeng dn\build\include\opencv2ng dn\build\include\opencvng dn \build\include Khai bo libng dn \build\x86\vc10\lib, nu dng WIN 64 thay x86 bng x64, nu dng VS2012 thay vc10 bng vc12Khai bo exe cho dllng dn \build\x86\vc10\binLinker-Input- Additional dependencies khai bo cc th vin

    *

  • opencv_core248.libopencv_highgui248.lib.Bm inherit from parent dng cho cc ln sauVo C++ -General -Common Language Run Time Support Chn Common Language Run Time Support (/clr)*

  • S DNG OPENCV 2.1 V VC 2008-2010 WINFORM APPLICATIONCh dng lnh private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { }Thm code vo gia: this->Close();Bm chut kp vo form

    * Thm code vo ch form_load

  • S DNG OPENCV 2.1 V VC 2008-2010 WINFORM APPLICATIONIplImage* img = cvLoadImage("c:/anh.jpg");cvNamedWindow("WINFORM", CV_WINDOW_AUTOSIZE);cvShowImage("WINFORM", img);cvWaitKey(0);cvReleaseImage(&img);cvDestroyAllWindows(); *Ch cvLoadImage() ta thm ng dn v tn file nh

  • PICTURE BOXTo picture box trn form*

  • PICTURE BOXVo Properties ca Picture Box1, chn StretchMode- StretchImage, BorderStyle- FixedSingleDi #pragma once ta thm cc dng khai bo nh trcThm dng khai bo bin chung IplImage* img= NULL;oan m sau c s dng hin nh ln PictureBox1 v thot chng trnh khi bmEXIT*

  • PICTURE BOXprivate: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {IplImage* img = cvLoadImage( "c:/anh.jpg" );pictureBox1->Image = gcnew //replacement of cvShowImageSystem::Drawing::Bitmap(img->width,img->height,img->widthStep,System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) img->imageData);pictureBox1->Refresh(); }private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {cvReleaseImage(&img); this->Close(); }

    *

  • PICTURE BOX*

  • Cau truc cua OpenCVOpenCV c th c dng trong cc ngn ng C, C++ v PythonBao gom cac Module sau:CXCORE: Cha cac nh ngha kieu d lieu c s. V du, cac cau truc d lieu cho anh, iem va hnh ch nhat c nh ngha trong cxtypes.h . CXCORE cung cha cac phep tinh ai so tuyen tnh va phng phap thong ke, chc nang duy tr va ieu khien chuoi. Mot so t cac chc nang o hoa e ve tren anh cung c at ay.CV: Cha cac thuat toan ve x ly anh, th giac may tnh va lay chuan (calibration) camera. Cac chc nang tai tao anh 3D cung c at ay.*

  • Cau truc cua OpenCVCVAUX: c mo ta trong van ban cua OpenCV nh la module cu va ch dung e th nghiem. Tuy nhien, giao dien n gian nhat cho nhan dang mat c nam trong module nay. Nhng ma nguon nam trong module nay rat phu hp cho viec nhan dang mat va chung c s dung rong rai cho muc ch nay.HIGHGUI: Cha cac giao dien vao ra c ban, no cung cha cac kha nang ca so m rong va vao ra video.CVCAM: Cha cac giao dien cho video truy cap qua DirectX tren nen Windows 32 bits.MACHINE LEARNING: cc thut ton hc

    *

  • *

  • CVAUXEigen objects, a computationally effi cient recognition technique t hat is, in essence, a template matching procedure1D and 2D hidden Markov models, a statistical recognition technique solved by dynamic programmingEmbedded HMMs (the observations of a parent HMM are themselves HMMs)Gesture recognition f rom stereo vision supportExtensions to Delaunay triangulation, sequences, and so forthStereo visionShape matching with region contoursTexture descriptorsEye and mouth tracking*

  • CVAUX3D trackingFinding skeletons (central lines) of objects in a sceneWarping intermediate views between two camera viewsBackground-foreground segmentationVideo surveillance (see Wiki FAQ for more documentation)Calibration C++ classes (the C functions and engine are in CV)*

  • *Face DetectionContour DetectionEdge Detection

  • MT S HM BT NH V CAMERACu trc nh: IplImage (Ipl l vit tt ca Image processing Library)typedef struct _IplImage{int nSize; int ID;// 0int nChannels;int alphaChannel; // khng dngint depth;char colorModel[4]; // OpenCV khong s dungchar channelSeq[4]; // OpenCV khong s dungint dataOrder;int origin;int align; // OpenCV khong s dungint width;int height;struct _IplROI *roi;struct _IplImage *maskROI; // bang NULL trong OpenCV

    *

  • void *imageId; // bang NULL trong OpenCVstruct _IplTileInfo *tileInfo; // bang NULL trong OpenCVint imageSize;char *imageData;int widthStep;int BorderMode[4]; // OpenCV khong s dungint BorderConst[4]; // OpenCV khong s dungchar *imageDataOrigin;}IplImage;nSize : kch thc cua anhID : ch so version, luon bang 0nChannels : so kenh, cac ham cua OpenCV eu ho tr t 1-4 kenhalphaChannel : OpenCV khong s dungdepth : chieu sau cua pixel theo bit, co cac dang sau :IPL_DEPTH_8U : unsigned 8-bit integerIPL_DEPTH_8S : signed 8-bit integerIPL_DEPTH_16U : unsigned 16-bit interger*

  • dataOder : 0 = IPL_DATA_ORDER_PIXEL cac kenh mau an xen nhau, 1 = cac kenh mau tach riorigin : goc toa o 0 = top-left origin, 1 = bottom-left origin ( kieu Window bitmap)width : be rong anh theo pixelheight : chieu cao anh theo pixelroi Region of Interest (ROI) neu bang NULL th ch co vung ROI c x lyimageSize : kch thc d lieu anh theo byteimageData : mot pointer tro ti vung a ch anhwidthStep : the size of an aligned image row, in bytes*

  • C V GHI NHViec oc mot file anh, ta goi ham cvLoadImage(), OpenCV ho tr hau het cac nh dang pho bien nh JPEG, PNG va BMP. IplImage* cvLoadImage(const char* filename, int iscolor = CV_LOAD_IMAGE_COLOR);Viec ghi mot anh vao file ta goi ham cvSaveImage(), ham nay quyet nh nh dang file ta s dung t file ban au. Ca cvLoadImage() va cvSaveImage() eu nam trong modul HighGUI.Int cvSaveImage(const char* filename, const CvArr* image);Khi chung ta a ket thuc qua trnh nhap anh va s dung xong ham cvLoadImage(), chung ta can goi ham cvReleaseImage() e giai phong vung nh cho anh.Void cvReleaseImage( IplImage** image); e tao mot hnh anh ta dung ham cvCreateImage(), ham tra ve mot pointer tro en hnh anh co cau truc IplImageIplImage* cvCreateImage(CvSize size, int depth, int channels);*

  • C V HiN NHe Copy anh ta dung ham CvCopy()void cvCopy( const CvArr* src, CvArr* dst, const CvArr* mask = NULL);Trong o :src : anh nguondst : anh chmask : mat na phu len anh nguon neu ta ch muon copy mot phan anh.V d:#include highgui.hint main( int argc, char** argv ) { IplImage* img = cvLoadImage( argv[1] ); cvNamedWindow( Example1, CV_WINDOW_AUTOSIZE ); cvShowImage( Example1, img ); cvWaitKey(0); cvReleaseImage( &img ); cvDestroyWindow( Example1 );}

    *

  • C V HiN NH#include "stdafx.h"#include #include int main(){IplImage* img =cvLoadImage("C:/SDC16361.JPG");cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );cvShowImage( "Example1", img );cvWaitKey(0);cvReleaseImage(&img );cvDestroyWindow( "Example1" );}

    *

  • C FILE VIDEO#include highgui.h int main() { cvNamedWindow( Example2, CV_WINDOW_AUTOSIZE ); CvCapture* capture = cvCreateFileCapture(Doraemon.avi); IplImage* frame; while(1) { frame = cvQueryFrame( capture ); if( !frame ) break; cvShowImage( Example2, frame ); char c = cvWaitKey(33); if( c == 27 ) break; } cvReleaseCapture( &capture );cvDestroyWindow( Example2 ); } *

  • WEBCAM CAPTURE#include "stdafx.h#include int main( ) { CvCapture* capture = cvCaptureFromCAM( 0 ); //trong highgui IplImage* src = NULL; cvNamedWindow( "Webcam", CV_WINDOW_AUTOSIZE ); while(1) { src = cvQueryFrame( capture ); if( !src ) break; *

  • WEBCAM CAPTURE char c = cvWaitKey(33); if (c == 27) { break; } cvShowImage( "Webcam", src ); } cvReleaseImage( &src ); cvDestroyWindow( "Webcam" ); return 0; }Hm cvCreateCameraCapture() , cvCaptureFromCAM( 0) hot ng tng t hm cvCreateFileCapture()Tham s l ID ca camera *

  • CC HM V HNH (CXCORE)The function circle draws a simple or filled circle with a given center and radiusvoid circle(Mat& img, Point center, int radius,const Scalar& color, int thickness=1,int lineType=8, int shift=0);img Image where the circle is drawncenter Center of the circleradius Radius of the circlecolor Circle colorthickness Thickness of the circle outline if positive; negative thickness means that a filled circleis to be drawnlineType Type of the circle boundary, see cv::line descriptionshift Number of fractional bits in the center coordinates and radius value

    *

  • CC HM V HNH (CXCORE)Draws a simple, thick, or filled up-right rectangle.void rectangle(Mat& img, Point pt1, Point pt2,const Scalar& color, int thickness=1,int lineType=8, int shift=0);img Imagept1 One of the rectangles verticespt2 Opposite to pt1 rectangle vertexcolor Rectangle color or brightness (grayscale image)thickness Thickness of lines that make up the rectangle. Negative values, e.g. CV FILLED,mean that the function has to draw a filled rectangle.lineType Type of the line, see cv::line descriptionshift Number of fractional bits in the point coordinatesThe function rectangle draws a rectangle outline or a filled rectangle, which two opposite corners are pt1 and pt2.*

  • D LiU S KHAI CA OPENCVCvPoint(x,y): xc nh mt im , xy l s nguynCvPoint2D32f(x,y): xy l s thcCvPoint3D32f(x,y,z): xyz l s thcCvSize (width, height): xc nh kch thc hnh CvSize2D32f(width, height): tham s l s thcCvRect(x,y,w,h): xc nh hnh ch nht c gc trn tri ta xy, rng w v cao hCvScalar(double val[4]) cha 4 s thc trong val l cc thnh phn RGB A, vi nh xm cng sng cha trong val(0), vi nh mu B trong val[0], G trong val[1], R trong val[2], *

  • MA TRNHm to ma trncvMat* cvCreateMat ( int rows, int cols, int type ); type cho bit loi d liu ca phn t ma trncvGetSize(CvMat*): cho kch thc ma trncvCloneMat(CvMat*): to ma trn ging ma trn c sncvReleaseMat(CvMat**): xa b nhcvInitMatHeader: to ma trn vi d liu cho trc// Create an OpenCV Matrix containing some fixed data.//bn phn tfloat vals[] = { 0.866025, -0.500000, 0.500000, 0.866025 };CvMat rotmat;cvInitMatHeader(*

  • MA TRN&rotmat,2,2,CV_32FC1,vals);cvGetElemType( const CvArr* arr ): loi d liu trong ma trn cvGetDims( const CvArr* arr, int* sizes=NULL ): s chiu ma trnand cvGetDimSize( const CvArr* arr, int index ). Kch thc ca mt chiu xc nhLy ra mt phn t ca ma trn:CvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );float element_3_2 = CV_MAT_ELEM( *mat, float, 3, 2 );*

  • MA TRNt gi tr cho mt phn tCvMat* mat = cvCreateMat( 5, 5, CV_32FC1 );float element_3_2 = 7.7;*( (float*)CV_MAT_ELEM_PTR( *mat, 3, 2 ) ) = element_3_2;

    c gi tr mt phn tdouble cvGetReal1D( const CvArr* arr, int idx0 );double cvGetReal2D( const CvArr* arr, int idx0, int idx1 );double cvGetReal3D( const CvArr* arr, int idx0, int idx1, int idx2 );double cvGetRealND( const CvArr* arr, int* idx );CvScalar cvGet1D( const CvArr* arr, int idx0 );CvScalar cvGet2D( const CvArr* arr, int idx0, int idx1 );CvScalar cvGet3D( const CvArr* arr, int idx0, int idx1, int idx2 );CvScalar cvGetND( const CvArr* arr, int* idx );*

  • MA TRNt gi tr mt phn tvoid cvSetReal1D( CvArr* arr, int idx0, double value );void cvSetReal2D( CvArr* arr, int idx0, int idx1, double value );void cvSetReal3D( CvArr* arr, int idx0, int idx1,int idx2,double value);void cvSetRealND( CvArr* arr, int* idx, double value );void cvSet1D( CvArr* arr, int idx0, CvScalar value );void cvSet2D( CvArr* arr, int idx0, int idx1, CvScalar value );void cvSet3D(CvArr* arr,int idx0,int idx1,int idx2,CvScalar value);void cvSetND( CvArr* arr, int* idx, CvScalar value );Vi ma trn mt chiu dng cc hmdouble cvmGet( const CvMat* mat, int row, int col )void cvmSet( CvMat* mat, int row, int col, double value )*

  • MA TRNCng cc phn t ma trn 3 chiufloat sum( const CvMat* mat ) {float s = 0.0f;for(int row=0; rowrows; row++ ) {const float* ptr = (const float*)(mat->data.ptr + row * mat->step);for( col=0; colcols; col++ ) {s += *ptr++;}}return( s );}*

  • NHnh c cu trc IplImage ta nh cvMat tuy nhin c rt nhiu tham s kh phc tpC th x l nh nh vi ma trnvoid cvSetImageROI( IplImage* image, CvRect rect ): ly vng nh quan tm region of interestvoid cvResetImageROI( IplImage* image );V d thay i gi tr mt vng nh#include #include int main(int argc, char** argv){IplImage* src;if( argc == 7 && ((src=cvLoadImage(argv[1],1)) != 0 )){

    *

  • NHint x = atoi(argv[2]);int y = atoi(argv[3]);int width = atoi(argv[4]);int height = atoi(argv[5]);int add = atoi(argv[6]);cvSetImageROI(src, cvRect(x,y,width,height));cvAddS(src, cvScalar(add),src);cvResetImageROI(src);cvNamedWindow( Roi_Add, 1 );cvShowImage( Roi_Add, src );cvWaitKey();}return 0;}

    *

  • *Cng 150 vo knh Blue trn vng mt ca con mo

  • HIN TH MT VNG NH#include "stdafx.h"#include #include int main(){IplImage* img =cvLoadImage("C:/SDC16361.JPG");cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );cvShowImage( "Example1", img );cvSetImageROI( img, cvRect (300,300, 400,400 ));cvShowImage( "2", img );cvWaitKey(0);cvReleaseImage(&img );cvDestroyWindow( "Example1" );cvDestroyWindow( 2" );

    }

    *

  • CC HM MA TRN V NHcvAbs Absolute value of all elements in an arraycvAbsDiff Absolute value of diff erences between two arrayscvAbsDiffS Absolute value of diff erence between an array and a scalarcvAdd Elementwise addition of two arrayscvAddS Elementwise addition of an array and a scalarcvAddWeighted Elementwise weighted addition of two arrays (alpha blending)cvAvg Average value of all elements in an arraycvAvgSdv Absolute value and standard deviation of all elements in an arraycvCalcCovarMatrix Compute covariance of a set of n-dimensional vectorscvCmp Apply selected comparison operator to all elements in two arrays*

  • cvCmpS Apply selected comparison operator to an array relative to a scalarcvConvertScale Convert array type with optional rescaling of the valuecvConvertScaleAbs Convert array type after absolute value with optional rescalingcvCopy Copy elements of one array to anothercvCountNonZero Count nonzero elements in an arraycvCrossProduct Compute cross product of two three-dimensional vectorscvCvtColor Convert channels of an array from one color space to anothercvDet Compute determinant of a square matrixcvDiv Elementwise division of one array by another*

  • cvDotProduct Compute dot product of two vectorscvEigenVV Compute eigenvalues and eigenvectors of a square matrixcvFlip Flip an array about a selected axiscvGEMM Generalized matrix multiplicationcvGetCol Copy elements from column slice of an arraycvGetCols Copy elements from multiple adjacent columns of an arraycvGetDiag Copy elements from an array diagonalcvGetDims Return the number of dimensions of an arraycvGetDimSize Return the sizes of all dimensions of an array*

  • cvGetRow Copy elements from row slice of an arraycvGetRows Copy elements from multiple adjacent rows of an arraycvGetSize Get size of a two-dimensional array and return as CvSizecvGetSubRect Copy elements from subregion of an arraycvInRange Test if elements of an array are within values of two other arrayscvInRangeS Test if elements of an array are in range between two scalarscvInvert Invert a square matrix*

  • OpenCV trn HH Ubuntu

    Ubuntu l mt h iu hnh hon ton m, c xy dng d trn nhn (kernel) Linux. Cng ng ngi dng Ubuntu c hnh thnh bi nhng l tng gn km theo trit l Ubuntu (Ubuntu Philosophy) l: ngi dng c s dng phn mm min ph, mi mt phn mm u c th s dng di giao din ngn ng bn a ca ngi dng v quan trng nht l ngi dng hon ton t do chnh sa v thay i phn mm ph hp vi nhu cu s dng ca mnh.

    *

  • OpenCV trn HH Ubuntu*

  • Ubuntu kt hp nhng c im ni bt chung ca h iu hnh nhn Linux, nh tnh bo mt trc mi virus v malware, kh nng ty bin cao, tc , hiu sut lm vic, v nhng c im ring tiu biu ca Ubuntu nh giao din bt mt, bng by, ci t ng dng n gin, s d dng trong vic sao lu d liu v s h tr ca mt cng ng ngi dng khng l.Cng ng Ubuntu bao gm ngi pht trin, lp trnh vin, ngi th nghim, ngi son ti liu k thut, ngi dng th cc ti liu k thut, ngi dch v, quan trng nht, nhng ngi dng Ubuntu hng ngy.Mt s tnh nng ca Ubuntu: Ubuntu s lun lun c min ph, bao gm c doanh nghip v bn cp nht bo mt. Ubuntu i km vi y h tr thng mi t Canonical v hng trm cng ty trn khp th gii

    *

  • Ci t th vin OpenCV trn HH Ubuntu l cng vic kh phc tpv mt thi gian, chng ta cn thc hin trnh t cc bc sau:u tin, ta phi m bo rng tt c mi th trong h thng c cp nht v nng cp: $sudo apt-get update $sudo apt-get upgradeTip theo, nhng g cn phi c thc hin l ci t cc iu kin tin quyt cn thit theo yu cu ca OpenCV th vin . Danh sch ph thuc c th c chnh sa theo nhu cu ca bn. Bc ny rt d dng, bn ch cn phi vit lnh di y trong Terminal:

    *

  • $sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev

    *

  • Tip theo, ta ti sourcecode OpenCV 2.4.2 v my:$cd ~$wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.2/OpenCV-2.4.2.tar.bz2$tar -xvf OpenCV-2.4.2.tar.bz2$cd OpenCV-2.4.2

    *

  • By gi, chng ta phi to ra cc Makefile bng cch s dng cmake . y chng ta c th xc nh cc b phn ca OpenCV m chng ta mun bin dch . Chng ta mun s dng Python, TBB, OpenGL, Qt, lm vic vi video, vv th y l ni cn phi thit lp . Ch cn thc hin lnh ny vo thit b u cui to ra Makefile ph hp . Lu rng c hai du chm cui dng , n l mt i s cho chng trnh cmake v n c ngha l th mc cha:

    *

  • $mkdir build$cd build$cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON .. Kim tra xem lnh trn c sinh ra cc li hay khng v c bit l n bo co FFMPEG l YES. Nu ngc li, ta s khng th c hoc vit cc on video. Ngoi ra, kim tra xem Python, TBB, OpenGL, V4L, OpenGL v Qt c c pht hin hay khng. Nu c bt k sai st xy ra, chng ta phi quay tr li v sa cha bng vic ci t thm cc gi cn thit v sau chy cmake mt ln na. Bn s thy mn hnh hin ra iu tng t nh sau:

    *

  • *

  • By gi, chng ta sn sng bin dch v ci t OpenCV 2.4.2:$make$sudo make install Cu hnh OpenCV . Trc tin, m tp tin opencv.conf vi m sau y:$sudogedit /etc/ld.so.conf.d/opencv.confThm dng sau /usr/local/lib vo cui ca tp tin ( n c th l mt tp tin rng ) v sau lu li:

    *

  • *

  • Chy on m sau cu hnh cc th vin:$sudoldconfigBy gi bn phi m mt tp tin:$sudogedit /etc/bash.bashrcThm hai dng vo cui ca tp tin v lu li:PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfigexport PKG_CONFIG_PATH*

  • *

  • OpenCV trn HH UbuntuCui cng, ng giao din iu khin v m mt ci mi, khi ng li my tnh hoc ng xut v sau ng nhp li. OpenCV s khng hot ng cho n khi bn lm iu ny.

    *