DEV Community

loading...

Getting started with OpenCV in python

George Marr
My curiosity is easily stimulated
・2 min read

Open Source Computer Vision Library (OpenCV) is a classic and sate of the art vision library that utilizes machine learning. It has the power to build applications such as: identify objects, classify human actions in videos, track camera movements, track moving objects, and many more. It is provided in python and C++, there is likely other wrappers around on Github or similar.

First we're going to need python version 3.6, if you're not on this version you can download it at: https://www.python.org

We're also going to need a few libraries, first being the OpenCV library, to install this enter the following:

pip install opencv-python

Enter fullscreen mode Exit fullscreen mode

You can additionally install the contributor kit if you wish (Not required)

pip install opencv-contrib-python

Enter fullscreen mode Exit fullscreen mode

In OpenCV projects you may find that you'll be using Number systems a lot, I recommend using the library Numpy. In this example it will not be required but you can install numpy by entering the following into your terminal

pip install Numpy

Enter fullscreen mode Exit fullscreen mode

Now that we have our libraries lets get to the fun stuff. In this example we will be taking a picture of multiple people (or yourself) and applying Split HSV, Saturation and hue filters, as well as showing a bitwise filter. The outcome should look something like this

If this shows I probably broke something

The code


import cv2

img = cv2.imread("mult.jpg", 1)  # image reading
# converting it into Hue, saturation, value (HSV)

hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)  

# the : in an array in python means that we're going to slice that part of the array

h = hsv[:, :, 0]
s = hsv[:, :, 1]
v = hsv[:, :, 2]

hsv_split = np.concatenate((h, s, v), axis=1)
cv2.imshow("Split hsv", hsv_split) 

#  some of the values require multiple variables, hence why ret is shown multiple times

ret, min_sat = cv2.threshold(s, 40, 255, cv2.THRESH_BINARY)
#  showing an image is very simple, first argument is the name, second is the image we wish to show
cv2.imshow("Sat filter", min_sat) 

ret, max_hue = cv2.threshold(h, 15, 255, cv2.THRESH_BINARY_INV)  # will do the inverse of the normal threshold

cv2.imshow("Hue filter", max_hue)

# the final image is the min saturation and the max hue put together

final = cv2.bitwise_and(min_sat, max_hue)
cv2.imshow("Final", final)

cv2.imshow("Original image", img)

#  the windows will display until a key is pressed, this is using key characters, in this case we're using escape, which is 27 but 0 also works
cv2.waitKey(0)
#  destroy all windows will prevent you from having to mass spam the kill keys
cv2.destoryAllWindows()

Enter fullscreen mode Exit fullscreen mode

And we're done. To test this simply run

python test.py

Enter fullscreen mode Exit fullscreen mode

In some operating systems you may need to run

python3 test.py

Enter fullscreen mode Exit fullscreen mode

Very simple introduction to OpenCV, the library has much potential.
Some useful links:
OpenCV documentation
Numpy/Spicy documentation
Python documentation
Link to image used in example

Discussion (0)