更快地计算大型阵列的平均值和标准差

发布于 2025-01-09 12:01:30 字数 1171 浏览 1 评论 0原文

我正在做一些图像处理,我需要做的一件事是计算 5x5 像素正方形的平均值和标准偏差,并将这些值分配给与 5x5 正方形的中心像素具有相同索引的数组(平均值)标准差可以进入单独的二维数组)。目前我这样做的方式是:

for i in range(2, num_rows+2, 1):
    for j in range(2, num_cols+2, 1):
        #enter pixel values of 5x5 cluster into a list
        pixel_list = [padded_grey[i-2,j-2],padded_grey[i-2,j-1],padded_grey[i-2,j],padded_grey[i-2,j+1],padded_grey[i-2,j+2],padded_grey[i-1,j-2],padded_grey[i-1,j-1],padded_grey[i-1,j],padded_grey[i-1,j+1],padded_grey[i-1,j+2],padded_grey[i,j-2],padded_grey[i,j-1],padded_grey[i,j+1],padded_grey[i,j+2],padded_grey[i+1,j-2],padded_grey[i+1,j-1],padded_grey[i+1,j],padded_grey[i+1,j+1],padded_grey[i+1,j+2],padded_grey[i+2,j-2],padded_grey[i+2,j-1],padded_grey[i+2,j],padded_grey[i+2,j+1],padded_grey[i+2,j+2],padded_grey[i,j]]

        #calculate mean and standard deviation of values in list and enter into index of central pixel
        mean_vals[i-2,j-2] = np.mean(pixel_list)
        stddev_vals[i-2,j-2] = np.std(pixel_list)

for 循环移动 2 个索引的原因是数组 padded_grey 是零填充的,以防止“当我靠近阵列边缘时,出现“超出范围”错误。当然,这需要很长时间才能完成执行,因为我必须逐像素地遍历整个 1024x1280 数组。

有人可以建议我优化它以使其运行得更快吗?先感谢您。

I am doing some image processing and one of the things I need to do is calculate the mean and standard deviations of 5x5 squares of pixels and assign those values to an array with the same index as the central pixel of the 5x5 square (the mean values and standard deviations can go to separate 2D arrays). At the moment the way I'm doing this is:

for i in range(2, num_rows+2, 1):
    for j in range(2, num_cols+2, 1):
        #enter pixel values of 5x5 cluster into a list
        pixel_list = [padded_grey[i-2,j-2],padded_grey[i-2,j-1],padded_grey[i-2,j],padded_grey[i-2,j+1],padded_grey[i-2,j+2],padded_grey[i-1,j-2],padded_grey[i-1,j-1],padded_grey[i-1,j],padded_grey[i-1,j+1],padded_grey[i-1,j+2],padded_grey[i,j-2],padded_grey[i,j-1],padded_grey[i,j+1],padded_grey[i,j+2],padded_grey[i+1,j-2],padded_grey[i+1,j-1],padded_grey[i+1,j],padded_grey[i+1,j+1],padded_grey[i+1,j+2],padded_grey[i+2,j-2],padded_grey[i+2,j-1],padded_grey[i+2,j],padded_grey[i+2,j+1],padded_grey[i+2,j+2],padded_grey[i,j]]

        #calculate mean and standard deviation of values in list and enter into index of central pixel
        mean_vals[i-2,j-2] = np.mean(pixel_list)
        stddev_vals[i-2,j-2] = np.std(pixel_list)

The reason why the for loops are shifted over by 2 indices is that the array padded_grey is zero-padded to prevent "Out of Range" errors when I am near the edge of the array. Now of course this takes forever to finish executing since I have to go through an entire 1024x1280 array pixel by pixel.

Could someone please suggest some ways I could optimise this to run faster? Thank you in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬花如无物 2025-01-16 12:01:30

使用sliding_window_view

window_view = np.lib.stride_tricks.sliding_window_view(pixel_array, (5, 5))

window_view_mean = window_view.mean(axis=(2, 3))
window_view_std = window_view.std(axis=(2, 3))

Use sliding_window_view:

window_view = np.lib.stride_tricks.sliding_window_view(pixel_array, (5, 5))

window_view_mean = window_view.mean(axis=(2, 3))
window_view_std = window_view.std(axis=(2, 3))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文