Вы можете решить эту проблему с помощью контуров.
im2, contours, hierarchy = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
for contour in contours:
(x, y, w, h) = cv2.boundingRect(contour)
# if h<500: # check the length of verical lines
if w<500: # check the length of horizontal lines
cv2.fillConvexPoly(gray,contour,0) #fill the contour with black color
![enter image description here](https://i.stack.imgur.com/cNwVM.png)