如何创建随机点立体图?

2024-09-28 23:16:37 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我用这张深度图把这一点付诸实践:

depth map star

结果如下:

enter image description here

但我不明白为什么我能在结果上看到两个物体,一颗星星“靠近”我,另一颗恒星“远离”我。根据我眼睛的聚焦方式,可能会有不同的结果。在

关于这个问题我读了很多东西,但我不明白。也许问题是我的英语不好或者对我所读内容的理解不好,但是我会很感激一些详细的解释,因为在网上没有太多的技术性解释来解释如何从头开始编写代码。在

注意:我试过在班次和样式上使用不同的尺寸,但似乎没有任何改变

代码:(如果您需要代码的其他部分或对其如何工作的一些注释,请告诉我。我还没打扫)

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

Tags: the代码inforwidthpygameseppoint
1条回答
网友
1楼 · 发布于 2024-09-28 23:16:37

问题“我能在结果上看到两个物体,一个星星“靠近”我,另一个恒星“远离”我”是因为当我试图将我对两幅图像的立体图的理解推广到使用重复模式的立体图时,我得到了错误的方法。在

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

在我的方法中,我错的是,我只移动了应该创建恒星的像素。我没有得到的是,因为RDS是由重复的模式构成的,移动这些像素也会产生与下一个模式相反的移位,从而产生另一个深度相反的恒星。在

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

结果如下:correct result

代码:(此代码是在尼尔·斯莱特的帮助下快速修改的前一个代码,因此还不干净。我会努力改进)

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

相关问题 更多 >