def highpass_filter(img, sigma=3):
lowpass = gaussian_filter(img, 3)
return img - lowpass
python类gaussian_filter()的实例源码
def speckle(img):
severity = np.random.uniform(0, 0.6)
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
img_speck = (img + blur)
img_speck[img_speck > 1] = 1
img_speck[img_speck <= 0] = 0
return img_speck
# paints the string in a random location the bounding box
# also uses a random font, a slight random rotation,
# and a random amount of speckle noise
def canny_demo():
# Generate noisy image of a square
im = np.zeros((128, 128))
im[32:-32, 32:-32] = 1
im = ndi.rotate(im, 15, mode='constant')
im = ndi.gaussian_filter(im, 4)
im += 0.2 * np.random.random(im.shape)
# Compute the Canny filter for two values of sigma
edges1 = feature.canny(im)
edges2 = feature.canny(im, sigma=3)
# display results
fig, (ax1, ax2, ax3) = plt.subplots(nrows=1, ncols=3, figsize=(8, 3),
sharex=True, sharey=True)
ax1.imshow(im, cmap=plt.cm.gray)
ax1.axis('off')
ax1.set_title('noisy image', fontsize=20)
ax2.imshow(edges1, cmap=plt.cm.gray)
ax2.axis('off')
ax2.set_title('Canny filter, $\sigma=1$', fontsize=20)
ax3.imshow(edges2, cmap=plt.cm.gray)
ax3.axis('off')
ax3.set_title('Canny filter, $\sigma=3$', fontsize=20)
fig.tight_layout()
plt.show()
def smooth_image(data, sigma):
from scipy import ndimage
return ndimage.gaussian_filter(data, sigma=sigma)
def speckle(img):
severity = np.random.uniform(0, 0.6)
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
img_speck = (img + blur)
img_speck[img_speck > 1] = 1
img_speck[img_speck <= 0] = 0
return img_speck
# paints the string in a random location the bounding box
# also uses a random font, a slight random rotation,
# and a random amount of speckle noise
def _smooth(image, sigma, mode, cval):
"""Return image with each channel smoothed by the Gaussian filter."""
smoothed = np.empty(image.shape, dtype=np.double)
# apply Gaussian filter to all dimensions independently
if image.ndim == 3:
for dim in range(image.shape[2]):
ndi.gaussian_filter(image[..., dim], sigma,
output=smoothed[..., dim],
mode=mode, cval=cval)
else:
ndi.gaussian_filter(image, sigma, output=smoothed,
mode=mode, cval=cval)
return smoothed
def _smooth_3d(volume, sigma, mode, cval):
"""Return volume with each channel smoothed by the Gaussian filter."""
smoothed = np.empty(volume.shape, dtype=np.double)
# apply Gaussian filter to all dimensions independently
# volume.ndim == 4 means the volume is multimodal
if volume.ndim == 4:
# compute 3d convolution for each modality, dim is a modality
for dim in range(volume.shape[3]):
ndi.gaussian_filter(volume[..., dim], sigma,
output=smoothed[..., dim],
mode=mode, cval=cval)
else:
ndi.gaussian_filter(volume, sigma, output=smoothed, mode=mode, cval=cval)
return smoothed
def speckle(img):
severity = np.random.uniform(0, 0.6)
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
img_speck = (img + blur)
img_speck[img_speck > 1] = 1
img_speck[img_speck <= 0] = 0
return img_speck
def _load_data(self):
with h5py.File(self.path, "r") as fp:
self.image = gaussian_filter(fp["image"].value, self.sigma)
self.gal_map = fp["segmaps/galaxy"].value
self.star_map = fp["segmaps/star"].value
def finish_image(img):
img = image.gaussian_filter(img, 3)
img = img + np.random.normal(0, 0.01, img.shape)
return img
def __blur(self, img):
"""
Gaussian blur
"""
print("Gaussian Filtering")
return ndimage.gaussian_filter(img, sigma=50)
def random_noise(img): #TODO dataug
severity = np.random.uniform(0, 0.6)
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
img_speck = (img + blur)
img_speck[img_speck > 1] = 1
img_speck[img_speck <= 0] = 0
return img_speck
def random_noise(img): #TODO dataug
"""
Puts random noise on image
:param img: image without noise
:return: image with noise
"""
severity = np.random.uniform(0, 0.6)
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
img_speck = (img + blur)
img_speck[img_speck > 1] = 1
img_speck[img_speck <= 0] = 0
return img_speck
def speckle(img):
severity = np.random.uniform(0, 0.6)
blur = ndimage.gaussian_filter(np.random.randn(*img.shape) * severity, 1)
img_speck = (img + blur)
img_speck[img_speck > 1] = 1
img_speck[img_speck <= 0] = 0
return img_speck
# paints the string in a random location the bounding box
# also uses a random font, a slight random rotation,
# and a random amount of speckle noise
def execGaussianFiltering(rawImgFloat):
filteredImg = gaussian_filter(rawImgFloat, 1)
seed = np.copy(filteredImg)
seed[1:-1, 1:-1] = filteredImg.min()
return filteredImg
generate_simple_template_phantoms.py 文件源码
项目:LabelsManager
作者: SebastianoF
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def apply_laplacian_filter(array, alpha=30):
"""
Laplacian is approximated with difference of Gaussian
:param array:
:param alpha:
:return:
"""
blurred_f = ndimage.gaussian_filter(array, 3)
filter_blurred_f = ndimage.gaussian_filter(blurred_f, 1)
return blurred_f + alpha * (blurred_f - filter_blurred_f)
def sharpen(blurred_im, alpha):
filter_blurred_im = ndi.gaussian_filter(blurred_im, 1)
sharp = blurred_im + alpha * (blurred_im - filter_blurred_im)
return sharp
def check_reg(fixed_img, moved_img, scale=15, norm=True, sigma=0.8, **kwargs):
dim = list(moved_img.shape)
resol = list(moved_img.header['pixdim'][1:4])
# Convert 4D image to 3D or raise error
data = convert_to_3d(moved_img)
# Check normalization
if norm:
data = apply_p2_98(data)
# Set slice axis for mosaic grid
slice_axis, cmap = check_sliceaxis_cmap(data, kwargs)
cmap = 'YlOrRd'
# Set grid shape
data, slice_grid, size = set_mosaic_fig(data, dim, resol, slice_axis, scale)
fig, axes = BrainPlot.mosaic(fixed_img, scale=scale, norm=norm, cmap='bone', **kwargs)
# Applying inversion
invert = check_invert(kwargs)
data = apply_invert(data, *invert)
# Plot image
for i in range(slice_grid[1] * slice_grid[2]):
ax = axes.flat[i]
edge = data[:, :, i]
edge = feature.canny(edge, sigma=sigma) # edge detection for second image
# edge = ndimage.gaussian_filter(edge, 3)
mask = np.ones(edge.shape)
sx = ndimage.sobel(edge, axis=0, mode='constant')
sy = ndimage.sobel(edge, axis=1, mode='constant')
sob = np.hypot(sx, sy)
mask[sob == False] = np.nan
m_norm = colors.Normalize(vmin=0, vmax=1.5)
if i < slice_grid[0] and False in np.isnan(mask.flatten()):
ax.imshow(mask.T, origin='lower', interpolation='nearest', cmap=cmap, norm=m_norm, alpha=0.8)
else:
ax.imshow(np.zeros((dim[0], dim[1])).T, origin='lower', interpolation='nearest', cmap='bone')
ax.set_axis_off()
fig.set_facecolor('black')
if notebook_env:
display(fig)
return fig, axes
def sample_transformation(self, imsz):
choices = len(self.alpha_dist)
c = int(n.random.randint(0, choices))
sigma = max(self.min_sigma[c], n.abs(self.sigma[c][1]*n.random.randn() + self.sigma[c][0]))
alpha = n.random.uniform(self.alpha_dist[c][0], self.alpha_dist[c][1])
dispmapx = n.random.uniform(-1*self.displacement_range, self.displacement_range, size=imsz)
dispmapy = n.random.uniform(-1*self.displacement_range, self.displacement_range, size=imsz)
dispmapx = alpha * ndimage.gaussian_filter(dispmapx, sigma)
dispmaxy = alpha * ndimage.gaussian_filter(dispmapy, sigma)
return dispmapx, dispmaxy
def surface_distortions(self, arr):
ds = self.surfdiststate.get_sample()
blur = ds['blur']
origarr = arr.copy()
arr = n.minimum(n.maximum(0, arr + n.random.normal(0, ds['noise'], arr.shape)), 255)
# make some changes to the alpha
arr[...,1] = ndimage.gaussian_filter(arr[...,1], ds['blur'])
ds = self.surfdiststate.get_sample()
arr[...,0] = ndimage.gaussian_filter(arr[...,0], ds['blur'])
if ds['sharpen']:
newarr_ = ndimage.gaussian_filter(origarr[...,0], blur/2)
arr[...,0] = arr[...,0] + ds['sharpen_amount']*(arr[...,0] - newarr_)
return arr