我正在输入一个 numpy 数组,该数组是从图像生成到函数中的。该函数通过水平重复图像并移动某些像素来制作自动立体图。
这是通过另一个 numpy 数组完成的,自动立体图数组的坐标 [r, c] 基于纹理数组的坐标,其中一些数学修改了坐标。
问题是,代码将其中一个坐标视为序列。该代码在索引方面存在一些问题。
import numpy as np
import matplotlib.pyplot as plt
import skimage, skimage.io
from PIL import Image
#%matplotlib inline
plt.rcParams['figure.dpi'] = 150
def normalize(depthmap):
"Normalizes values of depthmap to [0, 1] range."
if depthmap.max() > depthmap.min():
return (depthmap - depthmap.min()) / (depthmap.max() - depthmap.min())
else:
return depthmap
def display(img, colorbar=False):
"Displays an image."
plt.figure(figsize=(10, 10))
if len(img.shape) == 2:
i = skimage.io.imshow(img, cmap='gray')
#pass
else:
#pass
i = skimage.io.imshow(img)
i = skimage.io.imshow(img)
if colorbar:
plt.colorbar(i, shrink=0.5, label='depth')
plt.tight_layout()
print("display called")
def make_pattern(shape=(16, 16), levels=64):
"Creates a pattern from gray values."
return np.random.randint(0, levels - 1, shape) / levels
def create_circular_depthmap(shape=(600, 800), center=None, radius=100):
"Creates a circular depthmap, centered on the image."
depthmap = np.zeros(shape, dtype=float)
r = np.arange(depthmap.shape[0])
c = np.arange(depthmap.shape[1])
R, C = np.meshgrid(r, c, indexing='ij')
if center is None:
center = np.array([r.max() / 2, c.max() / 2])
d = np.sqrt((R - center[0])**2 + (C - center[1])**2)
depthmap += (d < radius)
return depthmap
def make_autostereogram(depthmap, pattern, shift_amplitude=0.1, invert=False):
"Creates an autostereogram from depthmap and pattern."
print("make_autostereogram called")
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[int(r % pattern.shape[0]), c] #pattern[r, c] #
else:
shift = int(depthmap[r, c] * shift_amplitude * pattern.shape[1])
autostereogram[r, c] = autostereogram[r, int(c - pattern.shape[1] + shift)] #autostereogram[r, c]#
return autostereogram
img = blank_image()
texture= Image.open('marble.png')
texture.show()
depthmap = create_circular_depthmap(radius=150)
#print("depthmap: ", depthmap)
depthmapImg = Image.fromarray(normalize(depthmap))
newPattern = np.array(texture)
print("new pattern: ", type(newPattern))
autostereogram2 = make_autostereogram(depthmap, newPattern)
"""
display(autostereogram)
autostereogram2.show()"""
错误信息:
TypeError: only length-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\geniu\OneDrive\GW\Spring_2024\CSCI_6527\final_project.py", line 171, in <module>
autostereogram2 = make_autostereogram(depthmap, newPattern)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\geniu\OneDrive\GW\Spring_2024\CSCI_6527\final_project.py", line 85, in make_autostereogram
autostereogram[r, c] = pattern[int(r % pattern.shape[0]), c] #pattern[r, c] #
~~~~~~~~~~~~~~^^^^^^
ValueError: setting an array element with a sequence.
罪魁祸首:
texture= Image.open('marble.png')
我假设您的图像是彩色的(或者您将其作为多通道图像加载),这意味着它有 3 个通道,因此您的图案由 3 个通道组成。
因此,当您访问
pattern[int(r % pattern.shape[0]), c]
时,这意味着您仅为三个通道中的两个指定两个索引。第三个通道是一个序列,因为您没有为其指定索引。
因此,如果您想继续使用此逻辑,您需要将图像转换为灰度或指定第三个索引以按原样使用图像。