コード例 #1
0
ファイル: nn_all.py プロジェクト: cyrilzhang/livemau5
def downscale(data, downscale_factor):
    new_data = []
    for i, (stk,roi) in enumerate(data):
        new_stk = downscale_local_mean(stk, (1,downscale_factor,downscale_factor))
        new_roi = downscale_local_mean(roi, (1,downscale_factor,downscale_factor))
        new_data.append((new_stk, new_roi))
    return new_data
コード例 #2
0
ファイル: test_warps.py プロジェクト: hbueno/scikit-image
def test_downscale_local_mean():
    image1 = np.arange(4 * 6).reshape(4, 6)
    out1 = downscale_local_mean(image1, (2, 3))
    expected1 = np.array([[4., 7.], [16., 19.]])
    assert_array_equal(expected1, out1)

    image2 = np.arange(5 * 8).reshape(5, 8)
    out2 = downscale_local_mean(image2, (4, 5))
    expected2 = np.array([[14., 10.8], [8.5, 5.7]])
    assert_array_equal(expected2, out2)
コード例 #3
0
ファイル: main.py プロジェクト: kctess5/sad_robot
def main():
	args = vars(parser.parse_args())
	filename = os.path.join(os.getcwd(), args["image"][0])

	image = skimage.img_as_uint(color.rgb2gray(io.imread(filename)))

	subsample = 1

	if (not args["subsample"] == 1):
		subsample = args["subsample"][0]

		image = transform.downscale_local_mean(image, (subsample, subsample))
		image = transform.pyramid_expand(image, subsample, 0, 0)

	image = exposure.rescale_intensity(image, out_range=(0,args["depth"][0]))

	if (args["visualize"]):
		io.imshow(image)
		io.show()

	source = generate_face(image, subsample, args["depth"][0], FLICKER_SPEED)

	if source:
		with open(args["output"][0], 'w') as file_:
			file_.write(source)
	else:
		print "Attempted to generate source code, failed."
コード例 #4
0
ファイル: maps.py プロジェクト: vorugantia/gamma-sky
def make_hips_allsky_jpeg_file(order):
    log.info(f'Making HiPS allsky image for order {order}')
    tiles = []
    for ipix in range(healpix_order_to_npix(order=order)):
        log.info(f'Processing tile {ipix}')
        meta = HipsTileMeta(order=order, ipix=ipix, file_format='jpg', frame='galactic', width=2 ** shift_order)
        filename = DATA_DIR / 'maps' / 'Fermi10GeV' / meta.tile_default_path
        tile = HipsTile.read(meta, filename)

        # Downsample the tile
        factor = 2 ** allsky_downsample_order
        # import IPython; IPython.embed(); 1/0
        data = downscale_local_mean(tile.data, factors=(factor, factor, 1))
        meta.order -= allsky_downsample_order
        meta.width //= factor

        # This is to debug when all-sky is shown
        data[:, :, 0] = 255

        tile = HipsTile.from_numpy(meta, data.astype('uint8'))
        # print(tile.data.shape, tile.meta)

        tiles.append(tile)

    allsky = HipsTileAllskyArray.from_tiles(tiles)

    path = DATA_DIR / 'maps' / 'Fermi10GeV' / f'Norder{order}' / 'Allsky.jpg'
    # path.parent.mkdir(exist_ok=True, parents=True)
    log.info(f'Writing {path}')
    allsky.write(path)
コード例 #5
0
def load_and_downscale(input_filename):
    """Load the image, covert to grayscale and downscale as needed."""

    image = Image.from_file(input_filename)
    blue_channel = image[:,:,2]
    downscaled = downscale_local_mean(blue_channel, (2, 2))

    return downscaled
コード例 #6
0
ファイル: image.py プロジェクト: histed/PyToolsMH
def downscale_in_chunks(im, downscale_tuple=None, nfrchunk=1000, print_status=True):
    cL = []
    if print_status: print('downscaling frames... ', end='')
    for iC in range(int(np.ceil(im.shape[0]/nfrchunk))):
        maxfr = np.min((nfrchunk*(iC+1),im.shape[0]))
        cL.append(transform.downscale_local_mean(im[nfrchunk*iC:maxfr,:,:],
                                                 downscale_tuple))
        if print_status: print(maxfr, end=' ')
    if print_status: print('done.')
    return(np.concatenate(cL,axis=0))
コード例 #7
0
        def generate_column(numbers):
            images = []
            for i in numbers:
                i = selected[i]
                image_fpath = dataset.item_content_abspath(i)
                images.append(
                    downscale_local_mean(Image.from_file(image_fpath), (5, 5, 1))
                )

            column = join_horizontally(images)

            return column
コード例 #8
0
ファイル: nn_prepost.py プロジェクト: cyrilzhang/livemau5
def preprocess(data, radius):
    for i, (stk,roi) in enumerate(data):
        # Normalize
        low_p = np.percentile(stk.flatten(), 3)
        high_p = np.percentile(stk.flatten(), 99)
        #print np.min(stk.flatten()), low_p, high_p, np.max(stk.flatten())
        
#        plt.figure()
#        plt.hist(stk.flatten())
#        plt.figure()
#        plt.imshow(downscale_local_mean(stk[0], (DOWNSCALE_FACTOR, DOWNSCALE_FACTOR)), cmap="gray")
#        plt.figure()
#        plt.imshow(downscale_local_mean(np.mean(stk, axis=0), (DOWNSCALE_FACTOR, DOWNSCALE_FACTOR)), cmap="gray")

        stk = np.clip(stk, low_p, high_p)
        stk = stk - stk.mean()
        stk = np.divide(stk-np.min(stk), np.max(stk) - np.min(stk))
        #print np.min(stk.flatten()),  np.max(stk.flatten())

#        plt.figure()
#        plt.hist(stk.flatten())

        # Downscale
        stk = downscale_local_mean(stk, (1,DOWNSCALE_FACTOR,DOWNSCALE_FACTOR))
        roi = downscale_local_mean(roi, (1,DOWNSCALE_FACTOR,DOWNSCALE_FACTOR))
        new_stk = np.zeros((stk.shape[1], stk.shape[2], IMG_CHANNELS))
        # Uncomment following 2 lines to use more channels
        #new_stk[:,:,2] = stk.max(axis=0)
        #new_stk[:,:,1] = np.std(stk, axis=0)
        new_stk[:,:,0] = np.mean(stk, axis=0)

#        plt.figure()
#        plt.imshow(stk[0], cmap="gray")
#        plt.figure()
#        plt.imshow(new_stk[:,:,0], cmap="gray")
#        plt.show()

        roi_centroids = get_centroids(roi, radius).max(axis=0)
        data[i] = (new_stk, roi.max(axis=0), roi.max(axis=0))#(new_stk, roi_centroids, roi.max(axis=0))
    return data
