0

I have a numpy array of size 8x8. Here is the numpy array:

QuantTable = np.array([[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, 29, 51, 87, 80, 62],
                    [24, 35, 55, 64, 81, 109, 103, 77],
                    [49, 64, 78, 87, 103, 121, 120, 101],
                    [72, 92, 95, 98, 112, 100, 103, 99]])

I would like to perform the operations on the elements in the array. I have created a function that accepts a scaling factor value and a Numpy Array. Here it is:

def quantizationTable(Qval, QuantTable):
    if Qval < 50:
        scalingFactor = 5000/Qval
        for x in range(QuantTable):
            for y in range(QuantTable):
                QuantTable[x][y] = ((scalingFactor * QuantTable[x][y] + 50/100)
                if QuantTable[x][y] == 0:
                    QuantTable[x][y] = 1
    return QuantTable
    else:
        scalingFactor = 200 - 2(Qval)
        for x in range(QuantTable):
            for y in range(QuantTable):
                QuantTable[x][y] = ((scalingFactor * QuantTable[x][y] + 50/100)
                if QuantTable[x][y] == 0:
                    QuantTable[x][y] = 1

return QuantTable

I am having trouble iterating over the numpy array and performing my operations. I am trying to apply the formula ((Scaling factor value * element of numpy array + 50)/100) to every element of the numpy array and return the modified array. Can someone please help?

7
  • I am having trouble iterating over the numpy array and performing my operations. - Elaborate? Commented Aug 25, 2017 at 16:43
  • 2
    Why the loops? return scalingFactor * QuantTable + 50/100 Commented Aug 25, 2017 at 16:45
  • Because I need to perform the operation on each and every element. Please correct me if I am wrong in my logic itself. Thanks. Commented Aug 25, 2017 at 16:46
  • 1
    You don't need to iterate yourself, numpy can do this much faster than any loop in Python could do. Commented Aug 25, 2017 at 16:50
  • 1
    @SanketWagh no - indeed, using loops is the inadvisable way of achieving what you want. The whole point of numpy arrays is that they allow vectorized operations that are fast and efficient. Commented Aug 25, 2017 at 16:54

1 Answer 1

3

Just remove the loops, and the indexing. Numpy automatically broadcasts those operations. Also, a lot of your code can be taken out of the if...else statements.

def quantizationTable(Qval, QuantTable):
    QuantTable = np.asarray(QuantTable, dtype=np.float32)
    if int(Qval) < 50:
        scalingFactor = 5000 / Qval
    else:
        scalingFactor = 200 - 2 * Qval # confirm that this is what you want? 

    QuantTable *= scalingFactor + 0.5
    QuantTable[QuantTable == 0] = 1

    return QuantTable
Sign up to request clarification or add additional context in comments.

4 Comments

You need to be careful with in-place operations. If QuantTable is an integer array that would lead to truncation (because scaling is a float).
@MSeifert Oh, good point. I should probably add a np.asarray(..., dtype) cast.
scalingFactor = 200 - 2 * Qval -> This is what I want.
@COLDSPEED I used this answer and modified it a little. It works like a charm. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.