opencv workshop

48
OpenCV C++ Workshop Lentin Joseph Founder of Qbotics Labs www.qboticslabs.com

Upload: lentin-joseph

Post on 08-Aug-2015

257 views

Category:

Software


16 download

TRANSCRIPT

Page 1: OpenCV Workshop

OpenCV C++ Workshop

Lentin JosephFounder of Qbotics Labswww.qboticslabs.com

Page 2: OpenCV Workshop

About OpenCV

● Open Source Computer Vision library● Real-time Computer vision library● Started by Intel Russia, launched in 1999● 2000 : First alpha release● 2006: First stable release● 2009: Second major release

http://www.qboticslabs.com

Page 3: OpenCV Workshop

About OpenCV

● 2012 : opencv -> opencv.org● Current version : OpenCV 3.0 beta● Opensource BSD license● Cross platform● Now supporting by Willow Garage and Itseez

http://www.qboticslabs.com

Page 4: OpenCV Workshop

About OpenCV

● Written in C++ and C● Full Interfaces for Python, Java, Matlab/

Octave● Wrappers in C#, Perl, Ruby● OS Support : Windows, Linux, Android, Maemo,

FreeBSD, IOS, OS X, BlackBerry 10

http://www.qboticslabs.com

Page 5: OpenCV Workshop

Applications of OpenCV

● Gesture recognition● Human-computer interaction(HCI)● Mobile robotics● Segmentation and recognition● Motion tracking● Augmented reality● Machine learning

http://www.qboticslabs.com

Page 6: OpenCV Workshop

Installing OpenCV

● Installing from source code● Installing from Ubuntu packages

http://www.qboticslabs.com

Page 7: OpenCV Workshop

Installing OpenCV from Ubuntu packages

$ sudo apt-get update

$ sudo apt-get install libopencv-dev

http://www.qboticslabs.com

Page 8: OpenCV Workshop

Installing OpenCV from source code

● Install prerequisites packages

● [compiler] sudo apt-get install build-essential

● [required] sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

● [optional] sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev

http://www.qboticslabs.com

Page 9: OpenCV Workshop

Installing OpenCV from source code

● cd ~/opencv

● git clone https://github.com/Itseez/opencv.git

● mkdir release

● cd release

● cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -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 ..

● make

http://www.qboticslabs.com

Page 10: OpenCV Workshop

Installing OpenCV from source code

● cd ~/opencv

● git clone https://github.com/Itseez/opencv.git

● mkdir release

● sudo apt-get install cmake-qt-gui

http://www.qboticslabs.com

Page 11: OpenCV Workshop

Installing OpenCV from source code

http://www.qboticslabs.com

Page 12: OpenCV Workshop

Check OpenCV is installed !!!

● $ pkg-config opencv –cflags

$ pkg-config opencv --libs

Page 13: OpenCV Workshop

Setting OpenCV in Eclipse

● Install OpenJDK from Ubuntu Software

Page 14: OpenCV Workshop

Check Ubuntu Version !!!

● System settings -> Details -> Overview

Page 15: OpenCV Workshop

Download Eclipse

● http://www.eclipse.org/downloads/

Page 16: OpenCV Workshop

Extract and Install Eclipse

● cd /opt/ && sudo tar -zxvf ~/Downloads/eclipse-*.tar.gz

● Without using command● $ gksudo nautilus

Page 17: OpenCV Workshop

Adding Shortcut for Eclipse

● $ gksudo gedit /usr/share/applications/eclipse.desktop

● Paste the following to this file

[Desktop Entry]Name=Eclipse 4Type=ApplicationExec=/opt/eclipse/eclipseTerminal=falseIcon=/opt/eclipse/icon.xpmComment=Integrated Development EnvironmentNoDisplay=falseCategories=Development;IDE;Name[en]=Eclipse

Page 18: OpenCV Workshop

Access eclipse via Unity Dash

Page 19: OpenCV Workshop

Setting OpenCV in Eclipse

● Setting Include path– Properties -> C/C++ Build->Settings->Cross G++

Compiler->Includes

– $ pkg-config opencv --cflags

Page 20: OpenCV Workshop

Setting OpenCV in Eclipse

● Setting Lib path and libraries– Properties -> C/C++ Build->Settings->Cross G++

Compiler->Libraries

– $ pkg-config opencv --libs

Page 21: OpenCV Workshop

Running test code !!!

● Reading an Image

#include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>#include <iostream>

using namespace cv;using namespace std;

