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

CV Fundamentals

Uploaded by

gauravrawat4657
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

CV Fundamentals

Uploaded by

gauravrawat4657
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

#the following are to do with this interactive notebook code


%matplotlib inline
from matplotlib import pyplot as plt # this lets you draw inline pictures in␣
↪the notebooks

import pylab # this allows you to control figure size


pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in␣
↪the notebook

'wget' is not recognized as an internal or external command,


operable program or batch file.
'wget' is not recognized as an internal or external command,
operable program or batch file.

---------------------------------------------------------------------------
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'

Now we can open an image:


[2]: input_image=cv2.imread('noidea.jpg')

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[2], line 1
----> 1 input_image=cv2.imread('noidea.jpg')

NameError: name 'cv2' is not defined

We can find out various things about that image


[ ]: print(input_image.size)

[ ]: 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')

1.2 converting between colour spaces, merging and splitting channels


We can convert between various colourspaces in OpenCV easily. We’ve seen how to split, above.
We can also merge channels:
[ ]: merged=cv2.merge([r,g,b])
# merge takes an array of single channel matrices
plt.imshow(merged)

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)

1.3 Getting image data and setting image data


Images in python OpenCV are numpy arrays. Numpy arrays are optimised for fast array operations
and so there are usually fast methods for doing array calculations which don’t actually involve
writing all the detail yourself. So it’s usually bad practice to access individual pixels, but you can.
[ ]: pixel = input_image[100,100]
print(pixel)

[ ]: input_image[100,100] = [0,0,0]
pixelnew = input_image[100,100]
print(pixelnew)

1.4 Getting and setting regions of an image


In the same way as we can get or set individual pixels, we can get or set regions of an image. This
is a particularly useful way to get a region of interest to work on.
[ ]: dogface = input_image[60:250, 70:350]
plt.imshow(dogface)

[ ]: fresh_image=cv2.imread('noidea.jpg') # it's either start with a fresh read of␣


↪the image,

# or end up with dogfaces on dogfaces on␣


↪dogfaces

# as you re-run parts of the notebook but␣


↪not others...

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)

1.5 Matrix slicing


In OpenCV python style, as I have mentioned, images are numpy arrays. There are some superb
array manipulation in numpy tutorials out there: this is a great introduction if you’ve not done it
before [http://www.scipy-lectures.org/intro/numpy/numpy.html#indexing-and-slicing]. The get-
ting and setting of regions above uses slicing, though, and I’d like to finish this notebook with a
little more detail on what is going on there.
[ ]: freshim2 = cv2.imread("noidea.jpg")
crop = freshim2[100:400, 130:300]
plt.imshow(crop)

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

ModuleNotFoundError: No module named 'tensorflow'

4
[ ]: pip install tfds-nightly

[ ]: pip install tensorflow

[4]: import tensorflow

---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[4], line 1
----> 1 import tensorflow

ModuleNotFoundError: No module named 'tensorflow'

[5]: import tensorflow_datasets as tfds

---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[5], line 1
----> 1 import tensorflow_datasets as tfds

ModuleNotFoundError: No module named 'tensorflow_datasets'

[ ]:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy