Is there shorthand for getting the center pixels of an image? - numpy

Is there any indexing shorthand in numpy to get the center pixels of an image (or any ND array)?
Example:
cutout = xc[xc.shape[0]/2-30:xc.shape[0]/2+30,xc.shape[1]/2-30:xc.shape[1]/2+30]
I could define a function
def get_center_pixels(arr, npix):
slices = [slice(shape/2-npix,shape/2+npix) for shape in arr.shape]
return arr[slices]
cutout = get_center_pixels(xc,30)
Is there a better way or a built-in way to do this?

The closest standard function in numpy I can think of is numpy.fft.fftshift, which rolls the data along the selected axis, so that the center point now is at [0,0].

Related

How to fill a line in 2D image along a given radius with the data in a given line image?

I want to fill a 2D image along its polar radius, the data are stored in a image where each row or column corresponds to the radius in target image. How can I fill the target image efficiently? Such as with iradius or some functions? I do not prefer a pix-pix operation.
Are you looking for something like this?
number maxR = 100
image rValues := realimage("I(r)",4,maxR)
rValues = 10 + trunc(100*random())
image plot :=realimage("Ring",4,2*maxR,2*maxR)
rValues.ShowImage()
plot.ShowImage()
plot = rValues.warp(iradius,0)
You might also want to check out the relevant example code from the F1 help documentation of GMS itself:
Explaining warp a bit:
plot = rValues.warp(iradius,0)
Assigns values to plot based on a value-lookup in rValues.
For each pixel in plot a coordinate position in rValues is computed, and the value is simply looked up. If the computed coordinate is non-integer, bilinear interpolation between the 4 closest points is used.
In the example, the two 'formulas' for the coordinate calculation are simple x' = iradius and y' = 0 where iradius is an expression computed from the coordinate in plot, for convenience.
You can feed any expression into the parameters for warp( ) and the command is closely related to just using the square bracket notation of addressing values. In fact, the only difference is that warp performs the bilinear interpolation of values instead of truncating the coordinates to integer values.

Rotating series of polygons aswell as the envelope

I have a polygon (rectangle or very close it) in a geopandas dataframe that is at an angle relative the x-axis, i.e. it is neither horizontal not vertical. I have a function that splits polygons into smaller rectangles (isometric) but it only works (as desired) on polygon making an angle that is a multiple of pi/2 with the x-axis.
So, my idea has been to rotate any polygon that does not satisfy my requirements, split it and rotate it back to its original position.
For instance:
polygon =
id geometry
85 POLYGON ((49.37794 51.395203, 49.37794 51.395203, 49.37794 51.395203, 49.37794 51.395203, 49.178337 50.363914, 49.178337 50.363914, 49.178337 50.363914, 49.178337 50.363914, 59.99021 48.733814, 59.99021 48.733814, 59.99021 48.733814, 59.99021 48.733814, 60.223083 49.698566, 60.223083 49.698566, 60.223083 49.698566, 60.223083 49.698566, 49.37794 51.395203))
which looks like this:
Now, I determine its angle with the x-axis and rotate it:
polygon = pd.DataFrame(geostore_obstacles_geometry_polygon.loc[85:85,])
polygon['angle'] = polygon.apply(lambda row : polygon_angle(row['geometry']), axis = 1)
polygon = gpd.GeoDataFrame(polygon)
polygon = polygon.set_geometry('geometry')
polygon['rotated'] = polygon.apply(lambda row : shapely.affinity.rotate(row['geometry'], row['angle']), axis = 1)
polygon = polygon.set_geometry('rotated')
which gives:
This step splits the polygon inte smaller pieces:
polygon['add'] = polygon.apply(lambda row : split_polygon_up(row['rotated'],side_length=side_length, shape="square", thresh=threshold), axis = 1)
polygon = polygon.explode('add')
polygon = polygon.set_geometry('add')
Before I finally rotate it back
polygon['rotated_add'] = polygon.apply(lambda row : shapely.rotate(row['add'], -row['angle']), axis = 1)
polygon = polygon.set_geometry('rotated_add')
But, as you can imagine, this is not what I expect to have (sorry for the very uggly image).
I understand WHY it does this but I cannot solve it. I have some ideas that the one possible solution would be to rotate all the smaller polygons together with the convex hull or envelope of their union, but I struggle using geopandas to do it.
I would be immensely grateful for any help on how to solve this issue. The dataframe obtained after all the transformations can be found here: https://drive.google.com/file/d/1wY7g3jsD7PNpaTkGBjbGvYArpRUr0UIk/view?usp=sharing
The relevant function shapely.rotate() has origin='center' as its default option. To rotate around a particular point (x,y), you must specify explicitly with origin=(x,y).
In your particular case, the centroid of the original polygon is a good choice for (x,y).

