How can I convert XYZ point cloud to binary mask image - python-3.x

I want to convert a set of point cloud (X, Y, Z) to a binary mask image using python. The problem is that these points are float and out of range of 0-255. To more specific, the points are related to an object (rectangle or ellipsoid), so I should make a binary image based on Z dimension, to specify the rectangle, for example, as 0 number and other points as 1 number in binary mask.
Can anyone give me some ideas to achieve my goal?
My point is like this array:
[[-1.56675167e+01 1.59539632e+01 1.15432026e-02]
[-1.26066835e+01 6.48645007e+00 1.15510724e-02]
[-1.18854252e+01 1.71767061e+01 1.15392632e-02]
...
[-1.45721083e+01 1.39116935e+01 -9.86438582e-04]
[-1.42607847e+01 1.28141373e+01 -1.73514791e-03]
[-1.48834319e+01 1.50092497e+01 7.59929187e-04]]
I was tried to get such binary mask that was answered in this example ():
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.path import Path
from descartes import PolygonPatch
import alphashape
from shapely.geometry import Point, Polygon
def poly2mask():
# First of all, I separated the contour of the polygon and get vertices
# in the border
hull = alphashape.alphashape(surface_org, 0.) # convex hull
poly = PolygonPatch(hull, alpha=0.2, edgecolor='#999999')
vertices = poly.get_path().vertices
x = vertices[:, 0] * 10
y = vertices[:, 1] * 10
vertices_ls = list(zip(x, y))
width, height = 120, 120
poly_path = Path(vertices_ls, closed=True)
x, y = np.mgrid[:height, :width]
coors = np.hstack((x.reshape(-1, 1), y.reshape(-1, 1)))
mask = poly_path.contains_points(coors)
mask = mask.reshape(height, width)
#print(mask)
plt.imshow(mask)
plt.ylim(-200, 200)
plt.xlim(-200, 200)
plt.show()
The image would look like this:
enter image description here

Related

How to do color code the 3D scatter plot according to time of occurrence in an animated graph?

I have developed a code to create an animated scatter graph.
About the dataset, I have the X,Y,Z coordinate of each point and each event point are assigned a value (M) and each happened at a specific time (t).
I have the size of each point to be proportional to their value (i.e., M), now I want to add the color to each point so that it also shows the time of occurrence. I know I have to use .set_color(c) but c value expects a tuple value. I tried to normalize the values of the time to map the color from this post. However, there is something that I miss because the code is not working to color the points with related time. I would appreciate it if someone could share their experiences?
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.animation as animation
from IPython.display import HTML # Animation on jupyter lab
from matplotlib.animation import PillowWriter # For GIF animation
#####Data Generation####
# Space Coordinate
X = np.random.random((100,)) * 255 * 2 - 255
Y = np.random.random((100,)) * 255 * 2 - 255
Z = np.random.random((100,)) * 255 * 2 - 255
# Magnitude of each point
# M = np.random.random((100,))*-1+0.5
M = np.random.randint(1,70, size=100)
# Time
t = np.sort(np.random.random((100,))*10)
#ID each point should be color coded. Moreover, each point belongs to a cluster `ID`
ID = np.sort(np.round([np.random.random((100,))*5]))
x = []
y = []
z = []
m = []
def update_lines(i):
# for i in range (df_IS["EASTING [m]"].size):
dx = X[i]
dy = Y[i]
dz = Z[i]
dm = M[i]
# text.set_text("{:d}: [{:.0f}] Mw[{:.2f}]".format(ID[i], t[i],ID[i])) # for debugging
x.append(dx)
y.append(dy)
z.append(dz)
m.append(dm)
graph._offsets3d = (x, y, z)
graph.set_sizes(m)
return graph,
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection="3d")
graph = ax.scatter(X, Y, Z, s=M, color='orange') # s argument here
text = fig.text(0, 1, "TEXT", va='top') # for debugging
ax.set_xlim3d(X.min(), X.max())
ax.set_ylim3d(Y.min(), Y.max())
ax.set_zlim3d(Z.min(), Z.max())
# Creating the Animation object
ani = animation.FuncAnimation(fig, update_lines, frames=100, interval=500, blit=False, repeat=False)
# plt.show()
ani.save('test3Dscatter.gif', writer='pillow')
plt.close()
HTML(ani.to_html5_video())
You need to change "Color" to "cmap" so that you are able to call set of colors, see below:
graph = ax.scatter(X, Y, Z, s=M, cmap='jet') #jet is similar to rainbow

