def make_pixel_lut(self, dims): """ Generate an x and y image which maps the array indices into floating point array indices (to be corrected for pixel size later) returns FIXME - check they are the right way around add some sort of known splinefile testcase """ # Cache the value in case of multiple calls if self.pixel_lut is None: x_im = numpy.outer(range(dims[0]), numpy.ones(dims[1])) y_im = numpy.outer(numpy.ones(dims[0]), range(dims[1])) # xcor is tck2 x_im = numpy.add( x_im, bisplev.bisplev(range(dims[1]), range(dims[0]), self.tck2).T, x_im) # ycor is tck1 y_im = numpy.add( y_im, bisplev.bisplev(range(dims[1]), range(dims[0]), self.tck1).T, y_im) self.pixel_lut = x_im, y_im return self.pixel_lut
def correct(self, xin, yin): """ Transform x,y in raw image coordinates into x,y of an idealised image. Returns a tuple (x,y), expects a pair of floats as arguments """ if self.orientation == "edf": xcor = xin + bisplev.bisplev(yin, xin, self.tck2) ycor = yin + bisplev.bisplev(yin, xin, self.tck1) else: # fit2d does a flip raise Exception("Spline orientations must be edf, convert " "your image to edf and remake the spline") # Unreachable code - we no longer accept this complexity # it means the spline file for ImageD11 bruker images # is not the same as for fit2d. xpos = self.xmax - xin xcor = xin - bisplev.bisplev(yin, xpos, self.tck2) ycor = yin + bisplev.bisplev(yin, xpos, self.tck1) return xcor, ycor
def distort(self, xin, yin): """ Distort a pair of points xnew, ynew to find where they would be in a raw image Iterative algorithm... """ yold = yin - bisplev.bisplev(yin, xin, self.tck1) xold = xin - bisplev.bisplev(yin, xin, self.tck2) # First guess, assumes distortion is constant ytmp = yin - bisplev.bisplev(yold, xold, self.tck1) xtmp = xin - bisplev.bisplev(yold, xold, self.tck2) # Second guess should be better error = math.sqrt((xtmp - xold) * (xtmp - xold) + (ytmp - yold) * (ytmp - yold)) ntries = 0 while error > self.tolerance: ntries = ntries + 1 xold = xtmp yold = ytmp ytmp = yin - bisplev.bisplev(yold, xold, self.tck1) xtmp = xin - bisplev.bisplev(yold, xold, self.tck2) error = math.sqrt((xtmp - xold) * (xtmp - xold) + (ytmp - yold) * (ytmp - yold)) # print error,xold,x,yold,y if ntries == 10: raise Exception("Error getting the inverse spline to converge") return xtmp, ytmp