opencv image addition add(), +, addWeighted(), image subtraction subtract(), -, absdiff(), image multiplication multiply(),*, image division, divide(),/

The dtype parameter does not use numpy's uint8, float32 and other data types, but OpenCV's CV_8UC1, CV_32FC1 and other data types.
Image addition
1.add()

img = cv2.imread('..\\lena.jpg')[0:512,0:512] #截取部分,保证大小一致
img2 = cv2.imread('..\\opencv-logo.png' )[0:512,0:512]
img3 = cv2.add(img,img2)
cv2.imshow('add',img3)
cv2.waitKey(0)

In OpenCV, image arithmetic operations follow the rules of "saturation operation". If the calculated result exceeds the threshold range, it will be truncated at the nearest location. For example, the data range of unit8 type is [0,255]. If the result of direct addition of two values ​​is greater than 255 will be assigned a value of 255.
2. +运算符
The + operator adds two images. When the sum of the two values ​​is greater than 255, this value will be modulated by 256 to obtain a new pixel value.
3. addWeighted()Weighted addition
The first parameter is image 1 src;
the second parameter is the weight alpha of image 1; the
third parameter is image 2 dst;
the fourth parameter is the weight beta of image 2;
the fifth parameter Additional numerical gamma, a single value, even a multi-channel image uses a single value;
the sixth parameter is optional, returns an instance of the image, which is equivalent to the function return result
The seventh parameter is optional, dtype, represents the data type
alpha of the pixel value The relationship with beta does not require that the sum of the two is 1. The result of the new image can be expressed as: dst(I)=saturate(src1(I)∗alpha+src2(I)∗beta+gamma).

import cv2

img = cv2.imread('..\\lena.jpg')[0:512,0:512] #截取部分,保证大小一致
img2 = cv2.imread('..\\opencv-logo.png' )[0:512,0:512] 
img3 = cv2.addWeighted(img,0.5,img2,0.2,10)
print('img[161,199]:',img[161,199])
print('img2[161,199]:',img2[161,199])
print('img3[161,199]:',img3[161,199])
cv2.imshow('addWeighted-0.5-0.2',img3)

img3 = cv2.addWeighted(img,0.2,img2,0.5,10)
print('img[161,199]:',img[161,199])
print('img2[161,199]:',img2[161,199])
print('img3[161,199]:',img3[161,199])
cv2.imshow('addWeighted-0.2-0.5',img3)
cv2.waitKey(0)

Similarly, weighted addWeighted() is also a saturation operation.

Image subtraction
1. subtract()The subtraction of two images still requires the same number of channels, the same image size, and also follows the saturation operation. If the subtraction is less than 0, use 0 to complement it.

import cv2
img = cv2.imread('..\\lena.jpg')[0:512,0:512] #截取部分,保证大小一致
img2 = cv2.imread('..\\opencv-logo.png' )[0:512,0:512]
img3 = cv2.subtract(img,img2)
print('img[161,199]:',img[162,200])
print('img2[161,199]:',img2[162,200])
print('img3[161,199]:',img3[162,200])
cv2.imshow('subtract(img,img2)',img3)

img3 = cv2.subtract(img2,img)
print('img2[161,199]:',img2[162,200])
print('img[161,199]:',img[162,200])
print('img3[161,199]:',img3[162,200])
cv2.imshow('subtract(img2,img)',img3)

cv2.waitKey(0)
  1. -Operator
    If the result obtained is less than 0, the result will not be a negative number, but 256 will be added to the negative number.
import cv2

img = cv2.imread('..\\lena.jpg')[0:512,0:512] #截取部分,保证大小一致
img2 = cv2.imread('..\\opencv-logo.png' )[0:512,0:512]
img3 = img-img2

cv2.imshow('img-img2',img3)
img4 = img2 - img
cv2.imshow('img2 - img',img4)
cv2.waitKey(0)

3. Absolute value subtraction absdiff()
absdiff() obtains the absolute difference of pixels between the two images. The effect of the two is the same when the subtraction and the minuend are swapped.
4. Image and scalar
img3 = cv2.add(img,50)#Add a scalar value to the 1st channel
img3 = cv2.subtract(img,(50,50,50,0))#Subtract a scalar value to the 3rd channel.
When adding and subtracting images and scalars, the scalar value of multiple channels is represented by a 4-tuple.
When the scalar value is only 1 value, only the first channel of the multi-channel image will be operated. If multi-channel operation is to be performed, the scalar value is represented by a tuple containing 4 values, even if it is 3 channels. The color image also uses a 4-tuple to represent this scalar value. The following example performs addition and subtraction operations on the 1st and 3rd channels of the image:

Image multiplication
1. multiply()
multiply() obeys the saturation operation
2. *Multiplication
The numerical type represents the upper limit of the range plus 1 modulo, for example, uint8 type data modulo 256.

Image division
1. divide()
divide() has two uses:
dst = cv2.divide( src1, src2, dst, scale, dtype): The first and second position parameters are both image objects, the optional parameter scale parameter specifies the magnification factor of src1, dst=saturate(src1*scale/src2); in The scale parameter in cv2.divide( src1, src2[, dst[, scale[, dtype]]] ) must be numerical data. First multiply src1 by scale and then divide by src2. If src1 is a multi-channel image, scale will Applies to all channels of src1.

dst = cv2.divide( scale, src2, dst, dtype): The first position parameter is a numeric type, the second position parameter is an image object, dst=saturate(scale/src2). To implement the usage of scale/src2, the name of the formal parameter must be explicitly declared. img_ret = cv2.divide(scale=250.0,src2=img)
If it is division of an integer type such as uint8, the result after the operation will be rounded. The divide() division also obeys the "saturation operation rule".
If you want to call the second form of interface, you must explicitly write the name of the formal parameter variable.
2. /
Symbolic division is actually the division of numpy arrays.
The default data type of the image read by imread() is uint8, and the data type obtained by symbolic division has changed and became float64.
When the divisor is 0, numpy division prints a warning message: divide by zero encountered in true_divide. The direct calculation result is inf, but the image converted to OpenCV is 0 with practical meaning.

Supongo que te gusta

Origin blog.csdn.net/aqiangdeba/article/details/129763007
Recomendado
Clasificación