Spatial Filtering 2d numpy array with a 3x3 mask

Click For Summary

Discussion Overview

The discussion revolves around filtering a 2D numpy array using a 3x3 mask, specifically in the context of image processing with matrices derived from DICOM files. Participants are focused on the syntax differences between SciLab and Python for implementing this filtering operation.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their approach in SciLab for applying a 3x3 mask to a large 2D matrix and seeks help with the equivalent Python syntax.
  • Another participant suggests a Python syntax for the filtering operation, emphasizing the importance of correctly indexing rows and columns, and noting that Python lists start at index 0.
  • A later reply questions whether the `ndimage.convolve` method from SciPy performs the filtering in a similar manner, suggesting it could simplify the implementation to a single line of code.
  • Another participant acknowledges the suggestion about `ndimage.convolve` but admits to not having used it, encouraging the original poster to try it out.

Areas of Agreement / Disagreement

Participants generally agree on the need for correct syntax in Python but do not reach a consensus on the best approach to implement the filtering, as there are multiple proposed methods (manual implementation vs. using `ndimage.convolve`).

Contextual Notes

There is an assumption that the filtering operation requires careful handling of matrix indices, and the discussion does not resolve whether the `ndimage.convolve` method is equivalent to the manual approach described.

ProPatto16
Messages
323
Reaction score
0
I have large 2d matrices from dicom files that i wish to filter with a 3x3 mask. the image arrays are of varying size and are padded with one border of zeros for the edge handling of the mask. i need to iterate over every element in the array and multiply it by the mask. I've done it in SciLab but so far in python i have the padded image matrix and the 3x3 mask ready to go i just need to get the syntax right.

g is the image matrix usually about 600 by 800, i.e. not square
w is the 3x3 filter

the function is SciLab is as follows

Code:
  for i=1:m;
        for j=1:n;
               g(i,j)= g(i,j)*w(1,1)+g(i+1,j)*w(2,1)+g(i+2,j)*w(3,1)...
               +g(i,j+1)*w(1,2)+g(i+1,j+1)*w(2,2)+g(i+2,j+1)*w(3,2)...
               +g(i,j+2)*w(1,3)+g(i+1,j+2)*w(2,3)+g(i+2,j+2)*w(3,3);
        end
  end

i, j and m,n refers to the array elements of the image matrix and the filter but it doesn't work like that in python.

Im just having trouble with the syntax conversions.

Thanks
 
Technology news on Phys.org
You'll need something like

Python:
for i in range(1,m):
    for j in range(1,n):
         g[i][j] = g[i][j] * w[1][1]+g[i+1][j]* etc etc etc

couple things to check when you do this,
make sure you haven't mixed up your rows/columns. I've done that before and its easy to miss (I mean make sure it shouldn't be g[ i])
Also the lists in python generally start at 0, so you may want to have range(0, m) instead of range(1,m)

Hope that helps
 
Last edited by a moderator:
Hey,
thanks, that will help..
just out of curiosity... does the ndimage.convolve method from scipy do the filtering in the same way? that would make it a one liner...
 
ProPatto16 said:
Hey,
thanks, that will help..
just out of curiosity... does the ndimage.convolve method from scipy do the filtering in the same way? that would make it a one liner...
Never used it. Try it out and see if it does :)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
6K
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K
Replies
2
Views
2K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 11 ·
Replies
11
Views
3K