SlideShare a Scribd company logo
1 of 29
Introduction
to OpenCV
Marvin Smith
Introduction
• OpenCV is an Image Processing library created by Intel
and maintained by Willow Garage.
• Available for C, C++, and Python
• Newest update is version 2.2
• Open Source and free
• Easy to use and install
Installation Instructions
• Do not use synaptic on Linux to install OpenCV.
• It is version 1.2.
• For Linux and Windows, follow the installation guide at
http://opencv.willowgarage.com/wiki/InstallGuide
• Linux users can come to me for help. I have built it on
Ubuntu dozens of times. I have built it successfully on
Windows once.
• Make sure to read the beginning as it gives you
precise commands to install ffmpeg, libavformat-dev,
libswscale-dev, and other required libraries.
BASIC OPENCV
STRUCTURES
• Point, Point2f - 2D Point
• Size - 2D size structure
• Rect - 2D rectangle object
• RotatedRect - Rect object with angle
• Mat - image object
Point
• 2D Point Object
- int x, y;
• Functions
- Point.dot(<Point>) - computes dot
product
- Point.inside(<Rect>) - returns true if
point is inside
• Math operators, you
may use
- Point operator +
- Point operator +=
- Point operator -
- Point operator -=
- Point operator *
- Point operator *=
- bool operator ==
- bool operator != double
norm
Size
• 2D Size Structure
- int width, height;
• Functions
- Point.area() - returns (width * height)
RECT
• 2D Rectangle Structure
- int x, y, width, height;
• Functions
- Point.tl() - return top left point
- Point.br() - return bottom right point
cv::Mat
• The primary data structure in
OpenCV is the Mat object. It
stores images and their
components.
• Main items
• rows, cols - length and
width(int)
• channels - 1: grayscale, 3:
BGR
• depth: CV_<depth>C<num
chan>
• See the manuals for more
information
cv::Mat
•Functions
- Mat.at<datatype>(row, col)[channel] - returns pointer to image
location
- Mat.channels() - returns the number of channels
- Mat.clone() - returns a deep copy of the image
- Mat.create( rows, cols, TYPE) - re-allocates new memory to
matrix
- Mat.cross(<Mat>) - computes cross product of two matricies
- Mat.depth() - returns data type of matrix
- Mat.dot(<Mat>) - computes the dot product of two matrices
cv::Mat
•Functions
- Mat(Range(xmin,xmax),Range(ymin,ymax)) - returns sub image
- Mat.type() - returns the TYPE of a matrix
•Iterator Usage
- Mat.begin() - moves Mat iterator to beginning of image
- Mat.end() - moves Mat iterator to end of image
Image TYPES
• The TYPE is a very important aspect of OpenCV
• Represented as CV_<Datatype>C<# Channels>
• Example Datatypes/ Depths
Pixeltypes
• PixelTypes shows how the image is represented in data
• BGR - The default color of imread(). Normal 3 channel color
• HSV - Hue is color, Saturation is amount, Value is lightness. 3
channels
• GRAYSCALE - Gray values, Single channel
• OpenCV requires that images be in BGR or Grayscale
in order to be shown or saved. Otherwise, undesirable
effects may appear.
HELLO WORLD
• Example Code
//Loads image and displays
//call by ./a.out image.jpg
//
#include <cv.h>
#include <cvaux.h>
#include <highgui.h>
using namespace cv;
int main(int argc, char* argv[ ]){
Mat image = imread(argv[1]);
namedWindow(“Sample Window”);
imshow(“Sample Window”,image);
waitKey(0);
return 0;
}
This program will load and show
an image
Starting Out in OpenCV
• OpenCV uses the cv namespace.
• cv::Mat object replaces the original C standard IplImage and
CvMat classes.
• All original functions and classes of the C standard OpenCV
components in the Bradski book are still available and
current. However you will need to read that book for it.
Image I/O
• OpenCV provides simple and useful
ways to read and write images.
• Note that there are many extra
options to these commands which
are available on the wiki.
• waitKey( int x ) has two main
features.
- if x > 0, then waitKey will wait x
milliseconds
- if x = 0, then waitKey will not move
until key is pressed
• Examples
//Read an image
Mat image = imread( <string>, <0 -gray, 1 -BGR>)
//Note 1 is default
//Write an image
imwrite( <string filename> , image );
//Create window for output
namedWindow( <window name> );
//Output image to window
imshow( <window name> , <image Mat to show> );
//pause program for input
key = waitKey( 0 );
DRAWING STUFF
• Sometimes it is necessary to draw stuff onto the image.
Instead of using complicated functions, why not just call a
simple function?
• Here are some simple examples...
• void circle(image, Point(x,y),int rad, CV_BGR(b,g,r), int
thickness=1)
• void ellipse(image, RotatedRect box, CV_BGR(b,g,r), int
thickness=1)
• void line(image, Point(x,y), Point(x,y), CV_BGR(b,g,r), int
thickness= 1)
• void rectangle(img, Point(x,y), Point(x,y), CV_BGR(b,g,r), int
thickness)
• NOTE: negative thickness will fill in the rectangle
Drawing stuff
Using the Mouse
• OpenCV allows you
to use the mouse to
interact with the
screen. Note that this
feature is from
OpenCV 1.0 and is
compatible with Mat
objects.
• This program allows
you to draw dots on
the image.
USING THE MOUSE
Converting colorspaces
• cvtColor( image, image, code)
• Codes
• CV_<colorspace>2<colorspace>
• Examples
• CV_BGR2GRAY
• CV_BGR2HSV
• CV_BGR2LUV
Image Normalization
• normalize(imagein, imageout, low, high,
method);
• Image normalization is the process of
stretching the range of an image from
[a, b] to [c, d].
• This is incredibly important for
visualization because if the image is
beyond [0,255] it will cause truncation or
unsightly effects.
Thresholding
• threshold( image, image, thresh, maxVal, CODE);
• CODE - this is the method of thresholding. Different actions will be
taken depending on this code.
Edge Detection
• Sobel Edge Detection
• void cv::Sobel(image in, image out, CV_DEPTH, dx, dy);
• Scharr Edge Detection
• void cv::Scharr(image in, image out, CV_DEPTH, dx, dy);
• Laplacian Edge Detection
• void cv::Laplacian( image in, image out, CV_DEPTH);
Image Smoothing
of edges and detail in an image.
• OpenCV includes most of the commonly used
methods.
• void GaussianBlur(imagein, imageout, Size ksize, sig);
• Note that there are more options, however this
should keep things simple
• void medianBlur (imagein, imageout, Size ksize);
• Other functions include generic convolution,
separable convolution, dilate, and erode.
Original Gaussian Blur Median Blur
STOP!
•This is not relevent until the last
part of the class.
•Beware!
Linear Algebra
•Operators
• given: Mat image;
• image.inv(); //inverse
• image.t(); //transpose
• image.clone(); //creates deep copy
• image.diag(int d=0) //returns diagonal
•image.mul(mat, double); //performs
elementwise multiplication.
•image.cross(mat); //performs cross
product
•image.dot(mat); //performs dot product
• OpenCV contains many
useful and simple functions
for applying linear algebra on
images.
• Most major operators are
allowed.
• operator * performs matrix
multiplication, NOT
elementwise multiplication.
Singular Value Decomposition
• Singular Value Decomposition is a
vital part of any computer vision
based system. Luckily, OpenCV
makes this a trivial task.
• To solve a least-squares problem,
simply call the solve command.
• bool solve(src1, src2, dst, int flags);
• Usually, src1 is A, src2 is b, and dst is x.
Remember flags is method...
• DECOMP_LU - Fast but cannot solve over-
determined systems.
• DECOMP_SVD - SVD, can solve just about
anything
Example
given:
-11x + 2y = 0
2x + 3y = 7
2x - y = 5
SVD Results
• Using OpenCV
• Using GNU Octave
Principle Component Analysis
• Since you will need to learn this, I will include it. Although you will
undoubtably will have to create your own PCA program, OpenCV
covers it very nicely.
• PCA( Mat data, Mat mean, int FLAG, int numcomp=0)
• FLAG: PCA_DATA_AS_ROW / PCA_DATA_AS_COL
• numcomp is the k value, 0 means all values retained
• in general, just pass the vectors into data and the mean will be
returned.
• PCA.project( Mat vector)
• projects the vector into the built eigenspace and returns the result
• PCA.backproject( Mat vector)
Important Tips
• Remember that images are read from file as 8-bit
unsigned integers. In order to do complicated math
operations, convert to 32-bit floating point type. Then
convert back to write to file.
• Always remember that rows is your y coordinate and
that cols is your x coordinate. Size objects are called
X,Y while images are referenced row, col. There are
many subtle things that will ruin good code.

