# [opencv3 learning record] Chapter 7 pixel operation on image

Posted by Bac on Fri, 14 Jan 2022 22:04:55 +0100

You can use the function CV2 Add() adds two images. Of course, numpy, res=img1+img can also be used directly. The size and type of the two images must be the same, or the second image can make a simple scalar value.

Note: addition in OpenCV is different from addition in Numpy. OpenCV addition is a saturation operation, while Numpy addition is a modular operation.

``` 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 is more obvious when you add two images. The result of OpenCV will be better. So we try to use the functions in OpenCV.

2: Image blending

This is actually addition, but the difference is that the weights of the two images are different, which will give people a feeling of mixing or transparency. The calculation formula of image blending is as follows:

Through modification α The value of (0 → 1) can achieve very cool mixing

1. Function: CV2 addweighted(img1, α, img2,1- α，)

Generally 0

2. For example: mix the two figures with weights of 0.7 and 0.3 respectively

```import cv2
import numpy as np
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindow()
```

The bitwise operations included here are: AND, OR, NOT, XOR, etc. These operations are useful when we extract part of the image AND select 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. If I use blending, I will get a transparent effect, but I don't want transparency. If it is rectangular, I can use ROI as in the previous chapter. But he's NOT rectangular. However, we can implement it through the following bitwise operation:

Mask is mentioned in the calculation parameters. The following describes the role of mask:

The image mask blocks the processed image (all or part) with the selected image, figure or object to control the area or process of image processing.
In digital image processing, masks are two-dimensional matrix arrays, and sometimes multivalued images are used. Image masks are mainly used for:
① The region of interest is extracted and multiplied by the pre-made region of interest mask and the image to be processed to obtain the region of interest image. The image value in the region of interest remains unchanged, while the image value outside the region of interest is 0.
② Shielding function: mask some areas on the image so that they do not participate in the processing or the calculation of processing parameters, or only process or count the shielding area.
③ Structural feature extraction, using similarity variables or image matching methods to detect and extract structural features similar to the mask in the image.
④ Production of special shape images.
Among the operation functions of all basic image operations, the mask of any processing function with mask is involved in the operation (after the input image is calculated, it is calculated with the mask image or matrix).

If the value of a given pixel is greater than zero, the pixel will be turned on. If its value is zero, it will be turned off. The bitwise function operates under these binary conditions.

AND: bitwise AND is true if AND only if both pixels are greater than zero, AND the smaller value of phase AND is the result
OR: if either of the two pixels is greater than zero, the bitwise "OR" is true, and the phase OR the larger value is the result
XOR XOR function: if and only if two pixels are converted to binary for XOR calculation
NOT invert: invert the "on" and "off" pixels in the image.

```import cv2
import numpy as np
#I want to put logo on top-left corner, So I create a ROI
rows,cols,channels = img2.shape
print(img2.shape)
print(img1.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)
#cv2.imshow("img2gray",img2gray)
ret, mask = cv2.threshold(img2gray, 175, 255, cv2.THRESH_BINARY)
#Now black-out the area of logo in ROI
#Take the value of the pixel corresponding to the non-zero value in the mask in the roi, and the other values are 0
cv2.imshow("img1_bg",img1_bg)
#Take the and mask in roi_ Inv is the value of the pixel corresponding to the non-zero value, and other values are 0.
#Take only region of logo from logo image.
cv2.imshow("img2_fg",img2_fg)
#Put logo in ROI and modify the main image
cv2.imshow("dst ",dst )
img1[0:rows, 0:cols ] = dst
#cv2.imshow('res',img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

```

The core of image bit operation: the gray image is represented by 0 ~ 255, 0 is black and 255 is white. From the perspective of bit operation, pure black is 0, not pure black is 1. On this basis, it is equivalent to Boolean logic operation.

Therefore, in some pure white or pure black backgrounds, it can be converted to gray images. The content of non background color can be pulled out as a template by using the threshold, and then do bit operation with the original image to carry out matting.