
#Transparent layer inkist code
57+ hours of on-demand code walkthrough videos.However, once we reach alpha=0.5 , both the “PyImageSearch” text and rectangle are substantially more transparent: Figure 4: Constructing transparent overlays with Python and OpenCV.Īt alpha=0.1 , the text and rectangle are barely visible: Figure 5: The smaller alpha gets, the more transparent the overlay will be.īelow you can see a GIF animation that visualizes each of the transparency levels: You should see the following image displayed to your screen: Figure 3: Notice how for alpha=1.0, the text and rectangle are entirely opaque (i.e., not transparent).
#Transparent layer inkist download
To execute our Python script, download the source code + example image to this post (using the “Downloads” form found at the bottom of this lesson) and execute the following command: $ python overlay.py # along with the text "PyImageSearch" at the top-leftĬv2.rectangle(overlay, (420, 205), (595, 385),Ĭv2.putText(overlay, "PyImageSearch: alpha=".format(alpha, 1 - alpha)) # draw a red rectangle surrounding Adrian in the image # the overlay and one for the final output image # create two copies of the original image - one for The next step is to loop over various values of alpha transparency between the range, allowing us to visualize and understand how the alpha value can influence our output image: # loop over the alpha transparency valuesįor alpha in np.arange(0, 1.1, 0.1): Line 7 loads our image from disk using the cv2.imread function. Lines 2-4 handle importing our required Python packages. Open up a new file, name it overlay.py, and insert the following code: # import the necessary packages Let’s go ahead and dive into some source code to create a transparent overlay with Python and OpenCV. To remedy this, we can leverage the cv2.addWeighted function to “blend” images together and construct the transparent overlay. You cannot see myself through the rectangle, nor does the “PyImageSearch” text contain any level of transparency. However, notice that both the rectangle and text are fully opaque! However, both the text and bounding box are entirely opaque - no transparency has been applied. The results of drawing the rectangle and text can be seen below: Figure 2: Drawing a rectangle and text on the original image.

:max_bytes(150000):strip_icc()/frame0803-58b9a4f33df78c353c142e61.png)
We’ll start with the following image: Figure 1: Our initial image that we are going to construct an overlay for.
#Transparent layer inkist how to
In the remainder of this lesson, I’ll demonstrate how to construct transparent overlays using Python and OpenCV. Looking for the source code to this post? Jump Right To The Downloads Section Transparent overlays with OpenCV The use cases for transparent overlays are nearly endless - my goal in today’s blog post is simply to show you how you can incorporate them into your own applications. Using transparent overlays alleviates the need to obfuscate the contents of the output image!Ī second, more obvious example is alpha transparency where you need to “blend” two images together. Instead of displaying runtime critical information in separate window or in the terminal, you can directly overlay the information on your output image. So, you might be wondering: “When/where will I use a transparent overlay in my computer vision application? And why are you sharing this tip?”įor starters, if your OpenCV app requires a Heads-up Display (HUD) of any kind, you should consider using transparent overlays. The results of applying such a transparent overlay can see seen at the top of this blog post.

An image containing what you want to “overlay” on top of the first using some level of alpha transparency.In order to construct a transparent overlay, you need two images: Today’s tip comes from my bag of experiences: constructing transparent overlays with OpenCV. One of my favorite aspects of running the PyImageSearch blog is sharing little bitesize OpenCV tips and tricks that I’ve learned after nearly 7 years of using the OpenCV library.