Matplotlib colours are wrong

NOTE: This is now resolved, although I made no changes to my code, but the images are now mysteriously coming out with perfect colour.
I am making plots using Python3 with a Spyder interface. I have looked at the plots both in the Spyder terminal, and in the saved PNGs of the images. Both show that the colours are coming out very badly.
A simple line in the script
plt.plot(x1, beta.transpose()[x1], color = 'r', linewidth = 0.2)
The red colour is showing as a light pink. When I change 'r' for 'k' to get a black line, it comes as a very light grey. Does anyone know of issues affecting Matplotlib, or Matplotlib used through Spyder, that might account for this?
Example:
Code:
Data-generating script logit_data.py
#Logistic regression data and link function.
import numpy as np
#Define link function here
def g(z):
g=1/(1+np.exp(-z))
return g
#For producing y data values given true paramters theta and number of covariates
def logit_data(n,p, theta):
#Define parameters
#1)Number of covariates
p_i = p+1 #with intercept
p_i=np.int(p_i)
#2) m as correct data type
n=np.int(n)
#4)Specify parameter valueas to be estimated
theta=np.reshape(theta, (p_i,1))
#5)Define distribution from which covariate values are drawn i.i.d., and initiate data values
X=np.zeros((n,p_i))
X[:,0]=1 #intercept
mean=0
sigma=1.5
X[:,1:]=np.random.normal(mean,sigma,(n,p))
#6)Produce y values treating y as a Bernoulli variable with p=g(X*theta)
r=np.random.uniform(0,1,n)
r=np.reshape(r, (len(r),1))
htrue=g(X.dot(theta))
y=htrue-r
y[y>=0]=1
y[y<0]=0
return X, y
Plotting script:
#Script for producing y data from p covariates from a specified distribution, specified beta paraemters,
#and n data samples for logit link function.
import numpy as np
import matplotlib.pyplot as plt
from logit_data import logit_data
import pylab
import statsmodels.stats as sms
import statsmodels.api as sma
import csv
def figure2():
#def MLE_logistic_function():
#1)Sample and observation numbers
samples = 30
observations = 40000
#2)Number of independent covariates
p=299
#3)True beta to be estimated (parameter values)
nonzerosN=30
beta1=np.append(np.full((1, nonzerosN),10),np.full((1,nonzerosN),-10), axis=1)
print(np.shape(beta1))
beta=np.append(beta1,np.zeros((1,p+1-2*nonzerosN)), axis=1)
print(np.shape(beta))
#4)#Initiate arrays to store estimates of beta (and errors) computed at specified sample numbers N
#Betas=np.zeros((len(npowers),p+1))
#Errors=np.zeros((len(npowers),p+1))
#5)Obtain random covariate values from specified distribution, and corresponding y values using true beta
#plus gaussian noise term.
X,y = logit_data(observations,p,beta)
logit = sma.Logit(y,X)
result = logit.fit()
print(result.summary())
MLEcoefficients = result.params
x1 = np.arange(0, nonzerosN,1)
x2 = np.arange(nonzerosN, 2*nonzerosN,1)
x3 = np.arange(2*nonzerosN, p+1,1)
plt.scatter(index, MLEcoefficients, 0.2)
plt.plot(x1, beta.transpose()[x1], color = 'black', linewidth = 0.2, alpha=1)
plt.plot(x2, beta.transpose()[x2], color = 'black', linewidth = 0.2)
plt.plot(x3, beta.transpose()[x3], color = 'black', linewidth = 0.2)
plt.xlabel('Index')
plt.ylabel('Coefficient values (true and fitted)')
plt.savefig('MMLTfig2_p%s_o%d.png' %(p,observations))
plt.show()
return
figure2()

How to create a grid according to the orientation of a polygon