python numpy/scipy zoom changing center

I have a 2D numpy array, say something like:
import numpy as np
x = np.random.rand(100, 100)
Now, I want to keep zoom this image (keeping the size the same i.e. (100, 100)) and I want to change the centre of the zoom.
So, say I want to zoom keeping the point (70, 70) at the centre and normally how one would do it is to "translate" the image to that point and then zoom.
I wonder how I can achieve this with scipy. I wonder if there is way to specify say 4 coordinates from this numpy array and basically fill the canvas with the interpolated image from this region of interest?
You could use ndimage.zoom to do the zooming part. I use ndimage a lot, and it works well and is fast. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.zoom.html
The 4 coordinates part you mention is I presume two corners of region you want to zoom into. That's easy by just using numpy slicing of your image (presuming your image is an np array):
your_image[r1:r2, c1:c2]
Assuming you want your output image at 100x100, then your r1-r2, and c1-c2 differences will be the same, so your region is square.
nd.zoom takes a zoom factor (float). You would need to compute whta athat zoom factor is in order to take your sliced image and turn it into a 100x100 sized array:
ndimage.zoom(your_image[r1:r2, c1:c2], zoom=your_zoom_factor)

How to Zero Pad RGB Image?

I want to Pad an RGB Image of size 500x500x3 to 512x512x3. I understand that I need to add 6 pixels on each border but I cannot figure out how. I have read numpy.pad function docs but couldn't understand how to use it. Code snippets would be appreciated.
If you need to pad 0:
RGB = np.pad(RGB, pad_width=[(6, 6),(6, 6),(0, 0)], mode='constant')
Use constant_values argument to pad different values (default is 0):
RGB = np.pad(RGB, pad_width=[(6, 6),(6, 6),(0, 0)], mode='constant', constant_values=0, constant_values=[(3,3),(5,5),(0,0)]))
We can try to get a solution by adding border padding, but it would get a bit complex. I would like to suggest you can alternate approach. First we can create a canvas of size 512x512 and then we place your original image inside this canvas. You can get help from the following code:
import numpy as np
# Create a larger black colored canvas
canvas = np.zeros(512, 512, 3)
canvas[6:506, 6:506] = your_500_500_img
Obviously you can convert 6 and 506 to a more generalized variable and use it as padding, 512-padding, etc. but this code illustrates the concept.

Put pcolormesh and contour onto same grid?

I'm trying to display 2D data with axis labels using both contour and pcolormesh. As has been noted on the matplotlib user list, these functions obey different conventions: pcolormesh expects the x and y values to specify the corners of the individual pixels, while contour expects the centers of the pixels.
What is the best way to make these behave consistently?
One option I've considered is to make a "centers-to-edges" function, assuming evenly spaced data:
def centers_to_edges(arr):
dx = arr[1]-arr[0]
newarr = np.linspace(arr.min()-dx/2,arr.max()+dx/2,arr.size+1)
return newarr
Another option is to use imshow with the extent keyword set.
The first approach doesn't play nicely with 2D axes (e.g., as created by meshgrid or indices) and the second discards the axis numbers entirely
Your data is a regular mesh? If it doesn't, you can use griddata() to obtain it. I think that if your data is too big, a sub-sampling or regularization always is possible. If the data is too big, maybe your output image always will be small compared with it and you can exploit this.
If you use imshow() with "extent" and "interpolation='nearest'", you will see that the data is cell-centered, and extent provided the lower edges of cells (corners). On the other hand, contour assumes that the data is cell-centered, and X,Y must be the center of cells. So, you need to be care about the input domain for contour. The trivial example is:
x = np.arange(-10,10,1)
X,Y = np.meshgrid(x,x)
P = X**2+Y**2
imshow(P,extent=[-10,10,-10,10],interpolation='nearest',origin='lower')
contour(X+0.5,Y+0.5,P,20,colors='k')
My tests told me that pcolormesh() is a very slow routine, and I always try to avoid it. griddata and imshow() always is a good choose for me.