def restore_labels(labels, roi, read_info):
if roi == -1:
# Pad first, then resize to original shape
labels = np.pad(labels, ((0, 0), (CROP, CROP), (CROP, CROP)), 'constant')
restored_labels = np.zeros(read_info['shape'], dtype=np.float32)
for z in range(N_CLASSES):
roi = resize((labels == z + 1).astype(np.float32), read_info['shape'], mode='constant')
roi[roi >= 0.5] = 1
roi[roi < 0.5] = 0
roi = clean_contour(roi, is_prob=False)
restored_labels[roi == 1] = z + 1
else:
labels = clean_contour(labels, is_prob=True)
# Resize to extracted shape, then pad to original shape
labels = resize(labels, read_info['extract_shape'], mode='constant')
restored_labels = np.zeros(read_info['shape'], dtype=np.float32)
extract = read_info['extract']
restored_labels[extract[0][0] : extract[0][1], extract[1][0] : extract[1][1], extract[2][0] : extract[2][1]] = labels
return restored_labels
python类resize()的实例源码
def test_image_transformation():
s = XSeries([generate_image(False) for _ in range(100)])
try:
image_transformer = ImageTransformer().fit()
assert False
except:
assert True
image_transformer = ImageTransformer(skimage_transform.hough_circle, radius=5).fit()
s_transformed = image_transformer.transform(s)
assert s_transformed.data_type == np.ndarray
image_transformer = ImageTransformer(skimage_transform.resize, output_shape=(10, 10)).fit()
s_transformed = image_transformer.transform(s)
assert s_transformed.data_type == np.ndarray
def load_box(inp, bboxes, img_input=False, norm=False):
n = bboxes.shape[0]
if img_input:
im = inp
else:
im = load_image(inp, norm)
res = np.zeros([n, 117, 117, 3])
for i in range(n):
img_crop = im_crop(im, bboxes[i])
img_resize = transform.resize(img_crop, [117, 117])
res[i] = img_resize
return res
###########################################################################
# load_patch #
###########################################################################
def transform_mnist_rts(in_data):
img, label = in_data
img = img[0] # Remove channel axis for skimage manipulation
# Rotate
img = transform.rotate(img, angle=np.random.uniform(-45, 45),
resize=True, mode='constant')
# Scale
img = transform.rescale(img, scale=np.random.uniform(0.7, 1.2),
mode='constant')
# Translate
h, w = img.shape
if h >= img_size[0] or w >= img_size[1]:
img = transform.resize(img, output_shape=img_size, mode='constant')
img = img.astype(np.float32)
else:
img_canvas = np.zeros(img_size, dtype=np.float32)
ymin = np.random.randint(0, img_size[0] - h)
xmin = np.random.randint(0, img_size[1] - w)
img_canvas[ymin:ymin+h, xmin:xmin+w] = img
img = img_canvas
img = img[np.newaxis, :] # Add the bach channel back
return img, label
def transfer(rgbImage, new_dims):
im = np.dot(rgbImage[..., :3],
[0.229, 0.587, 0.144])
im_min, im_max = im.min(), im.max()
if im_max > im_min:
# skimage is fast but only understands {1,3} channel images
# in [0, 1].
im_std = (im - im_min) / (im_max - im_min)
resized_std = resize(im_std, new_dims, order=1)
resized_im = resized_std * (im_max - im_min) + im_min
else:
# the image is a constant -- avoid divide by 0
ret = np.empty((new_dims[0], new_dims[1], im.shape[-1]),
dtype=np.float32)
ret.fill(im_min)
return ret
return resized_im.astype(np.float32)
def transfer(rgbImage, new_dims):
im = np.dot(rgbImage[..., :3],
[0.229, 0.587, 0.144])
im_min, im_max = im.min(), im.max()
if im_max > im_min:
# skimage is fast but only understands {1,3} channel images
# in [0, 1].
im_std = (im - im_min) / (im_max - im_min)
resized_std = resize(im_std, new_dims, order=1)
resized_im = resized_std * (im_max - im_min) + im_min
else:
# the image is a constant -- avoid divide by 0
ret = np.empty((new_dims[0], new_dims[1], im.shape[-1]),
dtype=np.float32)
ret.fill(im_min)
return ret
return resized_im.astype(np.float32)
def _extraction_iterator_corners(image, use_local_thresholding=False, apply_gaussian=False, n=5):
# If the image is too small, then double its scale until big enough.
img = image
while max(img.shape) < 500:
img = resize(img, np.array(img.shape) * 2)
if apply_gaussian:
img = gaussian_filter(image, (3.0, 3.0))
for corner_points in iter_blob_extremes(img, n=n):
try:
warped_image = geometry.warp_image_by_corner_points_projection(corner_points, img)
sudoku, bin_image = geometry.split_image_into_sudoku_pieces_adaptive_global(
warped_image, otsu_local=use_local_thresholding, apply_gaussian=apply_gaussian)
except SudokuExtractError:
# Try next blob.
pass
except Exception as e:
raise
else:
yield sudoku, bin_image
def phash64(img):
"""Compute a perceptual hash of an image.
:param img: a rgb image to be hashed
:type img: numpy.ndarray
:return: a perceptrual hash of img coded on 64 bits
:rtype: int
"""
resized = rgb2grey(resize(img, (8, 8)))
mean = resized.mean()
boolean_matrix = resized > mean
hash_lst = boolean_matrix.reshape((1, 64))[0]
hash_lst = list(map(int, hash_lst))
im_hash = 0
for v in hash_lst:
im_hash = (im_hash << 1) | v
return im_hash
def dhash(img):
"""Compute a perceptual has of an image.
Algo explained here :
https://blog.bearstech.com/2014/07/numpy-par-lexemple-une-implementation-de-dhash.html
:param img: an image
:type img: numpy.ndarray
:return: a perceptual hash of img coded on 64 bits
:rtype: int
"""
TWOS = np.array([2 ** n for n in range(7, -1, -1)])
BIGS = np.array([256 ** n for n in range(7, -1, -1)], dtype=np.uint64)
img = rgb2grey(resize(img, (9, 8)))
h = np.array([0] * 8, dtype=np.uint8)
for i in range(8):
h[i] = TWOS[img[i] > img[i + 1]].sum()
return (BIGS * h).sum()
def resize_mnist_data(images, new_size_a, new_size_b=None):
"""
Resizes a set of images
:param images:
:param new_size:
:return:
"""
from skimage.transform import resize
if new_size_b is None:
new_size_b = new_size_a
resized_data = np.zeros((images.shape[0], 1, new_size_a, new_size_b))
for i in range(len(images)):
resized_data[i, 0, :, :] = resize(images[i, 0, :, :], (new_size_a, new_size_b))
return np.float32(resized_data)
def pyramid_expand_3d(volume, upscale=2, sigma=None, order=1, mode='reflect', cval=0):
_check_factor(upscale)
#_check_float32(volume)
#volume=img_as_float(volume)
volume=volume.astype('float64') # /(12641.6) #
x = volume.shape[0]
y = volume.shape[1]
z = volume.shape[2]
out_x = math.ceil(upscale * x)
out_y = math.ceil(upscale * y)
out_z = math.ceil(upscale * z)
if sigma is None:
# automatically determine sigma which covers > 99% of distribution
sigma = 2 * upscale / 6.0
start_time = time.time()
resized = resize(volume, (out_x, out_y, out_z), order=order, mode=mode, cval=cval)
start_time = time.time()
out = _smooth_3d(resized, sigma, mode, cval)
# I want it to be float32
start_time = time.time()
out=out.astype('float32')
return out
def showImage( batch_id, dictionary, imSize, attr, outfile):
images = dictionary.get('data')
labels = dictionary.get('labels')
for i in xrange(10000):
singleImage = images[i]
recon = np.zeros( (imSize, imSize, 3), dtype = np.uint8 )
singleImage = singleImage.reshape( (imSize*3, imSize))
red = singleImage[0:imSize,:]
blue = singleImage[imSize:2*imSize,:]
green = singleImage[2*imSize:3*imSize,:]
recon[:,:,0] = red
recon[:,:,1] = blue
recon[:,:,2] = green
outpath = os.path.abspath(".") + "/" + attr + "/" + str(batch_id) + "_" + str(i) + ".jpg"
#recon = resize(recon, (256, 256))
io.imsave(outpath, recon)
outfile.write(outpath + " " + str(labels[i]) + "\n")
def sliding_window(num_boxes, image, windowSize, workers=3):
image_batch = None
#'''Guarantee that middle of picture is always covered'''
#data = get_middle_box(image, windowSize)
#img = image[data[1]:data[3],data[0]:data[2]]
#image_batch = img[np.newaxis]
for i in range((num_boxes / workers)):
y = random.randrange(0, image.shape[0] - windowSize[0])
x = random.randrange(0, image.shape[1] - windowSize[1])
img = image[y:y + windowSize[1], x:x + windowSize[0]]
img = resize(img, (299, 299))
if image_batch is None:
image_batch = img[np.newaxis]
else:
image_batch = np.append(image_batch, img[np.newaxis], axis=0)
return image_batch
def slide_window(img, F, window_size, stride):
window_list = []
w = img.shape[1]
h = img.shape[0]
w_re = int(float(w)*window_size/F)
h_re = int(float(h)*window_size/F)
if w_re<=window_size+stride or h_re<=window_size+stride:
return None
img = resize(img, (h_re, w_re, 3))
img = image_preprocess(img)
if len(img.shape)!=3:
return None
for i in range(int((w_re-window_size)/stride)):
for j in range(int((h_re-window_size)/stride)):
box = [j*stride, i*stride, j*stride+window_size, i*stride+window_size]
window_list.append(box)
return img, np.asarray(window_list)
def resize(image, w, h, **kwargs):
"""
Resize image.
Image can be up- or down-sized (using interpolation). For details see:
http://scikit-image.org/docs/dev/api/skimage.transform.html#skimage.transform.resize
>>> image = np.ones((10,5), dtype='uint8')
>>> resize(image, 4, 3)
array([[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]], dtype=uint8)
:param numpy array image: Numpy array with range [0,255] and dtype 'uint8'.
:param int w: Width in pixels.
:param int h: Height in pixels.
:param kwargs kwargs: Keyword arguments for the underlying scikit-image
resize function, e.g. order=1 for linear interpolation.
:return: Resized image
:rtype: numpy array with range [0,255] and dtype 'uint8'
"""
set_default_order(kwargs)
return skt.resize(image, (h, w), mode='constant',
preserve_range=True, **kwargs).astype('uint8')
def flatFieldFromFit(self):
'''
calculate flatField from 2d-polynomal fit filling
all high gradient areas within averaged fit-image
returns flatField, average background level, fitted image, valid indices mask
'''
fitimg, mask = self._prepare()
out = fitimg.copy()
lastm = 0
for _ in range(10):
out = polyfit2dGrid(out, mask, 2)
mask = highGrad(out)
m = mask.sum()
if m == lastm:
break
lastm = m
out = np.clip(out, 0.1, 1)
out = resize(out, self._orig_shape, mode='reflect')
return out, self.bglevel / self._n, fitimg, mask
def get_rgbd_file(self, dirname, offset):
associations = self.seq_dir_map[dirname]['associations']
if associations[offset, 1].startswith('depth'):
rgb_filename = os.path.join(dirname, associations[offset, 3])
depth_filename = os.path.join(dirname, associations[offset, 1])
else:
rgb_filename = os.path.join(dirname, associations[offset, 1])
depth_filename = os.path.join(dirname, associations[offset, 3])
rgb_img = ndimage.imread(rgb_filename)
depth_img = ndimage.imread(depth_filename)
width = height = 224
# Reshape
depth_img = np.reshape(depth_img, list(depth_img.shape) + [1])
depth_img = 255 * depth_img / np.max(depth_img)
rgbd_img = np.concatenate((rgb_img, depth_img), 2)
# Resize
rgbd_img = transform.resize(rgbd_img, [width, height], preserve_range=True)
return rgb_filename, depth_filename, rgbd_img.astype(np.float32)
def get_blend_map(img, att_map, blur=True, overlap=True):
# att_map -= att_map.min()
# if att_map.max() > 0:
# att_map /= att_map.max()
att_map = 1.0 - att_map
att_map = transform.resize(att_map, (img.shape[:2]), order = 3, mode='edge')
# print att_map.shape
if blur:
att_map = filters.gaussian(att_map, 0.02*max(img.shape[:2]))
att_map -= att_map.min()
att_map /= att_map.max()
cmap = plt.get_cmap('jet')
att_map_v = cmap(att_map)
att_map_v = np.delete(att_map_v, 3, 2)
if overlap:
att_map = 1*(1-att_map**0.7).reshape(att_map.shape + (1,))*img + (att_map**0.7).reshape(att_map.shape+(1,)) * att_map_v
return att_map
def update(self, obs):
obs = resize(obs, self.factors.shape, preserve_range=True)
obs = np.floor((obs*self.num_bins)).astype(np.int32)
context = [0, 0, 0, 0]
log_prob = 0.0
log_recoding_prob = 0.0
for i in range(self.factors.shape[0]):
for j in range(self.factors.shape[1]):
context[3] = obs[i, j-1] if j > 0 else 0
context[2] = obs[i-1, j] if i > 0 else 0
context[1] = obs[i-1, j-1] if i > 0 and j > 0 else 0
context[0] = obs[i-1, j+1] if i > 0 and j < self.factors.shape[1]-1 else 0
log_prob += self.factors[i, j].update(context, obs[i, j])
log_recoding_prob += self.factors[i, j].log_prob(context, obs[i, j])
return self.exploration_bonus(log_prob, log_recoding_prob)
def load_image(path, height, width, mode='RGB'):
"""
Load an image from disk
Returns an np.ndarray (channels x width x height)
Arguments:
path -- path to an image on disk
width -- resize dimension
height -- resize dimension
Keyword arguments:
mode -- the PIL mode that the image should be converted to
(RGB for color or L for grayscale)
"""
image = PIL.Image.open(path)
image = image.convert(mode)
image = np.array(image)
# squash
image = scipy.misc.imresize(image, (height, width), 'bilinear')
return image
# Forward pass of input through the network
def fits2jpg(fname):
hdu_list = fits.open(fname)
image = hdu_list[0].data
image = np.squeeze(image)
img = np.copy(image)
idx = np.isnan(img)
img[idx] = 0
img_clip = np.flipud(img)
sigma = 3.0
# Estimate stats
mean, median, std = sigma_clipped_stats(img_clip, sigma=sigma, iters=10)
# Clip off n sigma points
img_clip = clip(img_clip,std*sigma)
if img_clip.shape[0] !=150 or img_clip.shape[1] !=150:
img_clip = resize(img_clip, (150,150))
#img_clip = rgb2gray(img_clip)
outfile = fname[0:-5] +'.png'
imsave(outfile, img_clip)
return img_clip,outfile
# Do the fusion classification
def PostprocessImage(img):
"""
Postprocess target style image
1. add the images dataset mean to optimized image
2. swap axis (b,g,r) to (r,g,b) and save it
Parameters
--------
img: ndarray (1x3xMxN), optimized image
Returns
out : ndarray (3xMxN), Postprocessed image
"""
img = np.resize(img, (3, img.shape[2], img.shape[3]))
img[0, :] += 123.68
img[1, :] += 116.779
img[2, :] += 103.939
img = np.swapaxes(img, 0, 2)
img = np.swapaxes(img, 0, 1)
img = np.clip(img, 0, 255)
return img.astype('uint8')
def resize_img(img, opt):
"""
CNN predictions are made at the 36x36 pixel lvl and the test set needs to be at the 608x608
lvl. The function resizes.
Args:
numpy array 36x36 for test or 50x50 for train
Returns:
numpy array 608x608 for test or 400x400 for train
"""
#print(img.shape)
if opt == 'test':
img = resize(img, (conf.test_image_size, conf.test_image_size))
return img
elif opt == 'train':
size = conf.train_image_size
blocks = 8 #conf.gt_res # resolution of the gt is 8x8 pixels for one class
steps = 50 #conf.train_image_size // blocks # 50
dd = np.zeros((size, size))
for i in range(steps):
for j in range(steps):
dd[j*blocks:(j+1)*blocks,i*blocks:(i+1)*blocks] = img[j,i]
return dd
else:
raise ValueError('test or train plz')
def ll_of_count(counts, means, stds):
cm = np.copy(counts)
cm = (cm*255./cm.max()).astype(np.uint8)
cm = cm[np.where(cm.sum(axis=1))]
if cm.shape[0] == 0:
cm = np.zeros((10, 30), dtype = np.uint8)
im = Image.fromarray(cm).resize((30,10), Image.ANTIALIAS)
counts_resized_arr = np.array(im.getdata(), dtype=np.float32).reshape(10,30)/255.
max_ll = -10000000
for roll_by in xrange(30):
resized_counts = np.roll(counts_resized_arr, roll_by, axis=1).flatten()
ll = 0.
for i in xrange(resized_counts.shape[0]):
ll += np.log(scipy.stats.norm.pdf(resized_counts[i], loc=means[i], scale=stds[i]))
if ll > max_ll:
max_ll = ll
return max_ll
def ll_of_count(counts, means, stds):
cm = np.copy(counts)
cm = (cm*255./cm.max()).astype(np.uint8)
cm = cm[np.where(cm.sum(axis=1))]
if cm.shape[0] == 0:
cm = np.zeros((10, 30), dtype = np.uint8)
im = Image.fromarray(cm).resize((30,10), Image.ANTIALIAS)
counts_resized_arr = np.array(im.getdata(), dtype=np.float32).reshape(10,30)/255.
max_ll = -10000000
for roll_by in xrange(30):
resized_counts = np.roll(counts_resized_arr, roll_by, axis=1).flatten()
ll = 0.
for i in xrange(resized_counts.shape[0]):
ll += np.log(scipy.stats.norm.pdf(resized_counts[i], loc=means[i], scale=stds[i]))
if ll > max_ll:
max_ll = ll
return max_ll
def postprocess(imgs, size, grayscale=False):
print("Postprocessing images and resize (at %d)" % size)
keyname = ('gray_%d' if grayscale else 'color_%d') % size
for img in imgs:
# Continue if already calculated
if img.isSetByName(keyname):
continue
floatimg = img_as_float(img.image)
floatimg = resize(floatimg, (size, size))
if grayscale:
floatimg = rgb2gray(floatimg)
img.setByName(keyname, floatimg) # expect to return floats
# Augment images
def pre_proc(X):
'''????? ???.
Args:
X(np.array): ??? ???? ??? ???? ? 84X84? ????
??? ????? ??????(??? ?? ??? ??) 255? ??
Returns:
np.array: ??? ???
'''
# ?? ? frame? ???? max? ????? flickering? ??
# x = np.maximum(X, X1)
# ??? ????? ????? ?? ??? ?? ??
x = np.uint8(resize(rgb2gray(X), (HEIGHT, WIDTH), mode='reflect') * 255)
return x
def pre_proc(X):
'''????? ???.
Args:
X(np.array): ??? ???? ??? ???? ? 84X84? ????
??? ????? ??????(??? ?? ??? ??) 255? ??
Returns:
np.array: ??? ???
'''
# ?? ? frame? ???? max? ????? flickering? ??
# x = np.maximum(X, X1)
# ??? ????? ????? ?? ??? ?? ??
x = np.uint8(resize(rgb2gray(X), (HEIGHT, WIDTH), mode='reflect') * 255)
return x
def evaluate_sliding_window(img_filename, crops):
img = io.imread(img_filename).astype(np.float32)/255
if img.ndim == 2: # Handle B/W images
img = np.expand_dims(img, axis=-1)
img = np.repeat(img, 3, 2)
img_crops = np.zeros((batch_size, 227, 227, 3))
for i in xrange(len(crops)):
crop = crops[i]
img_crop = transform.resize(img[crop[1]:crop[1]+crop[3],crop[0]:crop[0]+crop[2]], (227, 227))-0.5
img_crop = np.expand_dims(img_crop, axis=0)
img_crops[i,:,:,:] = img_crop
# compute ranking scores
scores = sess.run([score_func], feed_dict={image_placeholder: img_crops})
# find the optimal crop
idx = np.argmax(scores[:len(crops)])
best_window = crops[idx]
# return the best crop
return (best_window[0], best_window[1], best_window[2], best_window[3])
def PreprocessContentImage(path, long_edge):
img = io.imread(path)
logging.info("load the content image, size = %s", img.shape[:2])
factor = float(long_edge) / max(img.shape[:2])
new_size = (int(img.shape[0] * factor), int(img.shape[1] * factor))
resized_img = transform.resize(img, new_size)
sample = np.asarray(resized_img) * 256
# swap axes to make image from (224, 224, 3) to (3, 224, 224)
sample = np.swapaxes(sample, 0, 2)
sample = np.swapaxes(sample, 1, 2)
# sub mean
sample[0, :] -= 123.68
sample[1, :] -= 116.779
sample[2, :] -= 103.939
logging.info("resize the content image to %s", new_size)
return np.resize(sample, (1, 3, sample.shape[1], sample.shape[2]))