Example #1
0
def imshow(img, minus_bg=False, div_bg=False, sample=False, cmap='plasma'):

    imarray = imtools.ImGrid(img)
    sample_block = 4

    if minus_bg or div_bg:
        bg = np.median([
            imarray[x::sample_block, y::sample_block]
            for x, y in np.ndindex(sample_block, sample_block)
        ],
                       axis=0)
        bg = np.repeat(np.repeat(bg, sample_block, axis=0),
                       sample_block,
                       axis=1)
    if minus_bg:
        imarray -= bg.astype(int)
    elif div_bg:
        imarray = imarray.astype(float) / bg

    if sample:
        imarray = np.amax([
            imarray[:, x::sample_block, y::sample_block]
            for x, y in np.ndindex(sample_block, sample_block)
        ],
                          axis=0)
        imarray = np.repeat(np.repeat(imarray, sample_block, axis=1),
                            sample_block,
                            axis=2)

    for cval, b in enumerate(imarray.bands):
        plt.figure(cval + 1)
        plotchannel(imarray, cval, minus_bg, div_bg, cmap)
        plt.title(b)

    plt.show()
Example #2
0
def plot_spectrum(images, rg, normalize):

    adc_counts = 0
    n_images = len(images)
    print "Calculating ADC Counts..."
    for i, im in enumerate(images):

        if n_images > 10 and i % (n_images / 10) == 0:
            print "%d/%d (%.1f%%)" % (i, n_images, 100. * i / n_images)

        imarray = imtools.ImGrid(im)
        adc_counts += imtools.spectrum(imarray, region=rg)

    if normalize:
        adc_counts /= float(adc_counts.sum())

    if np.count_nonzero(adc_counts[:, 256:]) == 0:
        adc_counts = adc_counts[:, :256]

    max_count = adc_counts.shape[1]

    for cval in xrange(adc_counts.shape[0]):
        plt.figure(cval + 1)
        plt.hist(np.arange(max_count),
                 bins=max_count,
                 weights=adc_counts[cval],
                 log=True,
                 histtype='stepfilled')
        plt.xlabel('ADC Counts')
        plt.ylabel('Frequency')
    plt.show()
Example #3
0
def find_l1(imlist, l1_target_rate, bg_grid, dev_grid, border=0):
    
    # we don't need to survey the whole video
    if len(imlist) > 10/l1_target_rate:
        imlist = imlist[:int(10/l1_target_rate)]
    target_saved = int(l1_target_rate*len(imlist))
    max_vals = np.zeros((len(imlist), bg_grid.shape[0]))
    for i,im in enumerate(imlist):
        imarray = imtools.ImGrid(im)
        if border: imarray = imarray[:,border:-border,border:-border]
        max_vals[i] = np.amax(np.amax(imarray-bg_grid)/dev_grid, axis=1), axis=1)
    l1array = np.sort(max_vals,axis=0)[-target_saved]
    
    return l1array
