Opencv (image processing)-based on Python-basic operations on images-adding watermarks to pictures

insert image description here

1. Concept of Image Computing

In the computer world, an image is composed of pixels, each pixel represents a number, and the size of the number represents the color of the pixel, and the colors of all pixels form an image. Therefore, images can be well represented using matrices.
The operation method provided by OpenCV is a matrix-based operation.

2. Image operation

2.1 add()

Add two pictures (each pixel of the matrix is ​​added correspondingly)
result = cv2.add(img1,img2)
parameters: twoexactly the same sizepicture of.
The image after the return value is calculated (deep copy)

insert image description here

Cut off the upper left corner of lina Figure 4 in Figure 1, and then add it to Girl Figure 2 to get the result shown in Figure 3

2.2 substract()

Make the difference between the two pictures (each pixel of the matrix corresponds to the subtraction)
result = cv2.subtract(img1,img2)
parameters: img1-img2
The result is saved in a new parameter (deep copy)

insert image description here

After the difference between image 1 and 2, the brightness is reduced

Some code examples:

import cv2
import numpy as np


img1 = cv2.imread('./image/girl.png')
img2 = cv2.imread('./image/lina.jpg')
# 打印两个图片的参数
print(img1.shape)
print(img2.shape)

# 创建像素点为1的矩阵,并且每个像素点都乘50
img3 = np.ones((185, 185, 3), np.uint8)*100

# img2过大,我们截取部分图像,让region与img1一样大
# 截取为深拷贝,对截取的图像运算,并不改变原图像
region = img2[0:185, 0:185]
# 将两个图像运算
result = cv2.add(img1, region)
# 降低图片的亮度
low_light_result = cv2.subtract(result, img3)
# 展示图片
# cv2.imshow("img1", img1)
# cv2.imshow("img2", img2)
# cv2.imshow("region", region)
cv2.imshow('result', result)
cv2.imshow('img3', img3)
cv2.imshow('low_light_result', low_light_result)

cv2.waitKey(0)

2.3 multiply()/divide()

Matrix multiplication (multiply each pixel of the two matrices)
result = cv2.multiply(img1,img2)
matrix division (divide each pixel of the two matrices)
result = cv2.divide()

2.4 addWeighted()

Image fusion, add by weight
result = cv2.addWeighted(A, alpha, B, bate, gamma)
parameters: alpha: weight of A; bate: weight of B; gamma: static weight (add gamma after the operation)

insert image description here

insert image description here
Add the weight of Figure 1 to 0.2 and Figure 2 to 0.8 to obtain Figure 3

Part of the code display

import cv2
import numpy as np


img1 = cv2.imread('./image/girl.png')
img2 = cv2.imread('./image/lina.jpg')
# 打印两个图片的参数
print(img1.shape)
print(img2.shape)

# img2过大,我们截取部分图像,让region与img1一样大
# 截取为深拷贝,对截取的图像运算,并不改变原图像
region = img2[0:185, 0:185]


# 展示图片
cv2.imshow("img1", img1)
cv2.imshow("region", region)

# 图像融合
result = cv2.addWeighted(region, 0.2, img1, 0.8, 0)

cv2.imshow('result', result)

cv2.waitKey(0)

2.5 AND/OR/NOT

The logical operations here are consistent with the logical operations of other languages ​​​​that we are familiar with. It is to perform logical operations on each pixel of the matrix.

bitwise_and()

Image bitwise AND operation
result = cv2.bitwise_and(img1, img2)

bitwise_or()

Image bitwise OR operation
result = cv2.bitwise_or(img1, img2)

bitwise_not()

Image non-operation
result = cv2.bitwise_not(img)

bitwise_xor

Bitwise XOR operation of the image (the same is 0, the difference is 1)
result = cv2.bitwise_xor(img1, img2)

Part of the code display

import cv2
import numpy as np

# 创建一个全0图像
img1 = np.zeros((400, 400, 3), np.uint8)
img2 = np.zeros((400, 400, 3), np.uint8)

# 将两幅图部分变成白色
img1[100:200, 100:200] = [255, 255, 255]
img2[150:250, 150:250] = [255, 255, 255]
cv2.imshow('img1', img1)
cv2.imshow('img2', img2)

# 位运算
img_and = cv2.bitwise_and(img1, img2)
cv2.imshow('img_and', img_and)
img_or = cv2.bitwise_or(img1, img2)
cv2.imshow('img_or', img_or)
img_not = cv2.bitwise_not(img1)
cv2.imshow('img_not', img_not)
img_xor = cv2.bitwise_xor(img1, img2)
cv2.imshow('img_xor', img_xor)



cv2.waitKey(0)

The code result looks like this:
insert image description here

3. Add watermark to an image

The following will show the general steps to add watermark to the picture:

  1. Introduce a picture lina
  2. To prepare a logo, create it yourself
  3. Calculate where to add in the picture, and turn the added place into black
  4. Use add to superimpose the logo and the picture together
import cv2
import numpy as np

img = cv2.imread('./image/lina.jpg')
col, row, chanles = img.shape

# 创建logo
logo = np.zeros((80, 80, 3), np.uint8)
mask1 = np.zeros((80, 80), np.uint8)# 只有一通道,是灰度图
# 画logo
logo[10:40, 10:40] = [0, 0, 255]
logo[30:70, 30:70] = [0, 255, 0]
# 为了将图片中的部分截取下来,把logo的地方给弄黑
mask1[10:40, 10:40] = 255
mask1[30:70, 30:70] = 255
mask1 = cv2.bitwise_not(mask1)
# 截取图像部分
region_img = img[0:80, 0:80]
# 单通道图像与三通道图像运算,智能这样运算。
mask1 = cv2.bitwise_and(region_img, region_img, mask = mask1)

finish_logo = cv2.add(logo, mask1)
# 将处理好的mask1与原图像赋值(添加成功)
img[0:80, 0:80] = finish_logo

cv2.imshow('img', img)
# cv2.imshow('logo', logo)
# cv2.imshow('mask1', mask1)
# cv2.imshow('fin_logo',finish_logo)

cv2.waitKey(0)

As shown in the figure, a logo is added in the upper left corner
insert image description here
The above are the basic APIs for image computing. It is inevitable that there will be unclear explanations. If you have any questions, please discuss them in the comment area.

Guess you like

Origin blog.csdn.net/weixin_45153969/article/details/130928470