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
- Download the latest version of OpenCV from the official website (https://opencv.org/releases/).
- Extract the downloaded file to a folder on your system.
- Add the path to the "bin" folder of the extracted file to your system's PATH environment variable.
Linux
- Open the terminal and run the following command to install the necessary packages:
csharpsudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
- Clone the OpenCV repository from Github:
bashgit clone https://github.com/opencv/opencv.git
- Create a new directory for building OpenCV and navigate to it:
bashmkdir build && cd build
- Configure the build using cmake:
bashcmake ../opencv
- Build and install OpenCV:
gomake && 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.
pythonimport 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.
makefileimport 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.
makefileimport 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.
pythonimport cv2
# Load an image
img = cv2.imread("path/to/image.jpg")
# Draw a rectangle on the image
cv2.rectangle(img, (x1, y1), (x2, y2), (