问题
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...?
回答1:
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.
来源:https://stackoverflow.com/questions/38320316/do-i-have-to-create-a-whole-new-array-to-store-results-from-convolution