CV2: Master Guide OpenCV for Python Developers

Python is a popular programming language that has numerous libraries and modules for different applications. One of these libraries that is commonly used for image and video processing is OpenCV. The cv2 module is the main module in OpenCV that provides developers with an easy-to-use interface for working with image and video processing functions.

In this article we will show some of the most commonly used functions in cv2.

cv2 - installation

Before we get into the features, cv2 needs to be installed. There are two main ways to install cv2:

  1. pip install opencv-python
  2. conda install -c conda-forge opencv

Once cv2 is installed, we can start using the various features that the library offers.

In the past we already wrote about Python packages such as

cv2.imread

cv2.imread is a function to read an image from a file. It takes the file path as input and returns a numpy array containing the image. Here is an example:

import cv2
img = cv2.imread('image.jpg')

cv2.resize

cv2.resize is a function that is used to resize an image. It takes the input image and the new dimensions as inputs and returns the resized image. Here's an example:

import cv2
img = cv2.imread('image.jpg')
resized_img = cv2.resize(img, (500, 500))

cv2.imshow

cv2.imshow is a function used to display an image. It takes the image as input and opens a new window to display the image. Here is an example:

import cv2
img = cv2.imread('image.jpg')
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

cv2.imwrite

cv2.imwrite is a function to save an image to a file. It takes the image and the file path as input and saves the image in the file path. Here is an example:

import cv2
img = cv2.imread('image.jpg')
cv2.imwrite('new_image.jpg', img)

cv2.threshold

cv2.threshold is a function that can be used to threshold an image. It takes the input image and a threshold as input and returns the thresholded image. Here is an example:

import cv2
img = cv2.imread('image.jpg', 0)
ret, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

cv2.rectangle

cv2.rectangle is a function to draw a rectangle on an image. It takes the input image, the coordinates of the rectangle, the color and the thickness as inputs and returns the image with the rectangle drawn. Here is an example:

import cv2
img = cv2.imread('image.jpg')
cv2.rectangle(img, (100, 100), (200, 200), (0, 255, 0), 2)

cv2.findContours

cv2.findContours is a function that is used to find the contours in an image. It takes the input image, the contour retrieval mode, and the contour approximation method as inputs and returns the contours. Here's an example:

import cv2
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours

Loading and displaying images

Once you have CV2 installed, you can start working with images. The first step is to load an image into your Python program. To do this, you can use the cv2.imread() function, which takes the path to the image file as input and returns a NumPy array representing the image.

import cv2
# Load an image
img = cv2.imread('image.jpg')
# Display the image
cv2.imshow('Image', img)
# Wait for a key press and then close the window
cv2.waitKey(0)
cv2.destroyAllWindows()

In the code above, we first load an image with the cv2.imread() function and store it in the img variable. Then we display the image with the cv2.imshow() function, which takes two arguments: the name of the window and the image itself. Finally, we wait for a keystroke with the function cv2.waitKey() and then close the window with the function cv2.destroyAllWindows().

Image manipulation

CV2 provides many functions for editing images, such as resizing, rotating, and cropping. Here are some examples:

# Resize an image
resized_img = cv2.resize(img, (500, 500))
# Rotate an image
(rows, cols) = img.shape[:2]
M = cv2.getRotationMatrix2D((cols/2, rows/2), 45, 1)
rotated_img = cv2.warpAffine(img, M, (cols, rows))
# Crop an image
cropped_img = img[100:300, 200:400]

In the code above, an image is resized with the cv2.resize() function, an image is rotated with the cv2.getRotationMatrix2D() and cv2.warpAffine() functions, and an image is cropped with NumPy array indexing.

Image filtering

Filtering images is an important technique in image processing that can be used to enhance or extract certain features of an image. CV2 provides several functions for filtering images, such as blur, sharpen, and edge detection. Here are some examples:

# Blur an image
blurred_img = cv2.blur(img, (5, 5))
# Sharpen an image
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
sharpened_img = cv2.filter2D(img, -1, kernel)
# Detect edges in an image
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges_img = cv2.Canny(gray_img, 100, 200)

In the code above, an image is blurred using the cv2.blur() function, sharpened using the cv2.filter2D() function and a custom kernel, and detected using the cv2.cvtColor() function to convert the image to grayscale and the cv2.Canny() function to detect edges in an image.

Conclusion

CV2 is a powerful library for working with images in Python. In this article, we have covered some of the most commonly used functions and methods in CV2, including image loading and display, image manipulation, and image filtering. With these tools at your disposal, you can explore the exciting world of computer vision and image processing.

CV2 can be found under PyPi to access the latest version.

For developers, our contributions are for OCR API and Document AI also very exciting.

"
"
Florian Zyprian Avatar

Latest articles