如何创建随机点立体图(RDS)?

cod*_*ish 5 python

我正在尝试理解并编写一个Python脚本,该脚本根据深度图和随机点生成的图案创建随机点立体图(RDS) 。根据我的理解,为了创造深度的错觉,像素会被移动,所以当我们通过改变焦点使它们合并时,移动的差异会产生错觉。

我用这个深度图将其付诸实践:

深度图星

结果如下:

在此输入图像描述

但我不明白为什么我可以在结果中看到 2 个物体,其中一颗星离我“近”,另一颗星离我“远”。根据我的眼睛聚焦方式,可能会产生不同的结果。

我读过很多关于这个主题的东西,但我不明白。也许问题是我的英语不好或对我读过的内容的理解不好,但我会欣赏一些详细的解释,因为网上没有太多关于如何从头开始编码的技术解释。

注意:我尝试过在班次和图案上使用不同的尺寸,但它似乎没有改变任何东西

代码:(告诉我您是否需要代码的其他部分或有关其工作原理的一些评论。我还没有清理它)

import os, sys
import pygame

def get_linked_point(depthmap, d_width, d_height, sep):
    """
    In this function we link each pixel in white in the depth map with the 
    coordinate of the shifted pixel we will need to create the illusion
    ex: [[x,y],[x_shifted,y]]

    :param sep: is the shift value in pixels
    """
    deptharray = pygame.PixelArray(depthmap)
    list_linked_point = []
    for x in range(d_width):
        for y in range(d_height):
            if deptharray[x][y] != 0x000000:
                list_linked_point.append([[x, y], [x+sep, y]])
    del deptharray
    return list_linked_point


def display_stereogram(screen, s_width, pattern, p_width, linked_points):
    """
    Here we fill the window with the pattern. Then for each linked couple of 
    point we make the shifted pixel [x_shifted,y] equal to the other one 
    [x,y]
    """
    x = 0
    while x < s_width:
        screen.blit(pattern, [x, 0])
        x += p_width
    pixAr = pygame.PixelArray(screen)
    for pair in linked_points:
        pixAr[pair[0][0], pair[0][1]] = pixAr[pair[1][0], pair[1][1]]
    del pixAr
Run Code Online (Sandbox Code Playgroud)

cod*_*ish 5

问题“我可以在结果中看到 2 个物体,一颗星离我“近”,另一颗星离我“远””是因为当我试图概括我对立体图的理解时,我采取了错误的方法使用重复模式将 2 个图像转换为立体图。

要创建 2 幅图像立体图,您需要移动一幅图像的像素以产生深度错觉。

我的方法的错误在于我只移动了应该创建星星的像素。我不明白的是,因为 RDS 是由重复的图案制成的,所以移动这些像素也会产生相反的移动,下一个图案会产生相反深度的另一颗星星。

为了纠正这个问题,我将深度图的每个点(不仅仅是白色的点)配对,以便在星星结束后回到基本移动量。

结果如下: 正确结果

代码:(这段代码是在Neil Slater的帮助下快速修改的上一个代码,所以它还不够干净。我会尽力改进它)

def get_linked_point(depthmap, d_width, d_height, p_width, sep):
    """
    In this function we link each pixel in white in the depth map with the 
    coordinate of the shifted pixel we will need to create the illusion
    ex: [[x,y],[x_shifted,y]]

    :param sep: is the shift value in pixels
    """
    deptharray = pygame.PixelArray(depthmap)
    list_linked_point = []
    for x in range(d_width):
        for y in range(d_height):
            if deptharray[x][y] == 0x000000:
                list_linked_point.append([[x, y], [x+p_width, y]])
            else:
                list_linked_point.append([[x, y], [x-sep+p_width, y]])
    del deptharray
    return list_linked_point


def display_stereogram(screen, s_width, pattern, p_width, linked_points):
    """
    Here we fill the window with the pattern. Then for each linked couple of 
    point we make the shifted pixel [x_shifted,y] equal to the other one 
    [x,y]
    """
    x = 0
    while x < s_width:
        screen.blit(pattern, [x, 0])
        x += p_width
    pixAr = pygame.PixelArray(screen)
    for pair in linked_points:
        pixAr[pair[1][0], pair[1][1]] = pixAr[pair[0][0], pair[0][1]]
    del pixAr
Run Code Online (Sandbox Code Playgroud)