1

I am doing some image processing within openCV, and am getting an index is Out of Bounds for Axis With Size Error.

I have tried adding in a

if(i+1 < len(img2) and j+1 < len(img2[0])):

to make sure we are in bounds within the array, but it just would skip each element of the array I believe.

I have tried removing the 0 and making it just a

for i in range(imgCol):

style loop.

I have also tried to reduce the length the array goes

for i in range(imgCol-1):

but the error persists.

Here is my current code.

img2 = cv2.imread('v2.jpg')

imgRow = img2.shape[0]
imgCol = img2.shape[1]

for i in range(0,imgCol):
    for j in range(0,imgRow):
        if ( img2[i,j,0] == 11 and img2[i,j,1] == 2 and img2[i,j,2] == 12):
            '''do something'''


  • the different between black and white and color is that in color there is 3 values, red. green, blue, could this give you the issue? – Leo Jun 29 '19 at 22:22
  • I take made a mistake, it seems like it errors either way. Not sure what was going on before then – Program232323 Jun 29 '19 at 22:24
  • run this: print( img2.shape[0]), the shape will not be what you expect – Leo Jun 29 '19 at 22:26
  • huh, your right. whats up with that? when i looked online people seemed to say this was the correct way to find the dimensions of your image, and go about looping through it. – Program232323 Jun 29 '19 at 22:33
  • if you do: print (img2.shape) what do you get? – Leo Jun 29 '19 at 22:34
  • im getting (203,293,3). The demension of the image when I check on my desktop is 200x273. – Program232323 Jun 29 '19 at 22:38
  • I think you just have the row/col reversed, your code runs on my image. do you have a url of your image to see if you still have the shape issue? – Leo Jun 29 '19 at 22:47
  • is in numpy the matrix index order really [col, row, channel]? in C++ openCV it is [row, col, channel] https://stackoverflow.com/questions/25642532/opencv-pointx-y-represent-column-row-or-row-column/25644503#25644503 – Micka Jun 29 '19 at 22:49
  • Woah, I think you guys are right. I need to do more testing, but that makes sense. Cant believe i didnt see that. Thanks – Program232323 Jun 29 '19 at 22:52

2 Answers2

0

I believe the error is in this line:

if ( img2[i,j,0] == 11 and img2[i,j,1] == 2 and img2[i,j,2] == 12):

I think you are trying to go through each pixel and if its value is (11, 2, 12) then do something. An image is a 3 dimensional array, instead of accessing the value with img2[i, j, 0], you may try img[i][j][0].


Editting to summarize the comments:

  • You may have the row and col reversed.
  • Also another option to do for trouble shooting is to add a read flag cv2.IMREAD_COLOR to force OpenCV to read the image in BGR format, although this is the default.
UdonN00dle
  • 723
  • 6
  • 28
0

when I run it like this (as UdonN00dle suggested) inverting rows and col, I get no error. I am not sure about your shape issue as I don't have your image

#importing a random image
from PIL import Image
import urllib.request
import numpy as np

URL = 'http://www.w3schools.com/css/trolltunga.jpg'

with urllib.request.urlopen(URL) as url:
    with open('temp.jpg', 'wb') as f:
        f.write(url.read())

img = Image.open('temp.jpg')

#the beginning of the code
img2 = np.asarray(img)


imgRow = img2.shape[0]
imgCol = img2.shape[1]
#inverted imgRow and imgCol
for i in range(0,imgRow):
    for j in range(0,imgCol):
        if ( img2[i,j,0] == 11 and img2[i,j,1] == 2 and img2[i,j,2] == 12):
            '''do something'''
Leo
  • 1,176
  • 1
  • 13
  • 33