Tutorial for OpenCV

 



OpenCV (Open Source Computer Vision) is an open-source library of programming functions mainly used for real-time computer vision tasks such as image processing, object detection, and tracking. In this tutorial, we will be discussing how to get started with OpenCV, its installation, and some basic functions that you can perform using the library.

Installation

Before we can start using OpenCV, we need to install it on our system. You can install OpenCV on your system using the following steps:

Windows

  1. Download the latest version of OpenCV from the official website (https://opencv.org/releases/).
  2. Extract the downloaded file to a folder on your system.
  3. Add the path to the "bin" folder of the extracted file to your system's PATH environment variable.

Linux

  1. Open the terminal and run the following command to install the necessary packages:
csharp
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
  1. Clone the OpenCV repository from Github:
bash
git clone https://github.com/opencv/opencv.git
  1. Create a new directory for building OpenCV and navigate to it:
bash
mkdir build && cd build
  1. Configure the build using cmake:
bash
cmake ../opencv
  1. Build and install OpenCV:
go
make && sudo make install

Basic Functions

Once you have installed OpenCV on your system, you can start using it in your programs. Here are some basic functions that you can perform using OpenCV:

Reading and Displaying Images

To read an image using OpenCV, you can use the cv2.imread() function. The function takes the path to the image as its argument and returns a NumPy array representing the image.

python
import cv2 # Load an image img = cv2.imread("path/to/image.jpg") # Display the image cv2.imshow("Image", img) cv2.waitKey(0) cv2.destroyAllWindows()

Resizing Images

To resize an image using OpenCV, you can use the cv2.resize() function. The function takes the image and the new size as its arguments and returns the resized image.

makefile
import cv2 # Load an image img = cv2.imread("path/to/image.jpg") # Resize the image resized_img = cv2.resize(img, (new_width, new_height)) # Display the resized image cv2.imshow("Resized Image", resized_img) cv2.waitKey(0) cv2.destroyAllWindows()

Converting Images to Grayscale

To convert an image to grayscale using OpenCV, you can use the cv2.cvtColor() function. The function takes the image and the conversion code as its arguments and returns the grayscale image.

makefile
import cv2 # Load an image img = cv2.imread("path/to/image.jpg") # Convert the image to grayscale gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Display the grayscale image cv2.imshow("Grayscale Image", gray_img) cv2.waitKey(0) cv2.destroyAllWindows()

Drawing on Images

To draw on an image using OpenCV, you can use the cv2.rectangle() or cv2.circle() functions. The functions take the image, the coordinates of the shape, and the color and thickness of the shape as their arguments.

python
import cv2 # Load an image img = cv2.imread("path/to/image.jpg") # Draw a rectangle on the image cv2.rectangle(img, (x1, y1), (x2, y2), (