Numpy flipped coordinate system - python

I used opencv to do some image processing. I tried to then plot the image on my plotter (origin at lower left), however the image is flipped. opencv's origin is in the upper left, and so the y coordinates of the image are flipped.
What function should I apply to my points such that it will plot properly in the new origin system (lower left)?
EDIT:
I am not concerned with changing the plot display, I actually need the points' coordinates flipped.
Using np.flipud did not change the points at all since the point are displayed by a N X 2 matrix.

The problem does not lie in numpy but in matplotlib way of displaying data. In order to produce valid visualization you should flip y-axis on the image generation level, not numpy analysis. It can be easily done through matplitlib API to the axes object:
plt.gca().invert_yaxis()

Are you asking how to flip a numpy array or how to display it?
If you're asking how to flip the array, have a look at np.flipud or equivalently your_data[::-1, ...]
numpy.flipud is a function that uses the exact slicing shown above. However, it's more readable if you're not familiar with numpy slicing.
To explain why data[::-1, ...] will flip the array vertically, you need to understand a bit about how indexing works.
In general, indexing in python works by specifying start:stop:step. Each of these may be None (e.g. :-10 specifies start=None, stop=-10, step=None).
Therefore, ::-1 specifies start=None, stop=None, step=-1 -- in other words, go over the full sequence, but increment with a negative step, effectively reversing the sequence.
... is an Ellipsis. In numpy, this is used to indicate including all other dimensions.
The ellipsis avoids the need to special case your array being 2D or 3D (or 27-dimensional, for that matter). If it's a 2D array, then x[::-1, ...] is equivalent to x[::-1, :]. If it's a 3D array, it's equivalent to x[::-1, :, :], etc.
In numpy, the first axis is rows. Therefore, x[::-1, ...] says "reverse the rows and leave all other dimensions alone. This will create a view, so the memory wont' be duplicated and no copy will be created.
In the specific case of rows, you could leave the ellipsis out. However, it's useful to think about for the general case. For example, flipping left-right would be x[:, ::-1, ...] (or np.fliplr).

Related

Applying a permutation along one axis in TensorFlow

How to permute "dimensions" along a single axis of a tensor?
Something akin to tf.transpose, but at the level of "dimensions" along an axis, instead of at the level of axes.
To permute them randomly (along the first axis), there it tf.random.shuffle, and to shift them, there is tf.roll. But I can't find a more general function that would apply any given permutation.
tf.gather can be used to that end. In fact, it is even more general, as the indices it takes as one of its inputs don't need to represent a permutation.

1D plots from 3D array

I have a 3D data cube and I am trying to make a plot of the first axis at a specific value of the other two axes. The goal is to make a velocity plot at given coordinates in the sky.
I have tried to create an 1D array from the 3D array by putting in my values for the last two axes. This is what I have tried
achan=50
dchan = 200
lmcdata[:][achan][dchan] #this array has three axes, vchan, achan, dchan.
I am expecting an array of size 120 as there are 120 velocity channels that make up the vchan axis. When trying the code above I keep getting an array of size 655 which is the number of entries for the dchan axis.
Python slicing works from left to right. In this case, lmcdata[:] is returning the whole lmcdata list. So, lmcdata[:][achan][dchan] is equivalent to just lmcdata[achan][dchan].
For higher level indexing and slicing tasks like this, I highly recommend the numpy package. You will be able to slice lmcdata as expected after turning it into a numpy array: lmcdata = np.asarray(lmcdata).

Seamlessly and efficiently flipping numpy array or sparse matrix along all axes

Let's say we have a matrix (numpy array) of unknown shape, the shape can be for example (1,5) (row), (5,1) (column), (5,5) (square), (5,6) (non-square) or (5,) (degenerated) (ok the last case isn't a matrix but is a valid input).
I would like to given a matrix of any shape (column, row, square, nonsquare, degenerated). I will return a flipped up/down left/right version of it.
Since np.flip has some issues with 1d arrays. My approach was:
def flipit(M):
return M.ravel()[::-1].reshape(M.shape)
It works, but is that acceptable? Any faster ways to do it?
In the other hand, how can I do the same for sparse matrices (for example if M is scipy.sparse.csr_matrix).
We can use slice notation with a step-size of -1 for the number of dims in the input to flip along all the axes, as that's what the original code is essentially doing. This would cover both arrays and sparse matrices -
def flip_allaxes(a): # a can be array or sparse matrix
# generate flipping slice
sl = slice(None,None,-1) # or np.s_[::-1] suggested by #kmario23
return a[tuple([sl]*a.ndim)]
Simplified on newer NumPy versions (15.1 onwards)
On newer NumPy versions : Version 15.1 and newer, that allows us to specify tuple of ints for the axes along which the flipping is needed. For the default case with axis=None from the docs, it flips along all axes. Thus, to solve our case, it would be simply np.flip(a) and this would again cover both generic ndarrays and sparse matrices.

