2

can u please help me understand reshaping a matrix with the *operator? Tried googling and check the documentation of reshaping but couldnt find anything using it with *:

matrix = matrix.reshape(*matrix.shape, 1)

Following up that question can one of you help me understand the use of the line in this code?

gt_bg = np.all(image == background_color, axis=2)
gt_bg = gt_bg.reshape(*gt_bg.shape, 1)
n_image = np.concatenate((gt_bg, np.invert(gt_bg)), axis=2)

So at first it searches the given image where it has the same coloring as the background_color variable(e.g [255,0,0]). It returns an array with shape of the image, so for example (1920,1080) and the values are either True of False. After the second line gt_bg has the shape (1920,1080,1), after the third line (1920,1080,2). The values of that matrix is True and False for every pixel i think? so if [1655,555,0] is True [1655,555,1] is False? But about that im not sure... And this is used for training a Convolutional Neural Network for image segmentation but I just cant get why i would create n_image...

Looking forward to some help...

Cheers, Felix

  • Possible duplicate of [What does asterisk \* mean in Python?](https://stackoverflow.com/questions/400739/what-does-asterisk-mean-in-python) – Joe Feb 28 '18 at 12:57
  • Duplicate: https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters or https://stackoverflow.com/questions/400739/what-does-asterisk-mean-in-python – Joe Feb 28 '18 at 12:57

1 Answers1

-1

The * operator used as described above is considered a starred expression.

Please find below its usage:

If the syntax *expression appears in the function call, expression must evaluate to an iterable. Elements from these iterables are treated as if they were additional positional arguments. For the call f(x1, x2, *y, x3, x4), if y evaluates to a sequence y1, …, yM, this is equivalent to a call with M+4 positional arguments x1, x2, y1, …, yM, x3, x4.

More information can be found here:

https://docs.python.org/dev/reference/expressions.html#calls

Hope it helps!