def loadRawImage(_rawImageFile="null"):
if _rawImageFile == "null":
#rawImageFile = "BF_position020200_time0001.tif"
_rawImageFile = "./test1.tif"
global rawImageFile
rawImageFile = _rawImageFile
rawImg = imread(_rawImageFile)
rawImgFloat = skimage.img_as_float(rawImg)
return rawImgFloat
python类img_as_float()的实例源码
def loadPreprocessingImage(preprocessingFile):
preprocessingImg = imread(preprocessingFile)
preprocessingImg = skimage.img_as_float(preprocessingImg)
return preprocessingImg
def main():
if Display:
cap = cv2.VideoCapture(0)
out = cv2.VideoWriter('output.avi', -1, 2.0, (600, 440))
count = 0
while(count < 20):
ret, frame1 = cap.read()
ret, frame2 = cap.read()
frame1 = skimage.img_as_float(frame1)
frame2 = skimage.img_as_float(frame2)
output_img = optical_flow_ssd(frame1, frame2)
out.write(output_img)
cv2.imshow('frame', output_img)
count = count + 1
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
out.release()
cv2.destroyAllWindows()
break
else:
test()
def read_img(in_filename, grayscale=False, extra_info={}):
"""Returns the image saved at in_filename as a numpy array.
If grayscale is True, converts from 3D RGB image to 2D grayscale image.
"""
if is_pypy:
ans = Image.open(in_filename)
height = ans.height
width = ans.width
channels = len(ans.getbands())
if ans.mode == 'I':
numpy_mode = 'uint32'
maxval = 65535.0
elif ans.mode in ['L', 'RGB', 'RGBA']:
numpy_mode = 'uint8'
maxval = 255.0
else:
raise ValueError('unknown mode')
ans = numpy.fromstring(ans.tobytes(), numpy_mode).reshape((height, width, channels))
ans = ans/maxval
if grayscale and (len(ans.shape) == 3 and ans.shape[2] == 3):
ans = ans[:,:,0]*0.2125 + ans[:,:,1]*0.7154 + ans[:,:,2]*0.0721
if len(ans.shape) == 3 and ans.shape[2] == 1:
ans = ans[:,:,0]
return ans
else:
ans = skimage.io.imread(in_filename)
if ans.dtype == numpy.int32: # Work around scikit-image bug #1680
ans = numpy.asarray(ans, numpy.uint16)
ans = skimage.img_as_float(ans)
if grayscale:
ans = skimage.color.rgb2gray(ans)
# print('here', use_4channel, len(ans.shape) == 3, ans.shape[2] == 3)
if use_4channel and len(ans.shape) == 3 and ans.shape[2] == 3:
ans = numpy.dstack((ans,) + (numpy.ones((ans.shape[0], ans.shape[1], 1)),))
extra_info['originally_3channel'] = True
return ans
def remove_seam(img, seam):
img = img_as_float(img)
img = img.tolist()
seam = seam.tolist()
for i in range(0, len(img)):
del img[i][seam[i]]
plt.imshow(img)
print "Width of the image is: ", len(img[0])
return img
def remove_multiple_pixels(img, count):
for i in range(0, count):
img = remove_seam(img, find_seam(img))
img = img_as_float(img)
plt.imshow(img)
def my_label2rgboverlay(labels, colors, image, bglabel=None,
bg_color=(0., 0., 0.), alpha=0.2):
image_float = gray2rgb(img_as_float(rgb2gray(image)))
label_image = my_label2rgb(labels, colors, bglabel=bglabel,
bg_color=bg_color)
output = image_float * alpha + label_image * (1 - alpha)
return output
# Save 3 images (Image, mask and result)
def find_dominant_colors(image):
"""Cluster the colors of the image in CLUSTER_NUMBER of clusters. Returns
an array of dominant colors reverse sorted by cluster size.
"""
array = img_as_float(fromimage(image))
# Reshape from MxNx4 to Mx4 array
array = array.reshape(scipy.product(array.shape[:2]), array.shape[2])
# Remove transparent pixels if any (channel 4 is alpha)
if array.shape[-1] > 3:
array = array[array[:, 3] == 1]
# Finding centroids (centroids are colors)
centroids, _ = kmeans(array, CLUSTER_NUMBER)
# Allocate pixel to a centroid cluster
observations, _ = vq(array, centroids)
# Calculate the number of pixels in a cluster
histogram, _ = scipy.histogram(observations, len(centroids))
# Sort centroids by number of pixels in their cluster
sorted_centroids = sorted(zip(centroids, histogram),
key=lambda x: x[1],
reverse=True)
sorted_colors = tuple((couple[0] for couple in sorted_centroids))
return sorted_colors
def fix_image(image_filename):
image = scipy.misc.imread(test_filenames[i], flatten=False)
image = scipy.misc.imresize(image, [image_size, image_size])
image = skimage.img_as_float(image)
image = np.swapaxes(image, 0, 2)
image = np.swapaxes(image, 1, 2)
return image
def augment_kanji(kanji, augmentation_idx):
angle = np.random.randint(0,360) * (np.pi / 180.0)
dist = np.random.randint(0,aug_translation_max_dist)
x = int(math.cos(angle) * dist)
y = int(math.sin(angle) * dist)
augmented = np.roll(kanji, [y, x], axis=[0, 1])
#angle_step = (np.pi * 2.0) / float(num_augmentations+1)
#angle = angle_step + (angle_step * float(augmentation_idx))
#angle *= (180.0 / np.pi) # degrees
rot_angle = np.random.randint(-2, 2)
augmented = scipy.misc.imrotate(augmented, rot_angle, interp="bilinear")
pad_max = 12
pad_w = np.random.randint(0, pad_max)
pad_h = pad_w
augmented = np.pad(augmented, ((pad_h, pad_h), (pad_w, pad_w)), mode="constant")
augmented = scipy.misc.imresize(augmented, [kanji_height, kanji_width])
augmented = skimage.img_as_float(augmented).astype(np.float32)
noise = np.random.uniform(low=0.1, high=0.5)
augmented += np.random.uniform(low=-noise, high=noise, size=augmented.shape)
augmented = np.maximum(0.0, np.minimum(augmented, 1.0))
return augmented
def rasterize_kanji(kanji, weight, output_file):
kanji = kanji[0:4] # strip extra stuff like footnotes off kanji
prop = fm.FontProperties(fname="ipam.ttc", size=70)
plt.figure(figsize=(1, 1))
plt.text(0, 0, kanji, ha='center', va='center', fontproperties=prop)
plt.xlim(-0.4, 0.1)
plt.ylim(-0.1, 0.1)
plt.axis("off")
#plt.savefig(output_file)
#image = scipy.misc.imread(output_file, flatten=True)
buf = io.BytesIO()
plt.savefig(buf, format="png")
buf.seek(0)
image = PIL.Image.open(buf).convert(mode="L")
buf.close()
image = np.asarray(image, dtype=np.uint8)
plt.close()
image = scipy.misc.imresize(image, [kanji_height, kanji_width])
image = skimage.img_as_float(image).astype(np.float32)
image = 1.0 - image # make the background black and the text white
if (weight == "bold"):
erosion_size = 5
elif (weight == "normal"):
erosion_size = 3
else:
erosion_size = 0
if (erosion_size > 0):
kernel = np.ones((erosion_size, erosion_size), np.float32)
image = cv2.dilate(image, kernel, iterations=1)
scipy.misc.imsave(output_file, (1.0 - image))
return image
def dump_heatmaps(filename, images, heatmaps, antialias=True, multiply=True):
images = images.numpy()
heatmaps = heatmaps.numpy()
all_masked = np.zeros(images.shape)
num_images = images.shape[0]
for i in range(num_images):
image = images[i] / 255.0
heatmap = heatmaps[i]
# resize the heatmap to be the same size as the image
if (antialias):
interp = "bilinear"
else:
interp = "nearest"
heatmap = img_as_float(scipy.misc.imresize(heatmap, [image.shape[1], image.shape[2]], interp=interp))
# tile the heatmap in each component so it's HxWx3 like the image
heatmap = heatmap.reshape(1, heatmap.shape[0], heatmap.shape[1])
heatmap = np.tile(heatmap, (3,1,1))
# mask the image by the heatmap
if multiply:
masked = image * heatmap
else:
masked = image + heatmap
all_masked[i] = masked
vutils.save_image(torch.FloatTensor(all_masked), filename, normalize=True)
def augment_image(image, augmentation_idx):
image_height = image.shape[0]
image_width = image.shape[1]
if (image_width > hr_size):
cropX = np.random.randint(0, image_width - hr_size)
else:
cropX = 0
if (image_height > hr_size):
cropY = np.random.randint(0, image_height - hr_size)
else:
cropY = 0
hr_image = image[cropY:cropY+hr_size,cropX:cropX+hr_size,...]
lr_image = scipy.misc.imresize(hr_image, [lr_size, lr_size], interp="bilinear")
# scale low res back to high res so we can learn something other than scaling up the input
lr_scaled = scipy.misc.imresize(lr_image, [hr_size, hr_size], interp="bilinear")
# resizing changes to int, go back to float
lr_image = skimage.img_as_float(lr_image)
lr_scaled = skimage.img_as_float(lr_scaled)
#scipy.misc.imsave("hr_" + str(augmentation_idx).zfill(5) + ".png", hr_image)
#scipy.misc.imsave("hr_" + str(augmentation_idx).zfill(5) + "_small_.png", lr_image)
#scipy.misc.imsave("hr_" + str(augmentation_idx).zfill(5) + "_scaled_.png", lr_scaled)
lr_image = np.swapaxes(lr_image, 0, 2)
lr_image = np.swapaxes(lr_image, 1, 2)
hr_image = np.swapaxes(hr_image, 0, 2)
hr_image = np.swapaxes(hr_image, 1, 2)
lr_scaled = np.swapaxes(lr_scaled, 0, 2)
lr_scaled = np.swapaxes(lr_scaled, 1, 2)
return lr_image, lr_scaled, hr_image
def read_caffe_img(path):
return skimage.img_as_float(skimage.io.imread(path)).astype(np.float32).copy()
def my_label2rgboverlay(labels, colors, image, bglabel=None,
bg_color=(0., 0., 0.), alpha=0.2):
image_float = gray2rgb(img_as_float(rgb2gray(image)))
label_image = my_label2rgb(labels, colors, bglabel=bglabel,
bg_color=bg_color)
output = image_float * alpha + label_image * (1 - alpha)
return output
# Save 3 images (Image, mask and result)
cnn_util.py 文件源码
项目:Handwritten_recognition_tensorflow
作者: sanjanaramprasad
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def crop_image(x, target_height=227, target_width=227, as_float=True):
image = skimage.io.imread(x)
if as_float:
image = skimage.img_as_float(image).astype(np.float32)
print(len(image.shape))
if len(image.shape) == 2:
image = np.tile(image[:, :, None], 3)
elif len(image.shape) == 4:
image = image[:, :, :, 0]
height, width, rgb = image.shape
if width == height:
resized_image = np.resize(image, (target_height,target_width))
elif height < width:
resized_image = np.resize(image, (int(width * float(target_height)/height), target_width))
cropping_length = int((resized_image.shape[1] - target_height) / 2)
resized_image = resized_image[:,cropping_length:resized_image.shape[1] - cropping_length]
else:
resized_image = np.resize(image, (target_height, int(height * float(target_width) / width)))
cropping_length = int((resized_image.shape[0] - target_width) / 2)
resized_image = resized_image[cropping_length:resized_image.shape[0] - cropping_length,:]
return np.resize(resized_image, (target_height, target_width))
def my_label2rgboverlay(labels, cmap, image, bglabel=None,
bg_color=(0., 0., 0.), alpha=0.2):
'''Superimpose a mask over an image
Convert a label mask to RGB applying a color map and superimposing it
over an image as a transparent overlay'''
image_float = gray2rgb(img_as_float(rgb2gray(image)))
label_image = my_label2rgb(labels, cmap, bglabel=bglabel,
bg_color=bg_color)
output = image_float * alpha + label_image * (1 - alpha)
return output
def pil_to_np(x):
"""Converts from PIL.Image to np float format image"""
x = np.asarray(x)
if len(x.shape) == 2:
x = x[:,:,np.newaxis]
return img_as_float(np.asarray(x))
def oversample(img, crop_ratio, horizon_flip=False, vertical_flip=False):
"""
4 corner crops , horizonly flips and vertically flips
Parameter:
-------------
img: h*w*c image; ndarray, dtype= np.float32
crop_ratio: (crop_height_ratio, crop_width_ratio); tuple
horizon_flip: flip the image horizonly;bool
vertical_flip:
Return:
------------
crops: N*h*w*c images; ndarray
"""
img_height = img.shape[0]
img_width = img.shape[1]
img_channel = img.shape[-1]
crop_dims = (int(img_height * crop_ratio[0]), int(img_width * crop_ratio[1]))
h_indices = (0, img_height - crop_dims[0])
w_indices = (0, img_width - crop_dims[1])
crop_idx = np.empty((4, 4), dtype=int)
idx = 0
for i in h_indices:
for j in w_indices:
#crop_idx : (ymin, xmin, ymax, xmax)
crop_idx[idx] = (i, j, i + crop_dims[0], j + crop_dims[1])
idx+=1
num = 4
if horizon_flip:
num += 4
if vertical_flip:
num += 4
crops = np.empty((num, crop_dims[0], crop_dims[1], img_channel), dtype=np.float32)
idx = 0
for crop in crop_idx:
crops[idx] = img[crop[0]:crop[2], crop[1]:crop[3], :]
img_crop = crops[idx]
idx += 1
if horizon_flip:
crops[idx] = np.flipud(img_crop)
idx += 1
if vertical_flip:
crops[idx] = np.fliplr(img_crop)
idx += 1
return crops
#test
# img = skimage.img_as_float(skimage.io.imread("./test.jpg")).astype(np.float32)
# # img = skimage.io.imread("./test.jpg")
# print img.shape
# print type(img)
# crops = oversample(img,(0.8,0.8),True,True)
# print crops.shape
# for idx in xrange(crops.shape[0]):
# skimage.io.imsave(os.path.join("./", str(idx) + ".jpg"), crops[idx, :, :, :])
def get_output(i):
image = load_image('../../data/sbdd/dataset', 'img_{}'.format(i))
res = net.forward()
score = res['score_output2'].transpose((2, 3, 1, 0))
label = res['label'].transpose((2, 3, 1, 0))
print('score shape:', score.shape)
print('label shape:', label.shape)
score = score[:, :, :, 0]
label = label[:, :, 0, 0]
print(score.shape)
print(label.shape, label.dtype)
width = 3
height = 2
image = img_as_float(np.reshape(image[:, :, 0], image.shape[:2]))
plt.subplot(height, width, 1)
plt.title('target image')
plt.imshow(image, cmap='gray')
plt.subplot(height, width, 2)
plt.title('network output\nlabel 0')
plt.imshow(score[:, :, 0], cmap='gray')
plt.subplot(height, width, 3)
plt.title('network output\nlabel 1')
plt.imshow(score[:, :, 1], cmap='gray')
prob_threshold = 0.5
binary_score = (score[:, :, 1] > prob_threshold).astype(np.float)
plt.subplot(height, width, 4)
plt.title('prob > {}'.format(prob_threshold))
plt.imshow(binary_score, cmap='gray')
print(image, image.shape)
plt.subplot(height, width, 5)
plt.title('image + output')
plt.imshow(image + binary_score, cmap='gray')
plt.subplot(height, width, 6)
plt.title('image + grand truth')
plt.imshow(image + label, cmap='gray')
plt.savefig('output/img_{}.png'.format(i), bbox_inches='tight')