我有一个场景的深度图像。我遍历该图像并计算检测窗口下深度的平均变化量。检测窗口的大小根据当前位置周围像素的平均深度而改变。我累积平均变化来生成一个简单的响应图像。
在我的机器上,对于512x52的图像,大部分时间都花费在for循环中,大约40秒左右。我希望能加快速度。是否有更有效/更快的遍历图像的方法?是否有更好的Pythonic/Numpy/Scipy方法来访问每个像素?还是我应该去学习Cython?
编辑:通过使用scipy.misc.imread()而不是skimage.io.imread(),我将运行时间缩短到约18秒。不确定两者之间的区别,我将尝试进行调查。
下面是代码的简化版本:
在我的机器上,对于512x52的图像,大部分时间都花费在for循环中,大约40秒左右。我希望能加快速度。是否有更有效/更快的遍历图像的方法?是否有更好的Pythonic/Numpy/Scipy方法来访问每个像素?还是我应该去学习Cython?
编辑:通过使用scipy.misc.imread()而不是skimage.io.imread(),我将运行时间缩短到约18秒。不确定两者之间的区别,我将尝试进行调查。
下面是代码的简化版本:
import matplotlib.pylab as plt
import numpy as np
from skimage.io import imread
from skimage.transform import integral_image, integrate
import time
def intersect(a, b):
'''Determine the intersection of two rectangles'''
rect = (0,0,0,0)
r0 = max(a[0],b[0])
c0 = max(a[1],b[1])
r1 = min(a[2],b[2])
c1 = min(a[3],b[3])
# Do we have a valid intersection?
if r1 > r0 and c1 > c0:
rect = (r0,c0,r1,c1)
return rect
# Setup data
depth_src = imread("test.jpg", as_grey=True)
depth_intg = integral_image(depth_src) # integrate to find sum depth in region
depth_pts = integral_image(depth_src > 0) # integrate to find num points which have depth
boundary = (0,0,depth_src.shape[0]-1,depth_src.shape[1]-1) # rectangle to intersect with
# Image to accumulate response
out_img = np.zeros(depth_src.shape)
# Average dimensions of bbox/detection window per unit length of depth
model = (0.602,2.044) # width, height
start_time = time.time()
for (r,c), junk in np.ndenumerate(depth_src):
# Find points around current pixel
r0, c0, r1, c1 = intersect((r-1, c-1, r+1, c+1), boundary)
# Calculate average of depth of points around current pixel
scale = integrate(depth_intg, r0, c0, r1, c1) * 255 / 9.0
# Based on average depth, create the detection window
r0 = r - (model[0] * scale/2)
c0 = c - (model[1] * scale/2)
r1 = r + (model[0] * scale/2)
c1 = c + (model[1] * scale/2)
# Used scale optimised detection window to extract features
r0, c0, r1, c1 = intersect((r0,c0,r1,c1), boundary)
depth_count = integrate(depth_pts,r0,c0,r1,c1)
if depth_count:
depth_sum = integrate(depth_intg,r0,c0,r1,c1)
avg_change = depth_sum / depth_count
# Accumulate response
out_img[r0:r1,c0:c1] += avg_change
print time.time() - start_time, " seconds"
plt.imshow(out_img)
plt.gray()
plt.show()