def pixel2scan(self, pt): """ Convert pixels coordinates to scan coordinates (mV) Arguments --------- pt : array points in pixel coordinates (x,y) Returns ------- ptx (array): point in scan coordinates (sweep, step) """ ptx = pgeometry.projectiveTransformation(self.Hi, np.array(pt).astype(float)) extent, g0, g1, vstep, vsweep, arrayname = dataset2Dmetadata( self.dataset, arrayname=self.arrayname, verbose=0) nx = vsweep.size ny = vstep.size xx = extent x = ptx nn = pt.shape[1] ptx = np.zeros((2, nn)) f = scipy.interpolate.interp1d([0, ny - 1], [xx[2], xx[3]], assume_sorted=False, fill_value='extrapolate') ptx[1, :] = f(x[1, :]) # step f = scipy.interpolate.interp1d([0, nx - 1], [xx[0], xx[1]], assume_sorted=False, fill_value='extrapolate') ptx[0, :] = f(x[0, :]) # sweep return ptx
def scan2pixel(self, pt): """ Convert scan coordinates to pixel coordinates Arguments --------- pt : array points in scan coordinates Returns: ptpixel (ndaray): points in pixel coordinates """ extent, g0, g1, vstep, vsweep, arrayname = dataset2Dmetadata( self.dataset, arrayname=self.arrayname, verbose=0) nx = vsweep.size ny = vstep.size xx = extent x = pt nn = pt.shape[1] ptpixel = np.zeros((2, nn)) f = scipy.interpolate.interp1d([xx[2], xx[3]], [0, ny - 1], assume_sorted=False) ptpixel[1, :] = f(x[1, :]) f = scipy.interpolate.interp1d([xx[0], xx[1]], [0, nx - 1], assume_sorted=False) ptpixel[0, :] = f(x[0, :]) # sweep to pixel x ptpixel = pgeometry.projectiveTransformation( self.H, np.array(ptpixel).astype(float)) return ptpixel
def transformGateScan(self, vals2D, nn=None): """ Get a list of parameter names and [c1 c2 c3 c4] 'corner' values to generate dictionary self.vals2D[name] = matrix of values. Args: vals2D (dict): keys are the gate names, values are matrices with the gate values. nn : TODO. Returns: dict: tranformed gate values. """ vals2Dout = {} zz = np.zeros(nn if nn is not None else (), dtype=float) if isinstance(vals2D, dict): xx = [vals2D.get(s, zz) for s in self.sourcenames] xx = [x.flatten() for x in xx] gate_values = np.vstack(xx).astype(float) else: xx = vals2D gate_values = np.array(xx).astype(float) gate_values_out = pgeometry.projectiveTransformation( self.Vmatrix, gate_values) for j, n in enumerate(self.targetnames): vals2Dout[n] = gate_values_out[j].reshape(nn).astype(float) return vals2Dout
def evaluateCross(param, im, verbose=0, fig=None, istep=1, istepmodel=1, linewidth=2, usemask=False, use_abs=False, w=2.5): """ Calculate cross matching score Args: param (array or list): used by createCross to create image template im (numpy array): Returns: cost, patch, cdata, tuple See also: createCross """ samplesize = [int(im.shape[1] * istep / istepmodel), int(im.shape[0] * istep / istepmodel)] param = np.array(param) aa = param[3:] H = evaluateCross.scaling0.copy() H[0, 0] = istep / istepmodel H[1, 1] = istep / istepmodel dsize = (samplesize[0], samplesize[1]) patch = cv2.warpPerspective(im.astype(np.float32), H, dsize, None, (cv2.INTER_LINEAR), cv2.BORDER_CONSTANT, -1) if verbose: print('evaluateCross: patch shape %s' % (patch.shape,)) modelpatch, cdata = createCross(param, samplesize, centermodel=False, istep=istepmodel, verbose=verbose >= 2, w=w) (cc, lp, hp, ip, op, _, _, _) = cdata if use_abs: dd = np.abs(patch) - modelpatch else: dd = patch - modelpatch if usemask: # near model mask #mask = (modelpatch>1).astype(int) # distance centre mask imtmp = 10 + 0 * modelpatch.copy() imtmp[int(imtmp.shape[1] / 2), int(imtmp.shape[0] / 2)] = 0 mask = scipy.ndimage.distance_transform_edt(imtmp) mask = 1 - .75 * mask / mask.max() dd = dd * mask cost = np.linalg.norm(dd) # area of intersection rr = np.array([[0., im.shape[1]], [0, im.shape[0]]]) ppx = pgeometry.region2poly(np.array([[0, samplesize[0]], [0., samplesize[1]]])) ppimage = pgeometry.region2poly(rr) pppatch = pgeometry.projectiveTransformation(H, ppimage) ppi = pgeometry.polyintersect(ppx.T, pppatch.T).T A = pgeometry.polyarea(ppi.T) A0 = pgeometry.polyarea(ppx.T) # special rules if np.abs(A / A0) < .85: if verbose: print(' add cost for A/A0: A %f A0 %f' % (A, A0)) cost += 4000 if aa[0] < 0 or aa[0] > np.pi / 2 - np.deg2rad(5): cost += 10000 if aa[1] < np.pi or aa[1] > 3 * np.pi / 2: if verbose: print(' add cost for alpha') cost += 10000 if aa[2] < np.pi or aa[2] > 3 * np.pi / 2: if verbose: print(' add cost for alpha') cost += 10000 if aa[3] < 0 or aa[3] > np.pi / 2: if verbose: print(' add cost for alpha') cost += 10000 if 1: ccim = (np.array(im.shape) / 2 + .5) * istep tmp = np.linalg.norm(ccim - param[0:2]) dcost = 2000 * pgeometry.logistic(tmp, np.mean(ccim), istep) if verbose: print(' add cost for image cc: %.1f' % dcost) cost += dcost if pgeometry.angleDiff(aa[0], aa[1]) < np.deg2rad(30): if verbose: print(' add cost for angle diff') cost += 1000 if pgeometry.angleDiff(aa[2], aa[3]) < np.deg2rad(30): if verbose: print(' add cost for angle diff') cost += 1000 if pgeometry.angleDiffOri(aa[0], aa[2]) > np.deg2rad(10): cost += 10000 if pgeometry.angleDiffOri(aa[1], aa[3]) > np.deg2rad(10): cost += 10000 if param[2] < 0: if verbose: print(' add cost for negative param') cost += 10000 if np.abs(param[2]) > 7.: if verbose: print(' add cost large param[2]') cost += 10000 if np.abs(param[2] - 10) > 8: cost += 10000 if len(param) > 7: if np.abs(pgeometry.angleDiff(param[7], np.pi / 4)) > np.deg2rad(30): cost += 10000 if not fig is None: _showIm(patch, fig=fig) plt.title('Image patch: cost %.1f: istep %.2f' % (cost, istepmodel)) pgeometry.addfigurecopy(fig=fig) plt.plot([float(lp[0]), float(hp[0])], [float(lp[1]), float(hp[1])], '.--m', linewidth=linewidth, markersize=10, label='transition line') plt.plot(cc[0], cc[1], '.m', markersize=12) for ii in range(4): if ii == 0: lbl = 'electron line' else: lbl = None plt.plot([op[ii, 0], ip[ii, 0]], [op[ii, 1], ip[ii, 1]], '.-', linewidth=linewidth, color=[0, .7, 0], label=lbl) pgeometry.plotLabels(np.array((op[ii, :] + ip[ii, :]) / 2).reshape((2, -1)), '%d' % ii) if verbose >= 1: _showIm(modelpatch, fig=fig + 1) plt.title('Model patch: cost %.1f' % cost) _showIm(np.abs(dd), fig=fig + 2) plt.title('diff patch: cost %.1f' % cost) plt.colorbar() plt.show() if verbose: print('evaluateCross: cost %.4f' % cost) #print('evaluateCross: param %s' % (str(param), )) return cost, patch, cdata, (H, ) pass