How it works...
The last dimension of the matrix is responsible for channels. That's why we're manipulating it in the code.
To swap channels, we should get access to the corresponding slices of our matrix. But slices aren't copies of the original matrix, they're just different views of the same data. This means we can't perform swaps through temporary variables as we do with plain types. We need something more complicated here, and NumPy allows us to get not only a single slice, but a bunch of slices as new views of the data. To do so, we should enumerate indexes for all needed slices in the desired order, instead of just a single index.
When we use a single index, we get an access to the corresponding channel, and we can perform some algebraic ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access