I have a NumPy array contours that I got from cv2.findContours and flattened using contours = np.concatenate(contours, axis = 0). It stores coordinates of contours of objects from an image. However, I want to delete coordinates whose either X or Y is lower than, let's say, 100, or bigger than 1000. I first tried using contours = np.delete(contours, 0) and contours = np.delete(contours[0], 0) to just delete any item, but I kept getting this error:
IndexError: invalid index to scalar variable.
How to delete such pairs of values?
print(type(contours))
→ <class 'numpy.ndarray'>
print(contours[0])
→ [[2834 4562]]
print(type(contours[0]))
→ <class 'numpy.ndarray'>
print(contours[0][0])
→ [2834 4562]
print(type(contours[0][0]))
<class 'numpy.ndarray'>
Also, I don't want to concatenate/flatten the list any further, because it's exactly in the form I need it to send to cv2.convexHull(contours).
Here's a minimal working sample of my code:
import cv2 # library for processing images
import numpy as np # numerical calculcations for Python
img = cv2.imread("img.png")
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
_, img_thr = cv2.threshold(img_gray,0,255,cv2.THRESH_OTSU)
img_rev = cv2.bitwise_not(img_thr)
img_cnt, contours, hierarchy = cv2.findContours(img_rev, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = np.concatenate(contours, axis = 0)
hull = cv2.convexHull(contours)
rect = cv2.minAreaRect(np.int0(hull))
box = cv2.boxPoints(rect)
box = np.int0(box)
img_cnt = cv2.drawContours(img, contours, -1, (0,255,0), 3)
img_cnt = cv2.drawContours(img, [box], -1, (0,0,255), 5)
cv2.imwrite("img_out.png", img_cnt)
Here is a sample input image, here is my output image. I want to ignore outlying "noise" for text selection. Assume I cannot use further noise reduction.