Example #4
0
def find_bg(images, out=None, conv_len=2, bg_img=50, clear_hotpix=False):

    vid = False

    def divisorGen(n):
        large_divisors = []
        for i in xrange(1, int(math.sqrt(n) + 1)):
            if n % i == 0:
                yield i
                if i * i != n:
                    large_divisors.append(n / i)
        for divisor in reversed(large_divisors):
            yield divisor

    n_img_bg = len(images)

    # establish grid dimensions
    if imtools.is_video(images[0]):
        vid = True
        print "Processing as video..."
        cap = VideoCapture(images[0])
        retval, frame = cap.read()
        bands = ['R', 'G', 'B']
        h, w, n_bands = frame.shape
        if bg_cutoff:
            cutoff = imtools.outlier_cutoff(frame.transpose(2, 0, 1), 2)
        cap.release()

        max_grid = np.zeros((h, w, n_bands), dtype=int)
        s_grid = np.zeros((h, w, n_bands), dtype=float)

    else:
        if bg_img and (len(images) > bg_img):
            images = images[:bg_img]
        im_grid = imtools.ImGrid(images[0])
        w, h = im_grid.width, im_grid.height
        bands = im_grid.bands
        n_bands = im_grid.n_bands

        max_grid = np.zeros((n_bands, h, w), dtype=int)
        s_grid = np.zeros((n_bands, h, w), dtype=float)

    # determine sampling resolution
    full_block_len = gcd(h, w)
    divisor_list = list(divisorGen(full_block_len))
    aspect_ratio = (w / full_block_len, h / full_block_len)
    print "Possible sample dimensions:"
    for i, d in enumerate(divisor_list):
        print " [%d] %d x %d" % (i + 1, w / divisor_list[i],
                                 h / divisor_list[i])

    sample_block = divisor_list[
        int(raw_input("Select [1]-[%d]: " % len(divisor_list))) - 1]

    print
    print "Calibrating S thresholds..."
    print "Processing levels..."

    # find second largest ADC count of each pixel
    if vid:
        cap = VideoCapture(images[0])
        iframe = 0
        ret, frame = cap.read()
        while ret and cap.isOpened():
            s_grid = np.median([max_grid, s_grid, frame], axis=0)
            max_grid = np.amax([max_grid, s_grid, frame], axis=0)
            if (iframe + 1) % 100 == 0:
                print " %d" % (iframe + 1)
            iframe += 1
            ret, frame = cap.read()
            if bg_image and iframe >= bg_img: break
        s_grid = s_grid.transpose(2, 0, 1)
    else:
        print " 0/%d" % n_img_bg
        for i, im in enumerate(images):
            if (i + 1) % 10 == 0:
                print " %d/%d" % (i + 1, n_img_bg)
            im_grid = imtools.ImGrid(im)
            s_grid = np.median([max_grid, s_grid, im_grid], axis=0)
            max_grid = np.amax([max_grid, s_grid, im_grid], axis=0)

    print "Downsampling image..."

    s_grid = np.mean([
        s_grid[:, x::sample_block, y::sample_block]
        for x, y in np.ndindex(sample_block, sample_block)
    ],
                     axis=0)

    if conv_len:
        print "Applying convolution kernel..."

        kernel_side = 2 * conv_len + 1
        s_kernel = np.repeat(1, kernel_side**2).reshape(
            (kernel_side, kernel_side)) / float(kernel_side)**2
        convolved_grid = np.array([
            convolve2d(s_grid[cval], s_kernel, mode='same', boundary='symm')
            for cval in xrange(n_bands)
        ])
        if clear_hotpix:
            hot_pix_cutoff = 1.5
            hot_pix = (s_grid - convolved_grid >= hot_pix_cutoff)
            print "%d hot pixels found" % np.sum(hot_pix)
            s_grid = np.where(hot_pix, 256, convolved_grid)

        else:
            s_grid = convolved_grid

    # resize
    s_grid = np.repeat(np.repeat(s_grid, sample_block, axis=1),
                       sample_block,
                       axis=2)

    if out:
        save_grid = np.where(s_grid > 255, 255, np.ceil(s_grid).astype(int))
        if n_bands == 1:
            s_img = Image.fromarray(save_grid[0].astype(np.uint8), mode='L')

        else:
            img_mode = ''.join(bands)
            s_img = Image.fromarray(save_grid.transpose(1, 2,
                                                        0).astype(np.uint8),
                                    mode=img_mode)

    # save as png
        print "Saving background as %s" % out
        s_img.save(out)

    if vid:
        cap.release()

    return s_grid
