使用for循环对图像进行多次卷积



下面是一个python代码,对图像执行10次卷积

import cv2
import numpy as np
from skimage.exposure import rescale_intensity
kernel = np.ones((5,5),np.float32)/25
img = cv2.imread(r"C:/Users/engjb/.spyder-py3/examples/1.jpeg")


opencvOutput1 = cv2.filter2D(img, -1, kernel)

opencvOutput2 = cv2.filter2D(opencvOutput1, -1, kernel)
opencvOutput3 = cv2.filter2D(opencvOutput2, -1, kernel)
opencvOutput4 = cv2.filter2D(opencvOutput3, -1, kernel)
opencvOutput5 = cv2.filter2D(opencvOutput4, -1, kernel)
opencvOutput6 = cv2.filter2D(opencvOutput5, -1, kernel)
opencvOutput7 = cv2.filter2D(opencvOutput6, -1, kernel)
opencvOutput8 = cv2.filter2D(opencvOutput7, -1, kernel)
opencvOutput9 = cv2.filter2D(opencvOutput8, -1, kernel)
opencvOutput10 = cv2.filter2D(opencvOutput9, -1, kernel)
cv2.imshow("res",opencvOutput10)
cv2.waitKey(0)
cv2.destroyAllWindows()

我怎么能做到这一点使用一个单一的for循环。(我是python的新手)我知道这一定很容易,但是我就是无法跳出常规

我的概念代码:

for i in rage(0,11):
output = cv2.filter2D(img, -1, kernel)

然而,这在每次迭代中对相同的源图像执行卷积,而我希望在每次迭代的输出上执行卷积

img = cv2.imread( ... )
for i in range(10):
print(i)
img = cv2.filter2D(img, -1, kernel)
注意,这和赋值 是完全不同的
output = cv2.filter2D(img, ... )

,因为我们希望下一个迭代使用输出。不把输出作为下一个输入,你只需要做一个卷积。你的速度会慢10倍,无用地计算相同的结果另外九次,每次都丢弃(覆盖)有效的内容。

最新更新