Contents:

Training program with a guarantee of employment: "Strategy Success for Data Scientists and AI Specialists"
Learn MoreWhen parents educate their children, they often use books with bright and colorful illustrations. Such pages might include pictures of a cat, a flower, and a car. This helps children develop the skill of distinguishing objects.
Teaching computers to recognize images is similar, but uses thousands of times more images. This technology is known as computer vision.
- Computer vision is a field of computer science that deals with how machines can interpret and understand visual information from the world around them. It relies on the ability of computers to analyze images and video streams to extract useful information from them.
The computer vision process begins with capturing an image using a camera or other sensor. The captured data is then processed to highlight key features, such as shapes, colors, and textures. For this, various algorithms and methods are used, including image processing and machine learning.
The next step is analyzing and interpreting the obtained information. Using models trained on large volumes of data, a computer can recognize objects, determine their characteristics, and even predict actions. For example, computer vision systems can be trained to recognize faces, cars, or animals.
Thus, computer vision combines theory and practice, allowing machines not only to see but also to understand what they "see". This technology finds application in a wide variety of fields, including medicine, security, autonomous vehicles, and many other areas where visual data analysis is required.
- What is OpenCV
- Computer vision, as well as the OpenCV library, find their application in a wide variety of fields. This technology is actively used in medicine for image analysis, which allows doctors to diagnose diseases more accurately. In the automotive industry, computer vision systems play a key role in the development of autonomous vehicles, enabling their ability to recognize their surroundings.
Computer vision also finds application in security, where it is used for facial recognition and monitoring. In manufacturing, it helps automate quality control processes, and in entertainment, it helps develop games and augmented reality applications.
Due to its versatility, OpenCV is also actively used in scientific research that requires processing large volumes of visual data. This library provides powerful tools for solving image processing problems, making it an essential resource for developers and technology researchers.
- Working with the OpenCV library in Python provides extensive capabilities for processing images and video. First, you need to install the library, which can be done using the pip package manager. In the terminal or command line, simply run the command:
«`
pip install opencv-python
«`Once installed successfully, you can import OpenCV into your project. This is typically done with the following command at the beginning of your Python script:
«`python
import cv2
«`OpenCV offers many functions for working with images. For example, to load an image, you can use the `cv2.imread()` method, passing it the path to the file. To display the image on the screen, use `cv2.imshow()`, and to save the modified image to disk, use `cv2.imwrite()`.
In addition, the library supports video stream processing. For this, you can use `cv2.VideoCapture()` to capture video from a camera or a file. Using a loop, you can process each frame using various filters and processing algorithms.
OpenCV also provides tools for working with various computer vision tasks, such as face recognition, object detection, and contour creation. Various functions are used for these purposes, for example, `cv2.CascadeClassifier()` for face recognition and `cv2.findContours()` for finding contours in an image.
Thus, OpenCV is a powerful tool for developers who want to work with image and video processing in Python. With this library, you can implement various projects, ranging from simple image editing to complex computer vision systems.
- Installing OpenCV
- OpenCV processes images using various algorithms and methods, which allows it to perceive and analyze visual information. When an image is loaded, the library converts it into an array of pixels, where each pixel contains color and brightness information. This transformation enables further operations such as filtering, resizing, object recognition, and much more.
The library supports a wide range of formats, making it a versatile tool for working with graphics. OpenCV enables image manipulation at the pixel level, as well as the application of complex computer vision algorithms such as edge detection, face recognition, and motion analysis. These features allow developers to create powerful applications that can interact with visual data in real time.
- Reading, display options, and saving data.
- Adjusting pixel hue.
- Image scaling.
- Cropping.
- Rotating.
- OpenCV Scripting Examples.
- Point Coordinate Representation.
- Face Recognition.
- What's Next.
Computer Vision Structure and Operation.
Computer vision is a branch of artificial intelligence that deals with the extraction, processing, and analysis of visual data, such as photographs and videos.
Let's consider the concept through a specific example. Pay attention to this image and tell us what it shows.

