Skip to content

Commit a66c8b5

Browse files
committed
submit code
1 parent e1f4721 commit a66c8b5

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

chaoxi/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ Python技术 公众号文章代码库
88
![](http://favorites.ren/assets/images/python.jpg)
99

1010
## 实例代码
11+
opencv_img
12+
13+
[惊艳!利用 Python 图像处理绘制专属头像](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/opencv_img) 惊艳!利用 Python 图像处理绘制专属头像
1114

1215
[七夕不懂浪漫?Python 帮你制造惊喜!!!](https://github.com/JustDoPython/python-examples/tree/master/chaoxi/qixi) 七夕不懂浪漫?Python 帮你制造惊喜!!!
1316

chaoxi/opencv_img/opencv_img.py

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
import cv2
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import math
5+
6+
# 读取图片
7+
img = cv2.imread('me1.jpg')
8+
src = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
9+
10+
# 新建目标图像
11+
dst1 = np.zeros_like(img)
12+
13+
# 获取图像行和列
14+
rows, cols = img.shape[:2]
15+
16+
# --------------毛玻璃效果--------------------
17+
# 像素点邻域内随机像素点的颜色替代当前像素点的颜色
18+
offsets = 5
19+
random_num = 0
20+
for y in range(rows - offsets):
21+
for x in range(cols - offsets):
22+
random_num = np.random.randint(0, offsets)
23+
dst1[y, x] = src[y + random_num, x + random_num]
24+
25+
# -------油漆特效------------
26+
# 图像灰度处理
27+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
28+
29+
# 自定义卷积核
30+
kernel = np.array([[-1, -1, -1], [-1, 10, -1], [-1, -1, -1]])
31+
32+
# 图像浮雕效果
33+
dst2 = cv2.filter2D(gray, -1, kernel)
34+
35+
# ----------素描特效-------------
36+
# 高斯滤波降噪
37+
gaussian = cv2.GaussianBlur(gray, (5, 5), 0)
38+
39+
# Canny算子
40+
canny = cv2.Canny(gaussian, 50, 150)
41+
42+
# 阈值化处理
43+
ret, dst3 = cv2.threshold(canny, 100, 255, cv2.THRESH_BINARY_INV)
44+
45+
# -------怀旧特效-----------------
46+
# 新建目标图像
47+
dst4 = np.zeros((rows, cols, 3), dtype="uint8")
48+
49+
# 图像怀旧特效
50+
for i in range(rows):
51+
for j in range(cols):
52+
B = 0.272 * img[i, j][2] + 0.534 * img[i, j][1] + 0.131 * img[i, j][0]
53+
G = 0.349 * img[i, j][2] + 0.686 * img[i, j][1] + 0.168 * img[i, j][0]
54+
R = 0.393 * img[i, j][2] + 0.769 * img[i, j][1] + 0.189 * img[i, j][0]
55+
if B > 255:
56+
B = 255
57+
if G > 255:
58+
G = 255
59+
if R > 255:
60+
R = 255
61+
dst4[i, j] = np.uint8((B, G, R))
62+
63+
# ---------------光照特效--------------------
64+
# 设置中心点
65+
centerX = rows / 2
66+
centerY = cols / 2
67+
print(centerX, centerY)
68+
radius = min(centerX, centerY)
69+
print(radius)
70+
71+
# 设置光照强度
72+
strength = 200
73+
74+
# 新建目标图像
75+
dst5 = np.zeros((rows, cols, 3), dtype="uint8")
76+
77+
# 图像光照特效
78+
for i in range(rows):
79+
for j in range(cols):
80+
# 计算当前点到光照中心的距离(平面坐标系中两点之间的距离)
81+
distance = math.pow((centerY - j), 2) + math.pow((centerX - i), 2)
82+
# 获取原始图像
83+
B = src[i, j][0]
84+
G = src[i, j][1]
85+
R = src[i, j][2]
86+
if (distance < radius * radius):
87+
# 按照距离大小计算增强的光照值
88+
result = (int)(strength * (1.0 - math.sqrt(distance) / radius))
89+
B = src[i, j][0] + result
90+
G = src[i, j][1] + result
91+
R = src[i, j][2] + result
92+
# 判断边界 防止越界
93+
B = min(255, max(0, B))
94+
G = min(255, max(0, G))
95+
R = min(255, max(0, R))
96+
dst5[i, j] = np.uint8((B, G, R))
97+
else:
98+
dst5[i, j] = np.uint8((B, G, R))
99+
100+
# --------------怀旧特效-----------------
101+
# 新建目标图像
102+
dst6 = np.zeros((rows, cols, 3), dtype="uint8")
103+
104+
# 图像流年特效
105+
for i in range(rows):
106+
for j in range(cols):
107+
# B通道的数值开平方乘以参数12
108+
B = math.sqrt(src[i, j][0]) * 12
109+
G = src[i, j][1]
110+
R = src[i, j][2]
111+
if B > 255:
112+
B = 255
113+
dst6[i, j] = np.uint8((B, G, R))
114+
115+
# ------------卡通特效-------------------
116+
# 定义双边滤波的数目
117+
num_bilateral = 7
118+
119+
# 用高斯金字塔降低取样
120+
img_color = src
121+
122+
# 双边滤波处理
123+
for i in range(num_bilateral):
124+
img_color = cv2.bilateralFilter(img_color, d=9, sigmaColor=9, sigmaSpace=7)
125+
126+
# 灰度图像转换
127+
img_gray = cv2.cvtColor(src, cv2.COLOR_RGB2GRAY)
128+
129+
# 中值滤波处理
130+
img_blur = cv2.medianBlur(img_gray, 7)
131+
132+
# 边缘检测及自适应阈值化处理
133+
img_edge = cv2.adaptiveThreshold(img_blur, 255,
134+
cv2.ADAPTIVE_THRESH_MEAN_C,
135+
cv2.THRESH_BINARY,
136+
blockSize=9,
137+
C=2)
138+
139+
# 转换回彩色图像
140+
img_edge = cv2.cvtColor(img_edge, cv2.COLOR_GRAY2RGB)
141+
142+
# 与运算
143+
dst6 = cv2.bitwise_and(img_color, img_edge)
144+
145+
# ------------------均衡化特效--------------------
146+
# 新建目标图像
147+
dst7 = np.zeros((rows, cols, 3), dtype="uint8")
148+
149+
# 提取三个颜色通道
150+
(b, g, r) = cv2.split(src)
151+
152+
# 彩色图像均衡化
153+
bH = cv2.equalizeHist(b)
154+
gH = cv2.equalizeHist(g)
155+
rH = cv2.equalizeHist(r)
156+
157+
# 合并通道
158+
dst7 = cv2.merge((bH, gH, rH))
159+
160+
# -----------边缘特效---------------------
161+
# 高斯滤波降噪
162+
gaussian = cv2.GaussianBlur(gray, (3, 3), 0)
163+
164+
# Canny算子
165+
# dst8 = cv2.Canny(gaussian, 50, 150)
166+
167+
# Scharr算子
168+
x = cv2.Scharr(gaussian, cv2.CV_32F, 1, 0) # X方向
169+
y = cv2.Scharr(gaussian, cv2.CV_32F, 0, 1) # Y方向
170+
absX = cv2.convertScaleAbs(x)
171+
absY = cv2.convertScaleAbs(y)
172+
dst8 = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
173+
174+
175+
# 用来正常显示中文标签
176+
plt.rcParams['font.sans-serif'] = ['SimHei']
177+
178+
# 循环显示图形
179+
titles = ['原图', '毛玻璃', '浮雕', '素描', '怀旧', '光照', '卡通', '均衡化', '边缘']
180+
images = [src, dst1, dst2, dst3, dst4, dst5, dst6, dst7, dst8]
181+
for i in range(9):
182+
plt.subplot(3, 3, i + 1), plt.imshow(images[i], 'gray')
183+
plt.title(titles[i])
184+
plt.xticks([]), plt.yticks([])
185+
186+
if __name__ == '__main__':
187+
188+
plt.show()

0 commit comments

Comments
 (0)