Essential OpenCV Functions to Get You Started into Computer Vision
Learn about common OpenCV functions, and their applications Computer Vision is a field of artificial intelligence that trains computers to interpret and understand the visual world. As such many projects involve the usage of images from cameras and videos and the use of several techniques such as image processing and deep learning models. OpenCV is a library designed to solve common computer vision problems, it’s super popular among those in the field and it’s great for learning and using in production. The library has interfaces for multiple languages, including Python, Java, and C . Throughout this article we will cover different (common) functions inside OpenCV, their applications, and how you can get started with each one. Even though I’ll be providing the examples in Python, the concepts and the functions will be the same for the different supported languages. What exactly are we going to learn today? Reading, writing and displaying images Changing color spaces Resizing images Image rotation Edge Detection Reading, writing and displaying images Before we can do anything with computer vision, we need to be able to read and understand how images are processed by computers. The only information computers can process is binary information (0 and 1), this includes text, images, and video. How do computers work with images To understand how a computer “understands” an image yo can picture a matrix of the size of the image where on each cell you assign a value that represents the color of the image in that position. Let’s take an example with an image in greyscale: For this particular case, we can assign each block (or pixel) in the image a numeric value (which can be interpreted as binary). This numeric value can be from any range, though it’s a convention to use 0 for black, 255 for white, and all the integers in between to represent the intensity. When we work with color images, things can get a bit different depending on the library and how we choose to represent the colors. We will talk more about that later in the post, however, they all share more or less the same idea, which is using different channels to represent the colors, being RGB (red, green, and blue) one of the most popular options. With RGB we need 3 channels to build each pixel, so our 2d matrix now is a 3d matrix with a depth of 3, where each channel is the intensity of a particular color, and when mixing we get the final color for the pixel. Working with images using OpenCV Let’s now jump into the code to perform 3 of the most important functions when dealing with images, reading, showing, and saving. import cv2 import matplotlib.pyplot as plt # Reading the image image = cv2.imread(‘sample1.jpg’) # Showing the image plt.imshow(image) plt.show() # Saving the image cv2.imwrite(‘sample1_output.jpg’, image) If you run our code, now you will get one image saved to disk, and another as a result of the plot. The image on the left is the one we plotted, vs the one on the right which is the image saved to disk. The difference in sizes aside (due to the plot), the image on the left side looks weird, looks bluish, but why is it different? (by the way, the image on the right is correct). The reason why the image on the left looks with strange colors has to do with how OpenCV read images by default. » Read More
Like to keep reading?
This article first appeared on livecodestream.dev. If you'd like to keep reading, follow the white rabbit.