def _key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key=='t': self._showverts = not self._showverts elif event.key=='d': ind = self._get_ind_under_point(event) if ind is not None: self._poly.xy = [tup for i,tup in enumerate(self._poly.xy) if i!=ind] self._line.set_data(zip(*self._poly.xy)) elif event.key=='i': xys = self._poly.get_transform().seq_xy_tups(self._poly.xy) p = event.x, event.y # display coords for i in range(len(xys)-1): s0 = xys[i] s1 = xys[i+1] d = dist_point_to_segment(p, s0, s1) if d<=self._epsilon: self._poly.xy.insert(i+1, (event.xdata, event.ydata)) self._line.set_data(zip(*self._poly.xy)) break s0 = xys[-1] s1 = xys[0] d = dist_point_to_segment(p, s0, s1) if d<=self._epsilon: self._poly.xy.append((event.xdata, event.ydata)) self._line.set_data(zip(*self._poly.xy)) self._draw_callback(event) self._canvas.draw()
def _key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key == 't': self._showverts = not self._showverts elif event.key == 'd': ind = self._get_ind_under_point(event) if ind is not None: self._poly.xy = [ tup for i, tup in enumerate(self._poly.xy) if i != ind ] self._line.set_data(zip(*self._poly.xy)) elif event.key == 'i': xys = self._poly.get_transform().seq_xy_tups(self._poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self._epsilon: self._poly.xy.insert(i + 1, (event.xdata, event.ydata)) self._line.set_data(zip(*self._poly.xy)) break s0 = xys[-1] s1 = xys[0] d = dist_point_to_segment(p, s0, s1) if d <= self._epsilon: self._poly.xy.append((event.xdata, event.ydata)) self._line.set_data(zip(*self._poly.xy)) self._draw_callback(event) self._canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [ tup for i, tup in enumerate(self.poly.xy) if i != ind ] self.line.set_data(zip(*self.poly.xy)) elif event.key == 'i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.array( list(self.poly.xy[:i]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i:])) self.line.set_data(zip(*self.poly.xy)) break self.canvas.draw()
def key_press_callback(self, event): "whenever a key is pressed" if not event.inaxes: return if event.key == "t": self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == "d": ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [tup for i, tup in enumerate(self.poly.xy) if i != ind] self.line.set_data(zip(*self.poly.xy)) elif event.key == "i": xys = self.poly.get_transform().seq_xy_tups(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy.insert(i + 1, (event.xdata, event.ydata)) self.line.set_data(zip(*self.poly.xy)) break self.canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = np.delete(self.poly.xy, ind, axis=0) self.line.set_data(zip(*self.poly.xy)) elif event.key == 'i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.insert( self.poly.xy, i+1, [event.xdata, event.ydata], axis=0) self.line.set_data(zip(*self.poly.xy)) break if self.line.stale: self.canvas.draw_idle()
def button_press_callback(self, event): 'whenever a mouse button is pressed' if event.inaxes is None: return self.buttonHold = True # source selection if self.v_active_ind is not None: self.v_source_ind = self.v_active_ind self.ax.mouse_init(rotate_btn=2) # disable mouse rotation self.background_newEdgeDrawing = self.canvas.copy_from_bbox( self.ax.bbox) return # edge selection p = event.x, event.y # display coords for i in range(len(self.edges.source_target)): (s, t) = self.edges.source_target[i] d = dist_point_to_segment(p, (self.x_scr[s], self.y_scr[s]), (self.x_scr[t], self.y_scr[t])) if d <= self.lw_active: self.select_edge(self.edges.ids[i]) return self.e_ind = None
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key=='t': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key=='d': ind = self.get_ind_under_cursor(event) if ind is None: return if ind == 0 or ind == self.last_vert_ind: print("Cannot delete root node") return self.poly.xy = [tup for i,tup in enumerate(self.poly.xy) if i!=ind] self._update_line() elif event.key=='i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # cursor coords for i in range(len(xys)-1): s0 = xys[i] s1 = xys[i+1] d = dist_point_to_segment(p, s0, s1) if d <= self.max_ds: self.poly.xy = np.array( list(self.poly.xy[:i+1]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i+1:])) self._update_line() break self.canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.lx.pop(ind) self.ly.pop(ind) self.line.set_data(self.lx, self.ly) elif event.key == 'i': #xys = self.poly.get_transform().seq_xy_tups(self.poly.verts) p = (event.xdata, event.ydata) # display coords for i in range(len(self.lx) - 1): s0 = (self.lx[i], self.ly[i]) #xys[i] s1 = (self.lx[i + 1], self.ly[i + 1]) #xys[i+1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.lx.insert(i + 1, event.xdata) self.ly.insert(i + 1, event.ydata) self.line.set_data(self.lx, self.ly) break self.canvas.draw_idle()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key=='t': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key=='d': ind = self.get_ind_under_cursor(event) if ind is None: return if ind == 0 or ind == self.last_vert_ind: print "Cannot delete root node" return self.poly.xy = [tup for i,tup in enumerate(self.poly.xy) if i!=ind] self._update_line() elif event.key=='i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # cursor coords for i in range(len(xys)-1): s0 = xys[i] s1 = xys[i+1] d = dist_point_to_segment(p, s0, s1) if d <= self.max_ds: self.poly.xy = np.array( list(self.poly.xy[:i+1]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i+1:])) self._update_line() break self.canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [tup for i, tup in enumerate(self.poly.xy) if i != ind] self.line.set_data(zip(*self.poly.xy)) elif event.key == 'i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.array( list(self.poly.xy[:i]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i:])) self.line.set_data(zip(*self.poly.xy)) break self.canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = np.delete(self.poly.xy, ind, axis=0) self.line.set_data(zip(*self.poly.xy)) elif event.key == 'i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.insert(self.poly.xy, i + 1, [event.xdata, event.ydata], axis=0) self.line.set_data(zip(*self.poly.xy)) break if self.line.stale: self.canvas.draw_idle()
def key_press_callback(self, event): "whenever a key is pressed" if not event.inaxes: return if event.key == "t": self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == "d": ind = self.get_ind_under_point(event) if ind is not None: self.lx.pop(ind) self.ly.pop(ind) self.line.set_data(self.lx, self.ly) elif event.key == "i": # xys = self.poly.get_transform().seq_xy_tups(self.poly.verts) p = (event.xdata, event.ydata) # display coords for i in range(len(self.lx) - 1): s0 = (self.lx[i], self.ly[i]) # xys[i] s1 = (self.lx[i + 1], self.ly[i + 1]) # xys[i+1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.lx.insert(i + 1, event.xdata) self.ly.insert(i + 1, event.ydata) self.line.set_data(self.lx, self.ly) break self.canvas.draw_idle()
def get_distance_to_line(p1, p2, p3): ''' Return the distance to the line, defined by point p1 and p2 of point p3 This method was redesigned after the matplotlib method was found ''' # print "Points {0},{1},{2}".format(p1,p2,p3) d = dist_point_to_segment(p3, p1, p2) return d
def get_line_under_point(self, event): 'get the index of the line under point if within epsilon tolerance' p = event.x, event.y # display coords for i in range(len(self.vertices) - 1): s0 = self.ax_udc.transData.transform(self.vertices[i]) s1 = self.ax_udc.transData.transform(self.vertices[i + 1]) d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: return i return None
def insert_datapoint(self, event): """ inserts a new data point between the segment that is closest in polygon """ if self.xy_values.shape[0] <= 2: self.add_point(event.xdata, event.ydata) else: event_point = event.xdata, event.ydata prev_d = dist_point_to_segment(event_point, self.xy_values[0], self.xy_values[-1]) prev_i = len(self.xy_values) for i in range(len(self.xy_values) - 1): seg_start = self.xy_values[i] seg_end = self.xy_values[i + 1] dist_p_s = dist_point_to_segment(event_point, seg_start, seg_end) if dist_p_s < prev_d: prev_i = i prev_d = dist_p_s self.xy_values = np.array( list(self.xy_values[:prev_i + 1]) + [(event.xdata, event.ydata)] + list(self.xy_values[prev_i + 1:])) self.refresh()
def insert_point(self, x, y, xdata, ydata): coords = np.vstack([self.poly.xy, self.poly.xy[0, :]]) xys = self.poly.get_transform().transform(coords) p = x, y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.array( list(coords[:i + 1]) + [(xdata, ydata)] + list(coords[i + 1:])) coords = np.vstack([self.poly.xy, self.poly.xy[0, :]]) self.line.set_data(zip(*coords)) break
def button_press_callback(self, event): """whenever a mouse button is pressed""" if event.inaxes is None: return if event.button != 1: return self._ind = self.get_ind_under_point(event) if self._ind is None: xys = self.poly.get_transform().transform(self.poly.xy) ptmp = event.x, event.y # display coords if len(xys) == 1: self.poly.xy = np.array( [(event.xdata, event.ydata)] + [(event.xdata, event.ydata)]) self.line.set_data(list(zip(*self.poly.xy))) self.canvas.restore_region(self.background) self.ax.draw_artist(self.poly) self.ax.draw_artist(self.line) self.canvas.update() return dmin = -1 imin = -1 for i in range(len(xys) - 1): s0tmp = xys[i] s1tmp = xys[i + 1] dtmp = dist_point_to_segment(ptmp, s0tmp, s1tmp) if dmin == -1: dmin = dtmp imin = i elif dtmp < dmin: dmin = dtmp imin = i i = imin self.poly.xy = np.array(list(self.poly.xy[:i + 1]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i + 1:])) self.line.set_data(list(zip(*self.poly.xy))) self.canvas.restore_region(self.background) self.ax.draw_artist(self.poly) self.ax.draw_artist(self.line) self.canvas.update()
def button_press_callback(self, event): """whenever a mouse button is pressed""" if event.inaxes is None: return if event.button != 1: return self._ind = self.get_ind_under_point(event) if self._ind is None: xys = self.poly.get_transform().transform(self.poly.xy) ptmp = event.x, event.y # display coords if len(xys) == 1: self.poly.xy = np.array([(event.xdata, event.ydata)] + [(event.xdata, event.ydata)]) self.line.set_data(list(zip(*self.poly.xy))) self.canvas.restore_region(self.background) self.ax.draw_artist(self.poly) self.ax.draw_artist(self.line) self.canvas.update() return dmin = -1 imin = -1 for i in range(len(xys) - 1): s0tmp = xys[i] s1tmp = xys[i + 1] dtmp = dist_point_to_segment(ptmp, s0tmp, s1tmp) if dmin == -1: dmin = dtmp imin = i elif dtmp < dmin: dmin = dtmp imin = i i = imin self.poly.xy = np.array( list(self.poly.xy[:i + 1]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i + 1:])) self.line.set_data(list(zip(*self.poly.xy))) self.canvas.restore_region(self.background) self.ax.draw_artist(self.poly) self.ax.draw_artist(self.line) self.canvas.update()
def _addVertexOnBoundary(self, event): if self.empty_pol: return xys = self.pol.get_transform().transform(self.pol.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self._getSnapTresh(): self.pol.xy = np.array( list(self.pol.xy[:i + 1]) + [(event.xdata, event.ydata)] + list(self.pol.xy[i + 1:])) self.line.set_data(list(zip(*self.pol.xy))) break self.Redraw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [ tup for i, tup in enumerate(self.poly.xy) if i != ind ] self.line.set_data(zip(*self.poly.xy)) elif event.key == 'i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.array( list(self.poly.xy[:i]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i:])) self.line.set_data(zip(*self.poly.xy)) break elif event.key == 'n': """ Flips the region inside out of the polygon to be masked """ print('Inverting the mask region') self.maskinvert = not self.maskinvert elif event.key == 'c': """ Confirm the drawn polynomial shape and add update the mask """ self.UpdateMask() #Update the imshowed image with new mask self.imgplot.set_data( np.ma.filled( np.ma.array(self.img, mask=self.Mask, fill_value=np.nan))) self.imgplot.figure.canvas.draw() self.canvas.draw()
def _addVertexOnBoundary(self, event): if self.empty_pol: return xys = self.pol.get_transform().transform(self.pol.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self._getSnapTresh(): self.pol.xy = np.array( list(self.pol.xy[:i + 1]) + [(event.xdata, event.ydata)] + list(self.pol.xy[i + 1:])) self.line.set_data(zip(*self.pol.xy)) break self.Redraw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key=='t': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key=='d': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [tup for i,tup in enumerate(self.poly.xy) if i!=ind] self.line.set_data(zip(*self.poly.xy)) elif event.key=='i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys)-1): s0 = xys[i] s1 = xys[i+1] d = dist_point_to_segment(p, s0, s1) if d<=self.epsilon: self.poly.xy = np.array( list(self.poly.xy[:i]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i:])) self.line.set_data(zip(*self.poly.xy)) break elif event.key=='n': """ Flips the region inside out of the polygon to be masked """ print('Inverting the mask region') self.maskinvert = not self.maskinvert elif event.key=='c': """ Confirm the drawn polynomial shape and add update the mask """ self.UpdateMask() #Update the imshowed image with new mask self.imgplot.set_data(np.ma.filled(np.ma.array(self.img,mask=self.Mask,fill_value=np.nan))) self.imgplot.figure.canvas.draw() self.canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key=='t' or event.key=='alt+t': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key=='d' or event.key=='alt+d': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [tup for i,tup in enumerate(self.poly.xy) if i!=ind] self.line.set_data(zip(*self.poly.xy)) elif event.key=='i' or event.key=='alt+i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys)-1): s0 = xys[i] s1 = xys[i+1] d = dist_point_to_segment(p, s0, s1) if d<=self.epsilon: self.poly.xy = np.array( list(self.poly.xy[:i]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i:])) self.line.set_data(zip(*self.poly.xy[:-1])) break elif event.key=='s' or event.key=='alt+s': filename = str('Contour(' + str(datetime.datetime.today()).replace(' ',', ').replace(':',' ') + ')' + '.txt') file = open(filename, 'w') file.write(str(self.poly.xy[0:-1])) self.canvas.draw()
def _key_press_callback(self, event): 'whenever a key is pressed' if not event.inaxes: return if event.key=='shift': return if event.key=='t': self._showbetas = not self._showbetas self._line.set_visible(self._showbetas) self._pline.set_visible(self._showbetas) self._mline.set_visible(self._showbetas) self._zline.set_visible(self._showbetas) self._sline.set_visible(self._showbetas) elif event.key=='d': ind = self._get_ind_under_point(event) if ind is not None: self._poly.xy = [tup for i,tup in enumerate(self._poly.xy) \ if i!=ind] self._line.set_data(zip(*self._poly.xy)) self.beta = [beta for i,beta in enumerate(self.beta) \ if i!=ind] elif event.key=='p': ind = self._get_ind_under_point(event) if ind is not None: self.beta[ind] = 1.0 elif event.key=='m': ind = self._get_ind_under_point(event) if ind is not None: self.beta[ind] = -1.0 elif event.key=='z': ind = self._get_ind_under_point(event) if ind is not None: self.beta[ind] = 0.0 elif event.key=='s': ind = self._get_ind_under_point(event) if ind is not None: self.gridgen_options['ul_idx'] = ind elif event.key=='i': xys = self._poly.get_transform().transform(self._poly.xy) p = event.x, event.y # display coords for i in range(len(xys)-1): s0 = xys[i] s1 = xys[i+1] d = dist_point_to_segment(p, s0, s1) if d<=self._epsilon: self._poly.xy = np.array( list(self._poly.xy[:i+1]) + [(event.xdata, event.ydata)] + list(self._poly.xy[i+1:])) self._line.set_data(zip(*self._poly.xy)) self.beta.insert(i+1, 0) break s0 = xys[-1] s1 = xys[0] d = dist_point_to_segment(p, s0, s1) if d<=self._epsilon: self._poly.xy = np.array( list(self._poly.xy) + [(event.xdata, event.ydata)]) self._line.set_data(zip(*self._poly.xy)) self.beta.append(0) elif event.key=='G' or event.key == '1': options = deepcopy(self.gridgen_options) shp = options.pop('shp') if self.proj is None: x = self.x y = self.y self.grd = Gridgen(x, y, self.beta, shp, proj=self.proj, **options) else: lon, lat = self.proj(self.x, self.y, inverse=True) self.grd = Gridgen(lon, lat, self.beta, shp, proj=self.proj, **options) self.remove_grid() self._showgrid = True gridlineprops = {'linestyle':'-', 'color':'k', 'lw':0.2} self._gridlines = [] for line in self._ax._get_lines(*(self.grd.x, self.grd.y), **gridlineprops): self._ax.add_line(line) self._gridlines.append(line) for line in self._ax._get_lines(*(self.grd.x.T, self.grd.y.T), **gridlineprops): self._ax.add_line(line) self._gridlines.append(line) elif event.key=='T' or event.key == '2': self._showgrid = not self._showgrid if hasattr(self, '_gridlines'): for line in self._gridlines: line.set_visible(self._showgrid) self._update_beta_lines() self._draw_callback(event) self._canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' # if not event.inaxes: # return if event.inaxes: if event.key in [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-' ]: ind = self.get_ind_under_point(event) if not (ind is None or ind == 0 or ind == len(self.poly.xy) - 1): if event.key == '-': val = 1 else: val = int(event.key) / 10 self.poly.xy[ind][1] = self.maxY * val self.line.set_data(zip(*self.poly.xy)) if event.key == 'ctrl+w': print('saving specrtum form... ', end='') x, y = zip(*self.poly.xy) try: f = open(self.file_name, 'wb') except (OSError) as msg: print(msg, file=sys.stderr) return None # print('%d %d' % (max(x), float(max(y)))) # при сохранении отбрасываем две точки - первую и последнюю, тк они имеют только декоративную функцию f.write( struct.pack(HEADER_STRUCT, FILE_DESIGNATION, FILE_VER, len(x) - 2, self.fmin, self.fmax)) for i in range(1, len(x) - 1, 1): f.write(struct.pack('d', float(y[i]))) f.close() print('ok') if event.key == 'ctrl+r': x, y = zip(*self.poly.xy) # fpcnt = # len(self.aspec) # int(self.sampling / 2) controls_count = len(x) step = get_xstep(self.fmax - self.fmin, controls_count - 2) # fpcnt / (controls_count - 3) # print('fpcnt=%i controls_count=%i step=%d' % (fpcnt,controls_count,step)) self.poly.xy[0][0] = self.fmin #0 self.poly.xy[-1][0] = self.fmax # fpcnt maxY = 100 # max(self.aspec) for i in range(1, controls_count - 1): self.poly.xy[i][0] = self.fmin + (i - 1) * step self.poly.xy[i][1] = maxY # print('x=%i y=%f' % (self.poly.xy[i][0], self.poly.xy[i][1])) self.line.set_data(zip(*self.poly.xy)) if event.key in ['ctrl+u', 'ctrl+d']: x, y = zip(*self.poly.xy) controls_count = len(x) step = get_xstep(self.fmax - self.fmin, controls_count - 2) # fpcnt / (controls_count - 3) # print('fpcnt=%i controls_count=%i step=%d' % (fpcnt,controls_count,step)) self.poly.xy[0][0] = self.fmin #0 self.poly.xy[-1][0] = self.fmax # fpcnt maxY = 100 # max(self.aspec) stepY = maxY / (controls_count - 3) for i in range(1, controls_count - 1): self.poly.xy[i][0] = self.fmin + (i - 1) * step if event.key == 'ctrl+u': self.poly.xy[i][1] = i * stepY elif event.key == 'ctrl+d': self.poly.xy[i][1] = maxY - i * stepY self.line.set_data(zip(*self.poly.xy)) if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [ tup for i, tup in enumerate(self.poly.xy) if i != ind ] self.line.set_data(zip(*self.poly.xy)) elif event.key == 'i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.array( list(self.poly.xy[:i]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i:])) self.line.set_data(zip(*self.poly.xy)) break self.canvas.draw()
def key_press_callback(self, event): 'whenever a key is pressed' def print_pts(pts): s = "[" pts2 = [] for i in range(4): s += "({:d}, {:d}), ".format(int(pts[i][0]), int(pts[i][1])) pts2.append((int(pts[i][0]), int(pts[i][1]))) s = s[:-2] + "]" print(s) return tuple(pts2) def CalcDstCoords(botL, topL, topR, botR): d_botL = (botL[0], 700) d_botR = (botR[0], 700) d_topL = (d_botL[0], 50) d_topR = (d_botR[0], 50) return d_botL, d_topL, d_topR, d_botR def transformImage(img, matrix): return cv2.warpPerspective(img, matrix, (img.shape[:2])[::-1], flags=cv2.INTER_LINEAR) def polylines(im, pts, color=[0, 200, 0], thickness=5, isClosed=True): return cv2.polylines(im, pts=np.array([pts], dtype=np.int32), isClosed=isClosed, color=color, thickness=thickness) def rect(im, topLeft, sizeWH, color=[0, 200, 0], thickness=5): x, y = topLeft w, h = sizeWH pts = np.array([(x, y), (x + w, y), (x + w, y + h), (x, y + h)], dtype=np.int) return polylines(im, pts, color=color, thickness=thickness, isClosed=True) def project(pts): src = np.array(pts) dst = np.array(CalcDstCoords(*src)) matrix = cv2.getPerspectiveTransform(src.astype(np.float32), dst.astype(np.float32)) imgNew = transformImage(IMG, matrix) rect(imgNew, dst[1], ((dst[2][0] - dst[1][0]), (dst[2][1] - dst[1][1]))) ax2.imshow(imgNew) fig2.canvas.draw() newPts = print_pts(self.poly.xy) project(newPts) if not event.inaxes: return if event.key == 't': self.showverts = not self.showverts self.line.set_visible(self.showverts) if not self.showverts: self._ind = None elif event.key == 'd': ind = self.get_ind_under_point(event) if ind is not None: self.poly.xy = [ tup for i, tup in enumerate(self.poly.xy) if i != ind ] self.line.set_data(zip(*self.poly.xy)) elif event.key == 'i': xys = self.poly.get_transform().transform(self.poly.xy) p = event.x, event.y # display coords for i in range(len(xys) - 1): s0 = xys[i] s1 = xys[i + 1] d = dist_point_to_segment(p, s0, s1) if d <= self.epsilon: self.poly.xy = np.array( list(self.poly.xy[:i]) + [(event.xdata, event.ydata)] + list(self.poly.xy[i:])) self.line.set_data(zip(*self.poly.xy)) break self.canvas.draw()