MadLabMatLab
MadLabMatLab

Reputation: 11

Need Help in Fixing Error in Python: index out of bounds for axis 0

This code was actually posted earlier, when it had even more errors.

Previously, the letters had wrong unicodes and could not be used. moreover, it had some minor errors with numpy.

Now it's fixed, resulting on a final(I wish) error of the code

import numpy as np
import math as mt
import cv2
import matplotlib.pyplot

I = cv2.imread()#plase image location

h, w, c = I.cv2.shape

largerLength = np.maximum(h, w)

power = np.ceil(mt.log2(largerLength))
lengthNum =2**power
grayIm = cv2.cvtColor(I, cv2.COLOR_BGR2GRAY)
ret, binaryIm = cv2.threshold(grayIm,125,256,cv2.THRESH_BINARY)

#get the amount of padding to add
padRow = lengthNum - I.shape
padCol = lengthNum − I.shape
#pad I with 0’s after its last row and column
I = np.padarray(I , [padRow , padCol], ’post’) 

boxCountstore = np.zeros(1 , power) 
#boxcountstore = zeros(1, power)
scalestore = np.zeros(1 , power) #??
#scalestore = zeros(1, power)
boxNum = 1

#use the for loop to shrink the box size
for i in range(1, power):
    boxCount=0
    for boxrow in range(1,2**i):#i was i-1
        for boxcol in range(1,2**i):
                #thefourtermsbelowaretheindexrange
                #ofthecurrentboxwearechecking
            var1 = lengthNum/boxNum
            var2 = boxrow-1
            minRow = 1 + var1∗var2
            minCol = 1 + var1∗var2
            maxRow = var1∗boxrow
            maxCol = var1∗boxcol
            contain=0
            for row in range(minRow,maxRow):
                for col in range(minCol,maxCol):
                    if I[row-1,col-1]: ###????
                    #ifture,thenthecurrentbox
                    #containstheobject
                        boxCount=boxCount+1
                        contain=1
                        break #breakfromthe”col”
                if contain:
                    break #breakfromthe”row”loop



scale=1/(lengthNum/boxNum)
boxNum =  2*boxNum #doublethenumberofboxes
#per dimension

#fit a line for the log − log plot in the least square
#sense


FD = np.polyfit(np.log(scalestore),np.log(boxCountstore),1)

The error message is this:

Traceback (most recent call last):

File "C:/Users/qkrgn/PycharmProjects/pythonProject/main.py", line 60, in <module>

if I[row-1, col-1]:  ###????

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

As you see,

Upvotes: 1

Views: 745

Answers (1)

Eumel
Eumel

Reputation: 1433

unlike matlab python starts its arrays with 0, so the last index is 32767

Upvotes: 2

Related Questions