open cv学习 (六) 图像的运算

图像的运算

demo1
import cv2
import numpy as np
mask = np.zeros((150, 150, 3), np.uint8)
mask[50:100, 20:80, :] = 255
cv2.imshow("mask1", mask)
mask[:, :, :] = 255
mask[50:100, 20:80, :] = 0
cv2.imshow("mask2", mask)
cv2.waitKey()
cv2.destroyAllWindows()
demo2
import cv2
import numpy as np
person = cv2.imread("./img.png")
mask = np.zeros(person.shape, np.uint8)

mask[120:180, :, :] = 255
mask[:, 80:180, :] = 255
img = cv2.bitwise_and(person, mask)

cv2.imshow("person", person)
cv2.imshow("mask", mask)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()
demo3
# 对图像进行加密 解密
import cv2
import numpy as np


def encode(img, img_key):
    result = img = cv2.bitwise_xor(img, img_key)
    return result

person = cv2.imread("./img.png")

# 获取原图像的行数、列数和通道数
rows, colmns, channel = person.shape

img_key = np.random.randint(0, 256, (rows, colmns, 3), np.uint8)

cv2.imshow("1", person)
cv2.imshow("2", img_key)
result = encode(person, img_key)
cv2.imshow("3", result)

result = encode(result, img_key)
cv2.imshow("4", result)
cv2.waitKey()
cv2.destroyAllWindows()
demo4
import cv2
import numpy as np

img_1 = cv2.imread("./img.png")
img_2 = cv2.imread("./img_1.png")
rows, colmns, channel = img_1.shape
img_2 = cv2.resize(img_2, (colmns, rows))
img = cv2.addWeighted(img_1, 0.6, img_2, 0.6, 0)
cv2.imshow("img_1", img_1)
cv2.imshow("img_2", img_2)
cv2.imshow("addWeighted", img)
cv2.waitKey()
cv2.destroyAllWindows()

你可能感兴趣的:(学习,opencv,人工智能)