More Related Content

Similar to Intro_OpenCV.ppt

Open Computer Vision Based Image Processing
Open Computer Vision Based Image ProcessingOpen Computer Vision Based Image Processing
Open Computer Vision Based Image ProcessingNEEVEE Technologies
 
Information from pixels
Information from pixelsInformation from pixels
Information from pixelsDave Snowdon
 
A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...
A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...
A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...Sangwoo Mo
 
Image processing for robotics
Image processing for roboticsImage processing for robotics
Image processing for roboticsSALAAMCHAUS
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017Andrey Karpov
 
DIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptx
DIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptxDIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptx
DIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptxMdMojnuMiah3
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfssuserff72e4
 
Cahall Final Intern Presentation
Cahall Final Intern PresentationCahall Final Intern Presentation
Cahall Final Intern PresentationDaniel Cahall
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++kinan keshkeh
 
Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015Christian Peel
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer GraphicsAdri Jovin
 
NVIDIA 深度學習教育機構 (DLI): Approaches to object detection
NVIDIA 深度學習教育機構 (DLI): Approaches to object detectionNVIDIA 深度學習教育機構 (DLI): Approaches to object detection
NVIDIA 深度學習教育機構 (DLI): Approaches to object detectionNVIDIA Taiwan
 
Introduction to computer vision with Convoluted Neural Networks
Introduction to computer vision with Convoluted Neural NetworksIntroduction to computer vision with Convoluted Neural Networks
Introduction to computer vision with Convoluted Neural NetworksMarcinJedyk
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerAndrey Karpov
 

