def getShift(imgdata1, imgdata2):
    # assumes images are square
    print "Finding shift between", apDisplay.short(imgdata1["filename"]), "and", apDisplay.short(imgdata2["filename"])
    dimension1 = imgdata1["camera"]["dimension"]["x"]
    binning1 = imgdata1["camera"]["binning"]["x"]
    dimension2 = imgdata2["camera"]["dimension"]["x"]
    binning2 = imgdata2["camera"]["binning"]["x"]
    finalsize = 512

    # test to make sure images are at same mag
    if imgdata1["scope"]["magnification"] != imgdata2["scope"]["magnification"]:
        apDisplay.printWarning("Defocus pairs are at different magnifications, so shift can't be calculated.")
        return None

    # test to see if images capture the same area
    if (dimension1 * binning1) != (dimension2 * binning2):
        apDisplay.printWarning("Defocus pairs do not capture the same imaging area, so shift can't be calculated.")
        return None

    # images must not be less than finalsize (currently 512) pixels. This is arbitrary but for good reason
    if dimension1 < finalsize or dimension2 < finalsize:
        apDisplay.printWarning("Images must be greater than " + finalsize + " pixels to calculate shift.")
        return None

    # low pass filter 2 images to twice the final pixelsize BEFORE binning
    shrinkfactor1 = dimension1 / finalsize
    shrinkfactor2 = dimension2 / finalsize
    binned1 = apImage.filterImg(imgdata1["image"], 1.0, shrinkfactor1 * 2)
    binned2 = apImage.filterImg(imgdata2["image"], 1.0, shrinkfactor2 * 2)

    # now bin 2 images
    binned1 = apImage.binImg(binned1, shrinkfactor1)
    binned2 = apImage.binImg(binned2, shrinkfactor2)

    ### fix for non-square images, correlation fails on non-square images
    mindim = min(binned1.shape)
    binned1 = binned1[:mindim, :mindim]
    binned2 = binned2[:mindim, :mindim]

    ### use phase correlation, performs better than cross
    pc = correlator.phase_correlate(binned1, binned2)
    apImage.arrayToMrc(pc, "phaseCorrelate.mrc")

    ### find peak, filtering to 10.0 helps
    peak = peakfinder.findSubpixelPeak(pc, lpf=10.0)
    subpixpeak = peak["subpixel peak"]
    shift = correlator.wrap_coord(subpixpeak, pc.shape)
    peak["scalefactor"] = dimension2 / float(dimension1)
    # print shift[0]*shrinkfactor1, shift[1]*shrinkfactor1
    xshift = int(round(shift[0] * shrinkfactor1))
    yshift = int(round(shift[1] * shrinkfactor1))
    peak["shift"] = numpy.array((xshift, yshift))
    apDisplay.printMsg("Determined shifts: %f %f" % (peak["shift"][0], peak["shift"][1]))
    # print peak['shift']
    # sys.exit(1)
    return peak
Exemple #2
0
def tmpRemoveCrud(params, imagefile):
    bin = int(params["bin"])
    apix = float(params["apix"])
    diam = float(params["diam"])
    lowpass = float(params["lp"])
    pixrad = diam / apix / 2.0

    #READ IMAGES
    #imagefile=imagefile+'.mrc'
    #image = Mrc.mrc_to_numeric(imagefile)
    image = apDatabase.getImageData(imagefile)['image']

    #BIN IMAGES
    image = bin_img(image, bin)

    #NORMALIZE
    image = normStdev(image)
    #       image    = PlaneRegression(image)
    #       image    = normStdev(image)

    #LOW PASS FILTER
    image = apImage.filterImg(image, apix * float(bin), lowpass)

    #BLACK OUT DARK AREAS, LESS THAN 2 STDEVS
    image = removeCrud(image, imagefile, -1.0, params)
    Mrc.numeric_to_mrc(image, (imagefile.split('.')[0] + '.dwn.mrc'))
    return ()