int main( int argc, char** argv ){ if( argc != 2) { cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; return -1; }

Mat image; image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file

if(! image.data ) // Check for invalid input { cout << "Could not open or find the image" << std::endl ; return -1; }

namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display. imshow( "Display window", image ); // Show our image inside it.

waitKey(0); // Wait for a keystroke in the window return 0;}

Page 22: OpenCV Workshop

Running test code !!!

● Setting command line argument

– Project -> Run as-> Run Configurations

Page 23: OpenCV Workshop

Output !!!

Page 24: OpenCV Workshop

Compile code without Eclipse

● Save code as .cpp using an text editor● Compile using following command● $ g++ <input_file.cpp> `pkg-config opencv –cflags –libs` -o <output_name>

● $ ./output_name

Page 25: OpenCV Workshop

OpenCV Modules

● OpenCV has modular structure● OpenCV contain several shared/static libraries● Core : Contain basic image data structure such as

Mat ● Imgproc : image processing module contain linear

and non linear filter, color space conversion, histogram etc

● Video : Motion estimation, background substraction, object tracking algorithms etc

Page 26: OpenCV Workshop

OpenCV Modules

● Calib3d : mainly for camera calibration

● Features2d : contain feature detectors, descriptors and descriptor matchers

● Objdetect: contain object detection algorithms

● Highgui: contain UI functionality to handle video and image

● Gpu : GPU-accelerated algorithms

Page 27: OpenCV Workshop

About OpenCV API's

● All OpenCV classes are placed in cv namespace

Page 28: OpenCV Workshop

Let's start coding

Page 29: OpenCV Workshop

Reading an Image

OpenCV Header Files Used

● #include <opencv2/highgui/highgui.hpp>

OpenCV API's used

● imread(file_name,flags)

● imshow(“window_title”,image_variable)

● namedWindow(“window_title”,flags)

● WaitKey(0)

● Refer http://docs.opencv.org/modules/refman.html

Page 30: OpenCV Workshop

Reading an Image

Output

Page 31: OpenCV Workshop

Reading a Video

OpenCV Header Files Used

● #include <opencv2/highgui/highgui.hpp>

OpenCV API's used

● VideoCapture cap(argv[1])

● waitKey(30)

Page 32: OpenCV Workshop

Reading from Camera

OpenCV Header Files Used

● #include <opencv2/highgui/highgui.hpp>

OpenCV API's used

● VideoCapture cap(argv[1])

● waitKey(30)

Page 33: OpenCV Workshop

Reading Pixel from Image and Mouse Interaction

OpenCV Header Files Used

● #include <opencv2/highgui/highgui.hpp>

OpenCV API's used

● image.at<uchar>(y,x);● image.at<Vec3b>(y,x)[0];● setMouseCallback("Display window", mouse_callback, NULL);

Page 34: OpenCV Workshop

Working with Mat type

OpenCV Header Files Used

● #include <opencv2/highgui/highgui.hpp>

OpenCV API's used

● Mat red(480,640,CV_8UC3,Scalar(0,0,255));● imwrite("red.jpg",red);

Page 35: OpenCV Workshop

Adjusting brightness and contrast

OpenCV Header Files Used

● #include <opencv2/highgui/highgui.hpp>

OpenCV API's used

● new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta );

● Alpha = contrast● Beta = Brightness● saturate_cast ensure value is valid or not

Page 36: OpenCV Workshop

Image Smoothing demo

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● Blur● GaussianBlur● MedianBlur● bilateralFilter

Page 37: OpenCV Workshop

Segmentation: Thresholding

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● cvtColor( src, src_gray, CV_RGB2GRAY );● threshold( src_gray, dst, threshold_value, max_BINARY_value,threshold_type );

● createTrackbar( trackbar_value, window_name,&threshold_value, max_value, Threshold_Demo );

Page 38: OpenCV Workshop

Edge Detection

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );

Page 39: OpenCV Workshop

Laplace & Sobel

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );

● Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );

Page 40: OpenCV Workshop

Hough Transform(Circle & Line)

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows/8, cannyThreshold, accumulatorThreshold, 0, 0 );

● HoughLines( edges, s_lines, 1, CV_PI/180, min_threshold + s_trackbar, 0, 0 );

Page 41: OpenCV Workshop

Contour Detection

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

Page 42: OpenCV Workshop

Histogram

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● calcHist( &bgr_planes[0], 1, 0, Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate );

Page 43: OpenCV Workshop

Harris_Corner_Detection

OpenCV Header Files Used

● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● cornerHarris( src_gray, dst, blockSize, apertureSize, k, BORDER_DEFAULT );

Page 44: OpenCV Workshop

Motion Detection, LK,FB,BS

OpenCV Header Files Used

● #include "opencv2/video/tracking.hpp"● #include "opencv2/imgproc/imgproc.hpp"● #include "opencv2/highgui/highgui.hpp"

OpenCV API's used

● calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,

● calcOpticalFlowFarneback(prevgray, gray, flow, 0.5, 3, 15, 3, 5, 1.2, 0);

● cvCalcMotionGradient( mhi, mask, orient, MAX_TIME_DELTA, MIN_TIME_DELTA, 3 );

Page 45: OpenCV Workshop

Face Detection

OpenCV Header Files Used

● #include "opencv2/objdetect/objdetect.hpp"● #include "opencv2/highgui/highgui.hpp"● #include "opencv2/imgproc/imgproc.hpp"

OpenCV API's used

● cascade.detectMultiScale( smallImg, faces2, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

Page 46: OpenCV Workshop

Conclusion

● Discussed basics of Computer Vision● Discussed about OpenCV● Discussed OpenCV programming using C++● Discussed basic level examples to advanced

level

Page 47: OpenCV Workshop

Questions !!!

Page 48: OpenCV Workshop

Thanks !!!

Lentin JosephFounder of QboticsLabshttp://www.qboticslabs.comhttp://www.lentinjoseph.com