Code combat
import os
import cv2
import numpy as np
# Read and zoom pictures
lenna=cv2.imread('images/lenna.png')
lenna=cv2.resize(src=lenna,dsize=(450,450))
# Create a picture of the same size
npimg=np.ones(shape=(lenna.shape[0],lenna.shape[1],lenna.shape[2]),dtype=np.uint8)*100
# And two pictures
dst=cv2.add(src1=lenna,src2=npimg)
# display picture
cv2.imshow('lenna',lenna)
cv2.imshow('dst',dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
You can see that the resulting image is similar to the exposure .
def Subtract():
# Read and zoom pictures
lenna = cv2.imread('images/lenna.png')
lenna = cv2.resize(src=lenna, dsize=(450, 450))
# Create a picture of the same size
npimg = np.ones(shape=(lenna.shape[0], lenna.shape[1], lenna.shape[2]), dtype=np.uint8) * 100
# First, add two pictures
dst = cv2.add(src1=lenna, src2=npimg)
# Subtract the added image , Restore to original drawing
original=cv2.subtract(src1=dst,src2=npimg)
print(" The maximum pixel value in the original image : {}".format(lenna.max()))
print(' The maximum pixel value after adding : {}'.format(dst.max()))
print(' The maximum pixel value after subtraction : {}'.format(original.max()))
# display picture
cv2.imshow('lenna', lenna)
cv2.imshow('dst', dst)
cv2.imshow('original',original)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(" The maximum pixel value in the original image : {}".format(lenna.max()))
print(' The maximum pixel value after adding : {}'.format(dst.max()))
print(' The maximum pixel value after subtraction : {}'.format(original.max()))
You can see that the maximum pixel value in the original image is 255, The maximum pixel value after adding must also be 255; But suppose there is a pixel value in the original image 160 Of , First add 100 after , Then the pixel value becomes 255( Because more than 255, So only for 255), So when subtracting , This pixel value becomes 155, And the original 160 Compared with , So even if the original graph first adds and then subtracts , The image will generally become a little darker .
def mutilpImg():
# Read and zoom pictures
lenna = cv2.imread('images/lenna.png')
lenna = cv2.resize(src=lenna, dsize=(450, 450))
# Create a picture of the same size
npimg = np.ones(shape=(lenna.shape[0], lenna.shape[1], lenna.shape[2]), dtype=np.uint8) * 100
# To multiply
dst=cv2.multiply(src1=lenna,src2=npimg)
# display picture
cv2.imshow('lenna', lenna)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
Why is the output mostly white , It is because many pixel values are greater than after multiplication 255, So the end result is 255, That is, many parts of the image are white .
def DividImg():
# Read and zoom pictures
lenna = cv2.imread('images/lenna.png')
lenna = cv2.resize(src=lenna, dsize=(450, 450))
# Create a picture of the same size
npimg = np.ones(shape=(lenna.shape[0], lenna.shape[1], lenna.shape[2]), dtype=np.uint8) * 100
# To multiply
dst = cv2.divide(src1=lenna, src2=npimg)
# display picture
cv2.imshow('lenna', lenna)
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(“ The maximum pixel value in the original image : {}”.format(lenna.max()))
print(“ Maximum pixel value after division : {}”.format(dst.max()))
You can see that the maximum pixel value in the original image is 255, All pixel values in the original image except 100 after , Then the final result can only be <=3, So it looks black .