AI Integration · Deep Learning
Mastering OpenCV: Computer Vision With Python
Learn OpenCV fundamentals including image I/O, pixel manipulation, color conversion, resizing, filtering, edge detection, and feature detection with SIFT and SURF.
Anurag Verma
5 min read
Sponsored
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.
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()
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()
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()
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()
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()
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()
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()
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()
Extracting features from an image
You can use feature extraction techniques, such as SIFT (Scale-Invariant Feature Transform) or SURF (Speeded Up Robust Features), 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()
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.
Wrapping up
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!
Frequently asked questions
- Why do my images look wrong when I display them with matplotlib?
- Because OpenCV reads images as BGR and matplotlib expects RGB, so the red and blue channels are swapped. Blue skies come out orange. The fix is one call, cvtColor with COLOR_BGR2RGB, before handing the array to matplotlib. It catches nearly everyone once, and it is worth knowing that cv2.imshow displays correctly because it also assumes BGR.
- Can I use SIFT and SURF in a commercial project?
- SIFT, yes. Its patent expired in 2020 and it moved from the contrib non-free module into mainline OpenCV, so it is freely usable. SURF is still patented and remains in opencv-contrib's non-free build, which means you need to compile with the non-free flag and check the licensing position for your use. For most work SIFT is the safer default, and ORB is a fully free alternative that is faster though less accurate under large scale changes.
- How do I choose Canny's two thresholds?
- Start from the ratio rather than the numbers. Edges above the high threshold are kept, edges below the low one are discarded, and edges in between survive only if connected to a strong edge. A low-to-high ratio around 1:2 or 1:3 is the standard advice. From there, raise both to get fewer, cleaner edges and lower both to catch faint ones at the cost of noise. Blurring first with GaussianBlur usually improves the result more than tuning the thresholds does.
- When should I convert to HSV instead of grayscale?
- When the thing you care about is colour. Grayscale throws colour away entirely, which is right before edge detection or template matching. HSV separates hue from saturation and brightness, so you can threshold on "this range of colours" without lighting changes ruining it, which is what BGR cannot do cleanly because brightness is mixed into all three channels.
- Why does modifying pixels use NumPy rather than an OpenCV function?
- Because the image already is a NumPy array. imread returns one, so slicing crops, assignment sets pixel values, and the usual vectorised operations work directly. That is a genuine design advantage: anything you know how to do to a NumPy array works on an image, and you only reach for an OpenCV function when you want an algorithm rather than an array manipulation.
Sponsored
More from this category
More from AI Integration
Sponsored
Discussion
Join the conversation.
Comments are powered by GitHub Discussions. Sign in with your GitHub account to leave a comment.
Sponsored