Spatial Filtering 2d numpy array with a 3x3 mask

In summary, the conversation discusses filtering large 2D matrices from DICOM files using a 3x3 mask in SciLab and Python. The function in SciLab is shown, but the syntax needs to be converted for Python. Suggestions are given, including using the ndimage.convolve method from scipy, which may simplify the process.
  • #1
ProPatto16
326
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
  • #2
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:
  • #3
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...
 
  • #4
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 :)
 

Related to Spatial Filtering 2d numpy array with a 3x3 mask

What is spatial filtering in 2d numpy arrays with a 3x3 mask?

Spatial filtering in 2d numpy arrays with a 3x3 mask is a technique used in image processing to remove noise and enhance image features by applying a mathematical operation to each pixel and its surrounding pixels within a 3x3 mask.

What is the purpose of using a 3x3 mask in spatial filtering?

The purpose of using a 3x3 mask in spatial filtering is to consider the immediate eight neighboring pixels of a central pixel and perform the filtering operation on them, resulting in a smoothing effect and reducing the impact of noise.

What is the difference between spatial filtering and spatial convolution?

Spatial filtering is a general term used to describe any operation performed on an image using a mask or kernel, while spatial convolution specifically refers to the process of applying a filter to each pixel and its neighboring pixels using a mathematical operation.

What are the common types of filters used in spatial filtering?

The common types of filters used in spatial filtering are Gaussian filter, median filter, mean filter, and Sobel filter. These filters differ in the type of mathematical operation used and the resulting effect on the image.

What is the role of numpy arrays in spatial filtering?

Numpy arrays are used in spatial filtering because they provide an efficient way to store and manipulate large amounts of data, such as images. The 2d numpy array structure allows for easy selection and manipulation of specific pixels and their neighboring pixels required for spatial filtering.

Similar threads

  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
977
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
15
Views
3K
  • Programming and Computer Science
Replies
14
Views
4K
  • Programming and Computer Science
Replies
4
Views
7K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top