# Wolfram 语言™

## 立体图的可视化与合成

In[1]:=
Out[1]=

In[2]:=
period = Length@ Last@FindTransientRepeat[First@ImageData@Binarize@stereogram, 3]
Out[2]=

In[3]:=
shifts = Map[Norm, First@ImageDisplacements[ {ImageTake[stereogram, All, {period + 1, -1}], ImageTake[stereogram, All, {1, -period - 1}]} ], {2}];

In[4]:=
shiftToDepth[shift_] := 1600 shift/(160 + shift);
In[5]:=
depthEstimate = ImagePad[Image[ shiftToDepth@shifts], {{period + 1 - Max[shifts], Max[shifts]}, {0, 0}}, Black];
In[6]:=
Out[6]=

In[7]:=
StereogramShift[maxshift_][pattern_Image, depth_Image] := Block[ {array, trafo, shifted, widht, height}, {widht, height} = {1/2, 1} ImageDimensions[pattern]; array = maxshift Transpose@ImageData[depth, DataReversed -> True]; trafo = position \[Function] (position + {Extract[array, Ceiling[position]], 0}); shifted = ImageTransformation[pattern, trafo, DataRange -> All, PlotRange -> {{0, widht}, {0, height}}, Resampling -> "Cubic", Padding -> "Periodic"]; ImageAssemble[{{ImageTake[pattern, All, {-widht, -1}], shifted}}] ]; CreateStereogram[pattern_Image, depth_Image, maxshift_: 24] := Module[ {W, H, w, h, depthMap, patternStrip}, {w, h} = ImageDimensions[pattern]; {W, H} = ImageDimensions[depth]; depthMap = ImageCrop[ ImageClip@ColorConvert[depth, "Grayscale"], {Ceiling[W, w], Full}]; patternStrip = ImageCrop[ ImageAssemble@ Transpose@{ConstantArray[pattern, {Ceiling[H/h]}]}, {w, H}]; ImageCrop[ ImageAssemble@List@ Map[ strip \[Function] ImageTake[strip, All, {w/2 + 1, -1}], FoldList[ StereogramShift[maxshift], patternStrip, Flatten@ ImagePartition[ depthMap, {1/2, 1} ImageDimensions[patternStrip]] ] ], {W, H} ] ]
In[8]:=