Create your own Random Dot Stereogram (RDS) in Python.

Random Dot Stereogram (RDS)

Mh_stereogramm_sirds.png The original uploader has been moved from Los Hawlos on German Wikipedia to Commons from de.wikipedia. , CC display-inheritance 3.0, by https://commons.wikimedia.org/w/index.php?curid=1966500

Can you see it? By shifting the focus of the eyes back and forth, pictures and letters emerge (look three-dimensional). I'm sorry for those who are watching from a smartphone. Look from your computer.

I want to make this myself.

There was an implementation example in Python on the following site, so I made an original random dot stereogram by referring to it.

What to use

import numpy as np
import matplotlib.pyplot as plt
import cv2

I've been experimenting with Notebook format and used the familiar matplotlib to display images, but anything is fine as long as you can see the images.

procedure

1. Create a random pattern

def make_pattern(shape=(16, 16)):
    return np.random.uniform(0, 1, shape)

Try to run it.

pattern = make_pattern((400,400))
plt.imshow(pattern, cmap='gray')

pattern.png

Something is already about to emerge.

2. Create a pattern that emerges

def make_depthmap(shape=(400, 600)):
    depthmap = np.zeros(shape, dtype=np.float)
    cv2.circle(depthmap, (int(shape[1]/2), int(shape[0]/2)), 100, (255 ,255, 255), -1)
    return depthmap

Try to run it.

depthmap = make_depthmap()
plt.imshow(depthmap, cmap='gray')

depthmap.png This one should come to the fore.

3. I tried it


def make_autostereogram(depthmap, pattern, shift_amplitude=0.1, invert=False):
    "Creates an autostereogram from depthmap and pattern."
    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]
    return autostereogram

def normalize(depthmap):
        return depthmap/255

The main process is quoted from the reference site mentioned at the beginning. Try to run it.

autostereogram = make_autostereogram(depthmap, pattern, 0.3)
plt.imshow(autostereogram, cmap='gray')

autostereogram.png

** A circle is clearly visible. ** ** (The circle mark appears to be dented in the crossing method, which brings the focus of the eyes closer.)

Because it's a big deal

I tried to make the text a random dot stereogram.

def make_text_depthmap(shape=(400, 600), text='Q i i t a'):
    font = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(depthmap, text, (50, 250), font, 4, (255,255,255), 12, cv2.LINE_AA)
    return depthmap

Something that emerges.

depthmap_text = make_text_depthmap()
plt.imshow(depthmap, cmap='gray')

depthmap_qiita.png Try to run it.


autostereogram = make_autostereogram(depthmap_text, pattern, 0.05)
plt.imshow(autostereogram, cmap='gray')

autostereogram_text.png It's a little vaguer than the circle, but you can see it.

Quiz for engineers

What is it written on? ?? The text. autostereogram_quiz.png

Answers are welcome in the comments ^^

Postscript

I wrote a sequel.

-In search of the best random dot stereogram (RDS).

I wrote a sequel.

-Create an original random dot stereogram (RDS) from 2D photos with depth estimation by machine learning

Recommended Posts

Create your own Random Dot Stereogram (RDS) in Python.
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Create your own Big Data in Python for validation
In search of the best random dot stereogram (RDS).
[Python] logging in your own module
Create a random string in Python
Easily use your own functions in Python
Create your own graph structure class and its drawing in python
Get your own IP address in Python
Import your own modules in Grasshopper's Python development
Create your own exception
Create SpatiaLite in Python
Random walk in Python
Try to improve your own intro quiz in Python
[Blender × Python] Create your own function & summary so far
Use the CASA Toolkit in your own Python environment
Create your first GDSII file in Python using gdspy
[Road to intermediate Python] Define in in your own class
Balanced Random Forest in python
Create a function in Python
Create a dictionary in Python
Create gif video in Python
Use Random Forest in Python
Create your own Django middleware
Weighted random choice in python
Try sorting your own objects with priority queue in Python
Create an original Random Dot Stereogram (RDS) from 2D photos with machine learning depth estimation
[Python] Random processing (create, select, sort)
[Python] Make your own LINE bot
Testing with random numbers in Python
Create a DI Container in Python
How to create your own Transform
Create a binary file in Python
Try docker: Create your own container image for your Python web app
Create Gmail in Python without API
Create your own name resolution service
[Django] Create your own 403, 404, 500 error pages
Create Python project documentation in Sphinx
Create a Kubernetes Operator in Python
Create and read messagepacks in Python
Specify your own class in class argument and return type annotation in Python
Make your own NOAA / GOES familiar X-ray flux 3days plot in Python
Create your own virtual camera with Python + OpenCV and apply original effects
Create wordcloud from your tweet with python3
Create ScriptableObject in Python when building ADX2
Create your own DNS server with Twisted
Create a python environment on your Mac
[Python] Package and distribute your own modules
Create a simple GUI app in Python
Create your own Composite Value with SQLAlchemy
Create a JSON object mapper in Python
Create Qt designer parts in Python (PyQt)
[Python] Register your own library on PyPI
Until you install your own Python library
[GPS] Create a kml file in Python
Publish your own Python library with Homebrew
Disease classification in Random Forest using Python
Try text mining your diary in Python
[Python] Disease classification in random forest-with LDA-
Create a Vim + Python test environment in 1 minute