Similar to Intro_OpenCV.ppt (20)

OpenCV+Android.pptx
OpenCV+Android.pptxOpenCV+Android.pptx
OpenCV+Android.pptx
 
Open Computer Vision Based Image Processing
Open Computer Vision Based Image ProcessingOpen Computer Vision Based Image Processing
Open Computer Vision Based Image Processing
 
Information from pixels
Information from pixelsInformation from pixels
Information from pixels
 
A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...
A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...
A Unified Framework for Computer Vision Tasks: (Conditional) Generative Model...
 
Image processing for robotics
Image processing for roboticsImage processing for robotics
Image processing for robotics
 
Effective C++
Effective C++Effective C++
Effective C++
 
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
PVS-Studio. Static code analyzer. Windows/Linux, C/C++/C#. 2017
 
DIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptx
DIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptxDIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptx
DIGITAL_SIGNAL_AND_IMAGE_PROCESSING_USIN.pptx
 
Lecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdfLecture1_computer vision-2023.pdf
Lecture1_computer vision-2023.pdf
 
OpenCV.pdf
OpenCV.pdfOpenCV.pdf
OpenCV.pdf
 
Visual CryptoGraphy
Visual CryptoGraphyVisual CryptoGraphy
Visual CryptoGraphy
 
Cahall Final Intern Presentation
Cahall Final Intern PresentationCahall Final Intern Presentation
Cahall Final Intern Presentation
 
2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++2 BytesC++ course_2014_c1_basicsc++
2 BytesC++ course_2014_c1_basicsc++
 
Surge2012
Surge2012Surge2012
Surge2012
 
Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015Ehsan parallel accelerator-dec2015
Ehsan parallel accelerator-dec2015
 
Computer Graphics
Computer GraphicsComputer Graphics
Computer Graphics
 
embedded C.pptx
embedded C.pptxembedded C.pptx
embedded C.pptx
 
NVIDIA 深度學習教育機構 (DLI): Approaches to object detection
NVIDIA 深度學習教育機構 (DLI): Approaches to object detectionNVIDIA 深度學習教育機構 (DLI): Approaches to object detection
NVIDIA 深度學習教育機構 (DLI): Approaches to object detection
 
Introduction to computer vision with Convoluted Neural Networks
Introduction to computer vision with Convoluted Neural NetworksIntroduction to computer vision with Convoluted Neural Networks
Introduction to computer vision with Convoluted Neural Networks
 
The operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzer
 

More from RithikRaj25

More from RithikRaj25 (17)

html1.ppt
html1.ppthtml1.ppt
html1.ppt
 
Data
DataData
Data
 
Data
DataData
Data
 
Introduction To Database.ppt
Introduction To Database.pptIntroduction To Database.ppt
Introduction To Database.ppt
 
Data.ppt
Data.pptData.ppt
Data.ppt
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
NoSQL.pptx
NoSQL.pptxNoSQL.pptx
NoSQL.pptx
 
NoSQL
NoSQLNoSQL
NoSQL
 
text classification_NB.ppt
text classification_NB.ppttext classification_NB.ppt
text classification_NB.ppt
 
html1.ppt
html1.ppthtml1.ppt
html1.ppt
 
slide-keras-tf.pptx
slide-keras-tf.pptxslide-keras-tf.pptx
slide-keras-tf.pptx
 
