0

I have a problem concerning dividing matrices element wise, whereby I mean that I want element [i,j] of the first matrix(foto_dcp, see code) to get divided by element[i,j] of the second matrix(Q) .

Some background information : I loaded in an image from my storage. I stored the monochrome values of each pixel in a matrix called "pixelMatrix" This command turns the big matrix (of 128x128) into smaller ones (of 8x8)

foto_dct = skimage.util.view_as_blocks(pixelMatrix, block_shape=(8, 8))

Now, after doing this, I need to divide each matrix in foto_dct by a different matrix (called 'Q' in this code) elementwise.

for x in foto_dct:
   for i in range(8):
      for j in range(8):
          x[i,j] = x[i,j] / Q[i,j]

problem is that I get incorrect results. This is the matrix 'Q':

[[ 16  11  10  16  24  40  51  61]
 [ 12  12  14  19  26  58  60  55]
 [ 14  13  16  24  40  57  69  56]
 [ 14  17  22  29  51  87  80  62]
 [ 18  22  37  56  68 109 103  77]
 [ 24  35  55  64  81 104 113  92]
 [ 49  64  78  87 103 121 120 101]
 [ 72  92  95  98 112 100 103  99]]

This is an example matrix ( foto_dct[3,3], though I have done some operations on it, the 3rd column of matrices, the 3rd row of matrices, if you remember from step 1.)

[[613 250 -86  64 -63  59 -44  24]
 [ 38 -84  50 -57  54 -47  35 -19]
 [-16   4  -4   7  -5   4  -3   1]
 [ 10 -18  19 -20  21 -20  16  -9]
 [-17  19 -18  16 -14  11  -7   4]
 [ -7   9 -10  12 -13  11  -9   5]
 [-11  15 -14  15 -14  11  -9   5]
 [ -1   2  -4   5  -5   4  -3   2]]

And this is what I get after division:

[[43 17 -6  4 -4  4 -3  1]
 [ 2 -4  2 -3  3 -2  2 -1]
 [ 0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0]
 [ 0  0  0  0  0  0  0  0]]

As you can see, for instance take element [0,0] = 613, so after division we should get [0,0] = 613/16 = 38.3125 (As you can see it also automaticaly rounds up?) By the way, I tried changing

x[i,j] = x[i,j] / Q[i,j]

by

x[i,j] = x[i,j] / 2

and got correct (though rounded) results. So it's something with Q[i,j]


I've also tried to do it this way:

for x in foto_dct:
    x = np.divide(x,Q)

But this returns me the original matrix,without doing anything, not even returning an error code, eventhough it should divide elementwise. Can anyone help me out?

Marius.db
  • 15
  • 1
  • 6
  • In the numpy example, you are not assigning the value back into `foto_dict`; the operation `np.divide()` is not mutating the actual elements inside the list but returning new ones. Regarding the first example, I suggest to simplify your testing. What happens if you just take a single 8x8 block, and try to divide by another 8x8 block elementwise? That is, not looking at a dict of the values but just two variables. Do you see any issues with that? What about if you create new arrays instead of mutating? – alkasm Dec 03 '18 at 18:59
  • 1
    Although the way this is written, you shouldn't be visiting the same index more than once, the fact that you're writing the result back to the original array makes me suspicious. Consider creating a third `(8,8)` array and writing the result to it. – Matthew Woodruff Dec 03 '18 at 18:59
  • @MatthewWoodruff But I have 256 8x8 matrices that each need to be divided by Q. I can't assign a third array for each and every one of them, can I? I'm quite the beginner in python – Marius.db Dec 03 '18 at 19:11
  • @AlexanderReynolds am I not? How would I have to store it back into a value? I thought the operation would take a matrix 'x' from foto_dct and replace itself with x/q. After that it should take a different 'x' and so on? – Marius.db Dec 03 '18 at 19:16
  • @Marius.db You could create a new dictionary, R. `for key in Q.keys(): R[key] = [[0 for h in range(8)] for k in range(8)]`. Then assign your results to `R[h,k]`. – Matthew Woodruff Dec 03 '18 at 19:41

0 Answers0