Python skimage.measure.block_reduce() Examples

The following are 11 code examples of skimage.measure.block_reduce(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module skimage.measure , or try the search function .
Example #1
Source File: toolbox.py    From Theano-Lights with MIT License 6 votes vote down vote up
def downsample(data):
    data['_tr_X'] = np.zeros((len(data['tr_X']), 14*14), dtype='float32')
    data['_va_X'] = np.zeros((len(data['va_X']), 14*14), dtype='float32')
    data['_te_X'] = np.zeros((len(data['te_X']), 14*14), dtype='float32')

    for i in xrange(0, len(data['tr_X'])):
        data['_tr_X'][i] = block_reduce(data['tr_X'][i].reshape(data['shape_x']), block_size=(2,2), func=np.mean).flatten() 

    for i in xrange(0, len(data['va_X'])):
        data['_va_X'][i] = block_reduce(data['va_X'][i].reshape(data['shape_x']), block_size=(2,2), func=np.mean).flatten() 

    for i in xrange(0, len(data['te_X'])):
        data['_te_X'][i] = block_reduce(data['te_X'][i].reshape(data['shape_x']), block_size=(2,2), func=np.mean).flatten() 

    data['tr_X'] = data['_tr_X']
    data['va_X'] = data['_va_X']
    data['te_X'] = data['_te_X']

    data['shape_x'] = (14,14)
    data['n_x'] = 14*14
    return data 
Example #2
Source File: video_transforms.py    From dmc-net with MIT License 5 votes vote down vote up
def __call__(self, clips):
        if isinstance(clips, np.ndarray):
            H, W, _ = clips.shape
            # handle numpy array
            clips = clips.reshape((H,W,-1,self.dim)).transpose((3, 2, 0, 1))
            if self.modality == 'flow+mp4':
                if self._flow_ds_factor is not 0 or 1:
                    clips = np.transpose(clips, (1,0,2,3))
                    # downsample to make OF blocky
                    factor = self._flow_ds_factor
                    w_max = H
                    h_max = W
                    input_flow = block_reduce(clips[:,0:2, :, :], block_size=(1, 1, factor, factor), func=np.mean)
                    # resize to original size by repeating or interpolation
                    if self._upsample_interp is False:
                        input_flow = input_flow.repeat(factor, axis=2).repeat(factor, axis=3)
                    else:
                        # interpolate along certain dimension? only interp1d can do so
                        w_max_ds = input_flow.shape[2]
                        h_max_ds = input_flow.shape[3]
                        f_out = interpolate.interp1d(np.linspace(0, 1, w_max_ds), input_flow, kind='linear', axis=2)
                        input_flow = f_out(np.linspace(0, 1, w_max_ds * factor))
                        f_out = interpolate.interp1d(np.linspace(0, 1, h_max_ds), input_flow, kind='linear', axis=3)
                        input_flow = f_out(np.linspace(0, 1, h_max_ds * factor))
                    clips[:,0:2, :, :] = input_flow[:, :, :w_max, :h_max]
                clips = np.transpose(clips, (1,0,2,3))
                
            clips = torch.from_numpy(clips)
            #print(clips.shape)
            # backward compatibility
            return clips.float() / 255.0 
Example #3
Source File: datagen.py    From Convolutional-Pose-Machine-tf with GNU Lesser General Public License v3.0 5 votes vote down vote up
def _generate_iou_map(self, width, height, bbox_list, anchors, upscale=2):
        """ generate anchor map for given anchors
        """
        #   upscale the size
        upwidth = upscale * width
        upheight = upscale * height

        iou_map = np.zeros((upheight, upwidth, len(anchors)), np.float)
        offset_map = np.zeros((upheight, upwidth, len(anchors), 4), np.float)

        bbox_list = np.array(bbox_list) * upscale
        #   convert bounding box
        bbox_list = self.cwh2tlbr(bbox_list, tolist=False)
        #   upsacle the anchor
        anchors = np.array(anchors) * upscale
        #   size * size * (x,y)
        #   construct numpy grid array
        pos = np.transpose(np.array(np.meshgrid(np.arange(upheight), np.arange(upwidth))), axes=[1,2,0])
        for bidx in range(bbox_list.shape[0]):
            for aidx in range(anchors.shape[0]):
                #   construct anchor bboxes
                anchor_bboxes = np.concatenate([pos, np.full((upheight, upwidth, 1),anchors[aidx,0]), np.full((upheight, upwidth, 1),anchors[aidx,1])], axis=-1)
                #   convert to top-left bottom-right format
                anchor_bboxes = self.cwh2tlbr(anchor_bboxes, tolist=False)
                #   reshape to dim-2 array
                anchor_bboxes = np.reshape(anchor_bboxes, (upheight * upwidth, 4))
                #   calculate boundingbox jaccard distance (IOU)
                iou = self.bb_intersection_over_union(np.expand_dims(bbox_list[bidx],axis=0), anchor_bboxes)
                #   reshape back and fill the channel
                iou_map[:, :, aidx] = np.maximum(np.reshape(iou, (upheight, upwidth)), iou_map[:, :, aidx])
                pass

        if upscale != 1:
            iou_map = msr.block_reduce(iou_map, (upscale,upscale,1), func=np.max)
            offset_map = msr.block_reduce(offset_map, (upscale,upscale,1,1), func=np.min)
        return iou_map, np.reshape(offset_map, (height, width, len(anchors)*4))

    # ----------------------- Batch Generator ---------------------------------- 
Example #4
Source File: georasters.py    From georasters with GNU General Public License v3.0 5 votes vote down vote up
def aggregate(raster, ndv, block_size):
    '''
    Aggregate raster to smaller resolution, by adding cells.
    Usage:
            aggregate(raster, ndv, block_size)

    where:
            raster is a Numpy array created by importing the raster (e.g. geotiff)

            ndv is the NoData Value for the raster (can be read using the get_geo_info function)

            block_size is a duple of factors by which the raster will be shrinked

    Example:
            raster = HMISea.tif

            ndv, xsize, ysize, geot, projection, datatype = get_geo_info(raster)

            costs = load_tiff(raster)

            costs2=aggregate(costs, ndv, (10,10))
    '''
    raster2 = block_reduce(raster, block_size, func=np.ma.sum)
    return raster2

# Function to write a new file. 
Example #5
Source File: georasters.py    From georasters with GNU General Public License v3.0 5 votes vote down vote up
def aggregate(self, block_size):
        '''
        geo.aggregate(block_size)

        Returns copy of raster aggregated to smaller resolution, by adding cells.
        '''
        raster2 = block_reduce(self.raster, block_size, func=np.ma.sum)
        geot = self.geot
        geot = (geot[0], block_size[0] * geot[1], geot[2], geot[3], geot[4],
                block_size[1] * geot[-1])
        return GeoRaster(raster2, geot, nodata_value=self.nodata_value,\
                        projection=self.projection, datatype=self.datatype) 
Example #6
Source File: georasters.py    From georasters with GNU General Public License v3.0 5 votes vote down vote up
def block_reduce(self, block_size, how=np.ma.mean):
        '''
        geo.block_reduce(block_size, how=func)

        Returns copy of raster aggregated to smaller resolution, by adding cells.
        Default: func=np.ma.mean
        '''
        raster2 = block_reduce(self.raster, block_size, func=how)
        geot = self.geot
        geot = (geot[0], block_size[0] * geot[1], geot[2], geot[3], geot[4],
                block_size[1] * geot[-1])
        return GeoRaster(raster2, geot, nodata_value=self.nodata_value,\
                        projection=self.projection, datatype=self.datatype) 
Example #7
Source File: voxels.py    From occupancy_flow with MIT License 5 votes vote down vote up
def down_sample(self, factor=2):
        if not (self.resolution % factor) == 0:
            raise ValueError('Resolution must be divisible by factor.')
        new_data = block_reduce(self.data, (factor,) * 3, np.max)
        return VoxelGrid(new_data, self.loc, self.scale) 
Example #8
Source File: h5py_patch.py    From CASED-Tensorflow with MIT License 5 votes vote down vote up
def get_patch(image, coords, offset, nodule_list, patch_flag=True):
    xyz = image[int(coords[0] - offset): int(coords[0] + offset), int(coords[1] - offset): int(coords[1] + offset),
          int(coords[2] - offset): int(coords[2] + offset)]

    if patch_flag:
        output = np.expand_dims(xyz, axis=-1)
    else:
        # resize xyz
        """
        xyz = scipy.ndimage.zoom(input=xyz, zoom=1/8, order=1) # nearest
        xyz = np.where(xyz > 0, 1.0, 0.0)
        """
        xyz = block_reduce(xyz, (9, 9, 9), np.max)
        output = np.expand_dims(xyz, axis=-1)

        output = indices_to_one_hot(output.astype(np.int32), 2)
        output = np.reshape(output, (label_size, label_size, label_size, 2))
        output = output.astype(np.float32)

        # print('------------------')
        # print(output)

        # print(output)
        # print(np.shape(output))

    nodule_list.append(output) 
Example #9
Source File: preprocess_utils.py    From CASED-Tensorflow with MIT License 5 votes vote down vote up
def get_patch(image, coords, offset, nodule_list, patch_flag=True):
    xyz = image[int(coords[0] - offset): int(coords[0] + offset), int(coords[1] - offset): int(coords[1] + offset),
          int(coords[2] - offset): int(coords[2] + offset)]

    if patch_flag:
        output = np.expand_dims(xyz, axis=-1)
        print(coords, np.shape(output))
    else:
        # resize xyz
        """
        xyz = scipy.ndimage.zoom(input=xyz, zoom=1/8, order=1) # nearest
        xyz = np.where(xyz > 0, 1.0, 0.0)
        """
        xyz = block_reduce(xyz, (9, 9, 9), np.max)
        output = np.expand_dims(xyz, axis=-1)

        output = indices_to_one_hot(output.astype(np.int32), 2)
        output = np.reshape(output, (label_size, label_size, label_size, 2))
        output = output.astype(np.float32)

        # print('------------------')
        # print(output)

        # print(output)
        # print(np.shape(output))

    nodule_list.append(output) 
Example #10
Source File: voxels.py    From occupancy_networks with MIT License 5 votes vote down vote up
def down_sample(self, factor=2):
        if not (self.resolution % factor) == 0:
            raise ValueError('Resolution must be divisible by factor.')
        new_data = block_reduce(self.data, (factor,) * 3, np.max)
        return VoxelGrid(new_data, self.loc, self.scale) 
Example #11
Source File: utils_processing.py    From deepslide with GNU General Public License v3.0 5 votes vote down vote up
def is_purple(crop: np.ndarray, purple_threshold: int,
              purple_scale_size: int) -> bool:
    """
    Determines if a given portion of an image is purple.

    Args:
        crop: Portion of the image to check for being purple.
        purple_threshold: Number of purple points for region to be considered purple.
        purple_scale_size: Scalar to use for reducing image to check for purple.

    Returns:
        A boolean representing whether the image is purple or not.
    """
    block_size = (crop.shape[0] // purple_scale_size,
                  crop.shape[1] // purple_scale_size, 1)
    pooled = block_reduce(image=crop, block_size=block_size, func=np.average)

    # Calculate boolean arrays for determining if portion is purple.
    r, g, b = pooled[..., 0], pooled[..., 1], pooled[..., 2]
    cond1 = r > g - 10
    cond2 = b > g - 10
    cond3 = ((r + b) / 2) > g + 20

    # Find the indexes of pooled satisfying all 3 conditions.
    pooled = pooled[cond1 & cond2 & cond3]
    num_purple = pooled.shape[0]

    return num_purple > purple_threshold


###########################################
#         GENERATING TRAINING DATA        #
###########################################