本文整理汇总了Python中jicbioimage.core.image.Image类的典型用法代码示例。如果您正苦于以下问题:Python Image类的具体用法?Python Image怎么用?Python Image使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Image类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sample_image_from_lines
def sample_image_from_lines(image_file, lines_file, dilation, reduce_method):
data_image = Image.from_file(image_file)
line_image = Image.from_file(lines_file)
segmented_lines = segment(line_image, dilation)
with open("all_series.csv", "w") as fh:
fh.write(csv_header())
for n, line_region in enumerate(yield_line_masks(segmented_lines)):
line_intensity = data_image * line_region
if reduce_method == "max":
line_profile = np.amax(line_intensity, axis=1)
elif reduce_method == "mean":
sum_intensity = np.sum(line_intensity, axis=1)
sum_rows = np.sum(line_region, axis=1)
line_profile = sum_intensity / sum_rows
else:
err_msg = "Unknown reduce method: {}".format(reduce_method)
raise(RuntimeError(err_msg))
series_filename = "series_{:02d}.csv".format(n)
save_line_profile(series_filename, line_profile, n)
fh.write(csv_body(line_profile, n))
示例2: test_repr_html
def test_repr_html(self):
from jicbioimage.core.image import MicroscopyCollection, MicroscopyImage, Image
microscopy_collection = MicroscopyCollection()
image = Image((50,50))
image.png = MagicMock(return_value=bytearray('image', encoding='utf-8'))
with patch('jicbioimage.core.image.Image.from_file', return_value=image) as patched_image:
microscopy_collection.append(MicroscopyImage('test0.tif',
dict(series=1, channel=2, zslice=3, timepoint=4)))
html = microscopy_collection._repr_html_()
self.assertEqual(html.strip().replace(' ', '').replace('\n', ''),
'''
<div style="float: left; padding: 2px;" >
<p>
<table>
<tr>
<th>Index</th>
<th>Series</th>
<th>Channel</th>
<th>Z-slice</th>
<th>Time point</th>
</tr>
<tr>
<td>0</td>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
</tr>
</table>
</p>
<img style="margin-left: auto; margin-right: auto;" src="data:image/png;base64,aW1hZ2U=" />
</div>
'''.strip().replace(' ', '').replace('\n', ''))
示例3: test_scaling_of_written_files
def test_scaling_of_written_files(self):
from jicbioimage.core.image import Image3D, Image
directory = os.path.join(TMP_DIR, "im3d")
z0 = np.zeros((50,50), dtype=np.uint8)
z1 = np.ones((50, 50), dtype=np.uint8)
stack = np.dstack([z0, z1])
im3d = Image3D.from_array(stack)
im3d.to_directory(directory)
im0 = Image.from_file(os.path.join(directory, "z0.png"))
im1 = Image.from_file(os.path.join(directory, "z1.png"))
self.assertTrue(np.array_equal(z0, im0))
self.assertTrue(np.array_equal(z1*255, im1))
z2 = np.ones((50, 50), dtype=np.uint8) * 255
stack = np.dstack([z0, z1, z2])
im3d = Image3D.from_array(stack)
im3d.to_directory(directory)
im0 = Image.from_file(os.path.join(directory, "z0.png"))
im1 = Image.from_file(os.path.join(directory, "z1.png"))
im2 = Image.from_file(os.path.join(directory, "z2.png"))
self.assertTrue(np.array_equal(z0, im0))
self.assertTrue(np.array_equal(z1, im1))
self.assertTrue(np.array_equal(z2*255, im1))
示例4: test_manual_image_creation_from_file
def test_manual_image_creation_from_file(self):
from jicbioimage.core.image import Image
# Preamble: let us define the path to a TIFF file and create a numpy
# array from it.
# from libtiff import TIFF
# tif = TIFF.open(path_to_tiff, 'r')
# ar = tif.read_image()
path_to_tiff = os.path.join(DATA_DIR, 'single-channel.ome.tif')
use_plugin('freeimage')
ar = imread(path_to_tiff)
# It is possible to create an image from a file.
image = Image.from_file(path_to_tiff)
self.assertEqual(len(image.history), 0)
self.assertEqual(image.history.creation,
'Created Image from {}'.format(path_to_tiff))
# With name...
image = Image.from_file(path_to_tiff, name='Test1')
self.assertEqual(image.history.creation,
'Created Image from {} as Test1'.format(path_to_tiff))
# Without history...
image = Image.from_file(path_to_tiff, log_in_history=False)
self.assertEqual(len(image.history), 0)
# It is worth noting the image can support more multiple channels.
# This is particularly important when reading in images in rgb format.
fpath = os.path.join(DATA_DIR, 'tjelvar.png')
image = Image.from_file(fpath)
self.assertEqual(image.shape, (50, 50, 3))
示例5: test_png_with_width
def test_png_with_width(self):
from jicbioimage.core.image import Image
image = Image((600, 800), dtype=np.uint64)
thumbnail = image.png(width=300)
ar = np.asarray(PIL.Image.open(io.BytesIO(thumbnail)))
self.assertEqual(ar.shape[0], 300)
self.assertEqual(ar.shape[1], 400)
示例6: test_png
def test_png(self):
from jicbioimage.core.image import Image
image = Image((600, 500), dtype=np.uint64)
png = image.png()
ar = np.asarray(PIL.Image.open(io.BytesIO(png)))
self.assertEqual(ar.shape[0], 600)
self.assertEqual(ar.shape[1], 500)
示例7: generate_composite_image
def generate_composite_image(base_image, trajectory_image):
still_image = Image.from_file(base_image)
trajectories = Image.from_file(trajectory_image)[:,:,0]
annotation_points = np.where(trajectories != 0)
color = 255, 0, 0
for x, y in zip(*annotation_points):
still_image[x, y] = color
still_image[x+1, y] = color
still_image[x-1, y] = color
still_image[x, y+1] = color
still_image[x, y-1] = color
imsave('annotated_image.png', still_image)
示例8: annotate_single_identifier
def annotate_single_identifier(dataset, identifier, output_path):
file_path = dataset.abspath_from_identifier(identifier)
image = Image.from_file(file_path)
grayscale = np.mean(image, axis=2)
annotated = AnnotatedImage.from_grayscale(grayscale)
xdim, ydim, _ = annotated.shape
def annotate_location(fractional_coords):
xfrac, yfrac = fractional_coords
ypos = int(ydim * xfrac)
xpos = int(xdim * yfrac)
for x in range(-2, 3):
for y in range(-2, 3):
annotated.draw_cross(
(xpos+x, ypos+y),
color=(255, 0, 0),
radius=50
)
for loc in find_approx_plot_locs(dataset, identifier):
annotate_location(loc)
output_basename = os.path.basename(file_path)
full_output_path = os.path.join(output_path, output_basename)
with open(full_output_path, 'wb') as f:
f.write(annotated.png())
示例9: find_kilobots
def find_kilobots(image_filename, output_filename):
"""Find kilobots in a still image file."""
kilobot_image = Image.from_file(image_filename)
red_only = kilobot_image[:,:,0]
imsave('red.png', red_only)
edges = find_edges(red_only)
blurred = gaussian_filter(edges, sigma=2)
# bot_template = blurred[135:185,485:535]
# imsave('bot_template.png', bot_template)
bot_template = load_bot_template('bot_template.png')
match_result = skimage.feature.match_template(blurred, bot_template, pad_input=True)
imsave('match_result.png', match_result)
selected_area = match_result > 0.6
imsave('selected_area.png', selected_area)
ccs = find_connected_components(selected_area)
centroids = component_centroids(ccs)
return centroids
示例10: test_creating_transformations_from_scratch
def test_creating_transformations_from_scratch(self):
# What if the default names of images was just the order in which they
# were created?
# Or perhaps the order + the function name, e.g.
# 1_gaussian.png
# 2_sobel.png
# 3_gaussian.png
# The order could be tracked in a class variable in an AutoName
# object. The AutoName object could also store the output directory
# as a class variable.
from jicbioimage.core.image import Image
from jicbioimage.core.transform import transformation
from jicbioimage.core.io import AutoName
AutoName.directory = TMP_DIR
@transformation
def identity(image):
return image
image = Image.from_file(os.path.join(DATA_DIR, 'tjelvar.png'))
image = identity(image)
self.assertEqual(len(image.history), 1, image.history)
self.assertEqual(str(image.history[-1]), '<History.Event(identity(image))>')
created_fpath = os.path.join(TMP_DIR, '1_identity.png')
self.assertTrue(os.path.isfile(created_fpath),
'No such file: {}'.format(created_fpath))
示例11: separate_plots
def separate_plots(dataset, identifier, resource_dataset, working_dir):
fpath = dataset.item_content_abspath(identifier)
segmentation = load_segmentation_from_rgb_image(fpath)
original_id = dataset.get_overlay('from')[identifier]
original_fpath = resource_dataset.item_content_abspath(original_id)
original_image = Image.from_file(original_fpath)
approx_plot_locs = find_approx_plot_locs(dataset, identifier)
sid_to_label = generate_segmentation_identifier_to_label_map(
approx_plot_locs,
segmentation
)
outputs = []
for identifier in segmentation.identifiers:
image_section = generate_region_image(
original_image,
segmentation,
identifier
)
fname = 'region_{}.png'.format(sid_to_label[identifier])
output_fpath = os.path.join(working_dir, fname)
imsave(output_fpath, image_section)
outputs.append((fname, {'plot_number': sid_to_label[identifier]}))
return outputs
示例12: process_single_identifier
def process_single_identifier(dataset, identifier, output_path):
print("Processing {}".format(identifier))
image = Image.from_file(dataset.abspath_from_identifier(identifier))
seeds = generate_seed_image(image, dataset, identifier)
segmentation = segment(image, seeds)
segmentation = filter_sides(segmentation)
segmentation = filter_touching_border(segmentation)
output_filename = generate_output_filename(
dataset,
identifier,
output_path,
'-segmented'
)
save_segmented_image_as_rgb(segmentation, output_filename)
false_colour_filename = generate_output_filename(
dataset,
identifier,
output_path,
'-false_colour'
)
with open(false_colour_filename, 'wb') as fh:
fh.write(segmentation.png())
示例13: test_from_array
def test_from_array(self):
from jicbioimage.core.image import Image
ar = np.zeros((50,50), dtype=np.uint8)
im = Image.from_array(ar)
self.assertTrue(isinstance(im, Image))
self.assertEqual(len(im.history), 0)
self.assertEqual(im.history.creation, 'Created Image from array')
示例14: find_single_seed
def find_single_seed(image_filename, output_filename):
image = Image.from_file(image_filename)
w, h = 500, 500
tube_section = image[1024-w:1024+w,1024-h:1024+h]
threshold = threshold_otsu(tube_section)
thresholded = tube_section > threshold
x, y, r = find_inner_circle_parameters(thresholded, 400, 500)
# FIXME - think routine is finding outer circle
stripped = strip_outside_circle(thresholded, (x, y), 300)
eroded = binary_erosion(stripped, structure=np.ones((10, 10)))
float_coords = map(np.mean, np.where(eroded > 0))
ix, iy = map(int, float_coords)
w, h = 100, 100
selected = tube_section[ix-w:ix+w,iy-h:iy+h]
with open(output_filename, 'wb') as f:
f.write(selected.view(Image).png())
示例15: find_grains
def find_grains(input_file, output_dir=None):
"""Return tuple of segmentaitons (grains, difficult_regions)."""
name = fpath2name(input_file)
name = "grains-" + name + ".png"
if output_dir:
name = os.path.join(output_dir, name)
image = Image.from_file(input_file)
intensity = mean_intensity_projection(image)
# Median filter seems more robust than Otsu.
# image = threshold_otsu(intensity)
image = threshold_median(intensity, scale=0.8)
image = invert(image)
image = erode_binary(image, selem=disk(2))
image = dilate_binary(image, selem=disk(2))
image = remove_small_objects(image, min_size=200)
image = fill_holes(image, min_size=50)
dist = distance(image)
seeds = local_maxima(dist)
seeds = dilate_binary(seeds) # Merge spurious double peaks.
seeds = connected_components(seeds, background=0)
segmentation = watershed_with_seeds(dist, seeds=seeds, mask=image)
# Remove spurious blobs.
segmentation = remove_large_segments(segmentation, max_size=3000)
segmentation = remove_small_segments(segmentation, min_size=100)
return segmentation