!pip install opencv-contrib-python
import cv2
- An open source computer vision and machine learning
library
. - Contains over 2,500 optimized algorithms.
- Algorithms for Facial recognition, Object identification, Human action, Track movements,etc.
- Computers gain high level understanding from digital
image
andvideo
- Understand and automate the task of
human visual system
Detecting
andlabelling
image or object has surpassed humans.Faster
than human reaction and99% Accuracy
- High amount of visual data i.e images and videos with high processing capabilities.
OpenCV and NumPy
OpenCV
code is orignally written inC++
- When we use OpenCV in Python, Python act as
wrapper
around C++ Code. - All the
OpenCV array
structures are converted to and fromNumPy arrays
- NumPy arrays are powerful
N dimensional
array objects. - NumPy have sophisticated
broadcasting
functions. - It makes processing for images faster.
- Every NumPy array is a grid of elements of the same type.
Image Processing
Extracting
infromation from image andenhancing
the image quality.- Computers visualize images as a
matrix
- Each element in matrix is a
pixel
OpenCV
reads the image in channel orderBGR
format butMatplotlib
and other visualization tools read inRGB
order.- Each pixel have color information composed of
3 channels
(B G R) containing numbers from0
to255
- Each pixel of
matrix
represents the intensity of an color. - Each channel need
8 bits
for storage therefore it become24 bits
for each pixel. - Some images need to be converted to
Grayscale
orBinary
before applying any image processing. Blending
: Overlaying images on top of each other by adjusting dimensions and opacity.Thresholding
: Segmenting or dividing images into different parts.Binary Thresholding
: Value below threshold will be shifted to0
else255
Bianry Inverse Thresholding
: Value below threshold will be shifted to255
else0
Truncation Thresholding
: Value below threshold will be shifted tothreshold value
elseremain same
Threshold To Zero
: Value below threshold will0
elseremain same
Threshold To Zero Inverse
: Value below threshold willremain same
else0
Blurring
: Adding noiseSmoothing
: Removing noise to focus more on general details of the images.Edge Detection
: Detect edged in high resolution images without bluring.Gamma Correction
: Makes imagesbrighter
ordarker
based on choosen gamma value ( Lower gamma value : Brighter images )Kernels
are used for Blurring and Smoothing
Erosion
: Remove pixels on objectforeground
andboundaries
(Shrink
the boundaries)Dilation
: Add pixels to the boundaries of object in an image (Enlarge
the boundaries)Opening
: Remove white noise from black background.Closing
: Remove black noise from white foreground.
print(Image.shape)
(340, 540, 3) : (height, width, channel)
340 represents rows | height
540 represents columns | width
3 represents channels.
Image Type | Storage (Bits Per Pixel) | Channel |
---|---|---|
Binary | 1 |
|
B/W | 8 |
1 |
Grayscale | 8 |
1 |
Color | 24 |
3 |
Color | Channel |
---|---|
B |
0 |
G |
1 |
R |
2 |
- Basic fundamental
unit
of an image. - Multiple pixels arranged in
rows
andcolumns
forms an image. - Binary, black and white, grayscale, RGB color image.
B/W
is only composed with eitherblack
orwhite
Greyscale
is combination of differentshades
of black and white.
HSV
HLS
Applications
Autonomous
self driving cars : Detect objects and humans infront of car, reverse parking.- Facial recognition : Attendance, FB friend recognition.
AR
: Augmented Reality andVR
: Virtual Reality- Health care : X Ray and MRI scan.
- Video motion analysis : Detect face, Object in video.
- Image
segmentation
: Camera detects the multiple faces in a group selfie. - Scene
reconstruction
: 3D model creation in architecture, room interior. - Image
restoration
: Filtering blur images and removing noise.
DNN
is OpenCV's deep learning module.
The below step you will find in many articles and videos, It simply represents :
- To
mirror
the image you simply need to reverse either thex
ory
coordinates.
# Trick to reverse BGR to RGB and keep x and y as it is (One liner code)
image[:, :, ::-1]
# You can also the method :
cv2.cvtColor(src=image, code=cv2.COLOR_BGR2RGB)