我是否必须创建一个全新的数组来存储卷积的结果? [英] Do I have to create a whole new array to store results from convolution?

查看:91
本文介绍了我是否必须创建一个全新的数组来存储卷积的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python进行卷积算法,我注意到在沿原始数组滑动过滤器并更新其中的条目时,结果变得很模糊:

I was playing with my convolution algorithm in Python and I noticed that while sliding the filter along the original array and updating entries therein, the result came out quite murky:

而如果我创建了一个全新的数组,它的输出类似于

whereas if I created a totally new array it came out with levels similar to the original.

我的愚蠢问题-是后者是编写此算法的正确方法(我猜是这样)?前者所丢失的是-或者,有一种方法可以编写此算法,从而不必初始化另一个整个数组并吞噬内存,而是将结果存储在原始数组中或... ?

My silly question - is the latter the right way to write this algorithm (I'm guessing it is)? What is lost in the former - or rather, is there a way that I can write this algorithm so that I don't have to initialize another whole array and gobble up memory, but rather store the results in the original array or...?

推荐答案

您应该使用第二个数组来存储结果。否则,大多数计算将基于已经更改的像素,而不是图像中的原始像素。这就是为什么您的第一个示例更改超出预期的原因。

You should use a second array to store the result. Otherwise you are basing most of your calculations on pixels you have already changed instead of on the original pixels that were in the image. That's why your first example changes more than you expect.

技术上,您可以使用线程来完成此操作而无需第二个数组。您只需要具有与像素一样多的线程即可。然后,每个线程计算一个像素并将其存储回图像中。但是,这将比仅使用第二个数组占用更多的内存,并且您必须仔细地进行同步,以避免修改仍需要计算的像素。而且,它会变慢。

Technically you can do it without a second array by using threads. You just need to have as many threads as you have pixels. Then each thread calculates one pixel and stores it back in the image. This will take even more memory than just using a second array, however, and you have to carefully synchronize things to avoid modifying pixels you still need for calculations. Also, it's going to be slower.

这篇关于我是否必须创建一个全新的数组来存储卷积的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