It is the foundation for many of the applications you know that deal with image processing.

Today we will learn how to work with OpenCV, and Ill do my best to keep it simple.

It’s free, every week, in your inbox.

A foolproof guide to image manipulation in Python with OpenCV

Well… the first thing we have to do is to import the library.

Only then we can read the image using theimreadmethod and pass the images path as the only parameter.

For that, we will usecv2.imshowand passing the window name and the image as arguments.

Article image

Lastly, we tell Python not to exit the program until we press a key or kill the window.

Then we clean everything up by destroying all windows we opened.

We use the methodVideoCaptureto load the video resource.

Article image

The first argument defines what input we are reading.

Passing a0, we are referring to the main webcam (if existent).

In case you have multiple webcams connect, you’re able to use1,2, etc.

Article image

Next, we start a loop that will only end on user command, but more on that later.

Whats important here is what happens inside the loop.

The first thing we are doing is asking ourVideoCaptureto read a frame of the video.

Article image

If it is, then we exit the loop for the cleanup activities.

We can do that by using the methodreleasefrom theVideoCaptureobject.

If resizing an image is what you want, OpenCV got you covered.

Article image

Note that the code will be simpler if we are targeting specific dimensions.

Optionally we can pass the third argument to define the interpolation as described on theresize function docs.

Each of them with different characteristics worth studying and learning.

Article image

Lets see an example of how we can transform a color image into a greyscale one.

Fortunately, OpenCV has defined values for each known color space transformation.

In our case, we useCOLOR_BGR2GRAY, which transforms BGR to GRAY.

Article image

Its the default way that OpenCV loads images.

The actual image that we need to save is the variable gray in this case.

Image smoothing

OpenCV offers tools to smooth an image and help reduce the noise in it.

Article image

Through the methodbluron the OpenCV library, which expects the image and the kernel size as arguments.

The kernel size being a tuple to reflect the x and y axis.

There are other ways to smooth an image using for example,gaussianBlurormedianBlurthat works similarly.

Article image

Its time we change that.

Lets draw a few geometric shapes on images to show how it works.

They are also super easy to use.

Article image

Here is an example:

Therectanglefunction is very similar to thelinefunction.

Drawing a circle

Again all these functions are pretty similar.

Conclusion

OpenCV is an exciting and powerful library for dealing with images and videos.

Article image

Today we covered just a small percentage of what this library is capable of.

Sign up for updates on everything related to programming, AI, and computer science in general.

Also tagged with

Article image

Article image

Article image

Article image

Article image

Article image

Article image

Article image