def dog(image, start=1.0, stop=None, levels=3, psize=1, threshold=0.0, edge_threshold=3.0): image = image.astype("f8") threshold *= 5.0 features = [] pyramid = mkpyramid(image, start=start, stop=stop, levels=levels) for o, (sizes, octave) in enumerate(pyramid): for (level, row, col), value in peaks.maxima(octave, psize): if value > threshold: s1, s2, psi = peaks.fit2d(octave[level], row, col) if s2 != 0 and ((s1 / s2) < edge_threshold): keypoint = keypoints.Keypoint(col, row, sizes[level], psi, value, level, 1) features += [keypoint] save(pyramid, features, "levels") return features
def create_preview(self, file): image = imaging.load(file)[0] image = imaging.filters.norm(image, 0.01, 0.01, 0, 255) # image = imaging.filters.zoom(image, 0.25) preview_file = '{}.preview.png'.format(file) imaging.save(image, preview_file) return preview_file
def save(pyramid, keys, root): sizes, octave = pyramid[0] octave = filters.norm(octave, 5, 5, 0, 255) key_levels = binby(keys, lambda x: int(x.octave)) for l, level in enumerate(octave): drawn = level if l in key_levels: drawn = keypoints.draw(level, key_levels[l]) save(drawn, pyfs.join(root, "%f.png" % sizes[l]), norm=False)
def topng(mrc, scale, force): png = pyfs.rext(mrc, 'png') png = mrc + '.png' if not force and pyfs.exists(png): return print(mrc, '->', png) image = imaging.load(mrc)[0] image = imaging.filters.zoom(image, scale) imaging.save(image, png)
def create_preview(self, file): try: image = imaging.load(file)[0] image = imaging.filters.norm(image, 0.01, 0.01, 0, 255) image = imaging.filters.zoom(image, 0.25) preview_file = '{}.preview.png'.format(file) imaging.save(image, preview_file) return preview_file except Exception as e: print(e) return None
def test(): path = "cache/13apr24b_00002sq_v02_00004hl_v01_00002en.mrc" start = 2 stop = 80 levels = 10 ring = 1.2 factor = 0.25 psize = 2 emax = 4.0 vmin = 0.01 image = filters.zoom(load(path), factor) pyramid = mkpyramid(image, start, stop, levels, ring) features = find_peaks(pyramid, vmin, emax, psize) save_pyramid(pyramid, "out") save(keypoints.draw(image, features[-3000:]), "out.png")
def binby(items, binner): bins = {} for item in items: bin = binner(item) if bin not in bins: bins[bin] = [] bins[bin] += [item] return bins def save(pyramid, keys, root): sizes, octave = pyramid[0] octave = filters.norm(octave, 5, 5, 0, 255) key_levels = binby(keys, lambda x: int(x.octave)) for l, level in enumerate(octave): drawn = level if l in key_levels: drawn = keypoints.draw(level, key_levels[l]) save(drawn, pyfs.join(root, "%f.png" % sizes[l]), norm=False) if __name__ == "__main__": import sys import imaging a = imaging.load( "/Users/craigyk/Desktop/classifier/cache/13apr24b_00002sq_v02_00002hl_v01_00003en.mrc" ) a = imaging.filters.zoom(a, 0.25) features = detect(a, start=1.5, levels=5) save(draw(a, features), sys.argv[1])
def save(image, keys, root): image = filters.norm(image, 5, 5, 0, 255) key_sizes = binby(keys, lambda x: int(x.size)) for size in key_sizes: drawn = keypoints.draw(image, key_sizes[size]) save(drawn, pyfs.join(root, "%f.png"%size))
def save_octave(radii, octave, root): levels = filters.norm(octave, 1, 1, 0, 255) for size, level in zip(radii, levels): save(level, pyfs.join(root, "%2.2f.png"%(size)), norm=False)
def preview_montage(self, src, dst, section=0): image_stack = imaging.load(str(src), format='mrc') image = image_stack[section] image = imaging.filters.norm(image, 0.01, 0.01, 0, 255) imaging.save(image, str(dst), norm=False) return dst
@jit def draw_gaussian(canvas, amp, cx, cy, psi, sx, sy): for row in range(canvas.shape[0]): for col in range(canvas.shape[1]): canvas[row, col] = gaussian_2d(amp, cx, cy, psi, sx, sy, col, row) return canvas if __name__ == "__main__": import time from imaging import save, load a = draw_gaussian(np.zeros([512, 512]), 1.0, 255.7, 256.4, np.radians(21.2), 20, 100) save(a, "temp_a.png") t0 = time.time() params = fit_gaussian2d(a, 256, 256) print("took:", time.time() - t0) print("amp:", params[0]) print("center:", params[2], params[1]) print("sigmas:", params[4], params[5]) print("psi:", np.degrees(params[3]) % 360) b = draw_gaussian(np.zeros([512, 512]), *params) save(b, "temp_b.png") c = np.zeros([4000, 4000]) c[23, 43] = 1.0 c[443, 3242] = 1.0