Opencv
Opencv
·
image
-
-
OpenCV
-
-
Introduction to OpenCV
§ OpenCV is a programming library with real-time computer vision capabilities
§ Officially it was launched in 1999, OpenCV from an intel initiative
§ It is written in C++
§ First major release 1.0 was in 2006, second in 2009 and third in 2015
§ It has many algorithms provided for processing
§ It is supported in many languages like Python, C, C++, Java etc
§ It is a free and open source library
OpenCV applications
§ Image file formats are standardized means of organizing and storing digital images
§ An image file format may store data in an uncompressed format, a compressed format (which may be
lossless or lossy), or a vector format
§ E.g.
§ Bitmap image file (BMP)
-
§ JPEG 2000
§ Graphics Interchange Format (GIF)
-
raw image
How image is stored on computer?
§ Every image is stored as binary data (pixel)
§ OpenCV uses RGB color space by default
§ Each pixel coordinate (x, y) contains 3 values ranging for intensities of 0 to 255 (8bit)
§ Red
§ Green
§ Blue
§ Mixing different intensities of each color gives us the full color spectrum
§ Yellow
§ Red - 255
§ Green - 255
§ Blue - 0
How image is stored on computer?
How image is stored on computer? [I Be255]
§ Image is stored in multi-dimensional arrays ↓ ,
255]
[255, 255 ,
,
-
,
I ..
-
-
2010
·
e)
-
I
3
S
,
S
OpenCV Image Representation
§ OpenCV uses BGR model instead of RGB model
§ The basic colors remain same but they are read in different order
Image Image
Files Files
OpenCV
Video Video
URL Output
cv2.imread() cv2.imshow()
cv2.VideoCapture() cv2.VideoWriter()
cv2.imwrite()
Reading and Writing files
import cv2
import cv2
capture = cv2.VideoCapture(0)
while capture.isOpened():
ret, frame = capture.read()
cv2.imshow('output', frame)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
capture.release()
cv2.destroyAllWindows()
Drawing Shapes
Introduction to shapes
§ OpenCV provides many functions to draw basic shapes
§ Common basic shapes include
§ Lines
§ Rectangles
§ Circles
§ Texts
§ It is useful in the scenario where the result needs to be highlighted
Creating empty image
§ Image is a collection of 2D binary data (pixel)
§ To create an empty image, just create an empty array
§ E.g.
§ E.g.
§ E.g.
§ E.g.
§ E.g.
§ After processing each channel, you can merge them back using merge function
§ E.g.
§ Image = cv2.merge((b, g, r))
Image Cropping
§ Extracting a segment of an image
§ Syntax:
img = cv2.imread('messi5.jpg')
cropped = img[50:155, 200:271]
cv2.imshow('cropped', cropped)
cv2.waitKey(0)
cv2.destroyAllWindows()
Resizing the image
§ Use cv2.resize() to resize the image
img = cv2.imread('messi5.jpg')
h, w = img.shape[:2]
new = cv2.resize(img, (w * 2, h * 2))
cv2.imshow('new image', new)
cv2.waitKey(0)
cv2.destroyAllWindows()
Rotations
§ Use cv2.warpAffine to implement the translations
§ Matrix
#$%& −%()&
!=
%()& #$%&
img = cv2.imread('messi5.jpg')
§ Use cv2.getRotationMatrix2D() to create the matrix h, w = img.shape[:2]
center = (w//2, h//2)
t = cv2.getRotationMatrix2D(center, -90, 1)
new = cv2.warpAffine(img, t, (w, h))
cv2.imshow('new image', new)
cv2.waitKey(0)
cv2.destroyAllWindows()
Translation
§ Use cv2.warpAffine to implement the translations
§ Matrix
S !=
1 0 !,
0 1 !-
img = cv2.imread('messi5.jpg')
h, w = img.shape[:2]
t = np.float32([[1, 0, ⑧
--
... 10], [0, 1, 10]])
-
img = cv2.imread('messi5.jpg')
m = np.ones(img.shape, dtype='uint8') * 105
added = cv2.add(img, m)
removed = cv2.subtract(img, m)
cv2.imshow('added', added)
cv2.imshow('removed', removed)
cv2.waitKey(0)
cv2.destroyAllWindows()
Edge Detection
§ Edge detection is a very important area in OpenCV, especially when dealing with contours
§ Edge can be defined as sudden changes (discontinuities) in an image and they can encode just as
much information as pixels
§ Types
§ Sobel: to emphasize vertical or horizontal edges
§ Laplacian: gets all orientations
§ Canny: optimal due to low error rate, well defined edges and accurate detection
Edge Detection - Canny Edge
-
-
im = cv.imread('test.jpg')
imgray = cv.cvtColor(im, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(imgray, 127, 255, 0)
im2, contours, hierarchy = cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
Draw the contours
§ To draw the contours, cv.drawContours function is used
§ It can also be used to draw any shape provided you have its boundary points
§ Its first argument is source image, second argument is the contours which should be passed as a
Python list, third argument is index of contours (useful when drawing individual contour)
§ To draw all contours, pass -1 and remaining arguments are color, thickness etc.
(x, y, w, h) = cv2.boundingRect(c)
Feature Detection
Cascading classifiers
§ Cascading is a particular case of ensemble learning based on the concatenation of several classifiers,
using all information collected from the output from a given classifier as additional information for the
next classifier in the cascade
§ Unlike voting or stacking ensembles, which are multiexpert systems, cascading is a multistage one
§ Cascading classifiers are trained with several hundred "positive" sample views of a particular object
and arbitrary "negative" images of the same size
§ After the classifier is trained it can be applied to a region of an image and detect the object in question
§ To search for the object in the entire frame, the search window can be moved across the image and
check every location for the classifier
§ This process is most commonly used in image processing for object detection and tracking, primarily
facial detection and recognition
Cascading classifiers in OpenCV
§ Object Detection using Haar feature-based cascade classifiers is an effective object detection method
proposed by Paul Viola and Michael Jones in their paper, "Rapid Object Detection using a Boosted
Cascade of Simple Features" in 2001
§ It is a machine learning based approach where a cascade function is trained from a lot of positive and
negative images. It is then used to detect objects in other images
eyeCascade = cv2.CascadeClassifier’(/haarcascade_eye.xml’)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
eyes = eyeCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))