CV Fundamentals
CV Fundamentals
October 7, 2024
1 OpenCV fundamentals
1.1 Python getting started
First we need to import the relevant libraries: OpenCV itself, Numpy, and a couple of others.
Common and Video are simple data handling and opening routines that you can find in the OpenCV
Python Samples directory or from the github repo linked above. We’ll start each notebook with
the same includes - you don’t need all of them every time (so this is bad form, really) but it’s easier
to just copy and paste.
[1]: # These imports let you use opencv
import cv2 #opencv itself
import common #some useful opencv functions
import numpy as np # matrix manipulations
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[1], line 6
3 get_ipython().system('wget --no-check-certificate https://raw.
↪githubusercontent.com/computationalcore/introduction-to-opencv/master/utils/
↪common.py -O common.py')
5 # These imports let you use opencv
----> 6 import cv2 #opencv itself
7 import common #some useful opencv functions
8 import numpy as np # matrix manipulations
1
ModuleNotFoundError: No module named 'cv2'
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 1
----> 1 input_image=cv2.imread('noidea.jpg')
[ ]: print(input_image.shape)
[ ]: print(input_image.dtype)
gotcha that last one (datatype) is one of the tricky things about working in Python. As it’s not
strongly typed, Python will allow you to have arrays of different types but the same size, and some
functions will return arrays of types that you probably don’t want. Being able to check and inspect
the datatype like this is very useful and is one of the things I often find myself doing in debugging.
[ ]: plt.imshow(input_image)
What this illustrates is something key about OpenCV: it doesn’t store images in RGB format, but
in BGR format.
[ ]: # split channels
b,g,r=cv2.split(input_image)
# show one of the channels (this is red - see that the sky is kind of dark. try␣
↪changing it to b)
plt.imshow(r, cmap='gray')
2
OpenCV also has a function specifically for dealing with image colorspaces, so rather than split
and merge channels by hand you can use this instead. It is usually marginally faster…
There are something like 250 color related flags in OpenCV for conversion and display. The ones you
are most likely to use are COLOR_BGR2RGB for RGB conversion, COLOR_BGR2GRAY for con-
version to greyscale, and COLOR_BGR2HSV for conversion to Hue,Saturation,Value colour space.
[http://docs.opencv.org/trunk/de/d25/imgproc_color_conversions.html] has more information on
how these colour conversions are done.
[ ]: COLORflags = [flag for flag in dir(cv2) if flag.startswith('COLOR') ]
print(len(COLORflags))
# If you want to see them all, rather than just a count uncomment the following␣
↪line
print(COLORflags[1:10])
[ ]: opencv_merged=cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB)
plt.imshow(opencv_merged)
[ ]: input_image[100,100] = [0,0,0]
pixelnew = input_image[100,100]
print(pixelnew)
fresh_image[200:200+dogface.shape[0], 200:200+dogface.shape[1]]=dogface
3
print(dogface.shape[0])
print(dogface.shape[1])
plt.imshow(fresh_image)
The key thing to note here is that the slicing works like
[top_y:bottom_y, left_x:right_x]
This can also be thought of as
[y:y+height, x:x+width]
You can also use slicing to separate out channels. In this case you want
[y:y+height, x:x+width, channel]
where channel represents the colour you’re interested in - this could be 0 = blue, 1 = green or
2=red if you’re dealing with a default OpenCV image, but if you’ve got an image that has been
converted it could be something else. Here’s an example that converts to HSV then selects the S
(Saturation) channel of the same crop above:
[ ]:
[ ]: hsvim=cv2.cvtColor(freshim2,cv2.COLOR_BGR2HSV)
bcrop =hsvim[100:400, 100:300, 1]
plt.imshow(bcrop, cmap="gray")
Next
[3]: import tensorflow
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[3], line 1
----> 1 import tensorflow
4
[ ]: pip install tfds-nightly
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[4], line 1
----> 1 import tensorflow
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[5], line 1
----> 1 import tensorflow_datasets as tfds
[ ]: