Unlock the Power of Computer Vision with OpenCV: 5 Amazing Applications
Computer vision has become an increasingly important field, powering everything from photo filters to self-driving cars to medical image analysis. At the heart of many computer vision applications is OpenCV, a powerful open-source library for image processing and computer vision. With OpenCV, you can easily perform complex techniques like object detection, facial recognition, and much more, using a few lines of code.
In this article, we‘ll take a deep dive into OpenCV and explore 5 amazing applications you can build with it. Whether you‘re a complete beginner or an experienced developer, you‘ll see the incredible potential of computer vision with OpenCV. Let‘s get started!
OpenCV Basics
Before we jump into the applications, let‘s cover some OpenCV fundamentals. OpenCV supports multiple programming languages including Python, Java, and C++. For this article, we‘ll use Python.
To get started, make sure you have OpenCV installed:
pip install opencv-python
Some of the most commonly used functions in OpenCV include:
cv2.imread()andcv2.imwrite()for reading and writing imagescv2.cvtColor()for converting between color spaces (e.g. BGR to RGB, BGR to HSV)cv2.threshold()for thresholding images into black and whitecv2.GaussianBlur(),cv2.blur(), and other filters for smoothing imagescv2.Canny()for detecting edges in images
These basic functions will come up often as we work through the applications. Let‘s dive in!
Application 1: Removing Watermarks with Inpainting
Have you ever had a photo ruined by an ugly watermark? With OpenCV, removing watermarks is easier than you might think! The key is a technique called inpainting.
The idea is to:
- Create a mask image where the watermark pixels are white and everything else is black
- Pass the mask and original image into
cv2.inpaint() - This will magically reconstruct the watermark region based on the surrounding pixels!
Here‘s a code snippet:
import cv2 import numpy as npimg = cv2.imread(‘watermarked.jpg‘) hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
lower = np.array([0,0,200]) upper = np.array([255,255,255]) mask = cv2.inRange(hsv, lower, upper)
dst = cv2.inpaint(img, mask, 5, cv2.INPAINT_TELEA)
The parameters to cv2.inpaint() are:
- The input image
- The mask image (white pixels are the watermark region)
- The radius of the neighborhood used for inpainting
- The inpainting method (
INPAINT_TELEAorINPAINT_NS)
I‘ve found that INPAINT_TELEA often works better for watermark removal. Here are the results:
As you can see, the watermark is gone and the image looks seamless! The inpainting technique intelligently fills in the gaps based on the neighboring pixels. To get the best results, you may need to tune the HSV threshold range for detecting the watermark and the inpainting radius.
This is a taste of the power of OpenCV. Now let‘s look at another application: background removal.
Application 2: Extracting Foregrounds with Perspective Transform
A common task in computer vision is to extract the foreground object from an image and remove the background. For example, you might want to cut out the subject of a photo to use in a photomontage. While traditionally this required manually outlining the foreground object, OpenCV makes it much easier using perspective transformation.
The idea is:
- Define 4 points that form a rectangle around the foreground object
- Define a rectangle of the desired output size
- Use
cv2.getPerspectiveTransform()to compute the transformation matrix between the rectangles - Use
cv2.warpPerspective()to apply the perspective transformation
Here‘s sample code:
foreground_pts = np.float32([[207,151], [683,188], [190,690], [667,722]]) output_pts = np.float32([[0,0], [500,0], [0,800], [500,800]])M = cv2.getPerspectiveTransform(foreground_pts, output_pts) out = cv2.warpPerspective(img, M, (500,800))
This maps the 4 foreground points to a 500×800 output image, cutting out the object inside the points. The result looks like:
[Original image with foreground points] [Extracted foreground object]Just like that, we‘ve cleanly extracted the notebook from the background! The key is defining appropriate foreground points – they should form a rectangle that tightly bounds the object you want to extract. You can find these points manually or detect them automatically with techniques like edge and corner detection.
Extracting foreground objects is useful for focusing on the essential parts of images. Now let‘s look at manipulating the artistic style of images with filters.
Application 3: Stylizing Images with Filtering
Filters are a fun way to change the mood and artistic style of photos. With a few lines of OpenCV code, you can easily create filters like blurring, sharpening, and edge enhancement. The secret sauce is convolution.
In convolution, we take a kernel (a small matrix) and slide it across the image, computing a dot product at each pixel. Different kernels can produce different visual effects. For example, this kernel performs an averaging blur:
kernel = np.ones((5,5), np.float32) / 25
To apply the kernel, we use cv2.filter2D():
img = cv2.imread(‘lena.jpg‘) blur = cv2.filter2D(img, -1, kernel)
The parameters are the input image, the output depth (-1 means same as input), and the kernel. The result looks like:
[Original image] [Blurred image]The image has been smoothed out and lost some detail. Here‘s the thing – by changing the kernel, we can get all sorts of different effects! For example, this kernel sharpens edges:
kernel = np.array([[-1,-1,-1],
[-1, 9,-1],
[-1,-1,-1]])
sharpened = cv2.filter2D(img, -1, kernel)
[Sharpened image]
See how the edges are enhanced? You can find many other interesting kernels to create effects like embossing, outlining, and more. I encourage you to experiment with different kernels and see what you can come up with!
Filtering is a core technique that pops up in all areas of image processing. Next we‘ll use some clever filtering to transform images into cartoons.
Application 4: Turning Photos into Cartoons
Ever wonder how apps like Prisma create those cool artistic effects? Many of them use computer vision techniques powered by OpenCV. Let‘s see how to turn a photo into a cartoon using edge detection and bilateral filtering.
The basic idea is:
- Use
cv2.adaptiveThreshold()to detect edges in the image - Use
cv2.bilateralFilter()to smooth the image while preserving edges - Combine the edge and color images using
cv2.bitwise_and()
Here‘s the code:
import cv2 import numpy as npimg = cv2.imread(‘lena.jpg‘) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) edges = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 9, 4)
color = cv2.bilateralFilter(img, 3, 150, 150) cartoon = cv2.bitwise_and(color, color, mask=edges)
The results look like:
[Original image] [Cartoon image]How cool is that? The bilateral filter smooths the color image while keeping strong edges intact. Then we overlay the edge mask to emphasize the lines and create that signature cartoon look.
There are many ways to improve on this basic cartoon effect – you can play with the filter sizes, use different edge detection methods, apply color quantization, and more. The possibilities are endless! Cartoonization is a great example of chaining together multiple vision techniques for a creative effect.
Application 5: Detecting Faces and Features
For our final application, let‘s look at something OpenCV is famous for – face detection! With OpenCV‘s pre-trained deep learning models, we can locate faces in images with incredible accuracy. We can even detect specific facial features like eyes, noses, and mouths. This has applications in security, biometrics, and more.
To try it out, download the face detection models from OpenCV‘s GitHub repo. Then use code like this:
import cv2faceCascade = cv2.CascadeClassifier(‘haarcascade_frontalface_default.xml‘) eyeCascade = cv2.CascadeClassifier(‘haarcascade_eye.xml‘)
img = cv2.imread(‘friends.jpg‘) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.1, 6) for (x,y,w,h) in faces: cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2) roi_gray = gray[y:y+h, x:x+w] eyes = eyeCascade.detectMultiScale(roi_gray) for (ex,ey,ew,eh) in eyes: cv2.rectangle(img, (x+ex,y+ey), (x+ex+ew, y+ey+eh), (0,255,0), 2)
cv2.imshow(‘img‘,img) cv2.waitKey(0) cv2.destroyAllWindows()
This code first loads the pre-trained face and eye detection models. It then searches for faces in the image using faceCascade.detectMultiScale(). This returns bounding box coordinates for any faces found.
For each detected face, we draw a blue rectangle around it. We also search for eyes within the face region and draw green rectangles around any eyes found. The result looks like:
[Image with face and eye detections]OpenCV has built-in models for detecting whole faces, eyes, noses, mouths, and even individual facial landmarks. This allows you to not only detect faces, but analyze attributes like facial expressions, gaze direction, and more.
Face detection is a complex computer vision problem, but OpenCV‘s high-quality detection models make it accessible to anyone. By building on these models, you can make all sorts of interesting applications like face filters, face recognition, facial motion capture, and more!
Conclusion and Next Steps
In this article, we‘ve looked at 5 amazing applications you can build using OpenCV:
- Removing watermarks with inpainting
- Extracting foregrounds with perspective transform
- Stylizing images with filtering
- Turning photos into cartoons
- Detecting faces and facial features
But really, we‘ve only scratched the surface of what‘s possible with OpenCV! As you‘ve seen, the library provides a whole range of powerful techniques – from low-level operations like thresholding and filtering to high-level APIs for deep learning. By chaining these together in creative ways, you can solve almost any computer vision problem.
What‘s even more exciting is that computer vision is still a rapidly developing field. Techniques like deep learning are advancing the state-of-the-art every day. And as cameras become ubiquitous in everything from phones to cars to home appliances, computer vision is becoming increasingly important.
No matter what your level of experience, I encourage you to keep exploring OpenCV and computer vision. Try out the code from this article and see what you can come up with! If you want to learn more, here are some great resources:
- OpenCV Python Tutorials: https://opencv-python-tutroals.readthedocs.io/
- PyImageSearch Blog: https://www.pyimagesearch.com
- LearnOpenCV: https://learnopencv.com
What will you build with OpenCV? Share your creations in the comments below!