本文整理汇总了Python中skimage.measure.block_reduce方法的典型用法代码示例。如果您正苦于以下问题:Python measure.block_reduce方法的具体用法?Python measure.block_reduce怎么用?Python measure.block_reduce使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类skimage.measure
的用法示例。
在下文中一共展示了measure.block_reduce方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: downsample
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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
示例2: __call__
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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
示例3: _generate_iou_map
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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 ----------------------------------
示例4: aggregate
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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.
示例5: block_reduce
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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)
示例6: down_sample
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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)
示例7: get_patch
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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)
示例8: get_patch
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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)
示例9: is_purple
# 需要导入模块: from skimage import measure [as 别名]
# 或者: from skimage.measure import block_reduce [as 别名]
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 #
###########################################