0% found this document useful (0 votes)
60 views

007 - Summer Training Report

The document is a summer training report on computer vision and OpenCV. It includes sections on Python, computer vision, OpenCV, image types, drawing on images, using the mouse, image normalization, thresholding, edge detection, image smoothing, and linear algebra. The report provides introductions and examples for key computer vision and OpenCV concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views

007 - Summer Training Report

The document is a summer training report on computer vision and OpenCV. It includes sections on Python, computer vision, OpenCV, image types, drawing on images, using the mouse, image normalization, thresholding, edge detection, image smoothing, and linear algebra. The report provides introductions and examples for key computer vision and OpenCV concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Python cum Computer Vision

SUMMER TRAINING REPORT


On
BLUE HEART PVT. LTD.

Submitted for partial fulfillment of Award of


BACHELOR OF TECHNOLOGY
In
Elelctronics & Communication Enginneering
(2021)
By
Abhishek Kumar Mishra
EC - 1712231007

SHRI RAMSWAROOP MEMORIAL GROUP OG PROFFESSIONAL


COLLEGES,LUCKNOW
Affiliated to
Dr. APJ ABDUL KALAM TECHNICAL UNIVERSITY, LUCKNOW
TABLE OF CONTENTS
Sr. No. Name Of Contents Page No.
1. Python 1-3
2. Computer Vision 4
3. Open CV 5-8
4. Image Types 9-12
5. Drawing Stuff 13-14
6. Using The Mouse 15-16
7. Image Normalization 17
8. Thresholding 18
9. Edge Detection 19
10. Image Smoothing 20
11. Linear Algebra 21
12. Morphological Transformation 22
13. Face Detection 23-24
14. Find And Contours 25-26
15. Corner Detection 27-28
16. Feature Matching (Homography) 29-31
17. Video From web cam in Open CV 32
Python
Python is an interpreted, object-oriented, high-level
programming language with dynamic semantics. Its high-
level built in data structures, combined with dynamic
typing and dynamic binding, make it very attractive for
Rapid Application Development, as well as for use as a
scripting or glue language to connect existing components
together. Python was conceived in the late 1980s by Guido
van Rossum at Centrum Wiskunde & Informatica (CWI) in
the Netherlands as a successor to the ABC language (itself
inspired by SETL), capable of exception handling and
interfacing with the Amoeba operating system. Its
implementation began in December 1989.
Why do people use python ?
•Developer productivity :
Python boosts developer productivity many
times beyond compiled or statically typed
languages such as C, C++, and Java. Python code
is typically one-third to one-fifth the size of eq.
uivalent C++ or Java code.

•Easy-to-learn:
Python has few keywords, simple structure, and
a clearly defined syntax. This allows the student
to pick up the language quickly.
•Interactive Mode:
Python has support for an interactive mode
which allows interactive testing and debugging of
snippets of code.

•Portable:
Python can run on a wide variety of hardware
platforms and has the same interface on all
platforms.
Python Advantages and Disadvantages
Introduction to Computer Vision
Computer Vision is an interdisciplinary field that deals
with how computers can be made to gain a high-level
understanding from digital images or videos.
The idea here is to automate tasks that the human visual
systems can do. So, a computer should be able to
recognize objects such as that of a face of a human being
or a lamppost or even a statue.
What is Open CV ?
OpenCV is a Python library which is designed to solve
computer vision problems. OpenCV was originally developed in
1999 by Intel but later it was supported by Willow Garage.
OpenCV supports a wide variety of programming languages
such as C++, Python, Java etc. Support for multiple platforms
including Windows, Linux, and MacOS.
OpenCV Python is nothing but a wrapper class for the original
C++ library to be used with Python. Using this, all of the
OpenCV array structures gets converted to/from NumPy arrays.
This makes it easier to integrate it with other libraries which
use NumPy. For example, libraries such as SciPy and Matplotlib.
Features Of Open CV

Cross Platform

Windows, Linux, Mac OS

Portable

iPhone

Android.

Language Support

C/C++

Python
Robot support
OpenCV Overview: > 500 functions
opencv.willowgarage.com

General Image Processing Functions Image Pyramids

