首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python中的索引超出界限

Python中的索引超出界限
EN

Stack Overflow用户
提问于 2022-10-16 01:58:51
回答 1查看 33关注 0票数 -1

有人能帮我做些什么来解决这个代码中的索引超出范围的问题吗?在开始之前和之后都有代码。你觉得这有关系吗?

代码语言:javascript
复制
print("1. Sum of Squared Differences") 

print("2. Correlation Coefficient") 

print("3. Normalized Cross-Correlation") 

cross_image_type = input("Enter the type of cross image referencing: ") 

if (cross_image_type == "1"): 

    h = len(grey_picture_data[0]) 

    w = len(grey_picture_data[0]) 

    r = np.zeros((h,w)) 

    for k in range(0,h): 

        for l in range(0,w): 

            for i in range(0,w): 

                for j in range(0,h):      

                        r[i][j] += (((grey_picture_data[i+k][j+l]) - (grey_picture_data_2[k][l]))**2)

屏幕上的输出-

代码语言:javascript
复制
Enter the name of a color image: crosswalk.jpg
Enter the name of the color template file: crosswalk_template.jpg
1. Sum of Squared Differences
2. Correlation Coefficient
3. Normalized Cross-Correlation
Enter the type of cross image referencing: 1
Traceback (most recent call last):

  File "C:\Program Files\Spyder\pkgs\spyder_kernels\py3compat.py", line 356, in compat_exec
    exec(code, globals, locals)

  File "d:\purdue\engr 133\proj.py", line 206, in <module>
    r[i][j] += (((grey_picture_data[i+k][j+l]) - (grey_picture_data_2[k][l]))**2)

IndexError: index 683 is out of bounds for axis 0 with size 683
EN

回答 1

Stack Overflow用户

发布于 2022-10-16 02:06:04

试试这个:

代码语言:javascript
复制
h = len(grey_picture_data[0]) 

w = len(grey_picture_data[0]) 

r = np.zeros((h,w)) 

for k in range(0,h): 

    for l in range(0,w): 
        
        for i in range(0,w):
                
                for j in range(0,h):
                    
                    r[k][l] = r[k][l] + grey_picture_data[i][j]*np.exp(-1j*2*np.pi*(i*k/w+j*l/h))
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74084137

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档