我对图像卷积感兴趣。下面是我用3x3内核执行卷积的代码。我在寻找如何让它跑得更快的想法
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from PIL import Image
import numpy as np
img = mpimg.imread('benfrank.png')
imgCopy = img.copy()
Width = 1200
Height = 1464
x1 = 0
y1 = 0
cWidth = 3
cHeight = 3
convul = np.array([[0,0,-5],
[0,1,0],
[-5,0,0]])
summ = convul[2,2]+convul[2,1]+convul[2,0]+convul[1,2]+convul[1,1]+convul[1,0]+convul[0,2]+convul[0,1]+convul[0,0]
def convulute3x3(x,y):
global convul
global img,imgCopy, Width, Height, summ
i = x
j = y
if(i < 1 or i > Width-2 ):
return
elif(j < 1 or j > Height-2 ):
return
for c in range(3):
n11 = img[j-1,i-1,c]*convul[0,0]
n22 = img[j-1,i,c]*convul[1,0]
n33 = img[j-1,i+1,c]*convul[2,0]
n44= img[j,i-1,c]*convul[0,1]
n55 = img[j,i,c]*convul[1,1]
n66 = img[j,i+1,c]*convul[2,1]
n77 = img[j+1,i-1,c]*convul[0,2]
n88 = img[j+1,i,c]*convul[1,2]
n99 = img[j+1,i+1,c]*convul[2,2]
color = (n11+n22+n33+n44+n55+n66+n77+n88+n99)/summ
imgCopy[j,i,c] = color
for x in img:
x1=0
for y in x:
convulute3x3(x1,y1)
x1 = x1+1
y1 = y1+1
plt.imshow(imgCopy)
plt.show()
正如@Reti43在评论中提到的那样,已经存在这样做的库,但我怀疑您只是想玩一些自制的实现
我也对如何在Python中手动实现卷积感兴趣。Python循环非常慢,如果您关心速度,那么应该远离纯粹的Python循环,而是坚持使用更矢量化的方法
到目前为止,我最好的方法是使用
numpy.lib.stride_tricks.as_strided
,它允许您获得非常定制的numpy数组视图。我使用as_strided
获得图像的滑动窗口视图,然后使用np.tensordot
对内核执行“更一般的矩阵乘法”(docs)。此外,NUMPY1.20(iirc)有numpy.lib.stride_tricks.sliding_window_view
,这是我下面代码的一个不太通用的版本(截至目前),因为它不能进行自定义跨步奖金
我实现了一个ascii可视化功能来检查滑动窗口是否正确:
相关问题 更多 >
编程相关推荐