Efficiently Using Multiple Numpy Slices for Random Image Cropping

I have a 4-D numpy array, with the first dimension representing the number of images in a data set, the second and third being the (equal) width and height, and the 4th being the number of channels (3). For example let's say I have 4 color images that are 28*28, so my image data looks like this:
X = np.reshape(np.arange(4*28*28*3), (4,28,28,3))
I would like to select a random 16*16 width x height crop of each of the 4 images. Critically, I want the crop to be different per-image, i.e I want to generate 4 random (x_offset, y_offset) pairs. In the end I want access to an array of shape (4, 16, 16, 3).
If I were to write this in a for loop it would look something like this:
x = np.random.randint(0,12,4)
y = np.random.randint(0,12,4)
for i in range(X.shape[0]):
cropped_image = X[i, x[i]:x[i]+16, y[i]:y[i]+16, :]
#Add cropped image to a list or something
But I'd like to do it as efficiently as possible and I'm wondering if there's a way to do it with strides and fancy indexing. I've seen the answers to this question, but can't quite wrap my head around how I might combine something like stride_tricks with random starting points for the strides on the second and third (width and height) axes.
Leverage strided-based method for efficient patch extraction
We can leverage np.lib.stride_tricks.as_strided based scikit-image's view_as_windows to get sliding windows that would be merely views into the input array and hence incur no extra memory overhead and virtually free! We can surely use np.lib.stride_tricks.as_strided directly, but the setup work required is hard to manage especially on arrays with higher dimensions. If scikit-image is not available, we can directly use the source code that works standalone.
Explanation on usage of view_as_windows
The idea with view_as_windows is that we feed in the input arg window_shape as a tuple of length same as the number of dimensions in the input array whose sliding windows are needed. The axes along which we need to slide are fed with the respective window lengths and rest are fed with 1s. This would create an array of views with singleton dims/axes i.e. axes with lengths=1 corresponding to the 1s in window_shape arg. So, for those cases we might want to index into the zeroth element corresponding to the axes that are fed 1 as the sliding window lengths to have a squeezed version of the sliding windows.
Thus, we would have a solution, like so -
# Get sliding windows
from skimage.util.shape import view_as_windows
w = view_as_windows(X, (1,16,16,1))[...,0,:,:,0]
# Index and get our specific windows
out = w[np.arange(X.shape[0]),x,y]
# If you need those in the same format as in the posted loopy code
out = out.transpose(0,2,3,1)

Changing colorspaces with numpy.tensordot

I have an image I've read from file with shape (m,n,3) (i.e. it has 3 channels). I also have a matrix to convert the color space with dimensions (3,3). I've already arrived at a few different ways of applying this matrix to each vector in the image; for example,
np.einsum('ij,...j',transform,image)
appears to make for the same results as the following (far slower) implementation.
def convert(im: np.array, transform: np.array) -> np.array:
""" Convert an image array to another colorspace """
dimensions = len(im.shape)
axes = im.shape[:dimensions-1]
# Create a new array (respecting mutability)
new_ = np.empty(im.shape)
for coordinate in np.ndindex(axes):
pixel = im[coordinate]
pixel_prime = transform # pixel
new_[coordinate] = pixel_prime
return new_
However, I found that the following is even more efficient while testing on the example image with line_profiler.
np.moveaxis(np.tensordot(transform, X, axes=((-1),(-1))), 0, 2)
The problem I'm having here is using just a np.tensordot, i.e. removing the need for np.moveaxis. I've spent a few hours attempting to find a solution (I'm guessing it resides in choosing the correct axes), so I thought I'd ask others for help.
You can do it concisely with tensordot if you make image the first argument:
np.tensordot(image, transform, axes=(-1, 1))
You can get better performance from einsum by using the argument optimize=True (requires numpy 1.12 or later):
np.einsum('ij,...j', transform, image, optimize=True)
Or (as Paul Panzer pointed out in a comment), you can simply use matrix multiplication:
image # transform.T
They all take about the same time on my computer.

Categories