How can I make a grid according to the orientation of a polygon with some interval, where the polygon has always 4 point data with different orientation, for example my polygon looks like this :
x1 = np.array([50,0,150,200,50])
y1 = np.array([10,-50,-60,0,10])
and I want to make grid like this :
You can use a interpolate function for each coordinate:
from scipy.interpolate import interp2d
x = np.array([0, 1], dtype=np.float)
y = np.array([0, 1], dtype=np.float)
Now we need to create the interpolate function so that on the small unit square (0,1)x(0,1), we get the result we want.
zx = np.array([[0, 150],[50, 200]])
fx = interp2d(x, y, zx)
fx(0.5, 0.5)
Do the same for zy to get the y coordinate inside your polygon.
You can create a LineCollection of "grid lines" by subdividing the polygon edges into equidistant parts as shown below. In the function grid, nx and ny are the number of lines to create per dimension of the polygon.
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.collections import LineCollection
def grid(x,y, nx, ny, **kwargs):
def verts(a,b,c,d,n):
l1x = np.linspace(x[a],x[b], n)
l1y = np.linspace(y[a],y[b], n)
l2x = np.linspace(x[d],x[c], n)
l2y = np.linspace(y[d],y[c], n)
return np.stack((np.c_[l1x, l2x], np.c_[l1y, l2y]), axis=-1)
v = np.concatenate((verts(0,1,2,3,ny), verts(1,2,3,4,nx)), axis=0)
return LineCollection(v, **kwargs)
x1 = np.array([50,0,150,200,50])
y1 = np.array([10,-50,-60,0,10])
fig, ax = plt.subplots()
ax.add_collection(grid(x1,y1,5,6, color="gray"))
rect=Polygon(np.c_[x1,y1], edgecolor="C0", linewidth=2, facecolor="none", zorder=3)
ax.add_patch(rect)
ax.autoscale()
plt.show()

Obtaining coordinates in projected map using Cartopy

I'm trying to obtain the coordinates of the features of a map using Cartopy but I would like to obtain the map projected coordinates instead of the data from the original projection.
For instance:
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
fig = plt.figure(figsize=(10, 10))
ax = plt.axes(projection=ccrs.epsg(3857))
fig.add_axes(ax)
ax.coastlines()
ax.set_global()
lines = ax.plot((0, 360), (-85.06, 85.06), transform=ccrs.PlateCarree())
fig.show()
The previous code shows a map with two lines using the map projection but lines (a list with matplotlib.lines.Line2D instances) is just only one object with the coordinates in the original projection of the data (lines[0].get_data() ---> (array([ 0, 360]), array([-85.06, 85.06]))).
On an interactive plot, a Qt5 backend obtained after fig.show(), I can see coordinates in EPSG:3857 and in PlateCarree when the cursor is over the map so I wonder if there is an easy way to get lines in EPSG:3857 coordinates.
EDIT: The example above is quite simplified. I've tried to do it simple for better understanding but maybe is better to show the real problem.
I have a grid of data with longitudes in the range [0, 360]. I can modify the arrays in order to have inputs in the range [-180, 180] and I'm using Cartopy/Matplotlib to plot contours. From the contours I'm obtaining a matplotlib.contour.QuadContourSet with several matplotlib.collections.LineCollection. From each matplotlib.collections.LineCollection I can obtain the matplotlib.path.Paths and I would like to have the coordinates of each Path in EPSG:3857 instead of in the original PlateCarree so I can use cartopy.mpl.patch.path_to_geos to convert each Path to a shapely geometry object in the EPSG:3857 projection without having to extract vertices from each Path, convert them from PlateCarree to EPSG:3857 and then create a new Path with the converted coordinates to use cartopy.mpl.patch.path_to_geos to obtain geometries in the crs I need.
The question asks for a coordinate transformation using Cartopy's feature, and maybe something else.
Here I provide the code that performs coordinate transformation and computation check.
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
# Test data in geographic lon, lat (degrees)
lons = np.array((0, 360.01)) # any number of longitude
lats = np.array((-85.06, 85.06)) # .. longitude
# define all CRS
crs_longlat = ccrs.PlateCarree()
crs_3857 = ccrs.epsg(3857)
# Transformation function
def coordXform(orig_crs, target_crs, x, y):
"""
Converts array of (y,x) from orig_crs -> target_crs
y, x: numpy array of float values
orig_crs: source CRS
target_crs: target CRS
"""
# original code is one-liner
# it leaves an open axes that need to plt.close() later
# return plt.axes( projection = target_crs ).projection.transform_points( orig_crs, x, y )
# new improved code follows
xys = plt.axes( projection = target_crs ).projection.transform_points( orig_crs, x, y )
# print(plt.gca()) # current axes: GeoAxes: _EPSGProjection(3857)
plt.close() # Kill GeoAxes
# print(plt.gca()) # AxesSubplot (new current axes)
return xys
# Transform geographic (lon-lat) to (x, y) of epsg(3857)
xys = coordXform(crs_longlat, crs_3857, lons, lats)
for ea in xys:
print("(x, y) meters: " + str(ea[0]) + ', ' + str(ea[1]))
#Output(1)
#(x, y) meters: 0.0, -20006332.4374
#(x, y) meters: 1113.19490794, 20006332.4374
# Computation check
# Transform (x, y) of epsg(3857) to geographic (lon-lat), degrees
xs = xys[:,0] # all x's
ys = xys[:,1] # all y's
lls = coordXform(crs_3857, crs_longlat, xs, ys)
for ea in lls:
print("(lon, lat) degrees: " + str(ea[0]) + ', ' + str(ea[1]))
#Output(2)
#(lon, lat) degrees: 0.0, -85.06
#(lon, lat) degrees: 0.01, 85.06
# plt.close() # no need now
Edit 2
According to the constructive comments, the transformation function above can be written as follows:
def coordXform(orig_crs, target_crs, x, y):
return target_crs.transform_points( orig_crs, x, y )

