-1

I am trying to segment text images, but I have a problem with one of the images of roi(region of interest) that its dimensions is (24, 3) and (44, 3) and it gives me IndexError: index 26 is out of bounds for axis 0 with size 17 for this particular image. I am using opencv to segment the image using numpy array. How can I solve this?Why is this happening? I have seen similar answers here on SO, but can you explain it to me why and how to correct it? Or give me -4.

import os
import cv2
import numpy as np
import matplotlib.pyplot as plt
#import image
image = cv2.imread('wonde_1.png')
#cv2.imshow('orig',image)
#cv2.waitKey(0)

#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
cv2.imshow('gray',gray)
cv2.waitKey(0)

#binary
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
cv2.imshow('second',thresh)
cv2.waitKey(0)

#dilation
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated',img_dilation)
cv2.waitKey(0)

#find contours
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y+h, x:x+w]

    # show ROI
    #cv2.imshow('segment no:'+str(i),roi)
    filename='crop/file_%i.png'%i
    print(roi[26].shape)
    #cv2.imwrite(filename,roi[i])


    #am_char=input()
    #cv2.rectangle(image,(x,y),( x + w, y + h ),(90,0,255),2)
    cv2.waitKey(0)

cv2.imshow('marked areas',image)
cv2.waitKey(0)
Kaleab Woldemariam
  • 2,567
  • 4
  • 22
  • 43
  • 1
    I'm not going to debug your code but if you segment your image and generate a new roi do you not think that there is a danger of out of bounds access if your roi is smaller than 26? – EdChum Dec 07 '18 at 13:31
  • @EdChum Can you explain in plain English? Obviously there is a danger, that's why the error . But for starters, can you tell me what the error meant? – Kaleab Woldemariam Dec 07 '18 at 13:38
  • It means you're trying to get the 27th item when you only have 18 of them. – Dan Mašek Dec 07 '18 at 13:42
  • 1
    It looks like the dimensions of `roi` (which has height `h` and width `w`) might not allow for an index of `26` to exist, so the line `print(roi[26].shape)` will throw the error. – DatHydroGuy Dec 07 '18 at 13:42
  • 1
    The error is telling you that your array has size of 17 but you're trying to access 26 (the 27th item) which doesn't exist. You've hard coded your index without checking if it's out of bounds or not, basically the roi is smaller than 26 – EdChum Dec 07 '18 at 13:43
  • @DatHydroGuy Excuse my python, but how does `h` and `w` affect the index, as I am iterating over about 500 images? The code is adopted. – Kaleab Woldemariam Dec 07 '18 at 13:46
  • 1
    @Kaleab It's true you are iterating over lots of smaller images, but some of those images are *too* small. So for some of them, when you create `roi`, you give it dimensions `[y:y+h, x:x+w]` so if you look at `roi.shape`, then it says (for example) `roi.shape = (13, 17, 3)` which means that it has 13 elements, each with 17 elements, and each of those has 3 elements. You are then trying to print the shape of `roi[26]` which doesn't exist because `roi` has only 13 elements, so 26 is out of bounds. – DatHydroGuy Dec 07 '18 at 13:53

1 Answers1

0

I didn't need roi [i], just roi is enough as roi is the iterable itself.

Kaleab Woldemariam
  • 2,567
  • 4
  • 22
  • 43