from ijutils import * RANGE = (10, 40) STEP_SIZE = 2.0 diam_disk = cover_detection.config.diameter_disk diam_target = cover_detection.config.diameter_target diam_dewar = cover_detection.config.diameter_dewar threshold_method = cover_detection.config.threshold_method center_x, center_y = ( cover_detection.config.center_x, cover_detection.config.center_y) radius_dewar = diam_dewar/2.0 roi = Rectangle(int(center_x-radius_dewar), int(center_y-radius_dewar), int(diam_dewar), int(diam_dewar)) area_disc = int(pow(diam_disk/2,2) * math.pi) area_target = int(pow(diam_target/2,2) * math.pi) area = area_disc - area_target if TARGET_HOLES: area -= int(4* pow(12/2,2) * math.pi) debug=True settling_time = cam.getAcquirePeriod()/1000.0 + 0.3 class Fitness(Readable): def read(self): try: ip = load_image(ImagingUtils.grayscale(img.output, None)) set_circle_mask(ip.getProcessor(), center_x=center_x, center_y=center_y, radius=radius_dewar, bitwise=False) #it = auto_threshold(ip, dark_background=True, method=threshold_method, in_place=False) it = threshold(ip, 128, 255, in_place=False) binary_open(it, dark_background=True, iterations=1, count=1); binary_open(it, dark_background=False, iterations=1, count=1); if debug: ImageBuffer.saveImage(it.getBufferedImage(), os.path.abspath(expand_path("{images}/ExposureScan/" + str( cam.getExposure()) + ".png")), "png") h = get_histogram(it) b,w= h[0], h[-1] error = abs(w-area) return error except: print sys.exc_info()[1] class Exposure(Writable): def write(self,pos): cam.setExposure(pos) fitness=Fitness() exposure=Exposure() ret = lscan(exposure,fitness,RANGE[0], RANGE[1], STEP_SIZE, settling_time, save=False) y, x = ret.getReadable(0), ret.getPositions(0) m=x[y.index(min(y))] p=get_plots()[0] p.addMarker(m, p.AxisId.X, str(m), Color.RED) print "Setting exposure = ", m exposure.write(m) time.sleep(settling_time) cover_detection.grab_ref_image() set_return("Optimal exposure: " + str(m))