Trapezoidal wave in Python

How do I generate a trapezoidal wave in Python?
I looked into the modules such as SciPy and NumPy, but in vain. Is there a module such as the scipy.signal.gaussian which returns an array of values representing the Gaussian function wave?
I generated this using the trapezoidal kernel of Astropy,
Trapezoid1DKernel(30,slope=1.0)
. I want to implement this in Python without using Astropy.
While the width and the slope are sufficient to define a triangular signal, you would need a third parameter for a trapezoidal signal: the amplitude.
Using those three parameters, you can easily adjust the scipy.signal.sawtooth function to give you a trapeziodal shape by truncating and offsetting the triangular shaped function.
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
def trapzoid_signal(t, width=2., slope=1., amp=1., offs=0):
a = slope*width*signal.sawtooth(2*np.pi*t/width, width=0.5)/4.
a[a>amp/2.] = amp/2.
a[a<-amp/2.] = -amp/2.
return a + amp/2. + offs
t = np.linspace(0, 6, 501)
plt.plot(t,trapzoid_signal(t, width=2, slope=2, amp=1.), label="width=2, slope=2, amp=1")
plt.plot(t,trapzoid_signal(t, width=4, slope=1, amp=0.6), label="width=4, slope=1, amp=0.6")
plt.legend( loc=(0.25,1.015))
plt.show()
Note that you may also like to define a phase, depeding on the use case.
In order to define a single pulse, you might want to modify the function a bit and supply an array which ranges over [0,width].
from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
def trapzoid_signal(t, width=2., slope=1., amp=1., offs=0):
a = slope*width*signal.sawtooth(2*np.pi*t/width, width=0.5)/4.
a += slope*width/4.
a[a>amp] = amp
return a + offs
for w,s,a in zip([2,5], [2,1], [1,0.6]):
t = np.linspace(0, w, 501)
l = "width={}, slope={}, amp={}".format(w,s,a)
plt.plot(t,trapzoid_signal(t, width=w, slope=s, amp=a), label=l)
plt.legend( loc="upper right")
plt.show()
From the SciPy website it looks like this isn't included (they currently have sawtooth and square, but not trapezoid). As a generalised version of the C example the following will do what you want,
import numpy as np
import matplotlib.pyplot as plt
def trapezoidalWave(xin, width=1., slope=1.):
x = xin%(4*width)
if (x <= width):
# Ascending line
return x*slope;
elif (x <= 2.*width):
# Top horizontal line
return width*slope
elif (x <= 3.*width):
# Descending line
return 3.*width*slope - x*slope
elif (x <= 4*width):
# Bottom horizontal line
return 0.
x = np.linspace(0.,20,1000)
for i in x:
plt.plot(i, trapezoidalWave(i), 'k.')
plt.plot(i, trapezoidalWave(i, 1.5, 2.), 'r.')
plt.show()
which looks like,
This can be done more elegantly with Heaviside functions which allow you to use NumPy arrays,
import numpy as np
import matplotlib.pyplot as plt
def H(x):
return 0.5 * (np.sign(x) + 1)
def trapWave(xin, width=1., slope=1.):
x = xin%(4*width)
y = ((H(x)-H(x-width))*x*slope +
(H(x-width)-H(x-2.*width))*width*slope +
(H(x-2.*width)-H(x-3.*width))*(3.*width*slope - x*slope))
return y
x = np.linspace(0.,20,1000)
plt.plot(x, trapWave(x))
plt.plot(x, trapWave(x, 1.5, 2.))
plt.show()
For this example, the Heaviside version is about 20 times faster!
The below example shows how to do that to get points and show scope.
Equation based on reply: Equation for trapezoidal wave equation
import math
import numpy as np
import matplotlib.pyplot as plt
def get_wave_point(x, a, m, l, c):
# Equation from: https://stackoverflow.com/questions/11041498/equation-for-trapezoidal-wave-equation
# a/pi(arcsin(sin((pi/m)x+l))+arccos(cos((pi/m)x+l)))-a/2+c
# a is the amplitude
# m is the period
# l is the horizontal transition
# c is the vertical transition
point = a/math.pi*(math.asin(math.sin((math.pi/m)*x+l))+math.acos(math.cos((math.pi/m)*x+l)))-a/2+c
return point
print('Testing wave')
x = np.linspace(0., 10, 1000)
listofpoints = []
for i in x:
plt.plot(i, get_wave_point(i, 5, 2, 50, 20), 'k.')
listofpoints.append(get_wave_point(i, 5, 2, 50, 20))
print('List of points : {} '.format(listofpoints))
plt.show()
The whole credit goes to #ImportanceOfBeingErnest . I am just revising some edits to his code which just made my day.
from scipy import signal
import matplotlib.pyplot as plt
from matplotlib import style
import numpy as np
def trapzoid_signal(t, width=2., slope=1., amp=1., offs=0):
a = slope*width*signal.sawtooth(2*np.pi*t/width, width=0.5)/4.
a += slope*width/4.
a[a>amp] = amp
return a + offs
for w,s,a in zip([32],[1],[0.0322]):
t = np.linspace(0, w, 34)
plt.plot(t,trapzoid_signal(t, width=w, slope=s, amp=a))
plt.show()
The result:
I'll throw a very late hat into this ring, namely, a function using only numpy that produces a single (symmetric) trapezoid at a desired location, with all the usual parameters. Also posted here
import numpy as np
def trapezoid(x, center=0, slope=1, width=1, height=1, offset=0):
"""
For given array x, returns a (symmetric) trapezoid with plateau at y=h (or -h if
slope is negative), centered at center value of "x".
Note: Negative widths and heights just converted to 0
Parameters
----------
x : array_like
array of x values at which the trapezoid should be evaluated
center : float
x coordinate of the center of the (symmetric) trapezoid
slope : float
slope of the sides of the trapezoid
width : float
width of the plateau of the trapezoid
height : float
(positive) vertical distance between the base and plateau of the trapezoid
offset : array_like
vertical shift (either single value or the same shape as x) to add to y before returning
Returns
-------
y : array_like
y value(s) of trapezoid with above parameters, evaluated at x
"""
# ---------- input checking ----------
if width < 0: width = 0
if height < 0: height = 0
x = np.asarray(x)
slope_negative = slope < 0
slope = np.abs(slope) # Do all calculations with positive slope, invert at end if necessary
# ---------- Calculation ----------
y = np.zeros_like(x)
mask_left = x - center < -width/2.0
mask_right = x - center > width/2.0
y[mask_left] = slope*(x[mask_left] - center + width/2.0)
y[mask_right] = -slope*(x[mask_right] - center - width/2.0)
y += height # Shift plateau up to y=h
y[y < 0] = 0 # cut off below zero (so that trapezoid flattens off at "offset")
if slope_negative: y = -y # invert non-plateau
return y + offset
Which outputs something like
import matplotlib.pyplot as plt
plt.style.use("seaborn-colorblind")
x = np.linspace(-5,5,1000)
for i in range(1,4):
plt.plot(x,trapezoid(x, center=0, slope=1, width=i, height=i, offset = 0), label=f"width = height = {i}\nslope=1")
plt.plot(x,trapezoid(x, center=0, slope=-1, width=2.5, height=1, offset = 0), label=f"width = height = 1.5,\nslope=-1")
plt.ylim((-2.5,3.5))
plt.legend(frameon=False, loc='lower center', ncol=2)
Example output:

Resources