[OpenCV-Python] 10 Arithmetic operations on images

OpenCV-Python: core operations

10 Arithmetic operations on images

Objectives
  • To learn arithmetic operations on images, addition, subtraction, bit operations, etc.
  • The functions we will learn are: cv2.add(), cv2.addWeighted() and so on.

10.1 Image addition

You can use the function cv2.add() to add two images. Of course, you can also use numpy directly, res=img1+img. The size and type of the two images must be the same, or the second image can be a simple scalar value.
Note: The addition in OpenCV is different from the addition in Numpy. OpenCV's addition is a saturation operation, while Numpy's addition is a modular operation.
For example, the following two examples:

x = np.uint8([250])
y = np.uint8([10])
print cv2.add(x,y) # 250+10 = 260 => 255
[[255]]
print x+y # 250+10 = 260 % 256 = 4
[4]

This difference will be more obvious when you add two images. The result of OpenCV will be better. So we try to use the functions in OpenCV.

10.2 Image mixing

This is actually an addition, but the difference is that the weights of the two images are different, which will give people a feeling of blending or transparency. The calculation formula for image blending is as follows:
    g (x) = (1 − α)f 0 (x) + αf 1 (x)
  By modifying the value of α (0 → 1), a very cool blending can be achieved.
  Now we mix the two pictures together. The weight of the first image is 0.7, and the weight of the second image is 0.3. The function cv2.addWeighted() can mix images according to the following formula.
    dst = α · img1 + β · img2 + γ
  where the value of γ is 0.

import cv2
import numpy as np

img1=cv2.imread('ml.png') img2=cv2.imread('opencv_logo.jpg')

dst=cv2.addWeighted(img1,0.7,img2,0.3,0)

cv2.imshow('dst',dst) cv2.waitKey(0) cv2.destroyAllWindow()

img

10.3 Bitwise operations

The bitwise operations included here are: AND, OR, NOT, XOR, etc. These operations will be useful when we extract a part of the image and select a non-rectangular ROI (you will understand in the next chapter). The following example is to teach us how to change a specific area of ​​a picture. I want to put the OpenCV logo on another image. If I use addition, the color will change, and if I use blending, I will get a transparent effect, but I don't want transparency. If it is a rectangle, I can use ROI as in the previous chapter. But he is not rectangular. But we can do it through the following bitwise operations:

import cv2
import numpy as np

# 加载图像
img1 = cv2.imread('roi.jpg')
img2 = cv2.imread('opencv_logo.png')

# I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask also
img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI
# 取 roi 中与 mask 中不为零的值对应的像素的值,其他值为 0
# 注意这里必须有 mask=mask 或者 mask=mask_inv, 其中的 mask= 不能忽略
img1_bg = cv2.bitwise_and(roi,roi,mask = mask)
# 取 roi 中与 mask_inv 中不为零的值对应的像素的值,其他值为 0。# Take only region of logo from logo image.
img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv)

# Put logo in ROI and modify the main image
dst = cv2.add(img1_bg,img2_fg) img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1) cv2.waitKey(0) cv2.destroyAllWindows()

The results are as follows. The image on the left is the mask we created. The one on the right is the final result. In order to help you understand, I also showed the intermediate results of the above program, especially img1_bg and img2_fg.

Insert picture description hereimg

For more information, please pay attention to the official account:
img

Guess you like

Origin blog.csdn.net/yegeli/article/details/113405861