-1

Could someone help me with what I need to change to resolve the index out-of-bounds problem in this code? There are codes before the beginning and later too. Do you think it would be relevant?

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)

The output on screen -

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
cottontail
  • 10,268
  • 18
  • 50
  • 51
  • 1
    The first dimension is `h`. The second dimension is `w`. That means `k` and `j` are first dimension, and `l` and `i` are second dimension. That means `r[i][j]` has the coordinates backwards. – Tim Roberts Oct 16 '22 at 02:01
  • Welcome to Stack Overflow. In your own words, where the code says `grey_picture_data[i+k][j+l]`, what values do you think will be possible for `i+k`? Why? (Hint: what is the largest possible value for `i`, according to the `for` loop? What is the largest possible value for `k`? What will you get if you add those numbers together?) Are all of those valid indices for `grey_picture_data`? Separately: where the code says `h = len(grey_picture_data[0])` and then `w = len(grey_picture_data[0])`, what do you think this means? **What will happen** if `grey_picture_data` is not square? – Karl Knechtel Oct 16 '22 at 02:10
  • (And what if `grey_picture_data2` has a different size?) – Karl Knechtel Oct 16 '22 at 02:12

1 Answers1

-1

Try this:

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))
  • IndexError: index 683 is out of bounds for axis 0 with size 683 at the same line. Casting complex values to real discards the imaginary part r[k][l] = r[k][l] + grey_picture_data[i][j]*np.exp(-1j*2*np.pi*(i*k/w+j*l/h)) Traceback (most recent call last): – Shashank Umesh Oct 17 '22 at 17:23