Why am I getting a index error for my loop in python?

Paige Kemp

I am trying to divide a raw image into 8x8 overlapping blocks so I can do feature extraction later.

Here is my code:

new0 = np.zeros((heightimage0R, widthimage0R), np.uint8)
k = 0
for i in range(heightimage0R):
    for j in range(widthimage0R):
        crop_tmp0R = image0R[i:i+8,j:j+8]
        new0[k, 0:64] = crop_tmp0R.flatten()
        k = k + 1

However, when ever I run my code I get the following error:

Traceback (most recent call last):

  File "<ipython-input-392-cf9c59842d3a>", line 6, in <module>
    new0[k, 0:64] = crop_tmp0R.flatten()

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

I have tried widthimage0R-1 in the for loop but it still does not work.

Ted Klein Bergman

new0 is of size heightimage0Rxwidthimage0R (which I'll refer to as hxw for now), which I assume is the same size of image0R (otherwise you have more problems).

What your code is doing is taking a 8x8 square from image0R and flattening it into the new array.

The problem arises because new0 is a hxw-matrix, but you're using it as a h*wx64-matrix. This is because the row has value k, which goes between 0 to h*w, and the column is always 64.

My guess is that you mean to do the following:

new0 = np.zeros((heightimage0R*widthimage0R, 64), np.uint8)
k = 0
for i in range(heightimage0R-8):  # Don't forget the -8 to not exceed the size of the image0R as well!
    for j in range(widthimage0R-8):
        crop_tmp0R = image0R[i:i+8,j:j+8]
        new0[k, 0:64] = crop_tmp0R.flatten()
        k = k + 1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why am I getting an Undifined Index error in my php code?

Why am I getting internal server error with my python code?

Why am i getting index of the range error?

Why am I getting an "undefined index" error?

why I am not getting error in this java for loop?

Why am I getting string out of range index error within my list?

Why am I getting this syntax error? (Python)

Why am I getting this Python datetime error?

Why am I getting this Key error? (Python)

Why am i getting this error (TypeError: '_io.TextIOWrapper' object is not subscriptable) after the first iteration of my for loop?

I am trying to search up to the right diagonally in my multidimensional array in java. Why am I getting index out of bounds error?

Why am I getting child index out of range error when I writing to XML file in python?

Why am I getting an index out of range in my code?

Why does my program break? Why am I getting this error?

Why am I getting segmentation fault in my while loop?

I am getting an Error : list index out of range while working on my python project .

Why am I getting this error in Python3: IndexError: list index out of range

Why am i getting the error of "List Index out of range"

Why am I getting this "missing index for constraint..." error on MySQL?

Why am I getting this Index out of range error?

why am i getting this error even that index is exist?

Why am I getting list index out of range Error

Why am I getting an error with Dir in a VBA loop?

Why am I getting an invalid syntax error in a for loop?

Why am I getting a for-loop error in Unity RayMarching shader?

Why am I getting a Referencing Error in my Laravel migration?

Why am I getting this error display for my video in Chrome?

Why am I getting the unhandled rejection error for my web application?

Why am I getting an xml error in my javafx project?