def grid_helper_plot_field(grid_helper, field, pylab): """ Plots the field values """ field = field.copy() # note transpose zeros = field == 0 field[zeros] = np.nan image = scale(field, min_value=0) z = image[:, :, 0] # just R component x = grid_helper._mgrids_plus[0] y = grid_helper._mgrids_plus[1] s0 = grid_helper._specs[0] s1 = grid_helper._specs[1] x = convert_unit(x, s0.units, s0.units_display) y = convert_unit(y, s1.units, s1.units_display) z = ma.masked_array(z, zeros) pylab.pcolor(x, y, np.ones(z.shape), cmap="Pastel1") pylab.pcolor(x, y, z, cmap="gray")
def write_similarity_matrix(A, out, more=False, images=None): dtu.safe_pickle_dump(A, out + '.pickle') # rgb = rgb_zoom(scale(A), 8) rgb = scale(A) rgb = dtu.d8_image_zoom_linear(rgb, 8) dtu.write_image_as_jpg(rgb, out + '.jpg') if more: r = Report() n = A.shape[0] for i in range(n): f = r.figure() ignore = 10 Ai = A[i, :].copy() for i2 in range(i - ignore, i + ignore): if 0 <= i2 < n: Ai[i2] = np.inf jbest = np.argmin(Ai) with f.plot('i-%d' % i) as pylab: pylab.plot(A[i, :], '-*') if images: f.data_rgb(str(i), dtu.bgr_from_rgb(images[i])) f.data_rgb(str(jbest), dtu.bgr_from_rgb(images[jbest])) r.to_html(out + '.html')
def posneg_hinton(value, max_value=None, skim=0, nan_color=[0.5, 0.5, 0.5], properties=None): value = value.astype('float32') # value = value.squeeze().copy() # squeeze: (1,1) -> () value = value.copy() isfinite = np.isfinite(value) isnan = np.logical_not(isfinite) # set nan to 0 value[isnan] = 0 if max_value is None: abs_value = abs(value) if skim != 0: abs_value = skim_top(abs_value, skim) max_value = np.max(abs_value) from reprep.graphics.filter_scale import scale rgb_p = scale(value, min_value=0, max_value=max_value, min_color=[0.5, 0.5, 0.5], max_color=[1.0, 1.0, 1.0], nan_color=[1, 0.6, 0.6], flat_color=[0.5, 0.5, 0.5]) rgb_n = scale(value, min_value=-max_value, max_value=0, max_color=[0.5, 0.5, 0.5], min_color=[0.0, 0.0, 0.0], nan_color=[1, 0.6, 0.6], flat_color=[0.5, 0.5, 0.5]) w_p = value >= 0 # w_z = value == 0 w_n = value <= 0 H, W = value.shape rgb = np.zeros((H, W, 3), 'uint8') for i in range(3): rgb[w_p, i] = rgb_p[w_p, i] rgb[w_n, i] = rgb_n[w_n, i] rgb[isnan, i] = nan_color[i] # rgb[w_z, i] = 128 return rgb
def any_image_to_rgb(y): def bail(): msg = 'Invalid image shape %s dtype %s' % (y.shape, y.dtype) raise ValueError(msg) if y.ndim == 2: if not(y.dtype == 'float32'): bail() return scale(y) if y.dtype == 'uint8': if not y.ndim == 3 and y.shape[2] == 3: bail() return y if not y.ndim == 3 and y.dtype == 'float32': bail() return (y * 255).astype('uint8')
def scalaruncertainty2rgb(x, umin=0, umax=1): """ Converts the scalar uncertainty (in [min, max]) to rgb. (green=1, red=0) """ # set exactly 0 to nan, and color it gray x = x.copy() # if min is None: # min = np.min(x) # if max is None: # max = np.nanmax(x) x[x == 0] = np.nan rgb = scale(x, max_value=umax, min_value=umin, min_color=[1, 0, 0], max_color=[0, 1, 0], nan_color=[0.5, 0.5, 0.5]) return rgb
def any_image_to_rgb(y): def bail(): msg = 'Invalid image shape %s dtype %s' % (y.shape, y.dtype) raise ValueError(msg) if y.ndim == 2: if not (y.dtype == 'float32'): bail() return scale(y) if y.dtype == 'uint8': if not y.ndim == 3 and y.shape[2] == 3: bail() return y if not y.ndim == 3 and y.dtype == 'float32': bail() return (y * 255).astype('uint8')
def plot_phi_d_diagram_bgr(lane_filter, belief, phi, d, dpi=120, bgcolor=dtu.ColorConstants.RGB_DUCKIETOWN_YELLOW, other_phi=None, other_d=None): """ Returns a BGR image """ figure_args = dict(facecolor=dtu.matplotlib_01_from_rgb(bgcolor)) a = dtu.CreateImageFromPylab(dpi=dpi, figure_args=figure_args) with a as pylab: f_d = lambda x: 100 * x f_phi = np.rad2deg # Where are the lanes? lane_width = lane_filter.lanewidth d_max = lane_filter.d_max d_min = lane_filter.d_min phi_max = lane_filter.phi_max phi_min = lane_filter.phi_min delta_d = lane_filter.delta_d delta_phi = lane_filter.delta_phi # note transpose belief = belief.copy() zeros = belief == 0 belief[zeros] = np.nan belief_image = scale(belief, min_value=0) x = f_d(lane_filter.d_pcolor) y = f_phi(lane_filter.phi_pcolor) z = belief_image[:, :, 0] # just R component z = ma.masked_array(z, zeros) pylab.pcolor(x, y, np.ones(z.shape), cmap='Pastel1') pylab.pcolor(x, y, z, cmap='gray') if other_phi is not None: for _phi, _d in zip(other_phi, other_d): pylab.plot(f_d(_d), f_phi(_phi), 'go', markersize=15, markeredgecolor='none', markeredgewidth=3, markerfacecolor='blue') pylab.plot(f_d(d), f_phi(phi), 'go', markersize=20, markeredgecolor='magenta', markeredgewidth=3, markerfacecolor='none') pylab.plot(f_d(d), f_phi(phi), 'o', markersize=2, markeredgecolor='none', markeredgewidth=0, markerfacecolor='magenta') W = f_d(lane_width / 2) width_white = f_d(lane_filter.linewidth_white) width_yellow = f_d(lane_filter.linewidth_yellow) pylab.plot([-W, -W], [f_phi(phi_min), f_phi(phi_max)], 'k-') pylab.plot([-W - width_white, -W - width_white], [f_phi(phi_min), f_phi(phi_max)], 'k-') pylab.plot([0, 0], [f_phi(phi_min), f_phi(phi_max)], 'k-') pylab.plot([+W, +W], [f_phi(phi_min), f_phi(phi_max)], 'y-') pylab.plot([+W + width_yellow, +W + width_yellow], [f_phi(phi_min), f_phi(phi_max)], 'y-') s = '' s += "status = %s" % lane_filter.getStatus() s += "\nphi = %.1f deg" % f_phi(phi) s += "\nd = %.1f cm" % f_d(d) s += "\nentropy = %.4f" % lane_filter.get_entropy() s += "\nmax = %.4f" % belief.max() s += "\nmin = %.4f" % belief.min() if other_phi is not None: s += '\n Other answers:' for _phi, _d in zip(other_phi, other_d): s += "\nphi = %.1f deg" % f_phi(_phi) s += "\nd = %.1f cm" % f_d(_d) y = f_phi(phi_max) - 10 args = dict(rotation=-90, color='white') annotate = True if annotate: pylab.annotate(s, xy=(0.7, 0.35), xycoords='figure fraction') pylab.annotate("in middle of right lane", xy=(0, y), **args) pylab.annotate("on right white tape", xy=(-W, y), **args) pylab.annotate("on left yellow tape", xy=(+W, y), **args) pylab.annotate("in other lane", xy=(+W * 1.3, y), **args) pylab.axis([f_d(d_min), f_d(d_max), f_phi(phi_min), f_phi(phi_max)]) pylab.ylabel('phi: orientation (deg); cell = %.1f deg' % f_phi(delta_phi)) pylab.xlabel('d: distance from center line (cm); cell = %.1f cm' % f_d(delta_d)) return a.get_bgr()
def plot_phi_d_diagram_bgr( lane_filter, belief, phi, d, dpi: int = 120, bgcolor: dtu.BGRColor8 = dtu.ColorConstants.BGR_DUCKIETOWN_YELLOW, other_phi=None, other_d=None, ) -> dtu.NPImageBGR: """Returns a BGR image""" facecolor = dtu.matplotlib_01_from_rgb( dtu.rgb_color_from_bgr_color(bgcolor)) figure_args = dict(facecolor=facecolor) a = dtu.CreateImageFromPylab(dpi=dpi, figure_args=figure_args) with a as pylab: f_d = lambda x: 100 * x f_phi = np.rad2deg # Where are the lanes? lane_width = lane_filter.lanewidth d_max = lane_filter.d_max d_min = lane_filter.d_min phi_max = lane_filter.phi_max phi_min = lane_filter.phi_min delta_d = lane_filter.delta_d delta_phi = lane_filter.delta_phi # note transpose belief = belief.copy() zeros = belief == 0 belief[zeros] = np.nan belief_image = scale(belief, min_value=0) x = f_d(lane_filter.d_pcolor) y = f_phi(lane_filter.phi_pcolor) z = belief_image[:, :, 0] # just R component z = ma.masked_array(z, zeros) pylab.pcolor(x, y, np.ones(z.shape), cmap="Pastel1") pylab.pcolor(x, y, z, cmap="gray") if other_phi is not None: for _phi, _d in zip(other_phi, other_d): pylab.plot( f_d(_d), f_phi(_phi), "go", markersize=15, markeredgecolor="none", markeredgewidth=3, markerfacecolor="blue", ) pylab.plot( f_d(d), f_phi(phi), "go", markersize=20, markeredgecolor="magenta", markeredgewidth=3, markerfacecolor="none", ) pylab.plot( f_d(d), f_phi(phi), "o", markersize=2, markeredgecolor="none", markeredgewidth=0, markerfacecolor="magenta", ) W = f_d(lane_width / 2) width_white = f_d(lane_filter.linewidth_white) width_yellow = f_d(lane_filter.linewidth_yellow) pylab.plot([-W, -W], [f_phi(phi_min), f_phi(phi_max)], "k-") pylab.plot([-W - width_white, -W - width_white], [f_phi(phi_min), f_phi(phi_max)], "k-") pylab.plot([0, 0], [f_phi(phi_min), f_phi(phi_max)], "k-") pylab.plot([+W, +W], [f_phi(phi_min), f_phi(phi_max)], "y-") pylab.plot([+W + width_yellow, +W + width_yellow], [f_phi(phi_min), f_phi(phi_max)], "y-") s = "" s += f"status = {lane_filter.getStatus()}" s += f"\nphi = {f_phi(phi):.1f} deg" s += f"\nd = {f_d(d):.1f} cm" s += f"\nentropy = {lane_filter.get_entropy():.4f}" s += f"\nmax = {belief.max():.4f}" s += f"\nmin = {belief.min():.4f}" if other_phi is not None: s += "\n Other answers:" for _phi, _d in zip(other_phi, other_d): s += f"\nphi = {f_phi(_phi):.1f} deg" s += f"\nd = {f_d(_d):.1f} cm" y = f_phi(phi_max) - 10 args = dict(rotation=-90, color="white") annotate = True if annotate: pylab.annotate(s, xy=(0.7, 0.35), xycoords="figure fraction") pylab.annotate("in middle of right lane", xy=(0, y), **args) pylab.annotate("on right white tape", xy=(-W, y), **args) pylab.annotate("on left yellow tape", xy=(+W, y), **args) pylab.annotate("in other lane", xy=(+W * 1.3, y), **args) pylab.axis([f_d(d_min), f_d(d_max), f_phi(phi_min), f_phi(phi_max)]) pylab.ylabel( f"phi: orientation (deg); cell = {f_phi(delta_phi):.1f} deg") pylab.xlabel( f"d: distance from center line (cm); cell = {f_d(delta_d):.1f} cm") return a.get_bgr()
def diffeo_show_mismatch(y0, y1): e = np.abs(y0 - y1) if e.ndim == 3: e.sum(axis=2) return scale(e, min_color=[0, 0, 0], max_color=[1, 0, 0])
def diffeo_show_mismatch(y0, y1): e = np.abs(y0 - y1) if e.ndim == 3: e.sum(axis=2) return scale(e, min_color=[0, 0, 0], max_color=[1, 0, 0])