Example #5
0
def convert_to_root(infiles, l1_target_rate=None, l2auto=0, l2manual=0, s_thresh=True, border=0, max_img=0, rawcam_format=False):
 
    avg3_kernel = np.array([[1,1,1],[1,0,1],[1,1,1]])/8.0
    avg5_kernel = np.array([[1,1,1,1,1],[1,0,0,0,1],[1,0,0,0,1],[1,0,0,0,1],[1,1,1,1,1]])/16.0
    
    n_images_auto = 50
    
    saved_pix = 0
    total_pix = 0

    # handle S threshold settings
    if s_thresh:
        bg_grid = find_bg(infiles[:n_images_auto])
        if border: bg_grid = bg_grid[:,border:-border,border:-border]
        infiles = infiles[n_images_auto:]
        dev_grid = np.sqrt(bg_grid)
    else:
        bg_grid = np.zeros(imtools.ImGrid(infiles[0]).shape)
        if border: bg_grid = bg_grid[:,border:-border,border:-border]
        dev_grid = np.ones(bg_grid.shape)
        


    # create TTree
    print
    print "Creating TTree..."
    t = r.TTree("events", 'TTree with image data')

    n_img = np.array([0], dtype=int)
    pix_n = np.array([0], dtype=int)
    bg_lvl = np.array([0], dtype=float)
    name = np.array('', dtype=str)
    color = np.array('', dtype=str)
   
    t.Branch('n_img', n_img, 'n_img/i')
    t.Branch('pix_n', pix_n, 'pix_n/i')
    t.Branch('bg_lvl', bg_lvl, 'bg_lvl/D')
    t.Branch('col', color, 'col/C')
    t.Branch('name', name, 'name/C') 
    
    if rawcam_format:
        time = np.array([0], dtype=long)
        t.Branch('t', time, 't/L')
        im_split = (infiles[0].split('.')[0]).split('_')
        if len(im_split) > 2:
            brancharray = np.zeros((len(im_split)-2,1)).astype(int)
            for i,b in enumerate(im_split[2:]):
                t.Branch(b[0], brancharray[i], b[0]+'/i')
        

    vbranch(t, 'pix_x', btype=int)
    vbranch(t, 'pix_y', btype=int)
    vbranch(t, 'pix_val', btype=int)
    vbranch(t, 'pix_avg3', btype=float)
    vbranch(t, 'pix_avg5', btype=float)
    vbranch(t, 'pix_bg', btype=float)

    print "Finding L1 Threshold..."

    if l1_target_rate:
        l1array = find_l1(infiles, l1_target_rate, bg_grid, dev_grid, border)
    else:
        l1array = np.zeros(bg_grid.shape[0])
    
    l1_grid = l1array.reshape(l1array.size,1,1) * dev_grid + bg_grid

    # fill TTree
    print "Starting loop..."
    prev_name = ''
    for i,im_name in enumerate(infiles):

        imarray = imtools.ImGrid(im_name)
        if border:
            imarray = imarray[:,border:-border,border:-border]
        im_base = im_name.split('.')

        # enforce L1S
        if np.count_nonzero(imarray >= l1_grid) == 0: continue
        if im_base[0] != prev_name:
            n_img[0] += 1

        print
        print "File %d/%d:" % (i+1,len(infiles))

        # set L2 threshold
        if l2auto:
            if s_thresh:
                cx, cy = imarray.width/2, imarray.height/2
                c_side_half = int(imarray.height/2/math.sqrt(2))
                l2array = set_L2_thresh(imarray[cx-c_side_half:cx+c_side_half,cy-c_side_half:cy+c_side_half], l2auto)
            else:
                l2array = set_L2_thresh(imarray, l2auto)
            
                
        elif l2manual:
            l2array = np.repeat(max(l2manual),imarray.n_bands)
            for i,v in enumerate(l2manual):
                l2array[i] = v

        elif s_thresh:
            l2array = 0.9*l1array

        else:
            l2array = l1array-1

        l2_grid = l2array.reshape(imarray.n_bands,1,1)*dev_grid + bg_grid    
        
            
        print "L2 threshold: \t",
        for cval, c in enumerate(imarray.bands):
            print "%s: %1.1f" % (c, l2array[cval]),
        print

        avg3_array = [convolve2d(imarray[cval], avg3_kernel, mode='same', boundary='symm') for cval in xrange(imarray.n_bands)]
        avg5_array = [convolve2d(imarray[cval], avg5_kernel, mode='same', boundary='symm') for cval in xrange(imarray.n_bands)]
            
        # fill TTree with image data for each band
        for cval, c in enumerate(imarray.bands):
            
            t.pix_x.clear()
            t.pix_y.clear()
            t.pix_val.clear()
            t.pix_avg3.clear()
            t.pix_avg5.clear()
            t.pix_bg.clear()
            
            for y,x in np.argwhere(imarray[cval] >= l2_grid[cval]):
                t.pix_x.push_back(x)
                t.pix_y.push_back(y)
                t.pix_val.push_back(imarray[cval][y,x])
                t.pix_avg3.push_back(avg3_array[cval][y,x])
                t.pix_avg5.push_back(avg5_array[cval][y,x])
                t.pix_bg.push_back(bg_grid[cval][y,x])
            
            color = np.array(c+'\0')
            name = np.array(im_base[0]+'\0')
            t.SetBranchAddress('col', color)
            t.SetBranchAddress('name', name)
            
            if rawcam_format:
                im_split = im_base[0].split('_')
                time[0] = int(im_split[1])
                if len(im_split) > 2:
                    for i,b in enumerate(im_split[2:]):
                        brancharray[i][0] = int(b[1:])
                        t.SetBranchAddress(b[0], brancharray[i])
            
            pix_n[0] = t.pix_x.size()
            bg_lvl[0] = np.mean(imarray[cval])
            print "%s: pix_n = %d" % (c, pix_n[0])
            saved_pix += pix_n[0]
            
            t.Fill()
        
        total_pix += imarray.size
        prev_name = im_base[0]
        if max_img and n_img >= max_img: break

    print "Done!"
    print
    print "Images saved:    %d\t(%.2f%%)" % (n_img[0], 100.*n_img[0]/float(len(infiles)))
    print "Total pixels:    %d\t(%.2f%%)" % (saved_pix, 100.*saved_pix/total_pix)           
    
    return t
Example #6
0
        
    ti = time.clock()
    t = convert_to_root(args.infiles, args.l1_rate, args.l2auto, args.l2manual,
                        args.s_thresh, args.border, args.max_img, args.rawcam_format)

    tf = time.clock()
      
   
    outfile.Write()
    
    if args.show:

        matplotlib.use('tkagg')                           
        
        print "Drawing saved pixels..."
        im = imtools.ImGrid(args.infiles[0])
        c1 = r.TCanvas('c1','Saved Pixels',300,250*im.n_bands)
        c1.Divide(1,im.n_bands,0,0)
        for cval,c in enumerate(im.bands):
            c1.cd(cval+1)
            t.Draw('pix_y:pix_x','col=="%s"' % c, 'colz')

    m,s = divmod(tf-ti,60)
    h,m = divmod(m,60)
    
    print "Done! Wrote to %s." % args.out
    print "Total time: ",
    if tf-ti > 3600:
        print "%d h %02d m %02d s" % (h,m,s)
    elif tf-ti > 60:
        print "%d m %02d s" % (m,s)