Getting Started with OpenCV for Image Processing in Python

OpenCV (Open Source Computer Vision Library) is an open-source computer vision and machine learning library. It has C++, Python, and Java interfaces and supports Windows, Linux, Mac OS, iOS, and Android. In this article, we will learn how to install OpenCV, the basics of reading, displaying, and saving images, and some common image processing techniques using Python.

Table of Contents

  1. Installing OpenCV in Python
  2. Reading and Displaying Images
  3. Saving Images
  4. Basic Image Processing Techniques

Installing OpenCV in Python

You can install OpenCV using pip. Run the following command in your terminal:

pip install opencv-python

To install OpenCV with additional modules, use:

pip install opencv-contrib-python

Now, let's import the library in our Python script:

import cv2

Reading and Displaying Images

To read an image, use the cv2.imread() function. It takes the image file path as its argument and returns the image as a NumPy array.

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

To display an image, use the cv2.imshow() function. It takes two arguments: the window name and the image array.

cv2.imshow('Image', image)

The cv2.waitKey(0) function waits for a key event indefinitely, and cv2.destroyAllWindows() closes all the windows when a key is pressed.

Saving Images

To save an image, use the cv2.imwrite() function. It takes two arguments: the file path where you want to save the image and the image array.

cv2.imwrite('output.jpg', image)

Basic Image Processing Techniques


To resize an image, use the cv2.resize() function. It takes the image array, the desired size as a tuple, and the interpolation method as its arguments.

resized_image = cv2.resize(image, (300, 200), interpolation=cv2.INTER_LINEAR)


Converting an image to grayscale is done using the cv2.cvtColor() function. It takes the image array and the color conversion code as its arguments.

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


To blur an image, use the cv2.GaussianBlur() function. It takes the image array, the kernel size as a tuple, and the standard deviation in the X and Y directions as its arguments.

blurred_image = cv2.GaussianBlur(image, (5, 5), 0)


Thresholding is used to create a binary image. Use the cv2.threshold() function. It takes the image array, the threshold value, the maximum value, and the thresholding type as its arguments.

ret, thresholded_image = cv2.threshold(gray_image, 127, 255, cv2.THRESH_BINARY)

Edge Detection

Edge detection can be done using the Canny edge detection algorithm, which is available in OpenCV through the cv2.Canny() function. It takes the image array, the lower threshold, and the upper threshold as its arguments.

edges = cv2.Canny(gray_image, 100, 200)

Now you know the basics of OpenCV in Python for image processing. Feel free to experiment with different functions and techniques to improve your skills in this powerful library.

An AI coworker, not just a copilot

View VelocityAI