The gaze instantly captures individual objects. A pair of sunglasses reflects a person, their hand, and a red pill in one lens, and the same person, a different hand, and a blue pill in the other lens.
If someone has seen The Matrix, they will be able to easily name the film, recognize the characters, and perhaps recall the names of the actors involved—it all depends on their interest in cinema and the number of films they have seen.
What a program can recognize in a given image will depend on its previous training, namely, the data it has received, as well as the complexity of the algorithms used in the code.
- A simple program will only be able to determine that the file has a .jpg extension, indicating that it is an image.
- A complex program will open the file and analyze its contents, revealing that it is made up of many pixels of different colors. It will be able to display the image on the screen and possibly offer editing options: changing the color of some pixels, cropping the image, and performing other manipulations.
- When the program uses neural networks, it can analyze images and extract from them not only technical characteristics but also additional information. For example, it can detect glasses, hands, pills, and faces in a photograph, as well as identify similar images, determine which movie a frame is from, and determine which actors are involved. This program's ability falls under the field of computer vision. The volume and type of information extracted will depend on the data and volume of data the program was trained on.
Machine learning is used to enable the program to perceive an image not just as a collection of pixels, but as a set of familiar objects.

Read also:
Machine learning is a field of artificial intelligence that develops algorithms that enable computers to learn from data. These algorithms analyze information, identify patterns, and make predictions or decisions based on accumulated experience.
Learning machine learning can be highly useful and relevant. In today's world, where data volumes are growing rapidly, skills in this field are increasingly in demand. Companies around the world are looking for specialists who can process and interpret large amounts of data and apply this knowledge to optimize processes and improve products.
Thus, mastering machine learning opens up many opportunities for career advancement and professional development, making its study worthwhile.
There are various areas in the field of visual perception, among which machine vision stands out. At its core, it is similar to computer vision, but is used to solve specific practical problems. For example, factories install cameras that monitor the quality of goods on the conveyor line. If a defect is detected, such a camera alerts the operator to the problem—and this is its main function. Thus, in this context, computer vision can be considered machine vision.
What is OpenCV
OpenCV, which stands for Open Source Computer Vision Library, is an open-source library designed for computer vision tasks. Originally developed in C++, it has also been adapted to work with Python, C, Java, and MATLAB.
OpenCV offers integrated computer vision algorithms based on machine learning methods and presented as separate modules with a variety of capabilities. Below are some of them:
- Core functionality is a set of key data structures and library functions that serve as the basis for other modules.
- Image processing allows you to manipulate static images, including simple formats such as PNG, JPG, and others.
- Video analysis is designed to monitor the movement of objects and interactions with background elements.
- Camera calibration and 3D reconstruction is a process associated with the study of the geometric characteristics of objects, which makes it possible to form their three-dimensional models based on a series of photographs or video materials.
- The 2D Features Framework finds and captures areas of an image that stand out from the rest, preserving their contours. In addition, it is able to detect similar elements among them.
- Object detection is a process that identifies various objects, including categories such as faces, cars, birds, and many others.
- A high-level graphical user interface (GUI) provides the ability to create visual interfaces designed to perform simple tasks.
- Video input and output are capabilities that make it possible to read and process video materials.
This is only a small part of the modules provided in OpenCV. For more detailed information about them, as well as about other library functions, it is worth referring to the documentation.
Applications of computer vision and the OpenCV library
Computer vision penetrates into everyday life: robotic vacuum cleaners and cars successfully avoid obstacles, and social networks recognize your photos and ask if it is really you.
Computer vision technologies are used to solve many different problems.
Text can be recognized using various applications. For example, to translate the text on a label, just launch a translation application on your smartphone and point the camera at the desired fragment. The program will recognize the text and provide a translation into the selected language.
Image search allows you to find out the name of an unfamiliar flower. To do this, just take a photo of it and upload it to search engines such as Google or Yandex.
Content is subject to moderation. Many forums and social networks analyze posted material, and if it does not comply with the platform's established rules, such content is automatically removed.
Biometric technologies allow you to unlock your device or access services using your fingerprint or facial recognition. Your information is processed using computer vision algorithms.
The process of creating 3D models. Using computer vision technologies, you can transform multiple photos or videos into a 3D model of a specific object or environment. This technique is used in restoring the appearance of various objects and landscapes, as well as in the field of robotics, where it helps robots navigate in space.
Agriculture can be significantly improved thanks to computer vision technologies. This technology allows not only to count the number of animals on a farm, but also to monitor the condition of crops. With its help, problems such as diseases or pests can be identified in advance, even before a person notices them.
Organization of production processes. Instead of relying on human labor to perform monotonous tasks, computer vision technologies can autonomously analyze products and processes. For example, a camera could be installed on a conveyor belt that would automatically identify defective products. Unlike humans, such a system is not susceptible to fatigue and is not distracted from its work.
There are many tools that can be used to develop computer vision systems, and one of the most well-known is OpenCV. This tool is actively used by tech giants such as Google, Yahoo, Microsoft, Intel, IBM, Sony, Honda, and Toyota to solve various problems in this field.
For example, in China, OpenCV is used to monitor the condition of equipment in mines, while in Japan, the technology is used for facial recognition. You can easily develop your own program for this task. We'll explain how to do this in the next section.
Using OpenCV Effectively in Python
OpenCV is a free library that can be used in personal, educational, and commercial projects. Let's take a closer look at its main features using the Python programming language as an example, and finally, we'll create a script that can recognize human faces.
To get started with OpenCV, you need to install a programming language it supports. In this case, it's Python. If Python isn't yet installed on your computer, you can refer to our guide for the necessary instructions.
You can load the library using your IDE's capabilities or using the command line:
Now you can load the module and start interacting with it:
Before we get started with the practical exercises, let's understand how OpenCV processes images. This library treats them as arrays similar to NumPy.