lec1b.ppt
lec1b.pptlec1b.ppt
lec1b.ppt
 
PR7.ppt
PR7.pptPR7.ppt
PR7.ppt
 
objectdetect_tutorial.ppt
objectdetect_tutorial.pptobjectdetect_tutorial.ppt
objectdetect_tutorial.ppt
 
14_ReinforcementLearning.pptx
14_ReinforcementLearning.pptx14_ReinforcementLearning.pptx
14_ReinforcementLearning.pptx
 
datamining-lect11.pptx
datamining-lect11.pptxdatamining-lect11.pptx
datamining-lect11.pptx
 
week6a.ppt
week6a.pptweek6a.ppt
week6a.ppt
 

Recently uploaded

GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Recently uploaded (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Intro_OpenCV.ppt

  • 2. Introduction • OpenCV is an Image Processing library created by Intel and maintained by Willow Garage. • Available for C, C++, and Python • Newest update is version 2.2 • Open Source and free • Easy to use and install
  • 3. Installation Instructions • Do not use synaptic on Linux to install OpenCV. • It is version 1.2. • For Linux and Windows, follow the installation guide at http://opencv.willowgarage.com/wiki/InstallGuide • Linux users can come to me for help. I have built it on Ubuntu dozens of times. I have built it successfully on Windows once. • Make sure to read the beginning as it gives you precise commands to install ffmpeg, libavformat-dev, libswscale-dev, and other required libraries.
  • 4. BASIC OPENCV STRUCTURES • Point, Point2f - 2D Point • Size - 2D size structure • Rect - 2D rectangle object • RotatedRect - Rect object with angle • Mat - image object
  • 5. Point • 2D Point Object - int x, y; • Functions - Point.dot(<Point>) - computes dot product - Point.inside(<Rect>) - returns true if point is inside • Math operators, you may use - Point operator + - Point operator += - Point operator - - Point operator -= - Point operator * - Point operator *= - bool operator == - bool operator != double norm
  • 6. Size • 2D Size Structure - int width, height; • Functions - Point.area() - returns (width * height) RECT • 2D Rectangle Structure - int x, y, width, height; • Functions - Point.tl() - return top left point - Point.br() - return bottom right point
  • 7. cv::Mat • The primary data structure in OpenCV is the Mat object. It stores images and their components. • Main items • rows, cols - length and width(int) • channels - 1: grayscale, 3: BGR • depth: CV_<depth>C<num chan> • See the manuals for more information
  • 8. cv::Mat •Functions - Mat.at<datatype>(row, col)[channel] - returns pointer to image location - Mat.channels() - returns the number of channels - Mat.clone() - returns a deep copy of the image - Mat.create( rows, cols, TYPE) - re-allocates new memory to matrix - Mat.cross(<Mat>) - computes cross product of two matricies - Mat.depth() - returns data type of matrix - Mat.dot(<Mat>) - computes the dot product of two matrices
  • 9. cv::Mat •Functions - Mat(Range(xmin,xmax),Range(ymin,ymax)) - returns sub image - Mat.type() - returns the TYPE of a matrix •Iterator Usage - Mat.begin() - moves Mat iterator to beginning of image - Mat.end() - moves Mat iterator to end of image
  • 10. Image TYPES • The TYPE is a very important aspect of OpenCV • Represented as CV_<Datatype>C<# Channels> • Example Datatypes/ Depths
  • 11. Pixeltypes • PixelTypes shows how the image is represented in data • BGR - The default color of imread(). Normal 3 channel color • HSV - Hue is color, Saturation is amount, Value is lightness. 3 channels • GRAYSCALE - Gray values, Single channel • OpenCV requires that images be in BGR or Grayscale in order to be shown or saved. Otherwise, undesirable effects may appear.
  • 12. HELLO WORLD • Example Code //Loads image and displays //call by ./a.out image.jpg // #include <cv.h> #include <cvaux.h> #include <highgui.h> using namespace cv; int main(int argc, char* argv[ ]){ Mat image = imread(argv[1]); namedWindow(“Sample Window”); imshow(“Sample Window”,image); waitKey(0); return 0; } This program will load and show an image
  • 13. Starting Out in OpenCV • OpenCV uses the cv namespace. • cv::Mat object replaces the original C standard IplImage and CvMat classes. • All original functions and classes of the C standard OpenCV components in the Bradski book are still available and current. However you will need to read that book for it.
  • 14. Image I/O • OpenCV provides simple and useful ways to read and write images. • Note that there are many extra options to these commands which are available on the wiki. • waitKey( int x ) has two main features. - if x > 0, then waitKey will wait x milliseconds - if x = 0, then waitKey will not move until key is pressed • Examples //Read an image Mat image = imread( <string>, <0 -gray, 1 -BGR>) //Note 1 is default //Write an image imwrite( <string filename> , image ); //Create window for output namedWindow( <window name> ); //Output image to window imshow( <window name> , <image Mat to show> ); //pause program for input key = waitKey( 0 );
  • 15. DRAWING STUFF • Sometimes it is necessary to draw stuff onto the image. Instead of using complicated functions, why not just call a simple function? • Here are some simple examples... • void circle(image, Point(x,y),int rad, CV_BGR(b,g,r), int thickness=1) • void ellipse(image, RotatedRect box, CV_BGR(b,g,r), int thickness=1) • void line(image, Point(x,y), Point(x,y), CV_BGR(b,g,r), int thickness= 1) • void rectangle(img, Point(x,y), Point(x,y), CV_BGR(b,g,r), int thickness) • NOTE: negative thickness will fill in the rectangle
  • 17. Using the Mouse • OpenCV allows you to use the mouse to interact with the screen. Note that this feature is from OpenCV 1.0 and is compatible with Mat objects. • This program allows you to draw dots on the image.
  • 19. Converting colorspaces • cvtColor( image, image, code) • Codes • CV_<colorspace>2<colorspace> • Examples • CV_BGR2GRAY • CV_BGR2HSV • CV_BGR2LUV
  • 20. Image Normalization • normalize(imagein, imageout, low, high, method); • Image normalization is the process of stretching the range of an image from [a, b] to [c, d]. • This is incredibly important for visualization because if the image is beyond [0,255] it will cause truncation or unsightly effects.
  • 21. Thresholding • threshold( image, image, thresh, maxVal, CODE); • CODE - this is the method of thresholding. Different actions will be taken depending on this code.
  • 22. Edge Detection • Sobel Edge Detection • void cv::Sobel(image in, image out, CV_DEPTH, dx, dy); • Scharr Edge Detection • void cv::Scharr(image in, image out, CV_DEPTH, dx, dy); • Laplacian Edge Detection • void cv::Laplacian( image in, image out, CV_DEPTH);
  • 23. Image Smoothing of edges and detail in an image. • OpenCV includes most of the commonly used methods. • void GaussianBlur(imagein, imageout, Size ksize, sig); • Note that there are more options, however this should keep things simple • void medianBlur (imagein, imageout, Size ksize); • Other functions include generic convolution, separable convolution, dilate, and erode. Original Gaussian Blur Median Blur
  • 24. STOP! •This is not relevent until the last part of the class. •Beware!
  • 25. Linear Algebra •Operators • given: Mat image; • image.inv(); //inverse • image.t(); //transpose • image.clone(); //creates deep copy • image.diag(int d=0) //returns diagonal •image.mul(mat, double); //performs elementwise multiplication. •image.cross(mat); //performs cross product •image.dot(mat); //performs dot product • OpenCV contains many useful and simple functions for applying linear algebra on images. • Most major operators are allowed. • operator * performs matrix multiplication, NOT elementwise multiplication.
  • 26. Singular Value Decomposition • Singular Value Decomposition is a vital part of any computer vision based system. Luckily, OpenCV makes this a trivial task. • To solve a least-squares problem, simply call the solve command. • bool solve(src1, src2, dst, int flags); • Usually, src1 is A, src2 is b, and dst is x. Remember flags is method... • DECOMP_LU - Fast but cannot solve over- determined systems. • DECOMP_SVD - SVD, can solve just about anything Example given: -11x + 2y = 0 2x + 3y = 7 2x - y = 5
  • 27. SVD Results • Using OpenCV • Using GNU Octave
  • 28. Principle Component Analysis • Since you will need to learn this, I will include it. Although you will undoubtably will have to create your own PCA program, OpenCV covers it very nicely. • PCA( Mat data, Mat mean, int FLAG, int numcomp=0) • FLAG: PCA_DATA_AS_ROW / PCA_DATA_AS_COL • numcomp is the k value, 0 means all values retained • in general, just pass the vectors into data and the mean will be returned. • PCA.project( Mat vector) • projects the vector into the built eigenspace and returns the result • PCA.backproject( Mat vector)
  • 29. Important Tips • Remember that images are read from file as 8-bit unsigned integers. In order to do complicated math operations, convert to 32-bit floating point type. Then convert back to write to file. • Always remember that rows is your y coordinate and that cols is your x coordinate. Size objects are called X,Y while images are referenced row, col. There are many subtle things that will ruin good code.