def tmpRemoveCrud(params,imagefile):
	bin     = int(params["bin"])
	apix    = float(params["apix"])
	diam    = float(params["diam"])
	lowpass	= float(params["lp"])
	pixrad  = diam/apix/2.0
	

	#READ IMAGES
	#imagefile=imagefile+'.mrc'
	#image = Mrc.mrc_to_numeric(imagefile)
	image = apDatabase.getImageData(imagefile)['image']

	#BIN IMAGES
	image    = bin_img(image,bin)

	#NORMALIZE
	image    = normStdev(image)
#	image    = PlaneRegression(image)
#	image    = normStdev(image)

	#LOW PASS FILTER
	image    = apImage.filterImg(image,apix*float(bin),lowpass)

	#BLACK OUT DARK AREAS, LESS THAN 2 STDEVS
	image = removeCrud(image,imagefile,-1.0,params)
	Mrc.numeric_to_mrc(image,(imagefile.split('.')[0]+'.dwn.mrc'))
	return()
def getShift(imgdata1, imgdata2):
    #assumes images are square
    print "Finding shift between", apDisplay.short(
        imgdata1['filename']), "and", apDisplay.short(imgdata2['filename'])
    dimension1 = imgdata1['camera']['dimension']['x']
    binning1 = imgdata1['camera']['binning']['x']
    dimension2 = imgdata2['camera']['dimension']['x']
    binning2 = imgdata2['camera']['binning']['x']
    finalsize = 512

    #test to make sure images are at same mag
    if imgdata1['scope']['magnification'] != imgdata2['scope']['magnification']:
        apDisplay.printWarning(
            "Defocus pairs are at different magnifications, so shift can't be calculated."
        )
        return None

    #test to see if images capture the same area
    if (dimension1 * binning1) != (dimension2 * binning2):
        apDisplay.printWarning(
            "Defocus pairs do not capture the same imaging area, so shift can't be calculated."
        )
        return None

    #images must not be less than finalsize (currently 512) pixels. This is arbitrary but for good reason
    if dimension1 < finalsize or dimension2 < finalsize:
        apDisplay.printWarning("Images must be greater than " + finalsize +
                               " pixels to calculate shift.")
        return None

    #low pass filter 2 images to twice the final pixelsize BEFORE binning
    shrinkfactor1 = dimension1 / finalsize
    shrinkfactor2 = dimension2 / finalsize
    binned1 = apImage.filterImg(imgdata1['image'], 1.0, shrinkfactor1 * 2)
    binned2 = apImage.filterImg(imgdata2['image'], 1.0, shrinkfactor2 * 2)

    #now bin 2 images
    binned1 = apImage.binImg(binned1, shrinkfactor1)
    binned2 = apImage.binImg(binned2, shrinkfactor2)

    ### fix for non-square images, correlation fails on non-square images
    mindim = min(binned1.shape)
    binned1 = binned1[:mindim, :mindim]
    binned2 = binned2[:mindim, :mindim]

    ### use phase correlation, performs better than cross
    pc = correlator.phase_correlate(binned1, binned2)
    apImage.arrayToMrc(pc, "phaseCorrelate.mrc")

    ### find peak, filtering to 10.0 helps
    peak = peakfinder.findSubpixelPeak(pc, lpf=10.0)
    subpixpeak = peak['subpixel peak']
    shift = correlator.wrap_coord(subpixpeak, pc.shape)
    peak['scalefactor'] = dimension2 / float(dimension1)
    #print shift[0]*shrinkfactor1, shift[1]*shrinkfactor1
    xshift = int(round(shift[0] * shrinkfactor1))
    yshift = int(round(shift[1] * shrinkfactor1))
    peak['shift'] = numpy.array((xshift, yshift))
    apDisplay.printMsg("Determined shifts: %f %f" %
                       (peak['shift'][0], peak['shift'][1]))
    #print peak['shift']
    #sys.exit(1)
    return peak