今回は子供の日という事で
ステレオグラムで遊んでみました。

解説動画はこちら



ステレオグラムとは(立体画)


立体図は、立体的印象をもつように描かれた
平面に描かれた図や絵あるいは写真のことで

目の焦点を意図的に前後にずらして合わせることで
左右の絵を別々の目で見ることにより
立体的に見ることができる画像です。


今回はドットのパターンを用意して
そこに浮かび上がらせる画像を組み合わせて作っています。

見方は、黒いドットが3つになるように
重ね合わせて目の焦点を合わしていくと
見る事ができると思います。



ステレオグラムを作成するコード

import numpy as np
import matplotlib.pyplot as plt
import cv2
import warnings
warnings.simplefilter("ignore")
%matplotlib inline

def make_dots(shape=(16, 16), levels=64):
    return np.random.randint(0, levels - 1, shape) / levels

def make_text_img(text='A', shape=(400, 600)):
    font = cv2.FONT_HERSHEY_SIMPLEX
    (text_width, text_height), _ = cv2.getTextSize(text, font, 5, 30)
    img = np.zeros((text_height + 50, text_width + 200), dtype=np.float)
    cv2.putText(img, text, (100, text_height+10), font, 5, (255, 255, 255), 30, cv2.LINE_AA)
    img_resized = cv2.resize(img, shape[::-1], interpolation=cv2.INTER_AREA)
    return img_resized

def normalize(depthmap):
    if depthmap.max() > depthmap.min():
        return (depthmap - depthmap.min()) / (depthmap.max() - depthmap.min())
    else:
        return depthmap

def make_autostereogram(text, shift_amplitude=0.1, invert=False):
    depthmap = make_text_img(text=text)
    pattern = make_dots(shape=(64, 64))
    depthmap = normalize(depthmap)
    if invert:
        depthmap = 1 - depthmap
    autostereogram = np.zeros_like(depthmap, dtype=pattern.dtype)
    for r in np.arange(autostereogram.shape[0]):
        for c in np.arange(autostereogram.shape[1]):
            if c < pattern.shape[1]:
                autostereogram[r, c] = pattern[r % pattern.shape[0], c]
            else:
                shift = int(depthmap[r, c] * shift_amplitude * pattern.shape[1])
                autostereogram[r, c] = autostereogram[r, c - pattern.shape[1] + shift]
    autostereogram[48:53, 248:253] = 0
    autostereogram[48:53, 348:353] = 0
    return autostereogram

実行する際は浮かび上がらせる画像を
テキストとして文字列を変更します。
autostereogram = make_autostereogram(text='A')
plt.figure(figsize=(8,8))
plt.imshow(autostereogram, cmap='gray')
plt.axis("off")
plt.show()
download

この画像の上にある黒い点が
4つになるようにぼんやり見た後に
点が3つになるように焦点をずらします。

そうすると A という文字が
浮かび上がって見えると思います。


問題

さっきの練習を少し難しくした3文字です。
g1





新宿の掲示板に書いてある3文字です。
g2




自分の大好きな5文字です。
g3





見えましたか?


見えない方は
努力が足りていないです!!!!!!!!

見えた方は
今日の1日がとても
すこやかに過ごせるでしょう!!!

色々画像を作って遊んでみて下さい。

それでは。