• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2017 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 
16 #include "tensorflow/core/framework/op.h"
17 #include "tensorflow/core/framework/op_kernel.h"
18 #include "tensorflow/core/framework/shape_inference.h"
19 
20 namespace tensorflow {
21 
22 using shape_inference::DimensionHandle;
23 using shape_inference::InferenceContext;
24 using shape_inference::ShapeHandle;
25 
26 REGISTER_OP("SingleImageRandomDotStereograms")
27     .Attr("T: {double,float,int64,int32}")
28     .Input("depth_values: T")
29     .Output("image: uint8")
30     .Attr("hidden_surface_removal: bool = true")
31     .Attr("convergence_dots_size: int = 8")
32     .Attr("dots_per_inch: int = 72")
33     .Attr("eye_separation: float = 2.5")
34     .Attr("mu: float = .3333")
35     .Attr("normalize: bool = true")
36     .Attr("normalize_max: float = -100.0")
37     .Attr("normalize_min: float = 100.0")
38     .Attr("border_level: float = 0.0")
39     .Attr("number_colors: int = 256")
40     .Attr(
41         "output_image_shape: shape = { dim {size:1024} dim {size: 768} dim "
42         "{size: 1}}")
43     .Attr("output_data_window: shape = { dim {size:1022} dim {size: 757}}")
__anonbda156eb0102(InferenceContext* c) 44     .SetShapeFn([](InferenceContext* c) {
45       // Validate that the output_image_shape attr is correct.
46       // NOTE: The output_image_shape is [X, Y, C]
47       // while the output data is [Y, X, C] (or [H, W, C]).
48       // As a result, by default the output_image_shape has the value
49       // of [1024, 768, 1] but the output data will be [768, 1024, 1].
50       PartialTensorShape shape;
51       TF_RETURN_IF_ERROR(c->GetAttr("output_image_shape", &shape));
52       ShapeHandle output_image_shape;
53       TF_RETURN_IF_ERROR(
54           c->MakeShapeFromPartialTensorShape(shape, &output_image_shape));
55       DimensionHandle x_dim = c->Dim(output_image_shape, 0);
56       DimensionHandle y_dim = c->Dim(output_image_shape, 1);
57 
58       int colors;
59       TF_RETURN_IF_ERROR(c->GetAttr("number_colors", &colors));
60 
61       c->set_output(
62           0, c->MakeShape(
63                  {y_dim, x_dim, colors > 256 ? c->MakeDim(3) : c->MakeDim(1)}));
64       return Status::OK();
65     })
66     .Doc(R"doc(
67 Outputs a single image random dot stereogram for export via encode_PNG/JPG OP.
68 
69 Given the 2-D tensor 'depth_values' with encoded Z values, this operation will
70 encode 3-D data into a 2-D image.  The output of this Op is suitable for the
71 encode_PNG/JPG ops.  Be careful with image compression as this may corrupt the
72 encode 3-D data within the image.
73 
74 This Op is based upon:
75 'http://www.learningace.com/doc/4331582/b6ab058d1e206d68ab60e4e1ead2fe6e/sirds-paper'
76 
77 Example use which outputs a SIRDS image as picture_out.png:
78 ```python
79 img=[[1,2,3,3,2,1],
80      [1,2,3,4,5,2],
81      [1,2,3,4,5,3],
82      [1,2,3,4,5,4],
83      [6,5,4,4,5,5]]
84 
85 session = tf.InteractiveSession()
86 
87 sirds = single_image_random_dot_stereograms(img,convergence_dots_size=8,number_colors=256,normalize=True)
88 
89 out = sirds.eval()
90 
91 png = tf.image.encode_png(out).eval()
92 
93 with open('picture_out.png', 'wb') as f:
94     f.write(png)
95 ```
96 
97 depth_values: Z values of data to encode into 'output_data_window' window,
98   lower values are further away {0.0 floor(far), 1.0 ceiling(near) after normalization}, must be 2-D tensor
99 hidden_surface_removal: Activate hidden surface removal
100 convergence_dots_size: Black dot size in pixels to help view converge image, drawn on bottom of image
101 dots_per_inch: Output device in dots/inch
102 eye_separation: Separation between eyes in inches
103 mu: Depth of field, Fraction of viewing distance (eg. 1/3 = .3333)
104 normalize: Normalize input data to [0.0, 1.0]
105 normalize_max: Fix MAX value for Normalization - if < MIN, autoscale
106 normalize_min: Fix MIN value for Normalization - if > MAX, autoscale
107 border_level: Value of border depth 0.0 {far} to 1.0 {near}
108 number_colors: 2 (Black & White),256 (grayscale), and Numbers > 256 (Full Color) are all that are supported currently
109 output_image_shape: Output size of returned image in X,Y, Channels 1-grayscale, 3 color (1024, 768, 1),
110   channels will be updated to 3 if 'number_colors' > 256
111 output_data_window: Size of "DATA" window, must be equal to or smaller than 'output_image_shape', will be centered
112   and use 'convergence_dots_size' for best fit to avoid overlap if possible
113 
114 image:= A tensor of size 'output_image_shape' with the encoded 'depth_values'
115 )doc");
116 
117 }  // namespace tensorflow
118