Home > Net >  Numpy matrix is reseting the values inside it
Numpy matrix is reseting the values inside it

Time:11-16

I am implementing a co-occurrence matrix for an image to be able to detect the edges of an image through the change in brightness. So I made a 256x256 numpy matrix to store the co-occurrences, and then I wrote a function that turns all of the values of occurrences in the matrix to 0 if the change between them is less than a certain value like 30, ie the difference between the i and j of the matrix is less than 30 then the value inside that cell is turned into 0.

Here is the function, it takes the co-occurrence matrix and turn the values into 0.

def nullify(matrix):
   for i in range (0,matrix.shape[0]):
      for j in range(0,matrix.shape[1]):
          if(abs(i-j)<30):
             matrix[i,j]=0
    return matrix

But for some reason it turn the entire matrix into 0's, the function work perfectly when I'm using a smaller matrix like a 3x3.

This is the function that I use to calculate the Cooccurrence

def calculateCooccurrence(im):
    Horizontal = np.zeros((256, 256))
    
    for i in range (0,im.size[0]):
        for j in range (0,im.size[1]-1):
            pixelRGB = im.getpixel((i,j))
            R,G,B = pixelRGB 
            brightness = int(sum([R,G,B])/3)
            pixelRGB1 = im.getpixel((i,j 1))
            R1,G1,B1 = pixelRGB 
            brightness1 = int(sum([R1,G1,B1])/3)
            Horizontal[brightness,brightness1] =1
            
    Vertical = np.zeros((256, 256))
    for i in range (0,im.size[0]-1):
        for j in range (0,im.size[1]):
            pixelRGB = im.getpixel((i,j))
            R,G,B = pixelRGB 
            brightness = int(sum([R,G,B])/3)
            pixelRGB1 = im.getpixel((i 1,j))
            R1,G1,B1 = pixelRGB 
            brightness1 = int(sum([R1,G1,B1])/3)
            Vertical[brightness,brightness1] =1
            
    return Horizontal,Vertical

And this is what I do exactly

horiz,vertic=calculateCooccurrence(im2)
horizon=nullify(horiz)

CodePudding user response:

there are some things to point about your code. I can't tell why your entire matrix turns into zeros, but these are things that should help you:

  1. This might be due to formatting in stackoverflow, but your matrix is returned after the first iteration of the i-loop.
  2. You actually are not working on the values of the matrix. You iterate over the range 0...256. This will set all values close to the diagonal to 0. Based on your text where you say that you want to detect the edges, I am not sure if this is what you actually want to do.
  3. instead of creating a variable named difference you can also put it simply in the if-statement if(abs(i-j)<30:

Edit: Found the problem: Your code works intended, which is the problem. All the elements are on the diagonal. I just used a test image myself and found that np.sum(matrix) and np.trace(matrix) returned the same result. So when your code eliminates all elements along the diagonal, it turns all elements to 0.

CodePudding user response:

Your code seems to work properly for Python 3.7.7 and NumPy 1.18.1:

>>> import numpy as np
>>> matrix = np.ones((256, 256))
>>> def nullify(matrix):
...    for i in range (0,matrix.shape[0]):
...       for j in range(0,matrix.shape[1]):
...           if(abs(i-j)<30):
...              matrix[i,j]=0
...    return matrix
... 
>>> nullify(matrix)
array([[0., 0., 0., ..., 1., 1., 1.],
       [0., 0., 0., ..., 1., 1., 1.],
       [0., 0., 0., ..., 1., 1., 1.],
       ...,
       [1., 1., 1., ..., 0., 0., 0.],
       [1., 1., 1., ..., 0., 0., 0.],
       [1., 1., 1., ..., 0., 0., 0.]])

You can see that the elements far away from the diagonal remain unchanged. You should provide a minimal working example of what goes wrong in your code.

  • Related