def loadTargetImageMeta(self, src): if True: # FIX: currently doesnt correspond with what is shown lblloader.load_image_meta(src, self.systemModel) else: self.systemModel.ast_x_rot.value = -83.88 self.systemModel.ast_y_rot.value = 74.38 self.systemModel.ast_z_rot.value = -77.98 self.systemModel.time.range = (1437391848.27 * 0.99, 1437391848.27 * 1.01) self.systemModel.time.value = 1437391848.27 self.systemModel.x_off.value = -0.42 self.systemModel.x_rot.value = -49.39 self.systemModel.y_off.value = 2.47 self.systemModel.y_rot.value = 123.26 self.systemModel.z_off.value = -158.39 self.systemModel.z_rot.value = -96.62 self.update()
def costfun(x, debug=0, verbose=True): set_params(x) err = 0 for file, real in imgs.items(): lblloader.load_image_meta(os.path.join(sm.asteroid.image_db_path, file + '.LBL'), sm) sm.swap_values_with_real_vals() synth2 = ab.render(shadows=True, reflection=model, gamma=1) err_img = (synth2.astype('float') - real)**2 lim = np.percentile(err_img, 99) err_img[err_img > lim] = 0 err += np.mean(err_img) if debug: if debug%2: cv2.imshow('real vs synthetic', np.concatenate((real.astype('uint8'), 255*np.ones((real.shape[0], 1), dtype='uint8'), synth2), axis=1)) if debug>1: err_img = err_img**0.2 cv2.imshow('err', err_img/np.max(err_img)) cv2.waitKey() err /= len(imgs) if verbose: print('%s => %f' % (', '.join(['%.4e' % i for i in np.array(x)*scales]), err)) return err
qfin = (q * q_crop.conj()) # light direction light, err_angle = m.gl_light_rel_dir(err_q, discretize_tol) self.latest_discretization_light_err_angle = err_angle if discretize_tol else False return (x, y, z), qfin, light if __name__ == '__main__': sm = RosettaSystemModel(rosetta_batch='mtp006') #img = 'ROS_CAM1_20140823T021833' img = 'ROS_CAM1_20140822T020718' #img = 'ROS_CAM1_20150606T213002' # 017 lblloader.load_image_meta( os.path.join(sm.asteroid.image_db_path, img + '.LBL'), sm) sm.swap_values_with_real_vals(), textures = True if True: re = RenderEngine(sm.cam.width, sm.cam.height, antialias_samples=16) #obj_idx = re.load_object(sm.asteroid.hires_target_model_file, smooth=False) #obj_idx = re.load_object(os.path.join(BASE_DIR, 'data/original-shapemodels/CSHP_DV_130_01_HIRES_00200.obj'), smooth=False) #obj_idx = re.load_object(os.path.join(BASE_DIR, 'data/original-shapemodels/dissolved_5deg_1.obj'), smooth=False) obj_idx = re.load_object(os.path.join( BASE_DIR, 'data/original-shapemodels/67P_C-G_shape_model_MALMER_2015_11_20-in-km.obj' ), smooth=False) textures = False else:
def generate_system_state(self, sm, i): # reset asteroid axis to true values sm.asteroid.reset_to_defaults() sm.asteroid_rotation_from_model() if self._state_list is not None: lblloader.load_image_meta( os.path.join(self._state_db_path, self._state_list[i]+'.LBL'), sm) return for i in range(100): ## sample params from suitable distributions ## # datetime dist: uniform, based on rotation period time = np.random.uniform(*sm.time.range) # spacecraft position relative to asteroid in ecliptic coords: sc_lat = np.random.uniform(-math.pi/2, math.pi/2) sc_lon = np.random.uniform(-math.pi, math.pi) # s/c distance as inverse uniform distribution if TestLoop.UNIFORM_DISTANCE_GENERATION: sc_r = np.random.uniform(self.min_r, self.max_r) else: sc_r = 1/np.random.uniform(1/self.max_r, 1/self.min_r) # same in cartesian coord sc_ex_u, sc_ey_u, sc_ez_u = spherical_to_cartesian(sc_r, sc_lat, sc_lon) sc_ex, sc_ey, sc_ez = sc_ex_u.value, sc_ey_u.value, sc_ez_u.value # s/c to asteroid vector sc_ast_v = -np.array([sc_ex, sc_ey, sc_ez]) # sc orientation: uniform, center of asteroid at edge of screen if self._opzone_only: # always get at least 50% of astroid in view, 5% of the time maximum offset angle max_angle = rad(min(sm.cam.x_fov, sm.cam.y_fov)/2) da = min(max_angle, np.abs(np.random.normal(0, max_angle/2))) dd = np.random.uniform(0, 2*math.pi) sco_lat = wrap_rads(-sc_lat + da*math.sin(dd)) sco_lon = wrap_rads(math.pi + sc_lon + da*math.cos(dd)) sco_rot = np.random.uniform(-math.pi, math.pi) # rotation around camera axis else: # follows the screen edges so that get more partial views, always at least 25% in view # TODO: add/subtract some margin sco_lat = wrap_rads(-sc_lat) sco_lon = wrap_rads(math.pi + sc_lon) sco_rot = np.random.uniform(-math.pi, math.pi) # rotation around camera axis sco_q = ypr_to_q(sco_lat, sco_lon, sco_rot) ast_ang_r = math.atan(sm.asteroid.mean_radius/1000/sc_r) # if asteroid close, allow s/c to look at limb dx = max(rad(sm.cam.x_fov/2), ast_ang_r) dy = max(rad(sm.cam.y_fov/2), ast_ang_r) disturbance_q = ypr_to_q(np.random.uniform(-dy, dy), np.random.uniform(-dx, dx), 0) sco_lat, sco_lon, sco_rot = q_to_ypr(sco_q * disturbance_q) sco_q = ypr_to_q(sco_lat, sco_lon, sco_rot) # sc_ast_p ecliptic => sc_ast_p open gl -z aligned view sc_pos = q_times_v((sco_q * sm.sc2gl_q).conj(), sc_ast_v) # get asteroid position so that know where sun is # *actually barycenter, not sun as_v = sm.asteroid.position(time) elong, direc = solar_elongation(as_v, sco_q) # limit elongation to always be more than set elong if elong > rad(sm.min_elong): break if elong <= rad(sm.min_elong): assert False, 'probable infinite loop' # put real values to model sm.time.value = time sm.spacecraft_pos = sc_pos sm.spacecraft_rot = (deg(sco_lat), deg(sco_lon), deg(sco_rot)) # save real values so that can compare later sm.time.real_value = sm.time.value sm.real_spacecraft_pos = sm.spacecraft_pos sm.real_spacecraft_rot = sm.spacecraft_rot sm.real_asteroid_axis = sm.asteroid_axis # get real relative position of asteroid model vertices sm.asteroid.real_sc_ast_vertices = sm.sc_asteroid_vertices()
ih, iw, cs = img.shape m = cv2.moments(img[:,:,0], binaryImage=binary) if np.isclose(m['m00'],0): if is_scene: raise PositioningException('No asteroid found') else: raise PositioningException('Algorithm failure: model moved out of view') # image centroid icx = m['m10']/m['m00']/iw*self._cam.width icy = m['m01']/m['m00']/ih*self._cam.height brightness = m['m00']/iw/ih*self._cam.width*self._cam.height # pixel spreads # used to model dimensions of asteroid parts visible in image hr = math.sqrt(m['mu20']/m['m00']) if m['mu20']>0 else 1 vr = math.sqrt(m['mu02']/m['m00']) if m['mu02']>0 else 1 return icx, icy, brightness, hr, vr if __name__ == '__main__': sm = RosettaSystemModel() lblloader.load_image_meta(sm.asteroid.sample_image_meta_file, sm) re = RenderEngine(sm.view_width, sm.view_height) obj_idx = re.load_object(sm.asteroid.real_shape_model) DEBUG = True algo = CentroidAlgo(sm, re, obj_idx) algo.adjust_iteratively(sm.asteroid.sample_image_file, None)