DEV Community

Cover image for Mastering OpenCV: A Comprehensive Introduction to Computer Vision with Python
Anurag Verma
Anurag Verma

Posted on • Updated on

Mastering OpenCV: A Comprehensive Introduction to Computer Vision with Python

OpenCV (Open Source Computer Vision) is a free and open-source library of computer vision and machine learning algorithms that has gained widespread popularity in the field of artificial intelligence. It is widely used in a variety of applications, including object detection, image classification, and video analysis.

In this article/blog, we will explore the basics of OpenCV and learn how to use it to perform common tasks in computer vision. We will cover topics such as reading and displaying images, accessing and modifying pixel values, converting between image formats, and applying image filters. We will also learn about more advanced techniques, such as detecting edges in an image and extracting features using SIFT and SURF.

By the end of this article, you will have a solid foundation in OpenCV and be able to use it to build your own computer vision projects.

  1. Reading and displaying an image: You can use the cv2.imread() function to read an image from a file and the cv2.imshow() function to display the image on the screen. For example:
import cv2

# Read an image from a file
img = cv2.imread('image.jpg')

# Display the image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Accessing and modifying pixel values: You can access and modify the pixel values of an image using the numpy array representation of the image. For example:
import cv2
import numpy as np

# Read an image from a file
img = cv2.imread('image.jpg')

# Access the pixel values of the image
rows, cols, channels = img.shape
for row in range(rows):
    for col in range(cols):
        # Access the blue, green, and red channels of the pixel
        b, g, r = img[row, col]
        # Modify the pixel values
        img[row, col] = (0, g, 0)

# Display the modified image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Converting between image formats: You can use the cv2.cvtColor() function to convert an image from one color space to another. For example:
import cv2

# Read an image from a file
img = cv2.imread('image.jpg')

# Convert the image from BGR to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Display the grayscale image
cv2.imshow('image', gray)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Resizing an image: You can use the cv2.resize() function to change the size of an image. For example:
import cv2

# Read an image from a file
img = cv2.imread('image.jpg')

# Resize the image to a different size
resized_img = cv2.resize(img, (200, 300))

# Display the resized image
cv2.imshow('image', resized_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Cropping an image: You can use NumPy slicing to crop an image to a desired region. For example:
import cv2
import numpy as np

# Read an image from a file
img = cv2.imread('image.jpg')

# Crop the image to a specific region
cropped_img = img[100:300, 200:400]

# Display the cropped image
cv2.imshow('image', cropped_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Drawing on an image: You can use the cv2.line(), cv2.rectangle(), cv2.circle(), and other drawing functions to draw shapes and text on an image. For example:
import cv2
import numpy as np

# Read an image from a file
img = cv2.imread('image.jpg')

# Draw a red line on the image
cv2.line(img, (0, 0), (img.shape[1], img.shape[0]), (0, 0, 255), thickness=5)

# Draw a green rectangle on the image
cv2.rectangle(img, (100, 100), (200, 200), (0, 255, 0), thickness=2)

# Draw a blue circle on the image
cv2.circle(img, (300, 300), 50, (255, 0, 0), thickness=-1)

# Display the modified image
cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Applying image filters: You can use the cv2.filter2D() function to apply various image filters to an image. For example:
import cv2
import numpy as np

# Read an image from a file
img = cv2.imread('image.jpg')

# Define a kernel for the blur filter
kernel = np.ones((5, 5), np.float32)/25

# Apply the blur filter to the image
blurred_img = cv2.filter2D(img, -1, kernel)

# Display the filtered image
cv2.imshow('image', blurred_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Detecting edges in an image: You can use the cv2.Canny() function to detect edges in an image. For example:
import cv2

# Read an image from a file
img = cv2.imread('image.jpg')

# Convert the image to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Detect edges in the image
edges = cv2.Canny(gray, 100, 200)

# Display the edge map
cv2.imshow('image', edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode
  1. Extracting features from an image: You can use feature extraction techniques, such as SIFT (Scale-Invariant Feature Transform) or SURF (Speeded Up Robust Feature), to identify keypoints and descriptors in an image. For example:
import cv2

# Read an image from a file
img = cv2.imread('image.jpg')

# Detect SIFT features in the image
sift = cv2.xfeatures2d.SIFT_create()
keypoints, descriptors = sift.detectAndCompute(img, None)

# Draw the keypoints on the image
img_keypoints = cv2.drawKeypoints(img, keypoints, None)

# Display the image with keypoints
cv2.imshow('image', img_keypoints)
cv2.waitKey(0)
cv2.destroyAllWindows()
Enter fullscreen mode Exit fullscreen mode

Thank you all for reading, and following me and my article. I hope that you have learned a lot about the capabilities of OpenCV and how it can be used to build computer vision projects in Python.

To summarize, we covered topics such as reading and displaying images, accessing and modifying pixel values, converting between image formats, and applying image filters. We also looked at more advanced techniques, such as detecting edges in an image and extracting features using SIFT and SURF.

Some key points to remember are that OpenCV is a powerful and versatile library for computer vision, that it is easy to use and can be integrated into various projects, and that it has a rich set of features and functions for image processing, analysis, and machine learning.

If you have any questions or want to learn more about OpenCV, feel free to reach out to me or explore the resources that I provided. I encourage you to continue learning and experimenting with OpenCV, and I hope that you will be inspired to build your own exciting computer vision projects.

Thank you again, and I hope you have a great day!

Buy Me A Coffee

Top comments (0)