コード例 #9
0
ファイル: load_scan.py プロジェクト: casimp/lightct
    def auto_centre(self, window=400, downsample_y=2, plot=True):
        """
        Automatic method for finding the centre of rotation.
        
        # window:     Window width to search across (pixels).
        """
        downsample = (downsample_y, 1)
        half_win = window // 2
        win_range = range(-half_win, half_win)
        
        # Compare ref image with flipped 180deg counterpart
        ref_image = self.im_stack[:, half_win:-half_win, self.p0]
        ref = downscale_local_mean(ref_image, downsample)
        im_180_image = self.im_stack[:, :, self.num_images // 2 + self.p0]
        im_180 = downscale_local_mean(im_180_image, downsample)
        flipped = np.fliplr(im_180)
        
        diff = np.nan * np.zeros(len(win_range))

        # Flip win_range as we are working on flipped data
        for idx, i in enumerate(win_range[::-1]):
            
            cropped = flipped[:, half_win + i: -half_win + i]
            tmp = cropped - ref
            diff[idx] = tmp.std()
            
        minima = np.argmin(diff)
        self.cor_offset = win_range[minima]
        print('COR = %i' % self.cor_offset)

        if plot:
            fig, ax = plt.subplots()
            fig.canvas.set_window_title('Centre Analysis')
            ax.plot([i for i in win_range], diff)
            ax.plot(self.cor_offset, np.min(diff), '*')
            ax.set_ylabel('Standard deviation (original v 180deg flipped)')
            ax.set_xlabel('2 * Centre correction (pixels)')
            im_copy = np.copy(self.im_stack[:, :, self.p0])
            recentre_plot(im_copy, self.cor_offset)
コード例 #10
0
ファイル: utils.py プロジェクト: mikekestemont/DeepScript
def load_dir(dir_name, ext='.tif'):
    X, Y = [], []
    fns = glob.glob(dir_name+'/*' + ext)
    random.shuffle(fns)

    for fn in fns:
        img = np.array(io.imread(fn), dtype='float32')
        scaled = img / np.float32(255.0)
        scaled = downscale_local_mean(scaled, factors=(2,2))
        X.append(scaled)
        Y.append(os.path.basename(fn).split('_')[0])

    return X, Y
コード例 #11
0
def process_line(t):
    i, row = t
    image_urls = row[0].split(',')
    for image_url in image_urls:
        if 'http' in image_url:
            try:
                image_data = imread(image_url)
                grayed = rgb2gray(image_data)
                image_resized = resize(grayed, (100, 100))
                descriptor = local_binary_pattern(image_resized, 3, 20, 'uniform')
                descriptor = downscale_local_mean(descriptor, (10, 10))
                descriptor_list = np.floor(descriptor.flatten()).tolist()
                return [image_url, row[2], descriptor_list]
            except:
                return [image_url, row[2], []]
コード例 #12
0
def resample_binary_majority(raster, factor):
    resampled = downscale_local_mean(raster, factors=factor)
    resampled_bool = np.zeros_like(resampled)
    resampled_bool[resampled >= 0.5] = 1

    mask = np.ones_like(resampled)
    if raster.shape[0] % factor[0] != 0:
        y = (factor[0] - (raster.shape[0] % factor[0])) * -1
        mask[y:,:] = 0
    if raster.shape[1] % factor[1] != 0:
        x = (factor[1] - (raster.shape[1] % factor[1])) * -1
        mask[:,x:] = 0
    # Create mask

    mask = mask.astype(bool)
    return [factor, resampled_bool, mask]
コード例 #13
0
ファイル: test_grey.py プロジェクト: AbdealiJK/scikit-image
    def _build_expected_output(self):
        funcs = (grey.erosion, grey.dilation, grey.opening, grey.closing,
                 grey.white_tophat, grey.black_tophat)
        selems_2D = (selem.square, selem.diamond,
                     selem.disk, selem.star)

        with expected_warnings(['Possible precision loss']):
            image = img_as_ubyte(transform.downscale_local_mean(
                color.rgb2gray(data.coffee()), (20, 20)))

        output = {}
        for n in range(1, 4):
            for strel in selems_2D:
                for func in funcs:
                    key = '{0}_{1}_{2}'.format(strel.__name__, n, func.__name__)
                    output[key] = func(image, strel(n))

        return output
コード例 #14
0
ファイル: voronoi.py プロジェクト: thypon/dotconfig
def render(img, color_samples):
    print("Rendering...")
    h, w = [2*x for x in img.shape[:2]]
    xx, yy = np.meshgrid(np.arange(h), np.arange(w))
    pixel_coords = np.c_[xx.ravel(), yy.ravel()]

    colors = np.empty([h, w, 3])
    coords = []
    for color_sample in color_samples:
        coord = tuple(x*2 for x in color_sample[:2][::-1])
        colors[coord] = color_sample[2:]
        coords.append(coord)

    tree = KDTree(coords)
    idxs = tree.query(pixel_coords)[1]
    data = colors[tuple(tree.data[idxs].astype(int).T)].reshape((w, h, 3))
    data = np.transpose(data, (1, 0, 2))

    return downscale_local_mean(data, (2, 2, 1))
コード例 #15
0
ファイル: test.py プロジェクト: mikekestemont/DeepScript
def test():

    test_images, test_categories = [], []
    for fn in glob.glob('data/splits/test/*.tif'):
        img = np.array(io.imread(fn), dtype='float32')
        scaled = img / np.float32(255.0)
        scaled = downscale_local_mean(scaled, factors=(2,2))
        test_images.append(scaled)
        test_categories.append(os.path.basename(fn).split('_')[0])

    label_encoder = pickle.load( open('models/' + MODEL_NAME + '/label_encoder.p', 'rb'))
    print('-> Working on classes:', label_encoder.classes_)

    test_int_labels = label_encoder.transform(test_categories)
    
    model = model_from_json(open('models/' + MODEL_NAME + '/architecture.json').read())

    model.load_weights('models/' + MODEL_NAME + '/weights.hdf5')

    test_preds = []

    for img in test_images:
        X = utils.augment_test_image(image=img,
                                        nb_rows=NB_ROWS,
                                        nb_cols=NB_COLS,
                                        nb_patches=NB_TEST_PATCHES)
        preds = np.array(model.predict(X), dtype='float64')
        av_pred = preds.mean(axis=0)
        test_preds.append(np.argmax(av_pred, axis=0))

    print('Test accuracy:', accuracy_score(test_int_labels, test_preds))

    # confusion matrix
    plt.clf()
    T = label_encoder.inverse_transform(test_int_labels)
    P = label_encoder.inverse_transform(test_preds)
    cm = confusion_matrix(T, P, labels=label_encoder.classes_)
    cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    np.set_printoptions(precision=2)
    sns.plt.figure()
    utils.plot_confusion_matrix(cm_normalized, target_names=label_encoder.classes_)
    sns.plt.savefig('models/' + MODEL_NAME + '/test_conf_matrix.pdf')
コード例 #16
0
def DetectLargeImage(img_path, div_shape, sigma, neighborhood_size, threshold):
    ds, ysize, xsize, yblocks, xblocks, block_size = div_shape
    xcoord = []
    ycoord = []
    for n in range(yblocks):
        for m in range(xblocks):
            try:
                # Read part image
                print('Block: n={}, m={}'.format(n, m))
                extra = 100
                img_extended = np.uint8(
                    ip.read_image_part_extra(img_path, div_shape, n, m, extra))
                img = np.uint8(img_extended[:, extra:-extra, :])
                # Convert to a* channel and Cb channel
                img_a = cv2.cvtColor(img_extended, cv2.COLOR_BGR2Lab)[:, :, 1]
                img_gauss = np.uint8(
                    ndimage.gaussian_filter(downscale_local_mean(
                        img, (10, 10, 1)),
                                            sigma=(2.0, 2.0, 0.0)))
                #img = None
                img_gauss_Cb = cv2.cvtColor(img_gauss, cv2.COLOR_BGR2Lab)[:, :,
                                                                          2]
                img_gauss = None
                img_gauss_Cb_bin = img_gauss_Cb < 140
                img_gauss_Cb = None
                img_gauss_Cb_bin_inv = np.invert(img_gauss_Cb_bin)
                img_gauss_Cb_bin = None
                img_temp = np.zeros((img_gauss_Cb_bin_inv.shape[0] + 2,
                                     img_gauss_Cb_bin_inv.shape[1] + 2),
                                    dtype=bool)
                img_temp[1:-1, 1:-1] = img_gauss_Cb_bin_inv
                img_gauss_Cb_bin = np.invert(
                    ndimage.morphology.binary_fill_holes(img_temp))[1:-1, 1:-1]
                img_temp = None
                img_gauss_Cb_bin_inv = None

                # =============================================================================
                #                 img_plot = ip.Otsu(cv2.cvtColor(img, cv2.COLOR_BGR2Lab)[:,:,1],rev=True)
                #                 grass_plot = resize(img_gauss_Cb_bin,img_plot.shape)
                #                 img_p = np.multiply(img_plot, grass_plot)
                #                 ip.show_image(img_p)
                # =============================================================================

                # Detect plants using local maxima
                try:
                    xy = Detect(img_a, sigma, neighborhood_size, threshold)
                    x_arr, y_arr = Filter(xy, block_size, extra=100)
                except IndexError:
                    print('Algorithm found zero points')
                    continue

                img_a = None

                sort_idx = y_arr.argsort()
                y_arr_sort = y_arr[sort_idx]
                x_arr_sort = x_arr[sort_idx]
                pos_arr = np.zeros((block_size // 10, block_size // 10),
                                   dtype=int)
                pos_arr[np.array(y_arr_sort // 10, dtype=int),
                        np.array(x_arr_sort // 10, dtype=int)] = np.arange(
                            0, len(x_arr), 1, dtype=int) + 1
                plant_pos = np.multiply(pos_arr, img_gauss_Cb_bin)
                pos = np.nonzero(plant_pos)
                x_plant = x_arr_sort[plant_pos[pos] - 1]
                y_plant = y_arr_sort[plant_pos[pos] - 1]

                xcoord += list(x_plant + m * block_size)
                ycoord += list(y_plant + n * block_size)

            except ValueError:
                print(
                    'Moving on to next block, since this block ({}, {}) is not part of the image'
                    .format(n, m))
    return np.array(xcoord), np.array(ycoord)
コード例 #17
0
 def preprocess(self, s):
     s = color.rgb2gray(s).astype('float16')
     s = s.reshape(list(s.shape) + [1])
     s = transf.downscale_local_mean(s, (2, 2, 1))  # Downsample
     s = s[17:-8, :, :]
     return s
コード例 #18
0
    print len(data)
    print len(labels)
    data = np.array(data)
    print data.shape
    new_data = []
    data = np.array(data)
    des = data.shape[0] / 100
if (whichdata == 2):
    data = []
    labels = []
    for i in range(1, 12):
        folder = "./Face_data/" + str(i) + "/"
        for face in os.listdir(folder):
            if ("yale" in face and "info" not in face):
                f = misc.imread(folder + face)
                f = downscale_local_mean(f, (12, 12))
                if (f.shape == (16, 14)):
                    data.append(f.ravel())
                    labels.append(i)
    data = np.array(data)
    labels = np.array(labels)
    print data.shape
    des = data.shape[0]

data = np.array(data)
acc = []
FPRS = []
TPRS = []
models = []
for _ in range(5):
    i_train = random.sample(range(0, data.shape[0]),
コード例 #19
0
ファイル: p_exp_batch.py プロジェクト: solversa/xlearn
def ds(img):
    il, ih, iw = img.shape
    img_tmp = np.zeros((il, ih / 2, iw / 2))
    for i in range(len(img)):
        img_tmp[i] = downscale_local_mean(img[i], (2, 2))
    return img_tmp
コード例 #20
0
def initialize_components(Y,
                          K=30,
                          gSig=[5, 5],
                          gSiz=None,
                          ssub=1,
                          tsub=1,
                          nIter=5,
                          maxIter=5,
                          nb=1,
                          kernel=None,
                          use_hals=True,
                          normalize=True,
                          img=None,
                          method='greedy_roi',
                          max_iter_snmf=500,
                          alpha_snmf=10e2,
                          sigma_smooth_snmf=(.5, .5, .5),
                          perc_baseline_snmf=20):
    """Initalize components

    This method uses a greedy approach followed by hierarchical alternative least squares (HALS) NMF.
    Optional use of spatio-temporal downsampling to boost speed.

    Parameters
    ----------
    Y: np.ndarray
         d1 x d2 [x d3] x T movie, raw data.
    K: [optional] int
        number of neurons to extract (default value: 30).
    tau: [optional] list,tuple
        standard deviation of neuron size along x and y [and z] (default value: (5,5).
    gSiz: [optional] list,tuple
        size of kernel (default 2*tau + 1).
    nIter: [optional] int
        number of iterations for shape tuning (default 5).
    maxIter: [optional] int
        number of iterations for HALS algorithm (default 5).
    ssub: [optional] int
        spatial downsampling factor recommended for large datasets (default 1, no downsampling).
    tsub: [optional] int
        temporal downsampling factor recommended for long datasets (default 1, no downsampling).
    kernel: [optional] np.ndarray
        User specified kernel for greedyROI (default None, greedy ROI searches for Gaussian shaped neurons)
    use_hals: [optional] bool
        Whether to refine components with the hals method
    normalize: [optional] bool
        Whether to normalize data before running the initialization
    img: optional [np 2d array]
        Image with which to normalize. If not present use the mean + offset 
    method: str  
        Initialization method 'greedy_roi' or 'sparse_nmf' 
    max_iter_snmf: int 
        Maximum number of sparse NMF iterations
    alpha_snmf: scalar
        Sparsity penalty

    Returns
    --------
    Ain: np.ndarray
        (d1*d2[*d3]) x K , spatial filter of each neuron.
    Cin: np.ndarray
        T x K , calcium activity of each neuron.
    center: np.ndarray
        K x 2 [or 3] , inferred center of each neuron.
    bin: np.ndarray
        (d1*d2[*d3]) x nb, initialization of spatial background.
    fin: np.ndarray
        nb x T matrix, initalization of temporal background.

    """

    if gSiz is None:
        gSiz = 2 * np.asarray(gSig) + 1

    d, T = np.shape(Y)[:-1], np.shape(Y)[-1]
    # rescale according to downsampling factor
    gSig = np.round(np.asarray(gSig) // ssub).astype(np.int)
    gSiz = np.round(np.asarray(gSiz) // ssub).astype(np.int)

    print('Noise Normalization')
    if normalize is True:
        if img is None:
            img = np.mean(Y, axis=-1)
            img += np.median(img)

        Y = old_div(Y, np.reshape(img, d + (-1, ), order='F'))
        alpha_snmf /= np.mean(img)

    # spatial downsampling
    mean_val = np.mean(Y)
    if ssub != 1 or tsub != 1:
        print("Spatial Downsampling ...")
        Y_ds = downscale_local_mean(Y,
                                    tuple([ssub] * len(d) + [tsub]),
                                    cval=mean_val)
    else:
        Y_ds = Y

    print('Roi Extraction...')

    if method == 'greedy_roi':
        Ain, Cin, _, b_in, f_in = greedyROI(Y_ds,
                                            nr=K,
                                            gSig=gSig,
                                            gSiz=gSiz,
                                            nIter=nIter,
                                            kernel=kernel,
                                            nb=nb)

        if use_hals:
            print('Refining Components...')
            Ain, Cin, b_in, f_in = hals(Y_ds,
                                        Ain,
                                        Cin,
                                        b_in,
                                        f_in,
                                        maxIter=maxIter)
    elif method == 'sparse_nmf':

        Ain, Cin, _, b_in, f_in = sparseNMF(Y_ds,
                                            nr=K,
                                            nb=nb,
                                            max_iter_snmf=max_iter_snmf,
                                            alpha=alpha_snmf,
                                            sigma_smooth=sigma_smooth_snmf,
                                            remove_baseline=True,
                                            perc_baseline=perc_baseline_snmf)
#        print np.sum(Ain), np.sum(Cin)
#        print 'Refining Components...'
#        Ain, Cin, b_in, f_in = hals(Y_ds, Ain, Cin, b_in, f_in, maxIter=maxIter)
#        print np.sum(Ain), np.sum(Cin)
    elif method == 'pca_ica':

        Ain, Cin, _, b_in, f_in = ICA_PCA(Y_ds, nr = K, sigma_smooth=sigma_smooth_snmf,  truncate = 2, fun='logcosh',\
                                          max_iter=max_iter_snmf, tol=1e-10,remove_baseline=True, perc_baseline=perc_baseline_snmf, nb=nb)

    else:

        print(method)
        raise Exception("Unsupported method")

    K = np.shape(Ain)[-1]
    ds = Y_ds.shape[:-1]

    Ain = np.reshape(Ain, ds + (K, ), order='F')

    if len(ds) == 2:

        Ain = resize(Ain, d + (K, ), order=1)

    else:  # resize only deals with 2D images, hence apply resize twice

        Ain = np.reshape([resize(a, d[1:] + (K, ), order=1) for a in Ain],
                         (ds[0], d[1] * d[2], K),
                         order='F')
        Ain = resize(Ain, (d[0], d[1] * d[2], K), order=1)

    Ain = np.reshape(Ain, (np.prod(d), K), order='F')

    #import pdb
    # pdb.set_trace()

    b_in = np.reshape(b_in, ds + (nb, ), order='F')

    if len(ds) == 2:
        b_in = resize(b_in, d + (nb, ), order=1)
    else:
        b_in = np.reshape([resize(b, d[1:] + (nb, ), order=1) for b in b_in],
                          (ds[0], d[1] * d[2], nb),
                          order='F')
        b_in = resize(b_in, (d[0], d[1] * d[2], nb), order=1)

    b_in = np.reshape(b_in, (np.prod(d), nb), order='F')

    Cin = resize(Cin, [K, T])

    f_in = resize(np.atleast_2d(f_in), [nb, T])
    # center = com(Ain, *d)
    center = np.asarray(
        [center_of_mass(a.reshape(d, order='F')) for a in Ain.T])

    if normalize is True:
        #import pdb
        # pdb.set_trace()
        Ain = Ain * np.reshape(img, (np.prod(d), -1), order='F')

        b_in = b_in * np.reshape(img, (np.prod(d), -1), order='F')
        # b_in = np.atleast_2d(b_in * img.flatten('F')) #np.reshape(img,
        # (np.prod(d), -1),order='F')
        Y = Y * np.reshape(img, d + (-1, ), order='F')

    return Ain, Cin, b_in, f_in, center
コード例 #21
0
ファイル: convnet.py プロジェクト: lenovor/simple_convnet
 def _forward(self, input_act):
     act = downscale_local_mean(np.rollaxis(input_act, 0, 4), (self.pool_size, self.pool_size, 1, 1))
     return np.rollaxis(act, 3, 0)
コード例 #22
0
ファイル: feature.py プロジェクト: ymnliu/cyto_lib
def pre_processing(img):
    return transform.downscale_local_mean(img, (2, 2))
コード例 #23
0
def read_and_downscale(f):
    img = plt.imread(f)
    ds = downscale_local_mean(img[:, :, 2], (10, 10))
    return ds[:225, :300]
コード例 #24
0
# There are lots of details to be worked out here, still -- mainly trying to select overlapping image patches.

# In[ ]:

from skimage.transform import downscale_local_mean

img1 = plt.imread(set175[1])[:, :, 2]
img2 = plt.imread(set175[3])[:, :, 2]

# In[ ]:

np.shape(img1), np.shape(img2)

# In[ ]:

img1ds = downscale_local_mean(img1, (10, 10))
img2ds = downscale_local_mean(img2, (10, 10))
plt.figure(figsize=(8, 10))
plt.subplot(121)
plt.imshow(img1ds[:225, :300])
plt.subplot(122)
plt.imshow(img2ds[:225, :300])

# For prototyping my goal is to get a bunch of downscaled images so that I can feed them in to test
# plausibility of basic NN architecture decisions.
#
# Dims are subsampled and hard-coded for now so we don't have wrong dimensions showing up here and there.

# In[ ]:

コード例 #25
0
def process_image(state):
    state = color.rgb2gray(state)
    state = np.resize(downscale_local_mean(state, (4, 4)), (1, 60, 64, 1))
    return state
コード例 #26
0
def sk_downscale(imageIn, factors):
    """
    use the scikit-image method for downsampling
    needs a tuple with same number of factors as dim of imgIn
    """
    return downscale_local_mean(imageIn, factors)
コード例 #27
0
ファイル: datasets.py プロジェクト: a-domrachev/IITP_diploma
def downsample(image, factors):
    return downscale_local_mean(image, tuple(factors))
コード例 #28
0
def train_model(train_set, images_folder, valid_set):
    print('Start training model...')

    # Number of training samples
    m = train_set.shape[0]

    print('Number of samples: ', m)

    # Create empty dataframe
    x = np.empty((0, 64 * 64 * 3), float)

    y = np.empty((0, 384 * 384), float)

    for i in range(m):
        #print(train_set.iloc[i, 0])

        # Read image
        image_path = images_folder + '/' + train_set.iloc[i, 0]
        img = io.imread(image_path)

        # Downscale
        img_t = transform.downscale_local_mean(img, (12, 12, 1))
        img_t = img_t.astype(np.uint8)

        #plt.imshow(img_t.astype(np.uint8))
        #plt.show()
        #exit(0)

        img_norm = img_t.astype(float) / 255.0

        # Append to dataframe
        rgb = np.reshape(img_norm, (-1, 3))
        array = np.concatenate((rgb[:, 0], rgb[:, 1], rgb[:, 2]))
        x = np.vstack((x, array))

        mask = get_mask(train_set.iloc[i, 1])

        y = np.vstack((y, mask))

    # Setup the classifier
    model = MLPClassifier(hidden_layer_sizes=(4096, 2048),
                          activation='relu',
                          max_iter=1000,
                          learning_rate_init=0.001,
                          verbose=10)

    # Training the model
    print('Training model...')
    model.fit(x, y)

    # Save the model on hard disk for future uses
    joblib.dump(model, 'model.joblib')

    # Plot loss
    fig = plt.figure()
    plt.plot(model.loss_curve_)
    plt.xlabel('Iteration')
    plt.ylabel('Cost')
    plt.show()
    fig.savefig('error_plot.png')

    return model
コード例 #29
0
    def __getitem__(self, idx):
        if torch.is_tensor(idx):
            idx = idx.tolist()

        img_name = os.path.join(self.root_dir, "t1/",
                                self.list_of_images.iloc[idx, 0])
        image_t1 = np.load(img_name)
        image_t2 = np.load(
            os.path.join(self.root_dir, "t2/", self.list_of_images.iloc[idx,
                                                                        0]))
        image_t3 = np.load(
            os.path.join(self.root_dir, "t3/", self.list_of_images.iloc[idx,
                                                                        0]))
        image_t4 = np.load(
            os.path.join(self.root_dir, "t4/", self.list_of_images.iloc[idx,
                                                                        0]))
        image_t5 = np.load(
            os.path.join(self.root_dir, "t5/", self.list_of_images.iloc[idx,
                                                                        0]))

        poss_targets = [image_t1, image_t2, image_t3, image_t4, image_t5]
        mediana = [
            image_t1.mean(),
            image_t2.mean(),
            image_t3.mean(),
            image_t4.mean(),
            image_t5.mean()
        ]

        for i in range(len(mediana)):
            for j in range(len(mediana) - 1):
                if mediana[i] > mediana[j + 1]:
                    var = poss_targets[i]
                    poss_targets[i] = poss_targets[j + 1]
                    poss_targets[j + 1] = var

        target = np.copy(poss_targets[2])

        image_t1 = np.transpose(image_t1, (1, 2, 0))  # formato HWC
        image_t1 = transform.downscale_local_mean(image_t1, (2, 2, 1))
        image_t1 = np.transpose(image_t1, (2, 0, 1))  # formato CHW

        image_t2 = np.transpose(image_t2, (1, 2, 0))  # formato HWC
        image_t2 = transform.downscale_local_mean(image_t2, (2, 2, 1))
        image_t2 = np.transpose(image_t2, (2, 0, 1))  # formato CHW

        image_t3 = np.transpose(image_t3, (1, 2, 0))  # formato HWC
        image_t3 = transform.downscale_local_mean(image_t3, (2, 2, 1))
        image_t3 = np.transpose(image_t3, (2, 0, 1))  # formato CHW

        image_t4 = np.transpose(image_t4, (1, 2, 0))  # formato HWC
        image_t4 = transform.downscale_local_mean(image_t4, (2, 2, 1))
        image_t4 = np.transpose(image_t4, (2, 0, 1))  # formato CHW

        image_t5 = np.transpose(image_t5, (1, 2, 0))  # formato HWC
        image_t5 = transform.downscale_local_mean(image_t5, (2, 2, 1))
        image_t5 = np.transpose(image_t5, (2, 0, 1))  # formato CHW

        if self.norm == True:
            image_t1 = image_t1 / 32767
            image_t2 = image_t2 / 32767
            image_t3 = image_t3 / 32767
            image_t4 = image_t4 / 32767
            image_t5 = image_t5 / 32767
            target = target / 32767

        if self.stand == True:
            image_t1_mean = image_t1.mean(keepdims=True, axis=(1, 2))
            image_t1_stddev = image_t1.std(keepdims=True, axis=(1, 2))
            image_t1 = (image_t1 - image_t1_mean) / image_t1_stddev

            image_t2_mean = image_t2.mean(keepdims=True, axis=(1, 2))
            image_t2_stddev = image_t2.std(keepdims=True, axis=(1, 2))
            image_t2 = (image_t2 - image_t2_mean) / image_t2_stddev

            image_t3_mean = image_t3.mean(keepdims=True, axis=(1, 2))
            image_t3_stddev = image_t3.std(keepdims=True, axis=(1, 2))
            image_t3 = (image_t3 - image_t3_mean) / image_t3_stddev

            image_t4_mean = image_t4.mean(keepdims=True, axis=(1, 2))
            image_t4_stddev = image_t4.std(keepdims=True, axis=(1, 2))
            image_t4 = (image_t4 - image_t4_mean) / image_t4_stddev

            image_t5_mean = image_t5.mean(keepdims=True, axis=(1, 2))
            image_t5_stddev = image_t5.std(keepdims=True, axis=(1, 2))
            image_t5 = (image_t5 - image_t5_mean) / image_t5_stddev

            target_mean = target.mean(keepdims=True, axis=(1, 2))
            target_stddev = target.std(keepdims=True, axis=(1, 2))
            target = (target - target_mean) / target_stddev

        samples = {
            'image_1': image_t1.astype('float32'),
            'image_2': image_t2.astype('float32'),
            'image_3': image_t3.astype('float32'),
            'image_4': image_t4.astype('float32'),
            'image_5': image_t5.astype('float32')
        }
        t = {'target': target.astype('float32')}

        if self.transform != None:
            samples, t = self.transform(samples, t)

        return samples, t
コード例 #30
0
def initialize_components(Y, K=30, gSig=[5, 5], gSiz=None, ssub=1, tsub=1, nIter=5, maxIter=5, nb=1,
                          kernel=None, use_hals=True, normalize=True, img=None, method='greedy_roi', max_iter_snmf=500, alpha_snmf=10e2, sigma_smooth_snmf=(.5, .5, .5), perc_baseline_snmf=20):
    """Initalize components

    This method uses a greedy approach followed by hierarchical alternative least squares (HALS) NMF.
    Optional use of spatio-temporal downsampling to boost speed.

    Parameters
    ----------
    Y: np.ndarray
         d1 x d2 [x d3] x T movie, raw data.
    K: [optional] int
        number of neurons to extract (default value: 30).
    tau: [optional] list,tuple
        standard deviation of neuron size along x and y [and z] (default value: (5,5).
    gSiz: [optional] list,tuple
        size of kernel (default 2*tau + 1).
    nIter: [optional] int
        number of iterations for shape tuning (default 5).
    maxIter: [optional] int
        number of iterations for HALS algorithm (default 5).
    ssub: [optional] int
        spatial downsampling factor recommended for large datasets (default 1, no downsampling).
    tsub: [optional] int
        temporal downsampling factor recommended for long datasets (default 1, no downsampling).
    kernel: [optional] np.ndarray
        User specified kernel for greedyROI (default None, greedy ROI searches for Gaussian shaped neurons)
    use_hals: [optional] bool
        Whether to refine components with the hals method
    normalize: [optional] bool
        Whether to normalize data before running the initialization
    img: optional [np 2d array]
        Image with which to normalize. If not present use the mean + offset 
    method: str  
        Initialization method 'greedy_roi' or 'sparse_nmf' 
    max_iter_snmf: int 
        Maximum number of sparse NMF iterations
    alpha_snmf: scalar
        Sparsity penalty

    Returns
    --------
    Ain: np.ndarray
        (d1*d2[*d3]) x K , spatial filter of each neuron.
    Cin: np.ndarray
        T x K , calcium activity of each neuron.
    center: np.ndarray
        K x 2 [or 3] , inferred center of each neuron.
    bin: np.ndarray
        (d1*d2[*d3]) x nb, initialization of spatial background.
    fin: np.ndarray
        nb x T matrix, initalization of temporal background.

    """

    
    if gSiz is None:
        gSiz = 2 * np.asarray(gSig) + 1

    d, T = np.shape(Y)[:-1], np.shape(Y)[-1]
    # rescale according to downsampling factor
    gSig = np.round(np.asarray(gSig) // ssub).astype(np.int)
    gSiz = np.round(np.asarray(gSiz) // ssub).astype(np.int)
    
   
    
    print('Noise Normalization')
    if normalize is True:
        if img is None:
            img = np.mean(Y, axis=-1)
            img += np.median(img)

        Y = old_div(Y, np.reshape(img, d + (-1,), order='F'))
        alpha_snmf /= np.mean(img)

    
    # spatial downsampling
    mean_val = np.mean(Y)
    if ssub != 1 or tsub != 1:
        print("Spatial Downsampling ...")
        Y_ds = downscale_local_mean(Y, tuple([ssub] * len(d) + [tsub]), cval=mean_val)
    else:
        Y_ds = Y

    
       
    print('Roi Extraction...')
    
    
    if method == 'greedy_roi':
        Ain, Cin, _, b_in, f_in = greedyROI(
            Y_ds, nr=K, gSig=gSig, gSiz=gSiz, nIter=nIter, kernel=kernel, nb=nb)
       
        if use_hals:
            print('Refining Components...')
            Ain, Cin, b_in, f_in = hals(Y_ds, Ain, Cin, b_in, f_in, maxIter=maxIter)
    elif method == 'sparse_nmf':
        Ain, Cin, _, b_in, f_in = sparseNMF(Y_ds, nr=K, nb=nb, max_iter_snmf=max_iter_snmf, alpha=alpha_snmf,
                                            sigma_smooth=sigma_smooth_snmf, remove_baseline=True, perc_baseline=perc_baseline_snmf)
#        print np.sum(Ain), np.sum(Cin)
#        print 'Refining Components...'
#        Ain, Cin, b_in, f_in = hals(Y_ds, Ain, Cin, b_in, f_in, maxIter=maxIter)
#        print np.sum(Ain), np.sum(Cin)
    else:
        print(method)
        raise Exception("Unsupported method")
        
        
    K = np.shape(Ain)[-1]
    ds = Y_ds.shape[:-1]

    Ain = np.reshape(Ain, ds + (K,), order='F')
    
    
    
    if len(ds) == 2:

        Ain = resize(Ain, d + (K,), order=1)

    else:  # resize only deals with 2D images, hence apply resize twice

        Ain = np.reshape([resize(a, d[1:] + (K,), order=1)
                          for a in Ain], (ds[0], d[1] * d[2], K), order='F')
        Ain = resize(Ain, (d[0], d[1] * d[2], K), order=1)

    Ain = np.reshape(Ain, (np.prod(d), K), order='F')
   
    #import pdb
    # pdb.set_trace()

    b_in = np.reshape(b_in, ds + (nb,), order='F')

    if len(ds) == 2:
        b_in = resize(b_in, d + (nb,), order=1)
    else:
        b_in = np.reshape([resize(b, d[1:] + (nb,), order=1)
                           for b in b_in], (ds[0], d[1] * d[2], nb), order='F')
        b_in = resize(b_in, (d[0], d[1] * d[2], nb), order=1)

    b_in = np.reshape(b_in, (np.prod(d), nb), order='F')

    Cin = resize(Cin, [K, T])

    f_in = resize(np.atleast_2d(f_in), [nb, T])
    # center = com(Ain, *d)
    center = np.asarray([center_of_mass(a.reshape(d, order='F')) for a in Ain.T])

    if normalize is True:
        #import pdb
        # pdb.set_trace()
        Ain = Ain * np.reshape(img, (np.prod(d), -1), order='F')
        
        b_in = b_in * np.reshape(img, (np.prod(d), -1), order='F')
        # b_in = np.atleast_2d(b_in * img.flatten('F')) #np.reshape(img,
        # (np.prod(d), -1),order='F')
        Y = Y * np.reshape(img, d + (-1,), order='F')

    
    
    return Ain, Cin, b_in, f_in, center
コード例 #31
0
ファイル: load_scan.py プロジェクト: casimp/lightct
    def reconstruct(self, downsample=(4, 4), crop=True, median_filter=False, 
                    kernel=9, recon_alg='fbp', sart_iters=1, 
                    crop_circle=False, save=True, fancy_out=True):
        """
        Reconstruct the data using a radon transform. Reconstructed slices
        saved in folder specified upon class creation.

        # downsample: Downsample (local mean) data before reconstructing.
                      Specify mean kernel size (height, width).
        # pre_filter: If True apply median filter to data before reconstructing
        # kernel:     Kernel size to use for median filter
        """
            
        if self.cor_offset >= 0:
            images = self.im_stack[:, self.cor_offset:]
        else:
            images = self.im_stack[:, :self.cor_offset]
            
        images = images[:, :, self.p0:self.num_images + self.p0]
        
        if crop:
            left, right, top, bottom = self.crop
            images = images[top:bottom, left:right]
            
        images = downscale_local_mean(images, downsample + (1, ))
        recon_height, recon_width = images.shape[:2]
        self.recon_data = np.zeros((recon_width, recon_width, recon_height))

        if median_filter:
            print('Applying median filter...')
            for i in range(images.shape[-1]):
                sys.stdout.write('\rProgress: [{0:20s}] {1:.0f}%'.format('#' *
                                 int(20 * (i + 1) / images.shape[-1]),
                                 100 * ((i + 1) / images.shape[-1])))
                sys.stdout.flush()
                images[:, :, i] = medfilt(images[:, :, i], kernel_size=kernel)

        print('\nReconstructing...')
        if save:            
            save_folder = os.path.join(self.folder, 'reconstruction')
            
            if not os.path.exists(save_folder):
                os.makedirs(save_folder)
            for the_file in os.listdir(save_folder):
                file_path = os.path.join(save_folder, the_file)
                if os.path.isfile(file_path):
                        os.unlink(file_path)

        if fancy_out:
            fig, ax = plt.subplots(figsize=(4, 4))
            fig.canvas.set_window_title('Reconstruction')
            patch = Wedge((.5, .5), .375, 90, 90, width=0.1)
            ax.add_patch(patch)
            ax.axis('equal')
            ax.set_xlim([0, 1])
            ax.set_ylim([0, 1])
            ax.axis('off')
            t = ax.text(0.5, 0.5, '0%%', fontsize=15, ha='center', va='center')

        for j in range(recon_height):
            # Update figure every other slice
            if fancy_out and j % 2 == 0:
                patch.set_theta1(90 - 360 * (j + 1) / recon_height)
                progress = 100 * (j + 1) / recon_height
                t.set_text('%02d%%' % progress)
                plt.pause(0.001)
            else:
                sys.stdout.write('\rProgress: [{0:20s}] {1:.0f}%'.format('#' *
                                 int(20 * (j + 1) / recon_height),
                                 100 * ((j + 1) / recon_height)))
                sys.stdout.flush()
            sino_tmp = np.squeeze(images[j, :, :])
            
            if recon_alg is 'sart':
                image_tmp = iradon_sart(sino_tmp, theta=self.angles)
                for i in range(sart_iters - 1):
                    image_tmp = iradon_sart(sino_tmp, theta=self.angles, 
                                            image=image_tmp)
            else:
                image_tmp = iradon(sino_tmp, theta=self.angles, 
                                   filter=None, circle=True)
#            if crop_circle:
#                image_tmp = image_tmp[w0:wf, w0:wf]
                
            self.recon_data[:, :, j] = image_tmp
            
        if crop_circle:
            w = int((recon_width**2 / 2)**0.5) 
            w = w if (w - recon_width) % 2 == 0 else w - 1
            w0 = int((recon_width - w) / 2 )
            wf = int(w0 + w)
            self.recon_data = self.recon_data[w0:wf, w0:wf]
            
        if save:    
            for j in range(recon_height):
                image_tmp = self.recon_data[:, :, j]
                imsave(os.path.join(save_folder, '%04d.tif' % j), image_tmp)

        if fancy_out:
            plt.close()
コード例 #32
0
ファイル: gradients.py プロジェクト: ivanmysin/Vision
abs_pix = np.sqrt((x**2 + y**2)).ravel()
angle_pix = np.arctan2(y, x).ravel()

angle_array, abs_array = np.meshgrid(np.linspace(-np.pi, np.pi, 900),
                                     np.linspace(0, np.sqrt(2), 900))

polar_image = griddata((angle_pix, abs_pix),
                       image.ravel(), (angle_array.ravel(), abs_array.ravel()),
                       method='nearest')  #nearest cubic linear

polar_image = polar_image.reshape(900, 900)

grad_r, grad_fi = np.gradient(polar_image)

grad_r = downscale_local_mean(grad_r, (30, 30))
grad_fi = downscale_local_mean(grad_fi, (30, 30))
abs_array = abs_array[::30, ::30]
angle_array = angle_array[::30, ::30]
polar_image = np.zeros((30, 30), dtype=np.float)

grad_r_na = grad_r[:, :450]
grad_r_na = np.fliplr(grad_r_na)
polar_image[:, :450] = np.fliplr(cumtrapz(grad_r_na, axis=0, initial=0))
polar_image[:, 450:] = cumtrapz(grad_r[:, 450:], axis=0, initial=0)

# polar_image = cumtrapz(grad_r, axis=0, initial=0)
# integral_over_fi = 0
# for i in range( grad_fi.shape[1] - 2 ):
#     integral_over_fi += cumtrapz(np.roll(grad_fi, i+1, axis=1), axis=1, initial=0)
# integral_over_fi /= (i+1)
コード例 #33
0
def downsample_4_image(path):
    image = io.imread(path, as_grey=True)
    image_downsampled = downscale_local_mean(image, (4, 4))
    plt.gray()
    matplotlib.image.imsave(path + "_downsampled.jpg", image_downsampled)
コード例 #34
0
    def _prepare_data(idx, batch_idx):

        # --------------------------------------------------------------------------------------------------------------
        # Preparation of the image data

        if len(im_list_raw) > idx:
            # Check whether the result exists if a target folder is given here
            if target_folder is not None:
                if os.path.isfile(os.path.join(target_folder, os.path.split(im_list_raw[idx])[1])):
                    # Return Nones for each slice that was already computed
                    if verbose:
                        print('{} already exists, nothing to do...'.format(os.path.split(im_list_raw[idx])[1]))
                    return None, None, None, None
            # Load the raw data slice
            im = imread(im_list_raw[idx])
            assert im.dtype in ['uint8', 'uint16'], 'Only the data types uint8 and uint16 are supported!'
        else:
            # This happens if the number of images does not divide by the number of batches (smaller last batch)
            # Return Nones for each missing slice to fill up the batch
            if verbose:
                print('Filling up batch...')
            return None, None, None, None

        # Start end end indices for the median filter, idx being the current slice position in the center
        start_id = idx - median_radius
        end_id = idx + median_radius + 1

        # Load the necessary data for the z-median filtered slice
        for load_idx, slice_idx in enumerate(range(start_id, end_id)):
            load_idx += idx - batch_idx
            # print('load_idx = {}; slice_idx = {}'.format(load_idx, slice_idx))
            if 0 <= slice_idx < len(im_list_pre):
                if template_data[load_idx] is None:
                    template_data[load_idx] = imread(im_list_pre[slice_idx])
                    assert template_data[load_idx].dtype == 'uint8', 'Only 8bit template data is supported!'
            # else:
            #     print('Template data is not None')
            # else:
            #     print('slice_idx < 0')

        # Do the median smoothing to obtain one composition image at the current z-position
        ims = [x for x in template_data[idx - batch_idx: idx - batch_idx + median_radius * 2 + 1] if x is not None]
        median_z = np.median(ims, axis=0).astype('uint8')
        del ims

        # The sift doesn't like images of different size. This fixes some cases by cropping away areas from the raw data
        # that are zero and padding it with zeros to the size of the template image
        # We are assuming here that the template data slices are larger than the non-zero region of interest in the raw
        # data.
        if im.shape != median_z.shape:
            try:
                bounds = _crop_zero_padding(im)
                cropped_im = im[bounds]
                t_im = np.zeros(median_z.shape, dtype=im.dtype)

                t_im[0:cropped_im.shape[0], 0:cropped_im.shape[1]] = cropped_im
                im = t_im
            except ValueError as e:
                # This happened when a white slice was present (no zero pixel)
                warnings.warn('Cropping zero-padding failed with ValueError: {}'.format(str(e)))
                print('This happens if a completely black slice is present in the data. ')
                print('Affected slice: {}'.format(im_list_raw[idx]))
                print('Replacing with empty slice ...')
                im = np.zeros(median_z.shape, dtype=im.dtype)

        # Gaussian smooth the image and the median_z for the SIFT step
        if sift_sigma is not None:
            median_z_smooth = gaussianSmoothing(median_z, sift_sigma)
            if im.dtype == 'uint8':
                im_smooth = gaussianSmoothing(im, sift_sigma)
            else:
                # vigra.gaussianSmoothing cannot handle 16 bit data
                im_smooth = gaussianSmoothing(im.astype('float32'), sift_sigma)
                # The data for the sift step does not require to be 16 bit
                im_smooth = (im_smooth / (2 ** 16) * (2 ** 8)).astype('uint8')
        else:
            median_z_smooth = median_z.copy()
            im_smooth = im.copy()
            if im_smooth.dtype == 'uint16':
                # The data for the sift step does not require to be 16 bit
                im_smooth = (im_smooth.astype('float32') / (2 ** 16) * (2 ** 8)).astype('uint8')

        # Downsample for SIFT step for speed-up of computation
        if sift_downsample is not None:
            median_z_smooth = downscale_local_mean(median_z_smooth, sift_downsample).astype('uint8')
            im_smooth = downscale_local_mean(im_smooth, sift_downsample).astype('uint8')

        # --------------------------------------------------------------------------------------------------------------
        # Return the results
        assert im.dtype in ['uint8', 'uint16']
        assert im_smooth.dtype == 'uint8'
        assert median_z.dtype == 'uint8'
        assert median_z_smooth.dtype == 'uint8'
        return im, im_smooth, median_z, median_z_smooth
コード例 #35
0
def cascadeCorners(img_path, georef_path, truth_corners, plot,
                   downscale_factor):
    img = imread(img_path, as_gray=True)
    georef_img = imread(georef_path, as_gray=True)

    # downscale images
    img_small = downscale_local_mean(img, (downscale_factor, downscale_factor))
    georef_img_small = downscale_local_mean(
        georef_img, (downscale_factor, downscale_factor))
    # blurring adds 0.8s (6.7->7.5) per image on average, but helps with correct corner identification (even though 3,3 gaussian is a bit much...)
    img_small = filters.gaussian(img_small, sigma=(3, 3), truncate=1.0)
    georef_img_small = filters.gaussian(georef_img_small,
                                        sigma=(3, 3),
                                        truncate=1.0)

    # rescale truth corners to small resolution
    scaled_corners = [(x // downscale_factor, y // downscale_factor)
                      for (x, y) in truth_corners]
    # find  corners in small image
    corner_points = findCorners(img_small,
                                georef_img_small,
                                scaled_corners,
                                template_size=20,
                                plot=plot)
    # rescale found coordinates to original resolution
    corner_points = [(x * downscale_factor, y * downscale_factor)
                     for (x, y) in corner_points]
    corner_coords = []
    for idx, corner in enumerate(corner_points):
        roi_size = 100
        template_size = 20
        # extract ROI from original size image
        x_min = max(0, corner[1] - roi_size)
        x_max = max(0, corner[1] + roi_size)
        y_min = max(0, corner[0] - roi_size)
        y_max = max(0, corner[0] + roi_size)
        roi = georef_img[x_min:x_max, y_min:y_max]
        ref_corner = truth_corners[idx]
        template = img[ref_corner[1] - template_size:ref_corner[1] +
                       template_size, ref_corner[0] -
                       template_size:ref_corner[0] + template_size]
        # match again in ROIs
        match = match_corner(roi, template)
        match = (match[0] + (y_min), match[1] + (x_min)
                 )  # scale match to non-ROI positions
        corner_coords.append(get_coords_from_raster(georef_path, match))

        if plot:
            # show corners
            ax = plt.subplot(2, 4, idx + 1)
            ax.set_title("original")
            plt.xticks([], [])
            plt.yticks([], [])
            plt.gray()
            plt.imshow(template)
            ax = plt.subplot(2, 4, idx + 5)
            ax.set_title("warped")
            plt.xticks([], [])
            plt.yticks([], [])
            plt.gray()
            x_min = max(0, match[1] - template_size)
            x_max = min(georef_img.shape[0], match[1] + template_size)
            y_min = max(0, match[0] - template_size)
            y_max = min(georef_img.shape[1], match[0] + template_size)
            plt.imshow(georef_img[x_min:x_max, y_min:y_max])
            # plt.imshow(georef_img[match[1]-template_size:match[1]+template_size, match[0]-template_size:match[0]+template_size])
            # TODO: plot center point, to show pixel perfect location
    if plot:
        plt.show()

    return corner_coords
コード例 #36
0
	img = mpimg.imread(x[i][2])
	gray_img = segment(img).ravel()
	trainX = np.vstack((trainX, gray_img))
	trainY.append(int(x[i][0]))

trainY = np.array(trainY)


f = open('test','r')
x = f.readlines()
testY = []
testX = np.array([]).reshape(0, 300*400)

for i in range(len(x)):
	x[i] = x[i].strip().split('\t')
	path = x[i][2]
	img = imread(x[i][2])
	print(i)
	gray_img = segment(img)
	gray_img = downscale_local_mean(gray_img, (4,4)).ravel()
	testX = np.vstack((testX, gray_img))
	testY.append(int(x[i][0]))

testY = np.array(testY)


np.save('testX.npy', testX)
np.save('testY.npy', testY)
# np.save('trainX.npy', trainX)
# np.save('trainY.npy', trainY)
コード例 #37
0
ファイル: utils.py プロジェクト: davidreiman/ulmo
def preproc_field(field,
                  mask,
                  inpaint=True,
                  median=True,
                  med_size=(3, 1),
                  downscale=True,
                  dscale_size=(2, 2),
                  sigmoid=False,
                  scale=None,
                  expon=None,
                  only_inpaint=False,
                  gradient=False,
                  log_scale=False):
    """
    Preprocess an input field image with a series of steps:
        1. Inpainting
        2. Median
        3. Downscale
        4. Sigmoid
        5. Scale
        6. Remove mean

    Parameters
    ----------
    field : np.ndarray
    mask : np.ndarray
        Data mask.  True = masked
    inpaint : bool, optional
        if True, inpaint masked values
    median : bool, optional
        If True, apply a median filter
    med_size : tuple
        Median window to apply
    downscale : bool, optional
        If True downscale the image
    dscale_size : tuple, optional
        Size to rescale by
    scale : float
        Scale the SSTa values by this multiplicative factor
    expon : float
        Exponate the SSTa values by this exponent
    gradient : bool, optional
        If True, apply a Sobel gradient enhancing filter

    Returns
    -------
    pp_field, meta_dict : np.ndarray, dict
        Pre-processed field, mean temperature

    """
    meta_dict = {}
    # Inpaint?
    if inpaint:
        if mask.dtype.name != 'uint8':
            mask = np.uint8(mask)
        field = sk_inpaint.inpaint_biharmonic(field, mask, multichannel=False)

    if only_inpaint:
        if np.any(np.isnan(field)):
            return None, None
        else:
            return field, None

    # Capture more metadata
    srt = np.argsort(field.flatten())
    meta_dict['Tmax'] = field.flatten()[srt[-1]]
    meta_dict['Tmin'] = field.flatten()[srt[0]]
    i10 = int(0.1 * field.size)
    i90 = int(0.9 * field.size)
    meta_dict['T10'] = field.flatten()[srt[i10]]
    meta_dict['T90'] = field.flatten()[srt[i90]]

    # Median
    if median:
        field = median_filter(field, size=med_size)

    # Reduce to 64x64
    if downscale:
        field = downscale_local_mean(field, dscale_size)

    # Check for junk
    if np.any(np.isnan(field)):
        return None, None

    # De-mean the field
    mu = np.mean(field)
    pp_field = field - mu
    meta_dict['mu'] = mu

    # Sigmoid?
    if sigmoid:
        pp_field = special.erf(pp_field)

    # Scale?
    if scale is not None:
        pp_field *= scale

    # Exponate?
    if expon is not None:
        neg = pp_field < 0.
        pos = np.logical_not(neg)
        pp_field[pos] = pp_field[pos]**expon
        pp_field[neg] = -1 * (-1 * pp_field[neg])**expon

    # Sobel Gradient?
    if gradient:
        pp_field = filters.sobel(pp_field)

    # Log?
    if log_scale:
        if not gradient:
            raise IOError("Only implemented with gradient=True so far")
        # Set 0 values to the lowest non-zero value
        zero = pp_field == 0.
        if np.any(zero):
            min_nonz = np.min(pp_field[np.logical_not(zero)])
            pp_field[zero] = min_nonz
        # Take log
        pp_field = np.log(pp_field)

    # Return
    return pp_field, meta_dict
コード例 #38
0
ファイル: feature_constructor.py プロジェクト: sguo28/IDQ_HEX
 def trip_diffusion_convolution(self, img, trip_filter):
     n = DESTINATION_PROFILE_SPATIAL_AGGREGATION
     M = downscale_local_mean(img, (n, n))
     M = np.tensordot(trip_filter, M)
     M = resize(M, img.shape, mode='edge')
     return M
コード例 #39
0
# 4. Register
###

path = sys.argv[1]
nframes = int(sys.argv[2])
#'/media/timrudge/tjr34_ntfs/Microscopy/Cavendish/10.01.16/Pos0000'
infile = os.path.join(path, 'Frame%04dStep%04d.tiff')
outfile = os.path.join(path, 'aligned_Frame%04dStep%04d.tiff')
outfile_mask = os.path.join(path, 'aligned_mask_Frame%04dStep%04d.tiff')
startframe = 0
step = 1
scale = 2

im1 = imread(infile % (0, startframe), plugin='tifffile').astype(np.float32)
im1 = gaussian_filter(im1, 1)
im1 = downscale_local_mean(im1, (scale, scale))

# Use first image to compute background of 1st channel
bgmean = np.mean(im1.ravel())
bgstd = np.std(im1.ravel())
bgval = bgmean + bgstd * 2
print(bgmean)

im2 = imread(infile % (2, startframe + step),
             plugin='tifffile').astype(np.float32)
im2 = gaussian_filter(im2, 1)
im2 = downscale_local_mean(im2, (scale, scale))

shifts = np.zeros((nframes - step, 2))

# Offset images to align to sequentially
コード例 #40
0
ファイル: raster.py プロジェクト: aman-thakral/pyspatial
def rasterize(shp, ext_outline=False, ext_fill=True, int_outline=False,
              int_fill=False, scale_factor=4):

    """Convert a vector shape to a raster. Assumes the shape has already
    been transformed in to a pixel based coordinate system. The algorithm
    checks for the intersection of each point in the shape with
    a pixel grid created by the bounds of the shape. Partial overlaps
    are estimated by scaling the image in X and Y by the scale factor,
    rasterizing the shape, and downscaling (using mean), back to the
    bounds of the original shape.

    Parameters
    ----------

    shp: shapely.Polygon or Multipolygon
        The shape to rasterize

    ext_outline: boolean (default False)
        Include the outline of the shape in the raster

    ext_fill: boolean (default True)
        Fill the shape in the raster

    int_outline: booelan (default False)
        Include the outline of the interior shapes

    int_fill: boolean (default False):
        Fill the interior shapes

    scale_factor: int (default 4)
        The amount to scale the shape in X, Y before downscaling. The
        higher this number, the more precise the estimate of the overlap.


    Returns
    -------
    np.ndarray representing the rasterized shape.
    """
    sf = scale_factor

    minx, miny, maxx, maxy = map(int, shp.bounds)
    if minx == maxx and miny == maxy:
        return np.array([[1.]])

    elif maxy > miny and minx == maxx:
        n = maxy - miny + 1
        return np.zeros([n, 1]) + 1./n

    elif maxy == miny and minx < maxx:
        n = maxx - minx + 1
        return np.zeros([1, n]) + 1./n

    if ((maxx - minx + 1) + (maxy - miny + 1)) <= 2*sf:
        sf = 1.0

    shp = scale(shp, xfact=sf, yfact=sf)
    minx, miny, maxx, maxy = shp.bounds
    width = int(maxx - minx + 1)
    height = int(maxy - miny + 1)

    img = Image.new('L', (width, height), 0)
    _shp = shp.geoms if hasattr(shp, "geoms") else [shp]

    ext_outline = int(ext_outline)
    ext_fill = int(ext_fill)
    int_outline = int(int_outline)
    int_fill = int(int_fill)

    for pg in _shp:
        ext_pg = [(x-minx, y-miny) for x, y in pg.exterior.coords]
        ImageDraw.Draw(img).polygon(ext_pg, outline=ext_outline, fill=ext_fill)
        for s in pg.interiors:
            int_pg = [(x-minx, y-miny) for x, y in s.coords]
            ImageDraw.Draw(img).polygon(int_pg, outline=int_outline,
                                        fill=int_fill)

    return downscale_local_mean(np.array(img), (sf, sf))
コード例 #41
0
def initialize_components(Y, K=30, gSig=[5, 5], gSiz=None, ssub=1, tsub=1, nIter=5, maxIter=5,
                          kernel=None, use_hals=True, Cn=None, sn=None):
    """Initalize components

    This method uses a greedy approach followed by hierarchical alternative least squares (HALS) NMF.
    Optional use of spatio-temporal downsampling to boost speed.

    Parameters
    ----------
    Y: np.ndarray
         d1 x d2 [x d3] x T movie, raw data.
    K: [optional] int
        number of neurons to extract (default value: 30).
    tau: [optional] list,tuple
        standard deviation of neuron size along x and y [and z] (default value: (5,5).
    gSiz: [optional] list,tuple
        size of kernel (default 2*tau + 1).
    nIter: [optional] int
        number of iterations for shape tuning (default 5).
    maxIter: [optional] int
        number of iterations for HALS algorithm (default 5).
    ssub: [optional] int
        spatial downsampling factor recommended for large datasets (default 1, no downsampling).
    tsub: [optional] int
        temporal downsampling factor recommended for long datasets (default 1, no downsampling).
    kernel: [optional] np.ndarray
        User specified kernel for greedyROI (default None, greedy ROI searches for Gaussian shaped neurons)
    use_hals: [bool]
        Whether to refine components with the hals method

    Returns
    --------
    Ain: np.ndarray
        (d1*d2[*d3]) x K , spatial filter of each neuron.
    Cin: np.ndarray
        T x K , calcium activity of each neuron.
    center: np.ndarray
        K x 2 [or 3] , inferred center of each neuron.
    bin: np.ndarray
        (d1*d2[*d3]) x nb, initialization of spatial background.
    fin: np.ndarray
        nb x T matrix, initalization of temporal background.

    """
    
        
    if gSiz is None:
        gSiz = 2 * np.asarray(gSig) + 1

    d, T = np.shape(Y)[:-1], np.shape(Y)[-1]
    # rescale according to downsampling factor
    gSig = np.round(np.asarray(gSig) / ssub).astype(np.int)
    gSiz = np.round(np.asarray(gSiz) / ssub).astype(np.int)

    print 'Noise Normalization'
    if sn is not None:
        min_noise = np.percentile(sn, 2)
        noise = np.maximum(sn, min_noise)
        Y = Y / np.reshape(noise, d + (-1,),order='F')

    # spatial downsampling
    mean_val = np.mean(Y)
    if ssub != 1 or tsub != 1:
        print "Spatial Downsampling ..."
        Y_ds = downscale_local_mean(Y, tuple([ssub] * len(d) + [tsub]), cval=mean_val)
        if Cn is not None:
            Cn = downscale_local_mean(Cn, tuple([ssub] * len(d)), cval=mean_val)
    else:
        Y_ds = Y

    print 'Roi Extraction...'

    Ain, Cin, _, b_in, f_in = greedyROI(
        Y_ds, nr=K, gSig=gSig, gSiz=gSiz, nIter=nIter, kernel=kernel)
    if use_hals:
        print 'Refining Components...'
        Ain, Cin, b_in, f_in = hals(Y_ds, Ain, Cin, b_in, f_in, maxIter=maxIter)

    ds = Y_ds.shape[:-1]
    Ain = np.reshape(Ain, ds + (K,), order='F')
    
    if len(ds) == 2:

        Ain = resize(Ain, d + (K,), order=1)

    else:  # resize only deals with 2D images, hence apply resize twice

        Ain = np.reshape([resize(a, d[1:] + (K,), order=1) for a in Ain], (ds[0], d[1] * d[2], K), order='F')
        Ain = resize(Ain, (d[0], d[1] * d[2], K), order=1)

    Ain = np.reshape(Ain, (np.prod(d), K), order='F')

    b_in = np.reshape(b_in, ds, order='F')

#    b_in = resize(b_in, ds)
    b_in = resize(b_in, d)
    
    b_in = np.reshape(b_in, (-1, 1), order='F')

    Cin = resize(Cin, [K, T])
    
    f_in = resize(np.atleast_2d(f_in), [1, T])
    # center = com(Ain, *d)
    center = np.asarray([center_of_mass(a.reshape(d, order='F')) for a in Ain.T])

    if sn is not None:        
        Ain = Ain * np.reshape(noise, (np.prod(d), -1),order='F')      
        b_in = b_in * np.atleast_2d(noise).T
        Y = Y * np.reshape(noise, d + (-1,),order='F')

    return Ain, Cin, b_in, f_in, center
コード例 #42
0
ファイル: 6-punk.py プロジェクト: mikalaisyty/python4zkm
# antialias not yet in skimage 0.13.1 ....
imm = rescale(imgray, 1.0 / 4.0, mode="reflect")  # , anti_aliasing=False)
lb = "Resscale / 4"
iml.append(imm)
lbl.append(lb)
print("shape: ", imm.shape, ", dtype: ", imm.dtype)

imm = resize(imgray, (imgray.shape[0] / 4, imgray.shape[1] / 4),
             mode="reflect")  #, anti_aliasing=True)
lb = "Resize to size"
iml.append(imm)
lbl.append(lb)
print("shape: ", imm.shape, ", dtype: ", imm.dtype)
print(lb)

imm = downscale_local_mean(imgray, (4, 3))
lb = "Downscale 4/3"
iml.append(imm)
lbl.append(lb)
print("shape: ", imm.shape, ", dtype: ", imm.dtype)
print(lb)

imm = rotate(imgray, 90, resize=True, mode="reflect")
lb = "Rotate"
iml.append(imm)
lbl.append(lb)
print("shape: ", imm.shape, ", dtype: ", imm.dtype)
print(lb)

imm = imgray > filters.threshold_li(imgray)
lb = "Threshold"
コード例 #43
0
ファイル: main.py プロジェクト: Achifaifa/cartoplotter
#! /usr/bin/env python

from skimage import io, feature, transform

startlong=-8.
startlat=43.
scale=10.

dbpic=io.imread("./example.jpg",as_grey=True)
dbpic=transform.downscale_local_mean(dbpic, (10, 10))
edges=feature.canny(dbpic)

with open("./out.csv","w+") as out:
  out.write("point,latitude,longitude\n")
  for ni,i in enumerate(edges):
    for nj,j in enumerate(i):
      if j:
        out.write("%02i-%02i,"%(ni,nj)) 
        out.write("%f,%f\n"%(startlat-(ni/scale),startlong+(nj/scale)))

コード例 #44
0
 def rebin(img):            
     img = img.reshape(inpshape)
     if crop_left:
         img = img[crop_left:,crop_left:]
     ret=downscale_local_mean(img, factors)
     return ret
コード例 #45
0
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches

from skimage import data, io
from skimage.filters import threshold_otsu
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import closing, square
from skimage.color import label2rgb, rgb2gray
from skimage.transform import downscale_local_mean

im_file = io.imread("photo.bmp")
#image = data.coins()[50:-50, 50:-50]
# image = scaled[:, :, 0]
im_gray = rgb2gray(im_file)
image = downscale_local_mean(im_gray, (10, 10))
# io.imsave("photo.png", image)

# apply threshold
thresh = threshold_otsu(image)
bw = closing(image > thresh, square(5))

# remove artifacts connected to image border
cleared = clear_border(bw)

# label image regions
label_image = label(bw)
image_label_overlay = label2rgb(label_image, image=image)

fig, ax = plt.subplots(figsize=(10, 6))
ax.imshow(image)  #_label_overlay)
コード例 #46
0
def filter_and_voxelise(corrected_path):
    mean_filtered_stack = mean_filter(corrected_path)
    print 'Voxelising stack'
    voxelised_stack = tf.downscale_local_mean(mean_filtered_stack, (1, 4, 4))
    return voxelised_stack
コード例 #47
0
def generate(csv_file, images_folder, fraction):
    print("Generating input...")

    # Read dataset
    df = pd.read_csv(csv_file)

    # Add label
    df.loc[df['EncodedPixels'].notnull(), 'EncodedPixels'] = 1
    df.loc[df['EncodedPixels'].isnull(), 'EncodedPixels'] = 0

    # Change column name
    df.columns = ['ImageId', 'Label']

    # Remove duplicates
    df_dropped = df.drop_duplicates(['ImageId'], keep='first')

    # Select a fraction of samples
    df_set = df_dropped.sample(frac=fraction, random_state=1)

    m = df_set.shape[0]
    print("Number of samples: ", m)

    # Create empty dataframe
    x = np.empty((0, 128 * 128), float)

    for i in range(m):
        # Read image
        image_path = images_folder + '/' + df_set.iloc[i, 0]
        img = io.imread(image_path)

        # Convert to grayscale
        img_gray = rgb2gray(img)

        # Compute FFT
        img_fft = fftpack.fft2(img_gray)
        img_abs = np.abs(img_fft) / (768 * 768)

        # Downscale
        fft_t = transform.downscale_local_mean(img_abs, (6, 6))

        # Append to dataframe
        array = np.reshape(fft_t, (-1))
        x = np.vstack((x, array))

    # Column names
    column_names = ['ImageId', 'Label']
    for i in range(x.shape[1]):
        column_names.append('Pixel_' + str(i))

    # Create dataframe from pixels values
    df_pixels = pd.DataFrame(x, columns=column_names[2:])

    # Concatenate the pixels values
    df_set.reset_index(drop=True, inplace=True)
    df_set = pd.concat([df_set, df_pixels], axis=1, ignore_index=True)

    # Add column names
    df_set.columns = column_names

    # Save csv
    df_set.to_csv('train_modified.csv', index=False)
コード例 #48
0
    def image_to_patches(self,
                         path,
                         patch_size=7,
                         iterations=500,
                         batch_size=20,
                         color=False,
                         downscale_factor=2,
                         is_matrix=False,
                         is_recons=False):
        '''
        #*****

        args:
            path (string): Path and filename of input image
            patch_size (int): Pixel dimension of square patches taken of image
            color (boolean): Specifies conversion of image to RGB (True) or grayscale (False).
                Default value = false. When color = True, images in gray colorspace will still appear
                gray, but will thereafter be represented in RGB colorspace using three channels.
            downscale_factor: Specifies the extent to which the image will be downscaled. Greater values
                will result in more downscaling but faster speed. For no downscaling, use downscale_factor=1.
        returns: #***

        '''
        #open image and convert it to either RGB (three channel) or grayscale (one channel)
        if is_matrix:
            img = np.load(path)
            data = (img + 1) / 2  # it was +-1 matrix; now it is 0-1 matrix
        else:
            img = Image.open(path)
            if color:
                img = img.convert('RGB')
                img = tl_unfold(img, mode=2).T
                print('img.shape_color_after_unfolding', img.shape)
            else:
                img = img.convert('L')
            # normalize pixel values (range 0-1)
            data = np.asarray(img) / 255
        '''
        img = np.load(path)
        data = (img + 1) / 2  # it was +-1 matrix; now it is 0-1 matrix
        '''
        if DEBUG:
            print(np.asarray(img))

        # downscale image for speed
        if downscale_factor > 1:
            data = downscale_local_mean(data,
                                        (downscale_factor, downscale_factor))

        # show_array(data)
        if not is_recons:
            patches = extract_patches_2d(data, (patch_size, patch_size),
                                         max_patches=iterations * batch_size)
        else:  # for reconstruction we need to use all patches not to mess up with the ordering
            patches = extract_patches_2d(data, (patch_size, patch_size))
        # we do not need more than iterations*batch_size patches for training dictionaries
        # convert 7x7 squares to 1d (49,) vectors
        patches_flat = patches.reshape(len(patches), -1).T
        # (Num patches)*(7*7) array
        # .reshape(len(patches),-1) makes it two dimensional -- (Num patches)*(whatever that's correct)
        # take transpose to make it 49 * (Num patches)
        print(patches_flat.shape)
        return patches_flat
コード例 #49
0
 def downscale_channel(self, channel):
      return downscale_local_mean(channel, (self.down_factor, self.down_factor)).astype(np.uint8)
コード例 #50
0
def main():
    """
    This script converts MRI scans in nifti format from a specified data path to numpy arrays (.npy). This is the
    required format to be used in the data generators of the Keras deep learning model created for the MRI-based
    classification of Alzheimer's Disease.

    In the settings can be specified which data set and data type should be converted. Also can be specified whether
    images should be converted to whole brain and/or down sampled images. A mask is applied to all images so that
    the area outside the brain is set to zero. Furthermore the images are cropped to this mask.

    This script should be run to create data before a model can be trained on this data with the 'main.py' script.
    """

    # SETTINGS
    dataset = "PSI"            # ADNI / PSI
    datatype = "GM"             # T1 / T1m / GM
    WB = False                   # if True: whole brain images, if False: downsampled by factor 4
    testing = True             # if True: only applied to 4 subjects

    # create output dir
    if WB:
        save_path_WB = f"path/to/WB/"
        create_data_directory(save_path_WB)
    else:
        save_path_f4 = f"path/to/f4/"
        create_data_directory(save_path_f4)

    # set path to data
    if dataset == "ADNI":
        if datatype == "GM":
            data_path = f"path/to/cartesius_mount/Template_space/*/Brain_image_in_template_space/gmModulatedJacobian.nii.gz"
        elif datatype == "T1":
            data_path = "/mnt/cartesius/ADNI/Template_space/*_bl/Brain_image_in_MNI_space/result.nii.gz"
        elif datatype == "T1m":
            data_path = "/mnt/cartesius/ADNI/Template_space/*_bl/Brain_image_in_template_space/T1wModulatedJacobian.nii.gz"
    elif dataset == "PSI":
        if datatype == "GM":
            data_path = f"path/to/cartesius_mount_parelsnoer/Template_space/*/Brain_image_in_template_space/gmModulatedJacobian.nii.gz"
        elif datatype == "T1":
            data_path = "/mnt/cartesius/Parelsnoer/Template_space/PSI_*/Brain_image_in_MNI_space/result.nii.gz"
        elif datatype == "T1m":
            data_path = "/mnt/cartesius/Parelsnoer/Template_space/PSI_*/Brain_image_in_template_space/T1wModulatedJacobian.nii.gz"

    # create mask of brain
    template_file = f"path/to/cartesius_mount/Template_space/brain_mask_in_template_space.nii.gz"
    template = nib.load(template_file).get_fdata()
    mask = np.zeros(template.shape)
    mask[np.where(template != 0)] = 1

    cnt = 0

    # loop over all files in data path
    for filename in glob.glob(data_path):

        cnt += 1

        # get subject ID
        if dataset == "ADNI":
            # format: ".../Template_space/*/Brain_image_in_template_space/..."
            common_substring_1 = "Template_space"
            common_substring_2 = "Brain_image_in_template_space"
            x1 = filename.index(common_substring_1)
            x2 = filename.index(common_substring_2)
            subject = filename[x1+15:x2-1]
        elif dataset == "PSI":
            #subject = filename[41:50]
            common_substring_1 = "Template_space"
            common_substring_2 = "Brain_image_in_template_space"
            x1 = filename.index(common_substring_1)
            x2 = filename.index(common_substring_2)
            subject = filename[x1+15:x2-1]

        if WB:
            if os.path.exists(save_path_WB + subject + ".h5py"):
                continue
        else:
            if os.path.exists(save_path_f4 + subject + ".h5py"):
                continue


        # for testing break early
        if testing:
            if cnt % 5 == 0:
                break
            print(subject)

        # print progress
        if cnt % 50 == 0:
            print('\n----- Working on subject number ' + str(cnt) + ' -----')

        # load .nii data as 3d numpy array
        image = nib.load(filename).get_fdata()

        # normalization for signal intensity
        if datatype == "T1":
            image = normalize(operator.mul(image, mask))

        # apply mask + crop image
        masked = apply_mask(image, mask)

        # save as .npy
        if WB:
            # .npy to .h5py in order to save storage space
            with h5py.File(save_path_WB + subject + ".h5py", "w") as hf:
                hf.create_dataset(subject, data=masked, compression="gzip", compression_opts=9)
            #np.save(save_path_WB + subject + '.npy', masked)
        else:
            # down sample by factor 4 based on local mean
            downsampled = downscale_local_mean(masked, (4, 4, 4))
            # .npy to .h5py in order to save storage space
            with h5py.File(save_path_f4 + subject + ".h5py", "w") as hf:
                hf.create_dataset(subject, data=downsampled, compression="gzip", compression_opts=9)
コード例 #51
0
def initialize_components(Y, K=30, gSig=[5,5], gSiz=None, ssub=1, tsub=1, nIter = 5, maxIter=5, kernel = None, use_hals=True,Cn=None,sn=None): 
    """Initalize components 
    
    This method uses a greedy approach followed by hierarchical alternative least squares (HALS) NMF.
    Optional use of spatio-temporal downsampling to boost speed.
     
    Parameters
    ----------   
    Y: np.ndarray
         d1 x d2 x T movie, raw data.
    K: [optional] int
        number of neurons to extract (default value: 30).
    tau: [optional] list,tuple
        standard deviation of neuron size along x and y (default value: (5,5).
    gSiz: [optional] list,tuple
        size of kernel (default 2*tau + 1).
    nIter: [optional] int
        number of iterations for shape tuning (default 5).
    maxIter: [optional] int
        number of iterations for HALS algorithm (default 5).
    ssub: [optional] int
        spatial downsampling factor recommended for large datasets (default 1, no downsampling).
    tsub: [optional] int
        temporal downsampling factor recommended for long datasets (default 1, no downsampling).  
    kernel: [optional] np.ndarray
        User specified kernel for greedyROI (default None, greedy ROI searches for Gaussian shaped neurons) 
    use_hals: [bool]
        Whether to refine components with the hals method
  
    Returns
    --------    
    Ain: np.ndarray        
        (d1*d2) x K , spatial filter of each neuron.
    Cin: np.ndarray
        T x K , calcium activity of each neuron.
    center: np.ndarray
        K x 2 , inferred center of each neuron.
    bin: np.ndarray
        (d1*d2) X nb, initialization of spatial background.
    fin: np.ndarray
        nb X T matrix, initalization of temporal background.    
        
    """

    if gSiz is None:
        gSiz=(2*gSig[0] + 1,2*gSig[1] + 1)
     
    d1,d2,T=np.shape(Y) 
    # rescale according to downsampling factor
    gSig = np.round([gSig[0]/ssub,gSig[1]/ssub]).astype(np.int)
    gSiz = np.round([gSiz[0]/ssub,gSiz[1]/ssub]).astype(np.int)    
    
    print 'Noise Normalization'
    if sn is not None:
        min_noise=np.percentile(sn,2)
        noise=np.maximum(sn,min_noise)
        Y=Y/np.reshape(noise,(d1,d2))[:,:,np.newaxis]
    
    # spatial downsampling
    mean_val=np.mean(Y)
    if ssub!=1 or tsub!=1:
        print "Spatial Downsampling ..."
        Y_ds = downscale_local_mean(Y,(ssub,ssub,tsub),cval=mean_val)
        if Cn is not None:
            Cn = downscale_local_mean(Cn,(ssub,ssub),cval=mean_val)
    else:
        Y_ds = Y
   
        

    print 'Roi Extraction...'    
    
    Ain, Cin, _, b_in, f_in = greedyROI2d(Y_ds, nr = K, gSig = gSig, gSiz = gSiz, nIter=nIter, kernel = kernel)
    if use_hals:    
        print 'Refining Components...'    
        Ain, Cin, b_in, f_in = hals_2D(Y_ds, Ain, Cin, b_in, f_in,maxIter=maxIter);
    
    #center = ssub*com(Ain,d1s,d2s) 
    d1s,d2s,Ts=np.shape(Y_ds)
    Ain = np.reshape(Ain, (d1s, d2s,K),order='F')
    Ain = resize(Ain, [d1, d2, K],order=1)
    
    Ain = np.reshape(Ain, (d1*d2, K),order='F') 
    
    b_in=np.reshape(b_in,(d1s, d2s),order='F')
    
    b_in = resize(b_in, [d1, d2]);
    
    b_in = np.reshape(b_in, (d1*d2, 1),order='F')
    
    Cin = resize(Cin, [K, T])
    f_in = resize(np.atleast_2d(f_in), [1, T])  
    center = com(Ain,d1,d2)
    
    if sn is not None:
        Ain=Ain*np.reshape(noise,(d1*d2,))[:,np.newaxis]
        b_in=b_in*np.reshape(noise,(d1*d2,))
        Y=Y*np.reshape(noise,(d1,d2))[:,:,np.newaxis]
    
    return Ain, Cin, b_in, f_in, center
コード例 #52
0
`Downscale` operation serves the purpose of downsampling an
n-dimensional image by calculating local mean on the elements of
each block of the size factors given as a parameter to the function.
"""

import matplotlib.pyplot as plt

from skimage import data
from skimage.transform import rescale, resize, downscale_local_mean

image = data.camera()

image_rescaled = rescale(image, 0.5)
image_resized = resize(image, (400, 400), mode='reflect')
image_downscaled = downscale_local_mean(image, (2, 3))

fig, axes = plt.subplots(nrows=2, ncols=2,
                         sharex=True, sharey=True)

ax = axes.ravel()

ax[0].imshow(image, cmap='gray')
ax[0].set_title("Original image")

ax[1].imshow(image_rescaled, cmap='gray')
ax[1].set_title("Rescaled image")

ax[2].imshow(image_resized, cmap='gray')
ax[2].set_title("Resized image")
コード例 #53
0
def normalize(x):
    # utility function to normalize a tensor by its L2 norm
    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)

if os.path.isdir('crop_viz'):
    shutil.rmtree('crop_viz')
os.mkdir('crop_viz')

test_crops = []
for fn in glob.glob('data/splits/dev/*.tif'):
    img = None
    img = np.array(io.imread(fn), dtype='float32')
    scaled = None
    scaled = img / np.float32(255.0)
    scaled = downscale_local_mean(scaled, factors=(2,2))
    test_crops.extend(utils.augment_test_image(scaled, img_width, img_height, NB_TEST_PATCHES))

print(len(test_crops))

if LAYER_NAME == 'prediction':
    label_encoder = pickle.load( open('models/' + MODEL_NAME + '/label_encoder.p', 'rb'))
    print('-> Working on classes:', label_encoder.classes_)
    NB_FILTERS = len(label_encoder.classes_)

for filter_idx in range(0, NB_FILTERS):
    print('Processing filter', filter_idx)
        
    layer_output = layer_dict[LAYER_NAME].output
    if LAYER_NAME == 'prediction':
        loss = K.mean(layer_output[:, filter_idx])
コード例 #54
0
example_dir = os.path.splitext(os.path.basename(FILEPATH))[0]

try:
    shutil.rmtree('viz/' + example_dir)
except:
    pass
os.mkdir('viz/' + example_dir)

img = io.imread(FILEPATH)

img = np.array(io.imread(FILEPATH), dtype='float32')
print(img.shape)
scipy.misc.imsave('viz/' + example_dir + '/orig.tiff', img)

scaled = img / np.float32(255.0)
scaled = downscale_local_mean(scaled, factors=(2, 2))

scipy.misc.imsave('viz/' + example_dir + '/downscaled' + '.tiff', scaled * 255)

train_crops, _ = utils.augment_train_images([scaled], [0], NB_ROWS, NB_COLS,
                                            NB_PATCHES)
for idx, x in enumerate(train_crops):
    x = x.reshape((x.shape[1], x.shape[2]))
    x *= 255
    scipy.misc.imsave(
        'viz/' + example_dir + '/train_crop_' + str(idx) + '.tiff', x)

test_crops = utils.augment_test_image(scaled, NB_ROWS, NB_COLS, NB_PATCHES)
for idx, x in enumerate(test_crops):
    x = x.reshape((x.shape[1], x.shape[2]))
    x *= 255
コード例 #55
0
ファイル: UAV2SEN_CrispCNN.py プロジェクト: PCdurham/UAV2Sen
print(report)


'''Show the classified validation images'''
if ShowValidation:
    for s in range(len(ValidationSites.index)):
        UAVRaster = io.imread(DataFolder+'Cropped_'+ValidationSites.Abbrev[s]+'_'+str(ValidationSites.Month[s])+'_'+str(ValidationSites.Year[s])+'_UAVCLS.tif')
        UAVRaster[UAVRaster>3] =0
        UAVRaster[UAVRaster<1] =0
        
        UAVRasterRGB = np.zeros((UAVRaster.shape[0], UAVRaster.shape[1], 4))
        UAVRasterRGB[:,:,0]=255*(UAVRaster==3)
        UAVRasterRGB[:,:,1]=255*(UAVRaster==2)
        UAVRasterRGB[:,:,2]=255*(UAVRaster==1)
        UAVRasterRGB[:,:,3] = 255*np.float32(UAVRaster != 0.0)
        UAVRasterRGB= downscale_local_mean(UAVRasterRGB, (10,10,1))
        ValidRasterName = DataFolder+'Cropped_'+ValidationSites.Abbrev[s]+'_'+str(ValidationSites.Month[s])+'_'+str(ValidationSites.Year[s])+'_S2.tif'
        ValidRaster = io.imread(ValidRasterName)
        ValidRasterIR = np.uint8(np.zeros((ValidRaster.shape[0], ValidRaster.shape[1],4)))
        stretch=2
        ValidRasterIR[:,:,0] = np.int16(stretch*255*ValidRaster[:,:,10])
        ValidRasterIR[:,:,1] = np.int16(stretch*255*ValidRaster[:,:,5])
        ValidRasterIR[:,:,2] = np.int16(stretch*255*ValidRaster[:,:,4])
        ValidRasterIR[:,:,3]= 255*np.int16((ValidRasterIR[:,:,0] != 0.0)&(ValidRasterIR[:,:,1] != 0.0))
        ValidTensor = slide_raster_to_tiles(ValidRaster, size)
        Valid=np.zeros(12)
        for n in range(1,13):
            if ('B'+str(n)) in FeatureSet:
                Valid[n-1]=1
        
        ValidTensor = np.compress(Valid, ValidTensor, axis=3)
コード例 #56
0
def plot_both(
    id_to_match,
    data_tree,
    data_pop,
    true_tree,
    true_pop,
    img_crop,
    widths=[14, 28, 56, 112, 224],
    save_dir=None,
):
    """Plot image, scene-level label, and super-res predictions at multiple scales.

    Parameters
    ----------
    id_to_match : str ("int,int")
        The grid ID of the image to plot
    data_[tree,pop] : :class:`numpy.ndarray`
        The pixel-level superresolution predictions as arrays for treecover and
        population.
    true_[tree,pop] : float
        The scene-level labels for treecover and population
    img_crop : len-2 tuple
        The amount to crop the image along left/top and right/bottom dimensions. This is
        needed right, top, bottom. This is needed both because the feature extraction
        causes the predictions to be slightly smaller than the image (e.g. 256x256
        image --> 254x254 predictions with a 3x3 filter), and because we may have
        cropped the predictions to be divisible by each scale of superresolution factor
        that we are displaying.
    widths : list of int
        The widths (in pixels) of the superresolution predictions to make. The last
        element should be equal to the length of the dimensions of ``data_[tree,pop]``
    save_dir : str
        Path to directory where these images are saved. If None, do not save
    """
    # plotting contexts
    context = sns.plotting_context("paper", font_scale=2)
    lines = True
    style = {
        "axes.grid": False,
        "axes.edgecolor": "0.0",
        "axes.labelcolor": "0.0",
        "axes.spines.right": lines,
        "axes.spines.top": lines,
        "axes.spines.left": lines,
        "axes.spines.bottom": lines,
    }

    sns.set_context(context)
    sns.set_style(style)

    # set plotting context
    plot_bounds_tree = TREECOVER_CMAP_BOUNDS
    plot_bounds_pop = [0, 1000]
    plot_bounds = [plot_bounds_tree, plot_bounds_pop]
    names = ["treecover", "population"]
    names_disp = ["% Forest", "Pop. Dens."]

    # grab plotting constants from config file
    c_plotting = getattr(config, "plotting")
    cmap_fxn = c_plotting["cmap_fxn"]
    cmaps = [cmap_fxn(getattr(config, task)["color"]) for task in names]

    # get the image (returning blank if imagery is not saved)
    image_dir = Path(config.data_dir) / "raw" / "imagery" / "CONTUS_UAR"
    try:
        image_this = io.load_img_from_ids_local(id_to_match, image_dir, c=config)
    except FileNotFoundError:
        image_this = None

    for t, data_this in enumerate([(data_tree, true_tree), (data_pop, true_pop)]):
        pred_map = data_this[0]
        label_this = data_this[1]
        task_this = names[t]

        # get clipping bounds
        c_app = getattr(config, task_this)
        if c_app["logged"]:
            bounds = [np.exp(i) for i in c_app["us_bounds_log_pred"]]
        else:
            bounds = c_app["us_bounds_pred"]

        # collect maps by downscale level
        superres_preds_by_scale = []
        for w in widths:

            # downscale
            assert pred_map.shape[0] % w == 0
            this_preds = downscale_local_mean(pred_map, (w, w))

            # clip if both bounds aren't None for this outcome
            if not (np.asarray(bounds) == None).all():
                this_preds = np.clip(this_preds, *bounds)

            superres_preds_by_scale.append(this_preds)

        # plot for this variable
        cmap_this = cmaps[t]
        bounds_this = plot_bounds[t]
        plot_img_and_heatmap_and_preds_multiscale(
            image_this,
            superres_preds_by_scale,
            widths,
            label_this,
            cmap=cmap_this,
            vmin=bounds_this[0],
            vmax=bounds_this[1],
            name=names_disp[t],
        )
        # save with descriptive name
        if save_dir is not None:
            plt.savefig(
                "{2}/{1}_multires_{0}.pdf".format(id_to_match, names[t], save_dir),
                bbox_inches="tight",
            )