ezwelty / glimpse

glimpse: Glacier Image Particle Sequencer
13 stars 1 forks source link

ValueError: zero-size array to reduction operation maximum which has no identity #18

Closed frigusgulo closed 4 years ago

frigusgulo commented 4 years ago

sensorx,sensory = 35.9,24 cam_coords = [393506.713,6695855.64 ,961.3370] #Easting, Northing, Elevation cam_viewdir = [72.08866164934302, -24.53,0.35382] # An intial guess cliff_camera = glimpse.Camera(xyz=cam_coords,viewdir=cam_viewdir,sensorsz (sensorx,sensory)) cliff_imgpts = np.array([[5193, 549],[3101, 642],[3680, 2456],[6153.0, 2297.0]]) cliff_worldpts = np.array([[408245.86,6695847.03,1560 ],[416067.22,6707259.97,988],[393506.713, 6695855.641, 961.337],[394569.509, 6695550.678, 621.075]]) cliff_points = glimpse.optimize.Points(cliff_camera,cliff_imgpts,cliff_worldpts)

cliff_cam_params = glimpse.optimize.Cameras([cliff_camera],[cliff_points]) cliff_cam_params = cliff_cam_params.fit()

Traceback:


ValueError Traceback (most recent call last)

in 1 cliff_cam_params = glimpse.optimize.Cameras([cliff_camera],[cliff_points]) ----> 2 cliff_cam_params = cliff_cam_params.fit() ~/anaconda3/lib/python3.7/site-packages/glimpse/optimize.py in fit(self, index, cam_params, group_params, full, method, nan_policy, reduce_fcn, **kwargs) 1383 self.update_params() 1384 result = lmfit.minimize(params=self.params, fcn=self.residuals, kws=dict(index=index), iter_cb=callback, -> 1385 method=method, nan_policy=nan_policy, reduce_fcn=reduce_fcn, **kwargs) 1386 sys.stdout.write('\n') 1387 if iterations: ~/anaconda3/lib/python3.7/site-packages/lmfit-0.9.11-py3.7.egg/lmfit/minimizer.py in minimize(fcn, params, method, args, kws, scale_covar, iter_cb, reduce_fcn, **fit_kws) 2137 iter_cb=iter_cb, scale_covar=scale_covar, 2138 reduce_fcn=reduce_fcn, **fit_kws) -> 2139 return fitter.minimize(method=method) ~/anaconda3/lib/python3.7/site-packages/lmfit-0.9.11-py3.7.egg/lmfit/minimizer.py in minimize(self, method, params, **kws) 1809 val.lower().startswith(user_method)): 1810 kwargs['method'] = val -> 1811 return function(**kwargs) 1812 1813 ~/anaconda3/lib/python3.7/site-packages/lmfit-0.9.11-py3.7.egg/lmfit/minimizer.py in least_squares(self, params, **kws) 1280 bounds=(lower_bounds, upper_bounds), 1281 kwargs=dict(apply_bounds_transformation=False), -> 1282 **kws) 1283 except AbortFitException: 1284 pass ~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_lsq/least_squares.py in least_squares(fun, x0, jac, bounds, method, ftol, xtol, gtol, x_scale, loss, f_scale, diff_step, tr_solver, tr_options, jac_sparsity, max_nfev, verbose, args, kwargs) 869 return J 870 --> 871 J0 = jac_wrapped(x0, f0) 872 873 if J0 is not None: ~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_lsq/least_squares.py in jac_wrapped(x, f) 863 J = approx_derivative(fun, x, rel_step=diff_step, method=jac, 864 f0=f, bounds=bounds, args=args, --> 865 kwargs=kwargs, sparsity=jac_sparsity) 866 if J.ndim != 2: # J is guaranteed not sparse. 867 J = np.atleast_2d(J) ~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_numdiff.py in approx_derivative(fun, x0, method, rel_step, f0, bounds, sparsity, as_linear_operator, args, kwargs) 398 return _sparse_difference(fun_wrapped, x0, f0, h, 399 use_one_sided, structure, --> 400 groups, method) 401 402 ~/anaconda3/lib/python3.7/site-packages/scipy/optimize/_numdiff.py in _sparse_difference(fun, x0, f0, h, use_one_sided, structure, groups, method) 490 fractions = [] 491 --> 492 n_groups = np.max(groups) + 1 493 for group in range(n_groups): 494 # Perturb variables which are in the same group simultaneously. ~/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in amax(a, axis, out, keepdims, initial) 2503 """ 2504 return _wrapreduction(a, np.maximum, 'max', axis, None, out, keepdims=keepdims, -> 2505 initial=initial) 2506 2507 ~/anaconda3/lib/python3.7/site-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs) 84 return reduction(axis=axis, out=out, **passkwargs) 85 ---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs) 87 88 ValueError: zero-size array to reduction operation maximum which has no identity
ezwelty commented 4 years ago

First, try to provide a complete code example (including import statements) and place the code in a code block with the appropriate language tag (i.e. ```python ... ```).

The verdict: Since you are not passing any cam_params or group_params to optimize.Cameras, there are no parameters to optimize. This is the source of the error.

Read on for a discussion of other issues with your code.

Camera

Here is the cleaner equivalent of what you are doing:

import glimpse
import numpy as np

params = dict(
  sensorsz=(35.9, 24),
  xyz=(393506.713, 6695855.64, 961.3370),
  viewdir=(72.08866164934302, -24.53, 0.35382)
)
cam = glimpse.Camera(**params)

All other parameters, like imgsz and f, are left at their default, which is probably not what you want!

cam.imgsz

array([100., 100.])

You can use Image, Exif, or manually type in the right values:

import glimpse
import numpy as np

params = dict(
  sensorsz=(35.9, 24),
  xyz=(393506.713, 6695855.64, 961.3370),
  viewdir=(72.08866164934302, -24.53, 0.35382),
  imgsz=(7360, 4912),
  fmm=28
)
cam = glimpse.Camera(**params)

optimize.Cameras

Now let's work our way down to your call to Cameras():

uv = np.array([
  [5193, 549],
  [3101, 642],
  [3680, 2456],
  [6153.0, 2297.0]
])
xyz = np.array([
  [408245.86, 6695847.03, 1560],
  [416067.22, 6707259.97, 988],
  [393506.713, 6695855.641, 961.337],
  [394569.509, 6695550.678, 621.075]
])
points = glimpse.optimize.Points(cam, uv, xyz)
model = glimpse.optimize.Cameras([cam], [points])
params = model.fit()

Since you are not passing any cam_params or group_params to optimize.Cameras, there are no parameters to optimize. This is the source of the error. I am guessing that at the very least, you want to optimize viewdir:

model = glimpse.optimize.Cameras([cam], [points], dict(viewdir=True))
params = model.fit()

But the residuals (pixel reprojection errors) are way too large to proceed, even after optimization:

model.residuals(params)

array([[ 1998.55743481, -1517.90345026], [ 1147.74632006, 293.201628 ], [-6969.12413606, 2479.49767523], [ 4970.93608408, -1752.52436834]])

Plotting confirms that your initial guess for viewdir is in the right ballpark:

import matplotlib.pyplot as plt

poly = cam.viewpoly(10e3)
plt.plot(poly[:, 0], poly[:, 1])
plt.plot(cam.xyz[0], cam.xyz[1], 'k.')
plt.plot(points.xyz[:, 0], points.xyz[:, 1], 'r.')
plt.gca().set_aspect('equal')
plt.show()

temp

So there most likely something wrong with your control points or your camera position (cam.xyz). Once thing I notice is that your third world point is equal to your camera position, which can't be right.