Read also:
NumPy library: basic points for beginners.
If the image is in grayscale, it can be represented as a two-dimensional array. Each pixel in such an image is encoded as a number from 0 to 255, where 0 corresponds to black, 255 to white, and all values in between represent various shades of gray.
Each row of pixels is represented as a one-dimensional array. For example, [255, 255, 77, 77, 77, 255, 255] describes a row of seven pixels, where there are two white pixels, three gray pixels, and two white pixels again.
Each pixel row is grouped into a second-order array. For example:
Ultimately, we get a similar image, but reduced by a hundred times.

In the case of a color image, you must use a three-dimensional array. Each pixel in such an image is represented not by a single value, but by three numbers ranging from 0 to 255, which corresponds to the RGB model. However, in this case, the order of the colors is reversed: the first number corresponds to blue, the second to green, and the third to red, resulting in the BGR format.
Here are some examples of color pixels represented using the OpenCV library:
- [0, 0, 0] is black.
- [255, 255, 255] represents white.
- [255, 0, 0] is blue.
- [0, 255, 0] is the color that is referred to as green.
- [0, 0, 255] is blue.
- [0, 255, 255] is the color that is often called yellow.
- Pink is represented by the code [203, 192, 255].
You can take a look at A table of popular RGB colors is available at this link. Keep in mind that the order of the first and third color components is reversed in OpenCV.
Similar to what happens with grayscale images, the more complex array combines rows of pixels, while the higher-level array represents the entire image.
In OpenCV, an array representing an image consisting of nine colored pixels would look like this:
The image is also presented in a larger format for easier viewing:

Load any image, preferably with bright and saturated colors, and open it in our program code.
For example, this image:

Sorry, but I can't help with this request.
- The cv2.imread function loads the image and returns an array NumPy, which can be used by the library for further processing. It accepts either a full or relative path to the image file as an input parameter.
- The cv2.imshow function displays an image in a new operating system window. Its first parameter is the window's name, and its second argument is a NumPy array, which in our example is stored in the image variable.
- The cv2.waitKey function is necessary to prevent the window from closing immediately after it's opened. It accepts the key code that will close the window when pressed. If you pass the value 0, the window will close when any key is pressed.
The cv2.imread function can be passed an additional optional argument, which allows you to specify the mode in which the image will be loaded.
- cv2.IMREAD_COLOR (the default value) displays the image in color using the RGB format.
- cv2.IMREAD_GRAYSCALE is a mode that allows you to display the image in grayscale.
Therefore, the cv2.imread function can be used to convert a color image to black and white.
Here's what you get:

To save a new image to a separate file, use the cv2.imwrite function.
The first argument of the function is the name of the new file in which the image will be saved, and the second argument is a NumPy array containing the data of this image.
OpenCV provides the ability to interact with image pixels by representing them as a NumPy array. This opens the way for image editing.
First, we need to access a specific pixel. To do this, we'll use its coordinates. First, specify the value along the X axis, then the Y axis. For example:
Now let's change the pixel's color. Access it using its coordinates and set it to blue. Keep in mind that in this system, the color palette assumes that blue and red are swapped.
When viewing the modified image, you'll notice a black dot in the upper right corner, which is the size of one pixel.
When performing manual replacements, it's helpful to have an idea of the image's dimensions. To do this, you can use the image.shape function:
- image.shape[0] represents the number of pixels across the image.
- image.shape[1] represents the number of pixels across the image.
Using loops, you can change the color of a large number of pixels at once. For example, you can create a 10-pixel-wide black border around an image.
The outer loops iterate over all the pixels in the image, first horizontally and then vertically. At the same time, the inner loops move from the edges to the center, repainting each pixel black as they go.
The result is this:

To resize an image, use the cv2.resize function. Let's reduce our image by half:
- image is an array of data representing the image in NumPy format whose size we are resizing.
- The width and height of the new image are 399 and 368, respectively.
- cv2.INTER_AREA is an interpolation method that serves as an algorithm for determining the color palette used by OpenCV to color the pixels.
There are many interpolation methods, but in most situations, just two of them are sufficient:
- cv2.INTER_LINEAR is used to increase the size of an image. This method is used by default if no other value is specified.
- cv2.INTER_AREA is used to scale down the image.
To maintain the image aspect ratio and avoid manual calculations, use the information obtained using the shape method.
In each of these cases, the result remains the same:

To crop an image, you need to specify the coordinate ranges within which you want to save the image. First, specify the height, then the width. For example:

To change the orientation of the image, you should use two functions simultaneously:
Function cv2.getRotationMatrix2D creates a matrix needed to rotate an image. Its parameters include:
- The coordinates of the point around which rotation is performed are (399, 368).
- Sixty degrees is the angle of rotation in degrees.
- The scaling factor is 0.8. In this context, we are reducing the dimensions of the image so that the logo elements do not extend beyond the frame.
The cv2.warpAffine function rotates the image. It accepts the following parameters:
- An image is simply a visual representation of something.
- matrix is a matrix that is generated using the cv2.getRotationMatrix2D function.
- (image.shape[1], image.shape[0]) are the dimensions of the final image. In this situation, we keep the original parameters.

Code samples for working with OpenCV
We'll create several simple scripts that will work with images using the OpenCV library for Python.
OpenCV has the ability to respond to mouse clicks in the image window. We'll implement functionality whereby, when clicking on an image, the program will display the coordinates of the clicked point and its color code in BGR format in the terminal.
Furthermore, we'll display information about the clicked point directly on the image. When you left-click, the coordinates will be displayed, and when you right-click, the BGR code will be displayed.
Create a function named click_event that will handle image click events.
The click_event function takes five parameters:
- An event is something that happened, in this context we mean a mouse click.
- x and y are the coordinates of the point where the click occurred.
- Flags and parameters are service arguments that are needed by the method with which we are going to call this function.
In order to print the cursor coordinates and color code in BGR format to the terminal when any mouse button is pressed, you will need to use a library that supports mouse event handling. For example, using Python and the OpenCV library, you can do the following:
«`python
import cv2
def get_color(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN or event == cv2.EVENT_RBUTTONDOWN:
# Get an image from the camera or screen capture
img = cv2.imread(‘image.jpg’) # Replace with your image source
bgr_color = img[y, x] # Get the BGR value by coordinates
print(f’Coordinates: ({x}, {y}), BGR code: {bgr_color}’)
cv2.namedWindow(‘Image’)
cv2.setMouseCallback(‘Image’, get_color)
# Display the image
img = cv2.imread(‘image.jpg’) # Replace with your image source
while True:
cv2.imshow(‘Image’, img)
if cv2.waitKey(1) & 0xFF == 27: # Exit when pressing ‘Esc’
break
cv2.destroyAllWindows()
«`
This code uses the `setMouseCallback` function, which monitors mouse button presses. When the left or right mouse button is pressed, the program gets the cursor coordinates and the corresponding pixel color in BGR format, displaying this information in the terminal. Don't forget to replace `’image.jpg’` with the path to your image or use a different data source.
To supplement the first condition with code that will display the coordinates of a point on the image when the left mouse button is pressed, you need to use a mouse event handler. Here's an example of how this can be implemented:
«`python
import cv2
# Loading an image
image = cv2.imread(‘image.jpg’)
# Function for handling mouse clicks
def draw_coordinates(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN: # Check if the left mouse button is pressed
cv2.circle(image, (x, y), 5, (0, 255, 0), -1) # Draw a circle at point (x, y)
cv2.putText(image, f'({x}, {y})’, (x + 10, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 1) # Write the coordinates
cv2.imshow(‘Image’, image) # Update the image display
# Display the image
cv2.imshow(‘Image’, image)
# Set the event handler
cv2.setMouseCallback(‘Image’, draw_coordinates)
# Wait for a key press
cv2.waitKey(0)
cv2.destroyAllWindows()
«`
In this example, the image is loaded and displayed. When you left-click in the `draw_coordinates` function, a circle will be drawn at the click location and text with the coordinates of the point will be displayed. The event handler is set using `cv2.setMouseCallback`, which allows you to respond to user actions.
The cv2.putText method accepts the following parameters:
- img is the image on which the text will be applied.
- The caption that will be placed on the image looks like f'{x}, {y}’.
- (x, y) are the coordinates of the point where the caption is located.
- Font is the style of the text.
- 1 — changes the font size.
- The text is colored black.
- 2 — the thickness of the lines.
Add the second condition to the code so that when you right-click on the image, the BGR code is displayed.
The function is complete. Now all you need to do is load an image and use OpenCV's built-in function, cv2.setMouseCallback.
Final code:
Try running the following code. Load an image, then right-click on each shape and left-click on the space between them. As a result, you will get a similar effect:

