我正在尝试理解并编写一个 python 脚本,该脚本根据深度图和随机点生成的图案创建随机点立体图 (RDS)。据我了解,为了创造深度错觉,像素会发生偏移,因此当我们通过改变焦点使它们合并时,偏移的差异会产生错觉。
我用这张深度图将其付诸实践:
这是结果:
但我不明白为什么我可以在结果上看到 2 个物体,一颗离我“近”的星和另一颗离我“远”的星。根据我的眼睛聚焦方式,可能会出现不同的结果。
我读过很多关于这个主题的东西,但我不明白。也许问题是我的英语不好或对我所读内容的理解不佳,但我会感谢一些详细的解释,因为网络上没有太多关于如何从头开始编写代码的技术解释。
注意:我已经尝试过在 shift 和 pattern 上使用不同的尺寸,但似乎没有任何改变
代码:(如果您需要代码的其他部分或关于它如何工作的一些评论,请告诉我。我还没有清理它)
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
最佳答案
问题 “我可以在结果上看到 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
关于python - 如何创建随机点立体图 (RDS)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43343503/