Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of constructing transparent overlay layer by OpenCV

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

OpenCV to build a transparent overlay example analysis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

In order to construct a transparent overlay layer, you need to prepare two images:

(1) original picture

(2) to "overlay" the image on the first image (including some level of Alpha transparency).

The use cases for transparent overlay layers are almost endless, two of which are:

You can overwrite important runtime information directly on the output image instead of displaying it in a separate window or terminal. Using transparent overlay layers can reduce the need to confuse the content of the output image!

Using Alpha transparency, two images are "fused" together.

Using cv2.addWeighted to apply transparent overlay function

Cv2.addWeighted (overlay, alpha, output, 1-alpha, 0, output)

-overlay: the overlay image we are going to apply

-cv2.addWeighted (overlay, alpha, output, 1-alpha, 0, output)

-alpha: the actual Alpha transparency of the overlay layer. The closer the alpha is to 1, the more opaque the image is; the closer the alpha is to 0, the more transparent the overlay layer appears.

-output: original image

-beta: Beta is defined as 1-alpha. We need to define alpha + beta = 1.0

Gamma: gamma value-the scalar added to the weighted sum. You can think of gamma as a constant added to the output image after applying weighted addition. Setting it to zero means that you do not need to apply an addition operation with constant values.

-output: final result, output image

Effect picture:

# USAGE# python overlay.py# import necessary package from _ future__ import print_functionimport numpy as npimport cv2import imutils# load image image = cv2.imread (".. / image/flower4.jpg") image = imutils.resize (image, width=600) cv2.imshow ("origin", image) cv2.waitKey (0) # Loop through [0 Transparency for alpha in np.arange (0,1.1,0.1) [::-1]: # create a copy of two original images # one for building an overlay layer and one for the output image overlay = image.copy () output = image.copy () # add text in the upper left corner: PyimageSerach:alpha # draw a rectangular red box cv2.rectangle (overlay) in the lower right corner (150,210), (500,630), (0,0,255),-1) cv2.putText (overlay, "PyImageSearch: alpha= {: .2f}" .format (alpha), (10,30), cv2.FONT_HERSHEY_SIMPLEX, 1.0,0,255) 3) # using cv2.addWeighted to apply transparent overlay function # overlay: the overlay layer image we want to apply # alpha: the actual Alpha transparency of the overlay layer The closer the alpha is to 1, the more opaque the image The closer the alpha is to 0, the more transparent the overlay layer appears; # output: original image # beta: we provide the beta value as the fourth parameter. Beta is defined as 1-alpha. We need to define alpha and beta so that alpha + beta = 1.0 # gamma: gamma value-a scalar added to the weighted sum. You can think of gamma as a constant added to the output image after applying weighted addition. In this case, we set it to zero because we don't need to apply constant value addition. # output: output image cv2.addWeighted (overlay, alpha, output, 1-alpha, 0, output) # display the output image, alpha,beta print ("alpha= {: .2f}, beta= {: .2f}" .format (alpha, 1-alpha)) cv2.imshow ("Output", output) cv2.waitKey (0) cv2.destroyAllWindows () will it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report