1# Copyright 2016 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Python layer for image_ops.""" 16from __future__ import absolute_import 17from __future__ import division 18from __future__ import print_function 19 20from tensorflow.contrib.image.ops import gen_single_image_random_dot_stereograms_ops 21from tensorflow.contrib.util import loader 22from tensorflow.python.framework import ops 23from tensorflow.python.platform import resource_loader 24 25_sirds_ops = loader.load_op_library( 26 resource_loader.get_path_to_datafile( 27 "_single_image_random_dot_stereograms.so")) 28 29 30def single_image_random_dot_stereograms(depth_values, 31 hidden_surface_removal=None, 32 convergence_dots_size=None, 33 dots_per_inch=None, 34 eye_separation=None, 35 mu=None, 36 normalize=None, 37 normalize_max=None, 38 normalize_min=None, 39 border_level=None, 40 number_colors=None, 41 output_image_shape=None, 42 output_data_window=None): 43 """Output a RandomDotStereogram Tensor for export via encode_PNG/JPG OP. 44 45 Given the 2-D tensor 'depth_values' with encoded Z values, this operation 46 will encode 3-D data into a 2-D image. The output of this Op is suitable 47 for the encode_PNG/JPG ops. Be careful with image compression as this may 48 corrupt the encode 3-D data within the image. 49 50 Based upon [this 51 paper](http://www.learningace.com/doc/4331582/b6ab058d1e206d68ab60e4e1ead2fe6e/sirds-paper). 52 53 This outputs a SIRDS image as picture_out.png: 54 55 ```python 56 img=[[1,2,3,3,2,1], 57 [1,2,3,4,5,2], 58 [1,2,3,4,5,3], 59 [1,2,3,4,5,4], 60 [6,5,4,4,5,5]] 61 session = tf.InteractiveSession() 62 sirds = single_image_random_dot_stereograms( 63 img, 64 convergence_dots_size=8, 65 number_colors=256,normalize=True) 66 67 out = sirds.eval() 68 png = tf.image.encode_png(out).eval() 69 with open('picture_out.png', 'wb') as f: 70 f.write(png) 71 ``` 72 73 Args: 74 depth_values: A `Tensor`. Must be one of the following types: 75 `float64`, `float32`, `int64`, `int32`. Z values of data to encode 76 into 'output_data_window' window, lower further away {0.0 floor(far), 77 1.0 ceiling(near) after norm}, must be 2-D tensor 78 hidden_surface_removal: An optional `bool`. Defaults to `True`. 79 Activate hidden surface removal 80 convergence_dots_size: An optional `int`. Defaults to `8`. 81 Black dot size in pixels to help view converge image, drawn on bottom 82 of the image 83 dots_per_inch: An optional `int`. Defaults to `72`. 84 Output device in dots/inch 85 eye_separation: An optional `float`. Defaults to `2.5`. 86 Separation between eyes in inches 87 mu: An optional `float`. Defaults to `0.3333`. 88 Depth of field, Fraction of viewing distance (eg. 1/3 = 0.3333) 89 normalize: An optional `bool`. Defaults to `True`. 90 Normalize input data to [0.0, 1.0] 91 normalize_max: An optional `float`. Defaults to `-100`. 92 Fix MAX value for Normalization (0.0) - if < MIN, autoscale 93 normalize_min: An optional `float`. Defaults to `100`. 94 Fix MIN value for Normalization (0.0) - if > MAX, autoscale 95 border_level: An optional `float`. Defaults to `0`. 96 Value of bord in depth 0.0 {far} to 1.0 {near} 97 number_colors: An optional `int`. Defaults to `256`. 2 (Black & 98 White), 256 (grayscale), and Numbers > 256 (Full Color) are 99 supported 100 output_image_shape: An optional `tf.TensorShape` or list of `ints`. 101 Defaults to shape `[1024, 768, 1]`. Defines output shape of returned 102 image in '[X,Y, Channels]' 1-grayscale, 3 color; channels will be 103 updated to 3 if number_colors > 256 104 output_data_window: An optional `tf.TensorShape` or list of `ints`. 105 Defaults to `[1022, 757]`. Size of "DATA" window, must be equal to or 106 smaller than `output_image_shape`, will be centered and use 107 `convergence_dots_size` for best fit to avoid overlap if possible 108 109 Returns: 110 A `Tensor` of type `uint8` of shape 'output_image_shape' with encoded 111 'depth_values' 112 """ 113 114 result = gen_single_image_random_dot_stereograms_ops.single_image_random_dot_stereograms( # pylint: disable=line-too-long 115 depth_values=depth_values, 116 hidden_surface_removal=hidden_surface_removal, 117 convergence_dots_size=convergence_dots_size, 118 dots_per_inch=dots_per_inch, 119 eye_separation=eye_separation, 120 mu=mu, 121 normalize=normalize, 122 normalize_max=normalize_max, 123 normalize_min=normalize_min, 124 border_level=border_level, 125 number_colors=number_colors, 126 output_image_shape=output_image_shape, 127 output_data_window=output_data_window) 128 return result 129 130 131ops.NotDifferentiable("SingleImageRandomDotStereograms") 132