opencv-python- study notes six (basic operation of the image)

Arithmetic Operators

Outline

We can OpenCV function (cv.add ()) or simple operation numpy (res = img1 + img2) operation on two images. two depth images (the number of bits used to store each image, each pixel of image depth determines that the color image may be any number of colors) and type should be the same, or the image may be only a second scalar value.

But OpenCV addition and Numpy addition there is a difference. OpenCV addition operation is saturated, Numpy addition is modulo operation.

for example:

Import CV2 AS CV
 Import numpy AS NP 


X = np.uint8 ([250 ]) # unsigned 8-bit integer, denotes an integer ranging from [0, 255] to 
Y = np.uint8 ([10 ])
 Print (CV. the Add (X, Y))   # 250 = + 10 = 260.> [[255]] 

Print (X + Y)           # 250 = 10 + 256 = 260.. 4% => [. 4]

Image arithmetic

Definition:

src1: first images

src2: Second image

dst: destination, destination image, you need to allocate space in advance, can be omitted

mask: 8 bit single-channel array element specifies the change of the output array.

scale: scaling ratio

dtype: the depth of the output array, the default is equal to -1

dst=cv.add(src1, src2[, dst[, mask[, dtype]]])

dst=cv2.subtract(src1,src2,dst,mask,dtype)

dst=cv2.multiply(src1,src2,dst,scale,dtype)       dst = scale * src1 * src2

dst=cv2.divide(src1,src2,dst,scale,dtype)          dst = scale * src1 / src2

For example:

Import CV2 AS CV
 Import numpy AS NP 


IMG1 = cv.imread ( ' 1.jpg ' )   # image. 1 
IMG2 = cv.imread ( ' 2.jpg ' )   # Image 2 

the Add = cv.add (IMG1, IMG2)   # two image adding 
subtract = cv.subtract (IMG1, IMG2)   # two image subtraction 
Multiply = cv.multiply (IMG1, IMG2)   # two images multiplied 
Divide = cv.divide (IMG1, IMG2)   # two images division 

cv.imshow ( " the Test " , the Add)

 

Image blending

This is also the added image, but to the image of different weights, so it gives people a feeling of mixed or transparent. Add image following formula:

d s t = a i m g 1 + b i m g 2 + c

 

dst=cv.addWeighted(src1, alpha, src2, beta, gamma[, dst[, dtype]])

parameter:

src1: first image

appha: the first array element occupies weight

src2: second image

beta: The second array element occupies weight

gamma: scalar and added to each of the

dst: the input array and output array have the same size and number of channels

dtype:

 

import numpy as np
import cv2 as cv


def nothing(x):
    pass


# Create a black image, a window
img1 = np.zeros((300, 528, 3), np.uint8)
img2 = cv.imread("2.jpg")

cv.namedWindow('image')
# create trackbars for color change
cv.createTrackbar('change', 'image', 0, 1, nothing)
while(1):
    k = cv.waitKey(1) & 0xFF
    if k == 27:
        break
    # get current positions of four trackbars
    change = cv.getTrackbarPos("change", 'image')
    dst = cv.addWeighted(img1, change, img2, 1-change, 0)
    cv.imshow("image", dst)
cv.destroyAllWindows()

       

 

Logical operators (subsequent updates)

 

Guess you like

Origin www.cnblogs.com/blog-xyy/p/11184095.html