In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of mixed arithmetic operations of opencv images in python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Add pictures to cv2.add ()
To stack two images, you can use the cv2.add () function, and the shape (height / width / number of channels) of the two images must be the same.
You can add res = img + img1 directly in numpy, but the results are not the same (see the following code):
Add () two pictures are added, and those greater than 255are counted.
Numpy takes a module of 256 (equivalent to 255 / 1) for the result:
Import numpy as npimport cv2 x = np.uint8) y = np.uint8 ([10]) print (cv2.add (x, y)) # 250mm 10 = 260 = > 255print (x + y) # 250mm 10 = 260% (255x + 1) = 4
If it's a binary image (there are only 0,255values), the result is the same (it's easier to use numpy).
Experimental picture:
Effect after add ()
Subtract, multiply, divide:
Subtract (img1,img2) # subtraction, can be used for target detection m
Multiply ultiply (img1,img2) #
Divide (img1,img2) # division
Image fusion, mixed addWeighted ()
Image mixing cv2.addWeighted () is also an operation of adding images, but the weights of the two images are not the same, and γ is equal to a correction value:
Img1 = cv2.imread ('lena_small.jpg') img2 = cv2.imread (' opencv-logo-white.png') res = cv2.addWeighted (img1, 0.6, img2, 0.4,0)
Effect:
When both α and β are equal to 1, it is equivalent to the addition of pictures.
Bitwise operation
Bitwise operations include bitwise and / or / non / XOR operations, what is the use? For example, we want to achieve the effect of the following figure:
If you add two pictures directly, it will change the color of the picture, and if you mix it with the image, it will change the transparency of the picture, so we need to use bitwise operation. First of all, let's take a look at the concept of mask: a mask is a binary image that partially obscures another picture. Take a look at the following picture:
So our idea is to dig out the area in the original image where the logo is to be put, and then put the logo in it:
Img1 = cv2.imread ('lena.jpg') img2 = cv2.imread (' opencv-logo-white.png') # put logo in the upper left corner So we only care about this area rows, cols = img2.shape [: 2] roi = img1 [: rows,: cols] # create a mask img2gray = cv2.cvtColor (img2, cv2.COLOR_BGR2GRAY) ret, mask= cv2.threshold (img2gray, 10,255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not (mask) # keep the background img1_bg = cv2.bitwise_and (roi, roi, mask=mask_inv) dst = cv2.add (img1_bg) except logo Img2) # Fusion img1 [: rows,: cols] = dst # put it on the original image after fusion
The concept of mask is often used in image mixing / overlay scenarios.
Above, we used
Bitwise and bitwise_and (roi, roi, mask=mask_inv)
Non-operational bitwise_not (mask)
In addition to bitwise and non-operations, there are also:
Or operation bitwise_or (img1,img2)
XOR bitwise_xor (img1,img2)
On the "python opencv image mixed arithmetic operation example analysis" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.