Geometric
Descriptors
Segmentation Camera
Calibration,
Stereo, 3D
Features
Transforms Utilities and
Data Structures

Tracking
Machine
Learning: Fitting
•Detection,
•Recognition

Matrix Math
12
Installation Instructions
• For Mac OS X. Simply install Mac Ports then type
• sudo port install opencv
• 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.
• Follow instructions exactly!!!!!
Image TYPES
• The TYPE is a very important aspect of OpenCV
• Represented as CV_<Datatype>C<# Channels>
• Example Datatypes/ Depths
Pixel types
• 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 This program will load and show an
//Loads image and displays image
//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;
}
Image I/O
• OpenCV provides simple and useful • Examples
ways to read and write images. //Read an image
Mat image = imread( <string>, <0 -gray, 1 -BGR>)
//Note 1 is default
• Note that there are many extra
options to these commands which //Write an image
are available on the wiki. imwrite( <string filename> , image );

//Create window for output


• waitKey( int x ) has two main namedWindow( <window name> );
features.
//Output image to window
- if x > 0, then waitKey will wait x
imshow( <window name> , <image Mat to show> );
milliseconds
- if x = 0, then waitKey will not //pause program for input
move until key is pressed 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
• MORE...
http://opencv.willowgarage.com/documentation/cpp/core_drawing_functi
ons.html
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
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
• Image smoothing is used to reduce the the sharpness 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


Linear Algebra
• Operators
• OpenCV contains many useful
and simple functions for applying
• given: Mat image;
linear algebra on images. • image.inv(); //inverse
• image.t(); //transpose
• Most major operators are allowed. • image.clone(); //creates deep copy
• image.diag(int d=0) //returns diagonal
• operator * performs matrix
multiplication, NOT elementwise •image.mul(mat, double); //performs
elementwise multiplication.
multiplication.
•image.cross(mat); //performs cross product
•image.dot(mat); //performs dot product
•image.eye(); //converts mat to identity
matrix
Morphological Transformations
• Morphological transformation is basically some simple
operations performed on a binary image. And the binary
image is basically an image that contains two colors
usually black and white.
• There are main two operations in Morphological
Transformation:
1.Erosion
2.dilation
• Erosion: In erosion, we are just omitting the boundaries
of the front image or the object image that is in the
process we are thinning the object. Here we
use cv2.erode() function.
• Dilation:In the dilation process we are just going to
thick the boundaries of a binary image. The bright area
of the binary image dilates around the black regions of
the background. It’s actually the reverse process of
Erosion.Here we use cv2.dilate() function.
Face Detection • for (x,y,w,h) in faces:
• # import libraries of python OpenCV • # To draw a rectangle in a face
• import cv2 • cv2.rectangle(img,(x,y),(x+w,y+h),(255,255,0),2)
• roi_gray = gray[y:y+h, x:x+w]
• roi_color = img[y:y+h, x:x+w]
• face_cascade =
cv2.CascadeClassifier('haarcascade_frontalface_default.xml') • # Detects eyes of different sizes in the input image
• eyes = eye_cascade.detectMultiScale(roi_gray)
• # https://github.com/Itseez/opencv/blob/master
• # /data/haarcascades/haarcascade_eye.xml • #To draw a rectangle in eyes
• # Trained XML file for detecting eyes • for (ex,ey,ew,eh) in eyes:
• eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') • cv2.rectangle(roi_color,(ex,ey),
(ex+ew,ey+eh),(0,127,255),2)
• # capture frames from a camera
• cap = cv2.VideoCapture(0) • # Display an image in a window
• cv2.imshow('img',img)
• # loop runs if capturing has been initialized.
• while 1: • # Wait for Esc key to stop
• k = cv2.waitKey(30) & 0xff
• # reads frames from a camera • if k == 27:
• ret, img = cap.read() • break

• # convert to gray scale of each frames • # Close the window


• gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) • cap.release()

