def nparr_to_full_jpeg(nparr,
out_fname,
outsizex=770,
outsizey=770,
scale=True,
scale_func=clipped_linscale_img,
scale_func_params={'cap':255.0,
'lomult':2,
'himult':2.5}):
'''
This just writes a numpy array to a JPEG.
'''
if scale:
scaled_img = scale_func(nparr,**scale_func_params)
else:
scaled_img = nparr
resized_img = scipy.misc.imresize(scaled_img,
(outsizex,outsizey))
if out_fname is None:
out_fname = fits_image + '.jpeg'
scipy.misc.imsave(out_fname,resized_img)
python类misc()的实例源码
def load_np_image_uint8(image_file):
"""Loads an image as a numpy array.
Args:
image_file: str. Image file.
Returns:
A 3-D numpy array of shape [image_size, image_size, 3] and dtype uint8,
with values in [0, 255].
"""
with tempfile.NamedTemporaryFile() as f:
f.write(tf.gfile.GFile(image_file, 'rb').read())
f.flush()
image = scipy.misc.imread(f.name)
# Workaround for black-and-white images
if image.ndim == 2:
image = np.tile(image[:, :, None], (1, 1, 3))
return image
def save_np_image(image, output_file, save_format='jpeg'):
"""Saves an image to disk.
Args:
image: 3-D numpy array of shape [image_size, image_size, 3] and dtype
float32, with values in [0, 1].
output_file: str, output file.
save_format: format for saving image (eg. jpeg).
"""
image = np.uint8(image * 255.0)
buf = io.BytesIO()
scipy.misc.imsave(buf, np.squeeze(image, 0), format=save_format)
buf.seek(0)
f = tf.gfile.GFile(output_file, 'w')
f.write(buf.getvalue())
f.close()
def imsave(image, path):
label_colours = [
(0,0,0),
# 0=background
(128,0,0),(0,128,0),(128,128,0),(0,0,128),(128,0,128),
# 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
(0,128,128),(128,128,128),(64,0,0),(192,0,0),(64,128,0),
# 6=bus, 7=car, 8=cat, 9=chair, 10=cow
(192,128,0),(64,0,128),(192,0,128),(64,128,128),(192,128,128),
# 11=diningtable, 12=dog, 13=horse, 14=motorbike, 15=person
(0,64,0),(128,64,0),(0,192,0),(128,192,0),(0,64,128)]
# 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
images = np.ones(list(image.shape)+[3])
for j_, j in enumerate(image):
for k_, k in enumerate(j):
if k < 21:
images[j_, k_] = label_colours[int(k)]
scipy.misc.imsave(path, images)
def imsave(image, path):
label_colours = [
(0,0,0),
# 0=background
(128,0,0),(0,128,0),(128,128,0),(0,0,128),(128,0,128),
# 1=aeroplane, 2=bicycle, 3=bird, 4=boat, 5=bottle
(0,128,128),(128,128,128),(64,0,0),(192,0,0),(64,128,0),
# 6=bus, 7=car, 8=cat, 9=chair, 10=cow
(192,128,0),(64,0,128),(192,0,128),(64,128,128),(192,128,128),
# 11=diningtable, 12=dog, 13=horse, 14=motorbike, 15=person
(0,64,0),(128,64,0),(0,192,0),(128,192,0),(0,64,128)]
# 16=potted plant, 17=sheep, 18=sofa, 19=train, 20=tv/monitor
images = np.ones(list(image.shape)+[3])
for j_, j in enumerate(image):
for k_, k in enumerate(j):
if k < 21:
images[j_, k_] = label_colours[int(k)]
scipy.misc.imsave(path, images)
def find_a_dominant_color(image):
# K-mean clustering to find the k most dominant color, from:
# http://stackoverflow.com/questions/3241929/python-find-dominant-most-common-color-in-an-image
n_clusters = 5
# Get image into a workable form
im = image.copy()
im = im.resize((150, 150)) # optional, to reduce time
ar = scipy.misc.fromimage(im)
im_shape = ar.shape
ar = ar.reshape(scipy.product(im_shape[:2]), im_shape[2])
ar = np.float_(ar)
# Compute clusters
codes, dist = scipy.cluster.vq.kmeans(ar, n_clusters)
vecs, dist = scipy.cluster.vq.vq(ar, codes) # assign codes
counts, bins = scipy.histogram(vecs, len(codes)) # count occurrences
# Get the indexes of the most frequent, 2nd most frequent, 3rd, ...
sorted_idxs = np.argsort(counts)
# Get the color
peak = codes[sorted_idxs[1]] # get second most frequent color
return [int(i) for i in peak.tolist()] # list comprehension to quickly cast everything to int
def getUnlinkedStationary(self, popSize, theta):
one_loc_probs = one_locus_probs(popSize=popSize, theta=theta, n=self.n)
assertValidProbs(one_loc_probs)
n = self.n
leftOnes, rightOnes, bothOnes = self.numOnes(0), self.numOnes(1), self.hapCount((1,1))
joint = one_loc_probs[leftOnes] * one_loc_probs[rightOnes]
if self.exact:
joint[self.numC > 0] = 0
else:
joint = joint * scipy.misc.comb(rightOnes, bothOnes) * scipy.misc.comb(n-rightOnes, leftOnes-bothOnes) / scipy.misc.comb(n, leftOnes)
joint = joint * self.n_unfolded_versions
assertValidProbs(joint)
return joint
def test():
test_filename = sys.argv[2]
test_image = scipy.misc.imread(test_filename, flatten=True)
test_image = scipy.misc.imresize(test_image, [kanji_height, kanji_width])
test_image = skimage.img_as_float(test_image).astype(np.float32)
#test_image = 1.0 - test_image
#test_image /= np.linalg.norm(test_image)
#test_image = 1.0 - test_image
scipy.misc.imsave("test.png", test_image)
model.load("kanji.tflearn")
Y = model.predict(test_image.reshape([-1, kanji_height, kanji_width]))[0]
Y_indices = np.argsort(Y)[::-1]
num_test = 5
for i in range(num_test):
print("Kanji: " + str(Y_indices[i]) + ", score=" + str(Y[Y_indices[i]]))
def test():
test_filename = sys.argv[2]
test_image = scipy.misc.imread(test_filename, flatten=True)
test_image = scipy.misc.imresize(test_image, [background_height, background_width])
test_image = skimage.img_as_float(test_image).astype(np.float32)
model.load("find_kanji.tflearn")
Y = model.predict(test_image.reshape([-1, background_height, background_width]))[0]
print(Y)
masked = np.square(Y)
masked = scipy.misc.imresize(masked, [background_height, background_width])
masked = test_image * masked
scipy.misc.imsave("y.png", masked)
#Y_indices = np.argsort(Y)[::-1]
def next(self):
sz = self.target_size
output = np.ones([1, sz, sz, 1]).astype(np.float32)
img = scipy.misc.imread(
self._image_paths[self._index], mode='L').astype(np.float32)
original_size = img.shape
bigger_size = max(original_size[0], original_size[1])
mult = 1
if bigger_size > self.target_size:
mult = self.target_size / float(bigger_size)
resized_size = (int(original_size[0] * mult), int(original_size[1]*mult))
img = scipy.misc.imresize(img, resized_size)
img = (img - 128.0) / 128.0
output[0, 0:resized_size[0], 0:resized_size[1], 0] = img
self._index += 1
return output, original_size, resized_size
def _random_preprocessing(self, image, size):
# rotate image
rand_degree = np.random.randint(0, 90)
rand_flip = np.random.randint(0, 2)
if rand_flip == 1:
image = np.flip(image, 1)
image = scipy.ndimage.interpolation.rotate(image, rand_degree, cval=255)
# Select cropping range between (target_size/2 ~ original_size)
original_h, original_w = image.shape
#crop_width = np.random.randint(self.target_size/3, min(self.target_size, original_w))
#crop_height = np.random.randint(self.target_size/3, min(self.target_size, original_h))
crop_width = self.target_size
crop_height = self.target_size
topleft_x = np.random.randint(0, original_w - crop_width)
topleft_y = np.random.randint(0, original_h - crop_height)
cropped_img = image[topleft_y:topleft_y+crop_height,
topleft_x:topleft_x+crop_width]
#output = scipy.misc.imresize(cropped_img, [self.target_size, self.target_size])
output = cropped_img
output = (output - 128.0) / 128.0
return output
def _enqueue_op(self, queue, msg_queue):
while msg_queue.qsize() == 0:
# randomly select index
indexes = np.random.randint(0, self._total_num, self.batch_size)
sz = self.target_size
output = np.zeros([self.batch_size, sz, sz, 1])
for i in range(len(indexes)):
index = indexes[i]
output[i] = self._random_preprocessing(scipy.misc.imread(
self._image_paths[index], mode='L').astype(np.float),
self.target_size).reshape([sz, sz, 1])
while np.amin(output[i]) == np.amax(output[i]): # some data are strange..
output[i] = self._random_preprocessing(scipy.misc.imread(
self._image_paths[index], mode='L').astype(np.float32),
self.target_size).reshape([sz, sz, 1])
queue.put(output)
def _random_preprocessing(self, image, size):
# rotate image
rand_degree = np.random.randint(0, 180)
rand_flip = np.random.randint(0, 2)
image = scipy.ndimage.interpolation.rotate(image, rand_degree, cval=255)
if rand_flip == 1:
image = np.flip(image, 1)
# Select cropping range between (target_size/2 ~ original_size)
original_h, original_w = image.shape
crop_width = np.random.randint(self.target_size/2, min(self.target_size*2, original_w))
crop_height = np.random.randint(self.target_size/2, min(self.target_size*2, original_h))
topleft_x = np.random.randint(0, original_w - crop_width)
topleft_y = np.random.randint(0, original_h - crop_height)
cropped_img = image[topleft_y:topleft_y+crop_height,
topleft_x:topleft_x+crop_width]
output = scipy.misc.imresize(cropped_img, [self.target_size, self.target_size])
# threshold
output_thres = np.where(output < 150, -1.0, 1.0)
return output_thres
def _enqueue_op(self, queue, msg_queue):
while msg_queue.qsize() == 0:
# randomly select index
indexes = np.random.randint(0, self._total_num, self.batch_size)
sz = self.target_size
output = np.ones([self.batch_size, sz, sz, 1])
for i in range(len(indexes)):
index = indexes[i]
output[i] = self._random_preprocessing(scipy.misc.imread(
self._image_paths[index], mode='L').astype(np.float32),
self.target_size).reshape([sz, sz, 1])
while np.amin(output[i]) == np.amax(output[i]): # some data are strange..
output[i] = self._random_preprocessing(scipy.misc.imread(
self._image_paths[index], mode='L').astype(np.float32),
self.target_size).reshape([sz, sz, 1])
queue.put(output)
def save_images(images, size, image_path, color=True):
h, w = images.shape[1], images.shape[2]
if color is True:
img = np.zeros((h * size[0], w * size[1], 3))
else:
img = np.zeros((h * size[0], w * size[1]))
for idx, image in enumerate(images):
i = idx % size[1]
j = math.floor(idx / size[1])
if color is True:
img[j*h:j*h+h, i*w:i*w+w, :] = image
else:
img[j*h:j*h+h, i*w:i*w+w] = image
if color is True:
scipy.misc.toimage(rescale_image(img),
cmin=0, cmax=255).save(image_path)
else:
scipy.misc.toimage(rescale_dm(img), cmin=0, cmax=65535,
low=0, high=65535, mode='I').save(image_path)
def imread(path, is_grayscale=False):
if (is_grayscale):
return scipy.misc.imread(path, flatten=True).astype(np.float)
else:
return scipy.misc.imread(path).astype(np.float)
def imsave(images, size, path):
return scipy.misc.imsave(path, merge(images, size))
utils_combine.py 文件源码
项目:adversarial-deep-structural-networks
作者: wentaozhu
项目源码
文件源码
阅读 33
收藏 0
点赞 0
评论 0
def fetchdatalabel(path, postfix='roienhance.mat', flag='train'): # 'enhance.mat' 'roienhance.jpeg'
data = np.zeros((58, 40, 40))
label = np.zeros((58, 40, 40))
if flag == 'train':
data = np.zeros((58*4, 40, 40))
label = np.zeros((58*4, 40, 40))
datacount = 0
fname = []
for file in os.listdir(path):
if file.endswith(postfix):
if postfix[-4:] == '.mat':
im = sio.loadmat(path+file)
im = im['im']
elif postfix[-5:] == '.jpeg':
im = scipy.misc.imread(path+file)
im = im*1.0 / 255.0
imlabel = sio.loadmat(path+file[:-len(postfix)]+'massgt.mat')
imlabel = imlabel['im']
data[datacount, :, :] = im
label[datacount, :, :] = imlabel
datacount += 1
if flag == 'train':
data[datacount, :, :] = im[:, ::-1]
label[datacount, :, :] = imlabel[:, ::-1]
data[datacount+1, :, :] = im[::-1, :]
label[datacount+1, :, :] = imlabel[::-1, :]
im1 = im[::-1, :] # vertical flip, then horizontal flip
imlabel1 = imlabel[::-1, :]
data[datacount+2, :, :] = im1[:, ::-1]
label[datacount+2, :, :] = imlabel1[:, ::-1]
datacount += 3
fname.append(file)
if flag == 'train': assert(datacount==58*4)
else: assert(datacount==58)
return data , label, fname
def fetchdatalabel(path, postfix='roienhance.mat', flag='train'): # 'enhance.mat' 'roienhance.jpeg'
data = np.zeros((58, 40, 40))
label = np.zeros((58, 40, 40))
if flag == 'train':
data = np.zeros((58*4, 40, 40))
label = np.zeros((58*4, 40, 40))
datacount = 0
fname = []
for file in os.listdir(path):
if file.endswith(postfix):
if postfix[-4:] == '.mat':
im = sio.loadmat(path+file)
im = im['im']
elif postfix[-5:] == '.jpeg':
im = scipy.misc.imread(path+file)
im = im*1.0 / 255.0
imlabel = sio.loadmat(path+file[:-len(postfix)]+'massgt.mat')
imlabel = imlabel['im']
data[datacount, :, :] = im
label[datacount, :, :] = imlabel
datacount += 1
if flag == 'train':
data[datacount, :, :] = im[:, ::-1]
label[datacount, :, :] = imlabel[:, ::-1]
data[datacount+1, :, :] = im[::-1, :]
label[datacount+1, :, :] = imlabel[::-1, :]
im1 = im[::-1, :] # vertical flip, then horizontal flip
imlabel1 = imlabel[::-1, :]
data[datacount+2, :, :] = im1[:, ::-1]
label[datacount+2, :, :] = imlabel1[:, ::-1]
datacount += 3
fname.append(file)
if flag == 'train': assert(datacount==58*4)
else: assert(datacount==58)
return data , label, fname
def make_generator(files, batch_size, n_classes):
if batch_size % n_classes != 0:
raise ValueError("batch size must be divisible by num classes")
class_batch = batch_size // n_classes
generators = []
def get_epoch():
while True:
images = np.zeros((batch_size, 3, DIM, DIM), dtype='int32')
labels = np.zeros((batch_size, n_classes))
n=0
for style in styles:
styleLabel = styleNum[style]
curr = curPos[style]
for i in range(class_batch):
if curr == styles[style]:
curr = 0
random.shuffle(list(files[style]))
t0=time.time()
image = scipy.misc.imread("{}/{}/{}.png".format(path, style, str(curr)),mode='RGB')
#image = scipy.misc.imresize(image,(DIM,DIM))
images[n % batch_size] = image.transpose(2,0,1)
labels[n % batch_size, int(styleLabel)] = 1
n+=1
curr += 1
curPos[style]=curr
#randomize things but keep relationship between a conditioning vector and its associated image
rng_state = np.random.get_state()
np.random.shuffle(images)
np.random.set_state(rng_state)
np.random.shuffle(labels)
yield (images, labels)
return get_epoch
def read_depth(self, filename):
depth_mat = sio.loadmat(filename)
depthtmp=depth_mat["depth"]
ds = depthtmp.shape
if self.is_crop:
depth = scipy.misc.imresize(depthtmp,(self.output_height,self.output_width),mode='F')
depth = np.array(depth).astype(np.float32)
depth = np.multiply(self.max_depth,np.divide(depth,depth.max()))
return depth
def read_img(self, filename):
imgtmp = scipy.misc.imread(filename)
ds = imgtmp.shape
if self.is_crop:
img = scipy.misc.imresize(imgtmp,(self.output_height,self.output_width,3))
img = np.array(img).astype(np.float32)
return img
def read_depth_small(self, filename):
depth_mat = sio.loadmat(filename)
depthtmp=depth_mat["depth"]
ds = depthtmp.shape
if self.is_crop:
depth = scipy.misc.imresize(depthtmp,(self.output_height,self.output_width),mode='F')
depth = np.array(depth).astype(np.float32)
depth = np.multiply(self.max_depth,np.divide(depth,depth.max()))
return depth
def read_depth_sample(self, filename):
depth_mat = sio.loadmat(filename)
depthtmp=depth_mat["depth"]
ds = depthtmp.shape
if self.is_crop:
depth = scipy.misc.imresize(depthtmp,(self.sh,self.sw),mode='F')
depth = np.array(depth).astype(np.float32)
depth = np.multiply(self.max_depth,np.divide(depth,depth.max()))
return depth
def read_img_sample(self, filename):
imgtmp = scipy.misc.imread(filename)
ds = imgtmp.shape
if self.is_crop:
img = scipy.misc.imresize(imgtmp,(self.sh,self.sw,3))
img = np.array(img).astype(np.float32)
return img
def read_img(self, filename):
imgtmp = scipy.misc.imread(filename)
ds = imgtmp.shape
if self.is_crop:
img = scipy.misc.imresize(imgtmp,(self.output_height,self.output_width,3))
img = np.array(img).astype(np.float32)
return img
def read_depth_small(self, filename):
depth_mat = sio.loadmat(filename)
depthtmp=depth_mat["depth"]
ds = depthtmp.shape
if self.is_crop:
depth = scipy.misc.imresize(depthtmp,(self.output_height,self.output_width),mode='F')
depth = np.array(depth).astype(np.float32)
depth = np.multiply(self.max_depth,np.divide(depth,depth.max()))
return depth
def read_depth_sample(self, filename):
depth_mat = sio.loadmat(filename)
depthtmp=depth_mat["depth"]
ds = depthtmp.shape
if self.is_crop:
depth = scipy.misc.imresize(depthtmp,(self.sh,self.sw),mode='F')
depth = np.array(depth).astype(np.float32)
depth = np.multiply(self.max_depth,np.divide(depth,depth.max()))
return depth
def read_img_sample(self, filename):
imgtmp = scipy.misc.imread(filename)
ds = imgtmp.shape
if self.is_crop:
img = scipy.misc.imresize(imgtmp,(self.sh,self.sw,3))
img = np.array(img).astype(np.float32)
return img
def process_file(params):
index, data, base_filename, db_name, C, aug_data = params
label = index % NUM_CLASSES
if C==1:
orig_im = data[0,:,:]
im = ndimage.interpolation.zoom(orig_im, DOWNSCALE_FACTOR)
elif C==2:
im = np.zeros((int(MAT_SHAPE[2]*DOWNSCALE_FACTOR),int(MAT_SHAPE[3]*DOWNSCALE_FACTOR),3))
orig_im = np.zeros((MAT_SHAPE[2],MAT_SHAPE[3],3))
im[:,:,0] = ndimage.interpolation.zoom(data[0,:,:], DOWNSCALE_FACTOR)
im[:,:,1] = ndimage.interpolation.zoom(data[1,:,:], DOWNSCALE_FACTOR)
orig_im[:,:,0] = data[0,:,:]
orig_im[:,:,1] = data[1,:,:]
else:
print "Error in reading data to db- number of channels must be 1 or 2"
im_name = '%s_%d%s' % (base_filename, index,IM_FORMAT)
scipy.misc.toimage(im, cmin=0.0, cmax=255.0).save(os.path.join(db_name,im_name))
im_names = [im_name]
if aug_data:
degrees = [-20, -10, 10, 20]
crop_dims = [2, 4, 6, 8]
for i, degree in enumerate(degrees):
im_name = '%s_%d_%d%s' % (base_filename,index,degree,IM_FORMAT)
im_names.append(im_name)
rot_im = rotate_im(orig_im, degree)
scipy.misc.toimage(rot_im, cmin=0.0, cmax=255.0).save(os.path.join(db_name,im_name))
for i, crop_dim in enumerate(crop_dims):
im_name = '%s_%d_%d%s' % (base_filename,index,crop_dim,IM_FORMAT)
im_names.append(im_name)
cr_im = crop_and_rescale(orig_im, crop_dim)
scipy.misc.toimage(cr_im, cmin=0.0, cmax=255.0).save(os.path.join(db_name,im_name))
return label, im_names