Skip to content

Commit 7cbe247

Browse files
authored
Add files via upload
1 parent 4a9f89e commit 7cbe247

6 files changed

Lines changed: 150 additions & 0 deletions

File tree

blog25-lvjing/blog25-image02.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#coding:utf-8
2+
import cv2
3+
import numpy as np
4+
5+
#读取原始图像
6+
img = cv2.imread('scenery.png')
7+
8+
#获取图像行和列
9+
rows, cols = img.shape[:2]
10+
11+
#新建目标图像
12+
dst = np.zeros((rows, cols, 3), dtype="uint8")
13+
14+
#图像怀旧特效
15+
for i in range(rows):
16+
for j in range(cols):
17+
B = 0.272*img[i,j][2] + 0.534*img[i,j][1] + 0.131*img[i,j][0]
18+
G = 0.349*img[i,j][2] + 0.686*img[i,j][1] + 0.168*img[i,j][0]
19+
R = 0.393*img[i,j][2] + 0.769*img[i,j][1] + 0.189*img[i,j][0]
20+
if B>255:
21+
B = 255
22+
if G>255:
23+
G = 255
24+
if R>255:
25+
R = 255
26+
dst[i,j] = np.uint8((B, G, R))
27+
28+
#显示图像
29+
cv2.imshow('src', img)
30+
cv2.imshow('dst', dst)
31+
cv2.waitKey()
32+
cv2.destroyAllWindows()

blog25-lvjing/blog25-image03.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#coding:utf-8
2+
import cv2
3+
import math
4+
import numpy as np
5+
6+
#读取原始图像
7+
img = cv2.imread('scenery.png')
8+
9+
#获取图像行和列
10+
rows, cols = img.shape[:2]
11+
12+
#设置中心点
13+
centerX = rows / 2
14+
centerY = cols / 2
15+
print centerX, centerY
16+
radius = min(centerX, centerY)
17+
print radius
18+
19+
#设置光照强度
20+
strength = 200
21+
22+
#新建目标图像
23+
dst = np.zeros((rows, cols, 3), dtype="uint8")
24+
25+
#图像光照特效
26+
for i in range(rows):
27+
for j in range(cols):
28+
#计算当前点到光照中心距离(平面坐标系中两点之间的距离)
29+
distance = math.pow((centerY-j), 2) + math.pow((centerX-i), 2)
30+
#获取原始图像
31+
B = img[i,j][0]
32+
G = img[i,j][1]
33+
R = img[i,j][2]
34+
if (distance < radius * radius):
35+
#按照距离大小计算增强的光照值
36+
result = (int)(strength*( 1.0 - math.sqrt(distance) / radius ))
37+
B = img[i,j][0] + result
38+
G = img[i,j][1] + result
39+
R = img[i,j][2] + result
40+
#判断边界 防止越界
41+
B = min(255, max(0, B))
42+
G = min(255, max(0, G))
43+
R = min(255, max(0, R))
44+
dst[i,j] = np.uint8((B, G, R))
45+
else:
46+
dst[i,j] = np.uint8((B, G, R))
47+
48+
#显示图像
49+
cv2.imshow('src', img)
50+
cv2.imshow('dst', dst)
51+
cv2.waitKey()
52+
cv2.destroyAllWindows()

blog25-lvjing/blog25-image04.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#coding:utf-8
2+
import cv2
3+
import math
4+
import numpy as np
5+
6+
#读取原始图像
7+
img = cv2.imread('scenery.png')
8+
9+
#获取图像行和列
10+
rows, cols = img.shape[:2]
11+
12+
#新建目标图像
13+
dst = np.zeros((rows, cols, 3), dtype="uint8")
14+
15+
#图像流年特效
16+
for i in range(rows):
17+
for j in range(cols):
18+
#B通道的数值开平方乘以参数12
19+
B = math.sqrt(img[i,j][0]) * 12
20+
G = img[i,j][1]
21+
R = img[i,j][2]
22+
if B>255:
23+
B = 255
24+
dst[i,j] = np.uint8((B, G, R))
25+
26+
#显示图像
27+
cv2.imshow('src', img)
28+
cv2.imshow('dst', dst)
29+
cv2.waitKey()
30+
cv2.destroyAllWindows()

blog25-lvjing/blog25-image05.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#coding:utf-8
2+
import cv2
3+
import numpy as np
4+
5+
#获取滤镜颜色
6+
def getBGR(img, table, i, j):
7+
#获取图像颜色
8+
b, g, r = img[i][j]
9+
#计算标准颜色表中颜色的位置坐标
10+
x = int(g/4 + int(b/32) * 64)
11+
y = int(r/4 + int((b%32) / 4) * 64)
12+
#返回滤镜颜色表中对应的颜色
13+
return lj_map[x][y]
14+
15+
#读取原始图像
16+
img = cv2.imread('scenery.png')
17+
lj_map = cv2.imread('table.png')
18+
print img.shape
19+
20+
#获取图像行和列
21+
rows, cols = img.shape[:2]
22+
23+
#新建目标图像
24+
dst = np.zeros((rows, cols, 3), dtype="uint8")
25+
26+
#循环设置滤镜颜色
27+
for i in range(rows):
28+
for j in range(cols):
29+
dst[i][j] = getBGR(img, lj_map, i, j)
30+
31+
#显示图像
32+
cv2.imshow('src', img)
33+
cv2.imshow('dst', dst)
34+
35+
cv2.waitKey()
36+
cv2.destroyAllWindows()

blog25-lvjing/scenery.png

393 KB
Loading

blog25-lvjing/table.png

291 KB
Loading

0 commit comments

Comments
 (0)