Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License
import numpy as np
import mahotas as mh
image = mh.imread('../SimpleImageDataset/building05.jpg')
image = mh.colors.rgb2gray(image)
# Compute Gaussian filtered versions with increasing kernel widths
im8 = mh.gaussian_filter(image, 8)
im16 = mh.gaussian_filter(image, 16)
im32 = mh.gaussian_filter(image, 32)
# We now build a composite image with three panels:
#
# [ IM8 | | IM16 | | IM32 ]
h, w = im8.shape
canvas = np.ones((h, 3 * w + 256), np.uint8)
canvas *= 255
canvas[:, :w] = im8
canvas[:, w + 128:2 * w + 128] = im16
canvas[:, -w:] = im32
mh.imsave('../1400OS_10_05+.jpg', canvas[:, ::2])
# Threshold the image
# We need to first stretch it to convert to an integer image
# This code is supporting material for the book
# Building Machine Learning Systems with Python
# by Willi Richert and Luis Pedro Coelho
# published by PACKT Publishing
#
# It is made available under the MIT License
import numpy as np
import mahotas as mh
image = mh.imread('../SimpleImageDataset/building05.jpg')
image = mh.colors.rgb2gray(image)
# Compute Gaussian filtered versions with increasing kernel widths
im8 = mh.gaussian_filter(image, 8)
im16 = mh.gaussian_filter(image, 16)
im32 = mh.gaussian_filter(image, 32)
# We now build a composite image with three panels:
#
# [ IM8 | | IM16 | | IM32 ]
h, w = im8.shape
canvas = np.ones((h, 3 * w + 256), np.uint8)
canvas *= 255
canvas[:, :w] = im8
canvas[:, w + 128:2 * w + 128] = im16
canvas[:, -w:] = im32
mh.imsave('../1400OS_10_05+.jpg', canvas[:, ::2])
# Threshold the image
# We need to first stretch it to convert to an integer image
im32 = mh.stretch(im32)
[row_seg,row_img] = self.edge_iter(seg_dict,img_dict,row_seg,row_img)
# temporarily flatten
for seg in self.ids: row_seg[np.where(row_seg == seg)] = self.label_id
#
# but take offset of tile into account
#
offset_x = self.x_tiles[0]*512
offset_y = self.y_tiles[0]*512
bb_relative = bb - np.array([offset_x]*2 + [offset_y]*2)
sub_tile = row_img[bb_relative[2]:bb_relative[3],bb_relative[0]:bb_relative[1]]
seg_sub_tile = row_seg[bb_relative[2]:bb_relative[3],bb_relative[0]:bb_relative[1]]
sub_tile = mh.gaussian_filter(sub_tile, 1).astype(np.uint8) # gaussian filter
sub_tile = (255 * exposure.equalize_hist(sub_tile)).astype(np.uint8) # enhance contrast
brush_size = values['brush_size']
i_js = values['i_js']
# make sparse points in i_js a dense line (with linear interpolation)
dense_brush = []
for i in range(len(i_js)-1):
# two sparse points
p0 = i_js[i]
p1 = i_js[i+1]
# x and y coordinates of sparse points
xp = [p0[1], p1[1]] if p0[1] < p1[1] else [p1[1], p0[1]]
t_score = tf.reduce_mean(t_obj) # defining the optimization objective
t_grad = tf.gradients(t_score, inputs)[0] # automatic differentiation
img = img0.copy()
for i in range(iter_n):
g, score = sess.run([t_grad, t_score], {inputs:img, is_training:False})
g[:,:,0] /= g[:,:,0].std()+1e-8
norm = np.abs(g*img)
img[:,:,0] += g[:,:,0]*step*(1.-i*decay)
img[:,:,0][norm[:,:,0]
import mahotas as mh
from os import path
import numpy as np
from matplotlib import pyplot as plt
nuclear = mh.demos.load('nuclear')
nuclear = nuclear[:,:,0]
nuclear = mh.gaussian_filter(nuclear, 1.)
threshed = (nuclear > nuclear.mean())
distances = mh.stretch(mh.distance(threshed))
Bc = np.ones((9,9))
maxima = mh.morph.regmax(distances, Bc=Bc)
spots,n_spots = mh.label(maxima, Bc=Bc)
surface = (distances.max() - distances)
areas = mh.cwatershed(surface, spots)
areas *= threshed
import random
from matplotlib import colors
from matplotlib import cm
cols = [cm.jet(c) for c in range(0, 256, 4)]