---
tags: OpenCV
---
# OpenCV with Python -- 1
[Back to book mode](https://hackmd.io/@Justin123/opencv)
## Backgronud knowledge
OpenCV is an **open-source** BSD-licensed library that includes several hundreds of **computer vision algorithms**.
## Application situation of OpenCV
OpenCV has a modular structure, which means that the package includes several shared or static libraries.
* Core functionality
* Image Processing
* Viedo Analysis
* Camera Calibration and 3D Reconstruction
* 2D Feautures Framewrok
* Object Detection
* High-level GUI
* Viedo I/O
> From my personal perspective, it is supported by both Python and C++ which makes it so popular in contrast to Pillow.
>
## Limitation of using OpenCV
Truly to say, there are bare limitation with aspect to OpenCV. However, I still list some disadvantages here.
* OpenCV does not provide the same ease of use when compared to MATLAB
* OpenCV has a flann library of its own. This causes conflict issues when you try to use OpenCV library with the PCL library
## Basic operation
1. **Read and show image**
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2 # import the neccessary module
src = "./scene.jpg" # Path for the image
image = cv2.imread(src) # Read the image
# Convert the image to gray scale
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Show the image on GUI window
cv2.imshow("Gray scale", image)
# Wait for the key press for every second
# After that, use close every windows
# For mac user, should add last lines(I still no idea for why)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
```

*<center>Output gray scale photo</center>*
---
2. **Viedo**
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
# Read the source
cap = cv2.VideoCapture("test.mp4")
'''
Video is the sequence of images.
We need while loop to show the video
'''
while True:
# success is to check whether the video is successfully captured
success, img = cap.read()
cv2.imshow("Video", img)
# Press q to quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break;
cv2.destroyAllWindows()
cv2.waitKey(1)
```
* **Default webcame**
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
# Use the default webcame
cap = cv2.VideoCapture(0)
'''
3 -> width: set to 640
4 -> height: set to 640
10 -> brightness: set to 100
'''
cap.set(3, 640)
cap.set(4, 480)
cap.set(10, 100)
'''
Video is the sequence of images.
We need while loop to show the video
'''
while True:
success, img = cap.read()
# To ensure the camera can successfully read the image
if (not success):
continue
cv2.imshow("Video", img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break;
# Close the webcame
cap.release()
cv2.destroyAllWindows()
cv2.waitKey(1)
```
## The inner vision of pictures
Pictures are made of lots of pixel. Each pixels is a number in **[0, 255]**. On the other hand, color pictures have another dimenstion for storing additional information. Especially, the number for stroing the color information equals 3 which relates to <font color="red">red</font>, <font color="green">green</font> and <font color="blue">blue</font>.
$$A=
\left\{
\begin{matrix}
123 &201 & \cdots & 33\\
198 & 130 & \cdots & 57 \\
\vdots & \vdots & \ddots & \vdots \\
0 & 67 & \cdots & 98
\end{matrix}
\right\}
$$
---
1. **Color pictures**
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Import the needed package
import cv2
# Read the image
image = cv2.imread('scene.jpg')
# Print out the number of image
print(image.shape)
# Uncomment next line to observe the matrix of the image
# print(image)
```
```python
# Output
(1280, 1920, 3)
```
==Notice that the dimension of the images is 3, and the number of the last dimension is 3.==
---
2. **Gray Scale pictures**
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Import the needed package
import cv2
# Read the image
#
image = cv2.imread('scene.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Print out the number of image
print(image.shape)
# Uncomment next line to observe the matrix of the image
# print(image)
```
```python
#Output
(1280, 1920)
```
==In contrast to the color image, the dimension of the image is 2.==
---
3. **Convert the color image to Gray Scale image**
* Using `cv2.cvtColor(image, cv2.BGRtoGRAY)` (Recommend)
* Manually (Just for knowing the concept)
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# Using axis=-1 to add all array by the last dimension
# Devided by 3 to get the average
# Change type to int in [0, 255] or float in [0, 1]
image = cv2.imread('scene.jpg')
image = (np.sum(image, axis=-1) / 3).astype("uint8")
cv2.imshow("Gray scale", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.waitKey(1)
exit(0)
```
---
4. **Extract each color matrix from RGB image**
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
# Split the color image to blue, green, red
image = cv2.imread('scene.jpg')
b, g, r = cv2.split(image)
# Print the matrix out
print(f'Red: {r}\nGreen: {g}\nBlue: {b}')
# Save all images
cv2.imwrite("red_gray.jpg", r)
cv2.imwrite("green_gray.jpg", g)
cv2.imwrite("blue_gray.jpg", b)
```
```python
# Output
Red: [[170 173 173 ... 215 216 216]
[174 175 177 ... 213 213 213]
[175 178 181 ... 210 209 208]
...
[ 76 73 74 ... 24 25 25]
[ 76 73 73 ... 25 24 24]
[ 72 69 68 ... 27 26 24]]
Green: [[125 128 129 ... 192 193 193]
[128 131 133 ... 190 190 190]
[131 134 136 ... 187 186 185]
...
[ 65 63 64 ... 20 21 21]
[ 66 63 62 ... 21 20 20]
[ 62 59 59 ... 23 22 20]]
Blue: [[ 58 61 65 ... 154 155 155]
[ 64 67 70 ... 152 152 152]
[ 68 71 75 ... 149 148 147]
...
[ 3 3 4 ... 0 0 0]
[ 6 3 4 ... 0 0 0]
[ 2 0 2 ... 0 0 0]]
```
<center><img src='https://i.imgur.com/zmjjUKV.jpg' height='300'></center>
*<center>Output blue gray scale photo</center>*
<center><img src='https://i.imgur.com/DMm5t46.jpg' height='300'></center>
*<center>Output green gray scale photo</center>*
<center><img src='https://i.imgur.com/Mmy5ckA.jpg' height='300'></center>
*<center>Output red gray scale photo</center>* <br/>
So far, you may wonder why the output images is in Gray Scale. The reason is that the type of the ouput images are decided by its dimension. If the dimension is 3, it corresponds to the color images. On the other hand, if the dimension is 2, it will show the Gray Scale images. Otherwise, the error messages will pop out.
---
5. **Expand to the rgb photo**
Now, we're going to broadcast our images to 3 dimension and show the RGB images.</br>
```python=
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import cv2
import numpy as np
# Extract color
image = cv2.imread('scene.jpg')
b, g, r = cv2.split(image)
# Build a dummy 3 dimensional matrix
blue_image = np.zeros(image.shape, image.dtype)
green_image = np.zeros(image.shape, image.dtype)
red_image = np.zeros(image.shape, image.dtype)
'''
Mix the images to corresponding place
Blue -> [0, 0], others remain 0
Green -> [1, 1], others remain 1
Red -> [2, 2], others remain 2
'''
cv2.mixChannels([b, g, r], [blue_image], [0,0])
cv2.mixChannels([b, g, r], [green_image], [1,1])
cv2.mixChannels([b, g, r], [red_image], [2,2])
# Write out the images
cv2.imwrite("blue.jpg", blue_image)
cv2.imwrite("green.jpg", green_image)
cv2.imwrite("red.jpg", red_image)
```
<center><img src='https://i.imgur.com/ojZ2fcp.jpg' height='300'></center>
*<center>Output blue photo</center>*
<center><img src='https://i.imgur.com/X1dUEJ4.jpg' height='300'></center>
*<center>Output green photo</center>*
<center><img src='https://i.imgur.com/iXVz05N.jpg' height='300'></center>
*<center>Output red photo</center>*
## Summary
In this article, we discuss how to use OpenCV to do the computer vision and also know the concept of images. In fact, there are lots of library to acheive the same results. However, after learning OpenCV, we can also implement the same way in C++.
## Reference
1. [Data dtype](https://docs.scipy.org/doc/numpy-1.13.0/user/basics.types.html)
2. [Getting Started with Image Processing using Python](https://medium.com/analytics-vidhya/getting-started-with-image-processing-using-python-c027fbdd4608)
3. [Exploring Image Processing Techniques — OpenCV](https://towardsdatascience.com/exploring-image-processing-techniques-opencv-4860006a243)
4. [Image processing with Python & Open-CV part-1](https://medium.com/datadriveninvestor/image-processing-using-python-open-cv-part-1-b5e83b5c2398)
5. [Image processing with Python & Open-CV part-2](https://medium.com/datadriveninvestor/image-processing-using-python-open-cv-part-2-72f2b75918e7)
6. [Image processing with Python & Open-CV part-3](https://medium.com/@ishan.cdixit/image-processing-using-python-open-cv-part-3-e3047ce58081)
7. [Image Processing using Python basic - I](https://medium.com/@vidushraj5/image-processing-using-python-basic-9e43cd9d7a01)