import matplotlib.pyplot as plt
import numpy as np
import pylab
def General_Conv(imatx, imaty, kernel, padding):
k_array = np.random.rand(kernel, kernel) # 卷积核矩阵
i_array = np.random.randint(0, 256,(imatx, imaty)) # 随机数矩阵
gap = kernel//2 # 计数空缺的行数和列数
new_array = np.zeros(shape=(imatx, imaty)) # 在原来的随机数矩阵外围补零
if padding == 0: # 不填充
b_array = np.zeros(shape=(imatx - gap*2, imaty - gap*2)) # 定义一个矩阵存放卷积以后的值
for i in range(gap, imatx - gap):
for j in range(gap, imaty - gap):
m_array = i_array[i - gap:i + gap + 1, j - gap:j + gap + 1]
b_array[i-gap][j-gap] = int(np.sum(k_array * m_array))
for x in range(imatx-2*gap):
for y in range(imaty-2*gap):
new_array[x+gap][y+gap] = b_array[x][y] # 将原来的随机数矩阵填入
return new_array
if padding == 1: # 填充0
b_array = np.zeros(shape=(imatx + gap*2, imaty + gap*2)) # 在原来的随机数矩阵外围补零
for i in range(imatx):
for j in range(imaty):
b_array[i+gap][j+gap] = i_array[i][j] # 将原来的随机数矩阵填入
for i in range(gap, imatx+1):
for j in range(gap, imaty+1): # gap下标实际上是第gap+1个元素的位置
m_array = b_array[i-gap:i+gap+1, j-gap:j+gap+1]
new_array[i-gap-1][j-gap-1] = int(np.sum(k_array * m_array)) # 为什么-1
return new_array
img_new = np.uint8(General_Conv(400,300,3,1))
print(img_new)
plt.imshow(img_new) # 卷积结果可视化
pylab.show()
print('*'*40)
img_new = np.uint8(General_Conv(10,10,3,0))
print(img_new)
plt.imshow(img_new) # 卷积结果可视化
pylab.show()
import cv2
def split(c,k): # c代表子图的长,k代表子图的宽
img = cv2.imread('D:/python/python project/pythonProject1/week2/week2_image.jpg/')
rows = img.shape[0]
cols = img.shape[1]
path = 'D:/python/python project/pythonProject1/photo/'
sum = 0
for i in range(int(rows / k) + 1): # (0,3)
for j in range(int(cols / c) + 1): # (0,3)
sum = sum + 1
if i * k > rows & j * c > cols:
new_img = img[k * (i - 1):rows, c * (j - 1):cols, :]
cv2.imwrite(path + str(sum) + "photo.png", new_img)
break
if i * k > rows:
new_img = img[k * (i - 1):rows, c * j:c* (j + 1), :]
cv2.imwrite(path + str(sum) + "photo.png", new_img)
break
if j * c > cols:
new_img = img[k * i:k * (i + 1), c * (j - 1):cols, :]
cv2.imwrite(path + str(sum) + "photo.png", new_img)
break
new_img = img[k * i:k * (i + 1), c * j:c * (j + 1), :]
cv2.imwrite(path + str(sum) + "photo.png", new_img)
split(300,400)
import numpy as np
import cv2
path = 'D:/python/python project/pythonProject1/week2/photo/'
for num in range(1, 10): # 实际取到(1,9)
img = cv2.imread(path + str(num) + 'photo.png')
imatx = img.shape[0]
imaty = img.shape[1]
def General_Conv(kernel, padding):
b, g, r = cv2.split(img)
color = [b, g, r]
count = 0
k_array = np.random.rand(kernel, kernel) # 卷积核矩阵
for a in color:
i_array = a
gap = kernel//2 # 计数空缺的行数和列数
new_array = np.zeros(shape=(imatx, imaty)) # 定义一个矩阵存放卷积以后的值
if padding == 0: # 不填充
b_array = np.zeros(shape=(imatx - gap*2, imaty - gap*2)) # 定义一个矩阵存放卷积以后的值
for i in range(gap, imatx - gap):
for j in range(gap, imaty - gap):
m_array = i_array[i - gap:i + gap + 1, j - gap:j + gap + 1]
b_array[i-gap][j-gap] = int(np.sum(k_array * m_array))
for x in range(imatx-2*gap):
for y in range(imaty-2*gap):
new_array[x+gap][y+gap] = b_array[x][y] # 将原来的随机数矩阵填入
if padding == 1: # 填充0
b_array = np.zeros(shape=(imatx + gap*2, imaty + gap*2)) # 在原来的随机数矩阵外围补零
for i in range(imatx):
for j in range(imaty):
b_array[i+gap][j+gap] = i_array[i][j] # 将原来的随机数矩阵填入
for i in range(gap, imatx+1):
for j in range(gap, imaty+1): # gap下标实际上是第gap+1个元素的位置
m_array = b_array[i-gap:i+gap+1, j-gap:j+gap+1]
new_array[i-gap-1][j-gap-1] = int(np.sum(k_array * m_array))
count = count + 1
if count == 1:
b = new_array
if count == 2:
g = new_array
if count == 3:
r = new_array
new_array = cv2.merge([b, g, r])
cv2.namedWindow("img", 0) # 可以拖动窗口大小
cv2.resizeWindow("img", 300, 400) # 设置窗口大小
cv2.moveWindow("img", 10, 20) # 设置窗口位置
"""
a_array = np.zeros(shape=(2,4)) # float
a_array = a_array.astype(np.uint8) # int
"""
cv2.imshow('img', new_array.astype(np.uint8)) # 将浮点型数组转化为整形
cv2.waitKey(0)
General_Conv(3, 1)
import numpy as np
import cv2
k_array1 = [[1, 0, -1], # 水平强化边缘
[1, 0, -1],
[1, 0, -1]]
k_array2 = [[1, 1, 1], # 竖直强化边缘
[0, 0, 0],
[-1, -1, -1]]
k_array3 = [[-2, -1, 0], # 浮雕效果
[-1, 1, 1],
[0, 1, 2]]
k_array4 = [[0, -1, 0], # 锐化
[-1, 5, -1],
[0, -1, 0]]
path = 'D:/python/python project/pythonProject1/photo/'
img = cv2.imread('D:\python\python project\pythonProject1\week2\week2_image.jpg')
# option等于1,2,3,4代表选择卷积核矩阵k_array1,k_array2,k_array3,k_array4
def General_Conv(imatx, imaty, kernel, padding, option):
if option == 1:
k_array = k_array1 # 卷积核矩阵1
if option == 2:
k_array = k_array2 # 卷积核矩阵2
if option == 3:
k_array = k_array3 # 卷积核矩阵3
if option == 4:
k_array = k_array4 # 卷积核矩阵4
b, g, r = cv2.split(img)
color = [b, g, r]
count = 0
for a in color:
i_array = a
gap = kernel//2 # 计数空缺的行数和列数
new_array = np.zeros(shape=(imatx, imaty)) # 定义一个矩阵存放卷积以后的值
if padding == 0: # 不填充
b_array = np.zeros(shape=(imatx - gap*2, imaty - gap*2)) # 定义一个矩阵存放卷积以后的值
for i in range(gap, imatx - gap):
for j in range(gap, imaty - gap):
m_array = i_array[i - gap:i + gap + 1, j - gap:j + gap + 1]
b_array[i-gap][j-gap] = int(np.sum(k_array * m_array))
for x in range(imatx-2*gap):
for y in range(imaty-2*gap):
new_array[x+gap][y+gap] = b_array[x][y] # 将原来的随机数矩阵填入
if padding == 1: # 填充0
b_array = np.zeros(shape=(imatx + gap*2, imaty + gap*2)) # 在原来的随机数矩阵外围补零
for i in range(imatx):
for j in range(imaty):
b_array[i+gap][j+gap] = i_array[i][j] # 将原来的随机数矩阵填入
for i in range(gap, imatx+1):
for j in range(gap, imaty+1): # gap下标实际上是第gap+1个元素的位置
m_array = b_array[i-gap:i+gap+1, j-gap:j+gap+1]
new_array[i-gap-1][j-gap-1] = int(np.sum(k_array * m_array)) # 为什么-1
count = count + 1
if count == 1:
b = new_array
if count == 2:
g = new_array
if count == 3:
r = new_array
new_array = cv2.merge([b, g, r])
cv2.namedWindow("img", 0) # 可以拖动窗口大小
cv2.resizeWindow("img", 300, 400) # 设置窗口大小
cv2.moveWindow("img", 10, 20) # 设置窗口位置
"""
a_array = np.zeros(shape=(2,4)) # float
a_array = a_array.astype(np.uint8) # int
"""
cv2.imshow('img', new_array.astype(np.uint8)) # 将浮点型数组转化为整形
cv2.waitKey(0)
General_Conv(1067, 800, 3, 1, 1)
# General_Conv(1067, 800, 3, 1, 2)
# General_Conv(400, 300, 3, 0, 3)
# General_Conv(1067, 800, 3, 0, 4)
General_Conv(1067, 800, 3, 1, 1):
General_Conv(1067, 800, 3, 1, 2):
General_Conv(1067, 800, 3, 1, 3):
General_Conv(1067, 800, 3, 1, 4):