Error in user YAML: (<unknown>): could not find expected ':' while scanning a simple key at line 3 column 1
---
- oeasy Python 0650
- 这是 oeasy 系统化 Python 教程,从基础一步步讲,扎实、完整、不跳步。愿意花时间学,就能真正学会。
本教程同步发布在:
个人网站: `https://oeasy.org`
蓝桥云课: `https://www.lanqiao.cn/courses/3584`
GitHub: `https://github.com/overmind1980/oeasy-python-tutorial`
Gitee: `https://gitee.com/overmind1980/oeasypython`
---
- 上次研究了键盘互动的效果
- 写字效果
- 下次玩点什么呢?
import cv2
img = cv2.imread("monkey.png")
sum1 = img + img
cv2.imshow("sum1",sum1)
sum2= cv2.add(img,img)
cv2.imshow("sum2",sum2) cv2.waitKey()
cv2.destroyAllWindows()

| 运算法 |
溢出处理 |
| 直接+法 |
溢出后 取模 |
| add加法 |
溢出后 等于255 |
import cv2
import numpy as np
blue = np.zeros((150,150,3),np.uint8)
blue[:,:,0] = 255
green = np.zeros((150,150,3),np.uint8)
green[:,:,1] = 255
red = np.zeros((150,150,3),np.uint8)
red[:,:,2] = 255
cv2.imshow("blue",blue)
cv2.imshow("green",green)
cv2.imshow("red",red)
cyan = cv2.add(blue,green)
cv2.imshow("cyan",cyan)
white = cv2.add(cyan,red)
cv2.imshow("white",white)
cv2.waitKey()
cv2.destroyAllWindows()

import cv2
import numpy as np
red = np.zeros((300,300),np.uint8)
green = np.zeros((300,300),np.uint8)
blue = np.zeros((300,300),np.uint8)
def draw_gradient(pos_x,pos_y,radius,image):
for x in range(0,300):
for y in range(300):
color = 255 - int(((y-pos_y)**2 + (x-pos_x)**2)**0.5/(radius*2**0.5)*255)
image[y,x] = color
draw_gradient(100,100,80,red)
cv2.imshow("red",red)
draw_gradient(200,100,80,green)
draw_gradient(150,105,80,blue)
rgb = cv2.merge([blue,green,red])
cv2.imshow("rgb",rgb)
key = cv2.waitKey(0)

import cv2
import numpy as np
red = np.zeros((300,300),np.uint8)
green = np.zeros((300,300),np.uint8)
blue = np.zeros((300,300),np.uint8)
black = np.zeros((300,300),np.uint8)
def draw_gradient(pos_x,pos_y,radius,image):
for x in range(0,300):
for y in range(300):
dx = abs(x - pos_x)
dy = abs(y - pos_y)
dis = (dy**2 + dx**2) ** 0.5
if dis < radius:
color = dis / radius * 255
color = 255 - int(color)
image[y,x] = color
draw_gradient(100,100,200,red)
draw_gradient(200,100,200,green)
draw_gradient(150,200,200,blue)
red_channel = cv2.merge([blue,black,black])
cv2.imshow("red_channel",red_channel)
r_b = cv2.merge([blue,green,black])
cv2.imshow("r_b",r_b)
rgb = cv2.merge([blue,green,red])
cv2.imshow("rgb",rgb)
key = cv2.waitKey(0)

#encoding:utf-8
import cv2
import numpy as np
import matplotlib.pyplot as plt
#读取图片
src1 = cv2.imread("lena.png")
src2 = cv2.imread("monkey.png")
rows,cols = src2.shape[:2]
print(rows,cols)
src1 = src1[0:rows,0:cols,]
print(src1.shape)
#图像融合
#result = cv2.addWeighted(src1, 1, src2, 1, 0)
result = cv2.addWeighted(src1, 0.6, src2, 0.8, 10)
#显示图像
cv2.imshow("src1", src1)
cv2.imshow("src2", src2)
cv2.imshow("result", result)
#等待显示
cv2.waitKey(0)
cv2.destroyAllWindows()



- multiply
- 正片叠底
- 200 ➗ 255 = 0.78
- 72 ➗ 255 = 0.28
- 0.22 会比原来变暗

- 相加模式
- 两者亮度值 直接相加
- 超过255 超过1
- 产生溢出
- 显示为 纯白色

| 运算法 |
溢出处理 |
| 直接+法 |
溢出后 取模 |
| add加法 |
溢出后 等于255 |
- BGR 三个通道混合的时候
- 除了 加减运算之外
- 下次再说👋
- 本文来自 oeasy Python 系统教程。
- 想完整、扎实学 Python,
- 搜索 oeasy 即可。