Search code examples
pythonnumpyvalueerror

Numpy greenkey : operands could not be broadcast together with shapes


I'm trying to create the simplest green screen algorithm. I have already generated "key" array which is shaped (1920,1080) and contains booleans. Just using foreground*key + background*inverted_key doesn't work because foreground and background are shaped (1920,1080,3) so it raises Value error: operands could not be broadcast together with shapes (1920,1080,3) (1920,1080). How then can I do this operation? Also, I've tried this with arrays shaped (3,3,3) and (3,3) - and it worked just fine. Explain please what's happening, I'm confused.


Solution

  • Python broadcasting rules are very simple:

    1. add leading 1's to the shape of array with smaller number of dimensions
    2. scale up all 1's to match the dimension in other array
    3. after the first two steps all dimension have to match

    So when you multiply (3, 3, 3) and (3, 3), first the second array is extended with one more dimension (1, 3, 3) and then all 1's are scaled to match meaning that you multiply (3, 3, 3) by (3, 3, 3) in the end. When you multiply (1920, 1000, 3) by (1920, 1000) the second array is extended to (1, 1920, 1000) then 1's are scaled up, so in the end you are trying to multiply (1920, 1000, 3) by (1920, 1920, 1000) hence the error.

    What you can do is this:

    key3dim = np.tile(key.reshape(key.shape[0], key.shape[1], 1), 3)
    # or
    key3dim = np.repeat(key.reshape(key.shape[0], key.shape[1], 1), 3, 2)
    # or
    key3dim = key.reshape(key.shape[0], key.shape[1], 1).repeat(3, 2)
    foreground*key3dim + background*~key3dim
    

    examples of broadcasting here