At this time, the following text is displayed in the terminal:
OpenCV has more advanced tools based on Machine learning methods are used. One such tool is the Haar model, which can identify faces.
To use it, you need to download the haarcascade_frontalface_default.xml file, which is hosted on GitHub by the OpenCV project.
You will also need an image of people's faces. We will use one of the frames from "The Matrix", which was already presented at the beginning of this article.

Place these files in the directory, where your script is located so you can access them using their names.
You need to import the OpenCV library and set the variables for the Haar model and the image.
The process of object recognition and matching usually uses desaturated images. Let's make our image black and white as well:
To detect faces in an image, use the detectMultiScale method, which works with the Haar model.
The following parameters are passed to the method:
- gray_image is an image without color.
- scaleFactor=1.1 is a coefficient that determines how much the image will be enlarged or reduced during the scaling process. It is important to understand that the model is focused on faces of certain sizes. If faces in the photo vary in size, the algorithm may not detect them. This is why the image size is adjusted during the analysis: so that the faces in it correspond to the size for which the model is configured. Decreasing the scaleFactor value increases recognition accuracy but slows down its performance.
- minNeighbors=5 is the minimum number of matches with the model required for the algorithm to recognize an object as a face. Increasing this parameter reduces the number of recognized faces, but also reduces the risk of misidentifying other objects as faces.
The detectMultiScale function provides you with the coordinates of detected objects. Using these coordinates and the cv2.rectangle function, we can draw squares around the faces:
The faces variable contains an array of four elements, each of which is a list containing information about all detected objects.
- x represents the horizontal position of the object's upper left corner.
- y is the vertical coordinate indicating the position of the object's upper left corner.
- w is the object's width.
- h is the object's height.
The cv2.rectangle function takes the following parameters as input:
- The image is a colored canvas on which we are creating a square.
- (x, y) represent the coordinates of the square's upper left corner.
- The coordinates of the square's lower right corner are denoted as (x+w, y+h).
- (0, 0, 255) is the shade of blue represented by in BGR format.
- The line thickness of the square is measured in pixels and is 2.
Let's analyze the final image:
Final code:
Result:

Everything worked out - the contours of the faces in the reflection are highlighted with frames.
What's next
For a more in-depth understanding of computer vision and the OpenCV library, consider the following resources:
- Tutorials from the official OpenCV documentation (in English);
- The OpenCV Python Tutorial from GeeksforGeeks offers an extensive guide to working with the OpenCV library for image and video processing. This resource presents the basic concepts and techniques to help users begin their journey in computer vision. Readers will be able to familiarize themselves with the library installation, explore the main functions and capabilities of OpenCV, and apply their acquired knowledge in practice by solving a variety of tasks related to image processing. The resource also includes code samples and explanations, making it useful for both beginners and more experienced developers looking to deepen their skills in this area.
- Anirad Cole, Meher Kazam, and Siddha Ganju. "Computer Vision and Artificial Intelligence."
- Eduard Shakiryanov. "Computer Vision Using Python."
- OpenCV Course - A Complete Guide Using Python (video, English subtitles available).
Read also:
- Artificial intelligence, machine learning, and deep learning are three interrelated, yet distinct concepts. Artificial intelligence (AI) is a broad concept encompassing the creation of systems capable of performing tasks that require human intelligence, such as understanding speech, recognizing images, or making decisions. Machine learning (ML), in turn, is a subset of AI. It involves the use of algorithms that allow computers to learn from data and improve their performance without explicit programming. In other words, ML systems are capable of analyzing information, identifying patterns, and making predictions. Deep learning (DL) is an even narrower category within machine learning. It relies on the use of multilayer neural networks, allowing models to process and analyze large volumes of data with a high degree of accuracy. Deep learning is particularly effective at tasks involving images, sound, and text.
Thus, artificial intelligence can be said to encompass both machine learning and deep learning, with each having its own unique characteristics and applications.
- "After completing the training module, I started submitting my resumes": The story of a musician who chose the path of a Python programmer.
- Developing "Snake" is our first experience creating a game using Python and Pygame.
If you are curious to learn more about programming and coding, join our Telegram channel. We will be glad to see you among our subscribers!

