### this step is TOO SLOW ## ellip angle is positive toward y-axis radial = ctftools.getEllipticalDistanceArray(ellipratio, math.degrees(alpha), shape) meanradius = math.sqrt(a*b) filledEllipse = numpy.where(radial > meanradius, False, True) return filledEllipse #================= #================= if __name__ == "__main__": from scipy.misc import lena from matplotlib import pyplot from appionlib.apCtf import canny lena = lena() edgeMap = canny.canny_edges(lena, 5, 0.25, 0.75) edgeMapInv = numpy.flipud(numpy.fliplr(edgeMap)) edgeMap = numpy.logical_or(edgeMap,edgeMapInv) thresh = 3 t0 = time.time() ellipseParams = ellipseRANSAC(edgeMap) print time.time()-t0, "seconds" if ellipseParams is None: raise filled1 = generateEllipseRangeMap2(ellipseParams, thresh, edgeMap.shape) filled2 = generateEllipseRangeMap2(ellipseParams, thresh*3, edgeMap.shape) filled = edgeMap + filled2 - filled1
def findEllipseEdge(self, fftarray, mindef=None, maxdef=None): if mindef is None: mindef = self.params['mindef'] if maxdef is None: maxdef = self.params['maxdef'] minpeaks = ctftools.getCtfExtrema(maxdef, self.freq*1e10, self.ctfvalues['cs'], self.ctfvalues['volts'], 0.0, numzeros=3, zerotype="peaks") minEdgeRadius = minpeaks[0] maxvalleys = ctftools.getCtfExtrema(mindef, self.freq*1e10, self.ctfvalues['cs'], self.ctfvalues['volts'], 0.0, numzeros=3, zerotype="valleys") minEdgeRadius = (maxvalleys[0]+minpeaks[0])/2 maxEdgeRadius = maxvalleys[2] minCircum = math.ceil(2 * minEdgeRadius * math.pi) minEdges = int(minCircum) maxCircum = math.ceil(2 * maxEdgeRadius * math.pi) maxEdges = 2*int(maxCircum) dograd = (minpeaks[1] - minpeaks[0])/3.0 dogarray = apDog.diffOfGauss(fftarray, dograd, k=1.2) #dogarray = fftarray print "Edge range: ", minEdges, maxEdges edges = canny.canny_edges(dogarray, low_thresh=0.01, minEdgeRadius=minEdgeRadius, maxEdgeRadius=maxEdgeRadius, minedges=minEdges, maxedges=maxEdges) #minedges=2500, maxedges=15000, invedges = numpy.fliplr(numpy.flipud(edges)) self.edgeMap = numpy.logical_and(edges, invedges) edges = self.edgeMap * (dogarray.max()/self.edgeMap.max()) imagedata = dogarray.copy() + edges imagefile.arrayToJpeg(imagedata, "edges.jpg") edgeThresh = 3 ellipseParams = ransac.ellipseRANSAC(self.edgeMap, edgeThresh, maxRatio=self.maxRatio) if ellipseParams is None: return None fitEllipse1 = ransac.generateEllipseRangeMap2(ellipseParams, edgeThresh, self.edgeMap.shape) fitEllipse2 = ransac.generateEllipseRangeMap2(ellipseParams, edgeThresh*3, self.edgeMap.shape) outlineEllipse = fitEllipse2 - fitEllipse1 # image is draw upside down #outlineEllipse = numpy.flipud(outlineEllipse) imagedata = imagedata + 0.5*outlineEllipse*imagedata.max() if self.debug is True: from matplotlib import pyplot pyplot.clf() pyplot.imshow(imagedata) pyplot.gray() pyplot.show() self.ellipseParams = ellipseParams #self.convertEllipseToCtf(node=3) return ellipseParams