• # Detects faces of different sizes in the input image • # De-allocate any associated memory usage
• faces = face_cascade.detectMultiScale(gray, 1.3, 5) • cv2.destroyAllWindows()
Output Of Face Detection Program
Find And Draw Contours
• Contours are defined as the line joining all the
points along the boundary of an image that are
having the same intensity. Contours come
handy in shape analysis, finding the size of the
object of interest, and object detection.
• OpenCV has findContour() function that helps
in extracting the contours from the image. It
works best on binary images, so we should first
apply thresholding techniques, Sobel edges, etc.
Code for finding Countours
• import cv2 • # since findContours alters the image
• import numpy as np • contours, hierarchy = cv2.findContours(edged,
• cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
• # Let's load a simple image with 3 black squares
• image = cv2.imread('C://Users//gfg//shapes.jpg') • cv2.imshow('Canny Edges After Contouring', edged)
• cv2.waitKey(0) • cv2.waitKey(0)

• # Grayscale • print("Number of Contours found = " + str(len(contours)))


• gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
• # Draw all contours
• # Find Canny edges • # -1 signifies drawing all contours
• edged = cv2.Canny(gray, 30, 200) • cv2.drawContours(image, contours, -1, (0, 255, 0), 3)
• cv2.waitKey(0)
• cv2.imshow('Contours', image)
• # Finding Contours • cv2.waitKey(0)
• # Use a copy of the image e.g. edged.copy() • cv2.destroyAllWindows()
Feature Matching (Homography)
We start with the image that we're hoping to find, and then we can search for this
image within another image. The beauty here is that the image does not need to
be the same lighting, angle, rotation...etc. The features just need to match up.
To start, we need some sample images. Our "template," or image we're going to
try to match:

Then our image to search for this template in:


 We're going to find all features in both images. Then we match these
features. We then can draw out as many as we want. Careful though. If
you draw say 500 matches, you're going to have a lot of false positives.
Draw the first few only.
import numpy as np import cv2 import matplotlib.pyplot as plt img1 = cv2.imread('opencv-
feature-matching-template.jpg',0) img2 = cv2.imread('opencv-feature-matching-image.jpg',0)
So far we've imported the modules we're going to use, and
defined our two images, the template (img1) and the image
we're going to search for the template in (img2).
orb = cv2.ORB_create()
This is the detector we're going to use for the features.
kp1, des1 = orb.detectAndCompute(img1,None) kp2, des2 = orb.detectAndCompute(img2,None)
Here, we find the key points and their descriptors with the orb
detector.
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1,des2)
matches = sorted(matches, key = lambda x:x.distance)

Here we create matches of the descriptors, then we sort them based


on their distances.

img3 = cv2.drawMatches(img1,kp1,img2,kp2,matches[:10],None, flags=2)


plt.imshow(img3)
plt.show()
Video From Web Cam In Open CV
• # import the opencv library • cv2.imshow('frame', frame)
• import cv2 •
• # the 'q' button is set as the
• # quitting button you may use any
• # desired button of your choice
• if cv2.waitKey(1) & 0xFF == ord('q'):
• # define a video capture object
• break
• vid = cv2.VideoCapture(0)

• # After the loop release the cap object


• while(True):
• vid.release()

• # Destroy all the windows
• # Capture the video frame
Output:
• # by frame
• cv2.destroyAllWindows()

• ret, frame = vid.read()

• # Display the resulting frame


Conclusion
Python is a real world programming language that can be and is used in academia and the
commercial world. It appears to be quicker to learn an, in combination with its many libraries,
this offers the possibility of more rapid student development allowing the course to be made more
challenging and varied. And most importantly, its clean syntax offers increased understanding and
enjoyment for students.
 
I learnt about the concept of face detection using Open CV in Python using Haar cascade.
There are a number of detectors other than the face, which can be found in the library. We feel free
to experiment with them and create detectors for eyes, license plates, etc.
 
This will be very handy when we are trying to develop applications that require image
recognition and similar principles. Now, we should also be able to use these concepts to develop
applications easily with the help of OpenCV in Python.
 
Overall I am glad to have taken this opportunity to participate in research and feel that I have
learned a lot, not only about Python/OpenCV but about the Virtual Reality field in general.
 
References
 
• We can access the examples of application of Open
CV from
http://github.com/ntudavid/CV lab

• Reference : Open CV – Python Tutorials


https://opencv-python tutorials.readthedocs.org/en/la
test/

• https://www.geeksforgeeks.org/

You might also like